RFR: 8366495: Incorrect minimum memory size allocated in allocateNativeInternal()

Jaikiran Pai jpai at openjdk.org
Tue Sep 2 05:18:41 UTC 2025


On Mon, 1 Sep 2025 05:31:43 GMT, Amit Kumar <amitkumar at openjdk.org> wrote:

> Originally Reported in OpenJ9, fix by @AditiS11 present here: https://github.com/ibmruntimes/openj9-openjdk-jdk25/pull/32
> 
> These test failure were reported in OpenJ9 (x86), I can't reproduce on my system (s390x): 
> 
> java/foreign/TestFill.java
> java/foreign/TestSegments.java
> java/foreign/TestSegmentBulkOperationsContentHash.java
> java/foreign/TestStringEncoding.java
> java/foreign/TestVarArgs.java
> 
> 
> 
> ```java 
>         // Always allocate at least some memory so that zero-length segments have distinct
>         // non-zero addresses.
>         alignedSize = Math.max(1, alignedSize);
> 
> 
> Here minimum-allocated size will be 1, which is incorrect because 
> 
> 
>     private static void initNativeMemory(long address, long byteSize) {
>         for (long i = 0; i < byteSize; i += Long.BYTES) {
>             UNSAFE.putLongUnaligned(null, address + i, 0);
>         }
>     }
> 
> `initNativeMemory()` is going to write Long.

src/java.base/share/classes/jdk/internal/foreign/SegmentFactories.java line 201:

> 199:         // Always allocate at least some memory so that zero-length segments have distinct
> 200:         // non-zero addresses.
> 201:         alignedSize = Math.max(Long.BYTES, alignedSize);

A few lines above this code there's a comment which says:

// Align the allocation size up to a multiple of 8 so we can init the memory with longs
long alignedSize = init ? Utils.alignUp(byteSize, Long.BYTES) : byteSize;


Should we have a similar comment as well as a check for `init` here? After all, we are increasing this size here because of `init` initializing the allocated memory. So perhaps something like:


// Always allocate at least some memory so that zero-length segments have distinct
// non-zero addresses. If we are initializing the allocated memory, then use a minimum
// size of 8 because we init the memory with longs.
alignedSize = Math.max((init ? Long.BYTES : 1), alignedSize);



If you do use this newer proposed change, then please have it verified against the original reproducer.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/27027#discussion_r2314932199


More information about the core-libs-dev mailing list