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

Uwe Schindler uschindler at apache.org
Thu May 12 15:39:48 UTC 2022


Hi Maurizio,
 
> I have spotted something...
> 
> The code that creates a mapped buffer is like this:
> 
> ```
> return Util.newMappedByteBufferR((int)unmapper.cap,
>                      unmapper.address + unmapper.pagePosition,
>                      unmapper.fd,
>                      unmapper, isSync);
> ```
> 
> While the code that creates the mapped segment is like this:
> 
> ```
> AbstractMemorySegmentImpl segment = new
> MappedMemorySegmentImpl(unmapper.address(), unmapper, size,
>                      modes, session);
> ```
> 
> While the two are similar, there are some differences:
> 
> * the BB version is using unmapper.cap for the size, while the segment
> version is just passing the size - on further inspection, this
> difference seems benign, as unmapper.cap seems to be always set to the
> user specified byte size

This is no issue, because the "java seen" size must be the one that the user specified. If the internal mapping is larger by the pagePosition it does not matter to user code.

> * the BB address is set to unmapper.address + pagePosition - now this is
> an actual difference between the two snippets - the memory segment
> version ignores pagePosition.

There is another slight difference! The MemorySegemnt code uses the "address()" abstraction method, while the FileChannel code uses the fields in unmapper object directly. The implementation of this address() method adds the pagePosition. Actually Lucene people found this bug very early in Java 16 EA releases: https://bugs.openjdk.java.net/browse/JDK-8259032

Generally, I would change FileChannel#map code to use the same address() accessor instead of doing the calculation in the factory code for MappedByteBuffer. The current code is indeed inconsistent.

Uwe

> 
> The second difference seems like a bug - that said, it should only
> manifest when mapping a file with a non-zero offset, as pagePosition is

The alignment is respected correctly - it's hard to understand the code, but the alignment workarounds are unfortunately needed. Linux does the same! At Lucene we have many tests for this.

Uwe




More information about the panama-dev mailing list