JDK-8253590 : java/foreign tests are still failing on x86_32 after foreign-memaccess integration
  • Type: Bug
  • Component: core-libs
  • Affected Version: 16
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2020-09-24
  • Updated: 2021-08-19
  • Resolved: 2020-09-29
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 16
16 b18Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
These two:

$ CONF=linux-x86-server-fastdebug make images run-test TEST=java/foreign/TestByteBuffer.java

java.lang.OutOfMemoryError: Unable to allocate 2147483660 bytes
	at java.base/jdk.internal.misc.Unsafe.allocateMemory(Unsafe.java:632)
	at jdk.incubator.foreign/jdk.internal.foreign.NativeMemorySegmentImpl.makeNativeSegment(NativeMemorySegmentImpl.java:91)
	at jdk.incubator.foreign/jdk.incubator.foreign.MemorySegment.allocateNative(MemorySegment.java:612)
	at jdk.incubator.foreign/jdk.incubator.foreign.MemorySegment.allocateNative(MemorySegment.java:559)
	at TestByteBuffer.testTooBigForByteBuffer(TestByteBuffer.java:464)

$ CONF=linux-x86-server-fastdebug make images run-test TEST=java/foreign/TestMismatch.java

test TestMismatch.testLarge(): failure
java.lang.OutOfMemoryError: Unable to allocate 2147483660 bytes
	at java.base/jdk.internal.misc.Unsafe.allocateMemory(Unsafe.java:632)
	at jdk.incubator.foreign/jdk.internal.foreign.NativeMemorySegmentImpl.makeNativeSegment(NativeMemorySegmentImpl.java:91)
	at jdk.incubator.foreign/jdk.incubator.foreign.MemorySegment.allocateNative(MemorySegment.java:612)
	at jdk.incubator.foreign/jdk.incubator.foreign.MemorySegment.allocateNative(MemorySegment.java:559)
	at TestMismatch.testLarge(TestMismatch.java:114)

They were failing before, and fixed by JDK-8237521 and using "(long) Integer.MAX_VALUE * 2" that would expose this path to additional checking on Unsafe side, which will throw the exception the test expects.

However, that was reverted during the JDK-8243491 integration back to "(long)Integer.MAX_VALUE + 10L)":
https://hg.openjdk.java.net/jdk/jdk/diff/8113059a83b2/test/jdk/java/foreign/TestByteBuffer.java#l1.118

But I find the whole thing rather fragile.
Comments
Fix does not seem to be complete. x86_32 still fails java/foreign/TestMismatch.java: test TestMismatch.testLarge(): failure java.lang.OutOfMemoryError: Unable to allocate 2147483660 bytes at java.base/jdk.internal.misc.Unsafe.allocateMemory(Unsafe.java:632) at jdk.incubator.foreign/jdk.internal.foreign.NativeMemorySegmentImpl.makeNativeSegment(NativeMemorySegmentImpl.java:91) at jdk.incubator.foreign/jdk.incubator.foreign.MemorySegment.allocateNative(MemorySegment.java:612) at jdk.incubator.foreign/jdk.incubator.foreign.MemorySegment.allocateNative(MemorySegment.java:559) at TestMismatch.testLarge(TestMismatch.java:114)
29-09-2020

Changeset: ebf443a1 Author: Maurizio Cimadamore <mcimadamore@openjdk.org> Date: 2020-09-29 09:40:21 +0000 URL: https://git.openjdk.java.net/jdk/commit/ebf443a1
29-09-2020

I think the best fix here is to avoid allocation altogether. The main thing we wanna check here is that it is not possible to create a bytebuffer out of a segment that is bigger than Integer.MAX_VALUE. All problems occurring on the allocation side (Unsafe:allocateMemory) are, in a way, NOT what the test is designed to look out for. I'll try to change the test so that it no longer depends on allocation, by using the MemorySegment.ofNativeRestricted API.
28-09-2020