Java 17 bug (?) on Windows with MemorySegment.asSlice and offset = 3705032704

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu May 12 09:26:22 UTC 2022


Control question - can a similar program (but with smaller values, of 
course) be made to fail in same way with ByteBuffer?

The logic for handling force() etc. is shared between segments and 
ByteBuffer, so I strongly suspect that the issue should be shared too.

Unless, of course, this is an issue that has to do with truncation (e.g. 
some long parameter being converted to int under the hood).

I'll look deeper.

Thanks
Maurizio

On 12/05/2022 10:12, Radosław Smogura wrote:
> Hi,
>
> I guess differences between Windows & Linux comes from processing signal or other checks happening during execution.
>
> If I remember well how pages are mapped, then.
>
> 3,705,032,704 is the start address of next memory page, which looks like not mapped (I think that even pages is not in memory it should not cause fault, but just program should work a bit slower).
>
> 3705032702   ÷   4096 = 904,548.9995117188
>
> 904548   ×   4096 = 3,705,028,608 - start of first mapped paged
>
> 3,705,028,608 + 4096 = 3,705,032,704 - next mapped page.
>
> Kind regards,
> Rado
> ________________________________
> From: panama-dev <panama-dev-retn at openjdk.java.net> on behalf of erel at eth.gl <erel at eth.gl>
> Sent: Thursday, May 12, 2022 07:38
> To: panama-dev at openjdk.java.net <panama-dev at openjdk.java.net>
> Subject: Java 17 bug (?) on Windows with MemorySegment.asSlice and offset = 3705032704
>
> Hi,
>
> It doesn’t happen on Linux. I’ve tested it with Oracle Java 17.0.3.1 and OpenJDK 17.0.2.
>
>
>
> Code:
>
> package test.test;
>
>
>
> import java.io.RandomAccessFile;
>
> import java.nio.channels.FileChannel.MapMode;
>
> import java.nio.file.Path;
>
>
>
> import jdk.incubator.foreign.MemorySegment;
>
> import jdk.incubator.foreign.ResourceScope;
>
>
>
> public class Test {
>
>
>
>        public static void main(String[] args) throws Exception {
>
>              System.out.println(System.getProperty("sun.arch.data.model"));
>
>              System.out.println(System.getProperty("os.arch"));
>
>              System.out.println(System.getProperty("java.version"));
>
>              Path p = Path.of("test.dat");
>
>              RandomAccessFile raf = new RandomAccessFile("test.dat", "rw");
>
>              raf.setLength(8000000000L); //~8gb
>
>              raf.close();
>
>              MemorySegment mappedMemorySegment = MemorySegment.mapFile(p, 0, p.toFile().length(), MapMode.READ_WRITE, ResourceScope.newSharedScope());
>
>              System.out.println("byteSize: " + mappedMemorySegment.byteSize());
>
>              long offset = 3705032702L;
>
>              while (true) {
>
>                    System.out.println("offset: " + offset);
>
>                    MemorySegment target = mappedMemorySegment.asSlice(offset, 1);
>
>                    offset = offset + 1;
>
>                    try {
>
>                          target.force();
>
>                    } catch (Exception e) {
>
>                          e.printStackTrace();
>
>                          return;
>
>                    }
>
>              }
>
>        }
>
> }
>
>
>
> Output:
>
> WARNING: Using incubator modules: jdk.incubator.foreign
>
> 64
>
> amd64
>
> 17.0.3.1
>
> byteSize: 8000000000
>
> offset: 3705032702
>
> offset: 3705032703
>
> offset: 3705032704
>
> java.io.UncheckedIOException: java.io.IOException: Attempt to access invalid address
>
>         at java.base/java.nio.MappedMemoryUtils.force(MappedMemoryUtils.java:102)
>
>         at java.base/java.nio.Buffer$1.force(Buffer.java:839)
>
>         at java.base/jdk.internal.misc.ScopedMemoryAccess.forceInternal(ScopedMemoryAccess.java:337)
>
>         at java.base/jdk.internal.misc.ScopedMemoryAccess.force(ScopedMemoryAccess.java:325)
>
>         at jdk.incubator.foreign/jdk.internal.foreign.MappedMemorySegmentImpl.force(MappedMemorySegmentImpl.java:104)
>
>         at test.test.Test.main(Test.java:30)
>
> Caused by: java.io.IOException: Attempt to access invalid address
>
>         at java.base/java.nio.MappedMemoryUtils.force0(Native Method)
>
>         at java.base/java.nio.MappedMemoryUtils.force(MappedMemoryUtils.java:100)
>
>         ... 5 more
>
>
>
> This is a blocking issue for us. Suggestions will be welcomed.
>
>
>
> Thanks,
>
> Erel
>
>
>


More information about the panama-dev mailing list