[9] RFR (S): 8161720: Better byte behavior for off-heap data
Zoltán Majó
zoltan.majo at oracle.com
Mon Aug 22 14:25:11 UTC 2016
Hi,
please review the fix for JDK-8161720.
https://bugs.openjdk.java.net/browse/JDK-8161720
Problem: As John pointed out in the bug description, when a boolean
value is loaded using Unsafe.getBoolean(), the VM should normalize the
byte value from the range 0..255 to 0..1.
The patch I'm proposing includes a new test,
UnsafeOffHeapBooleanTest.java [1]. The test does the following (in
essence):
boolean bool1 = Unsafe.getBoolean(<off-heap memory region 1 containing a
non-null value, e.g., 0x2>);
boolean bool2 = Unsafe.getBoolean(<off-heap memory region 2 containing a
non-null value, e.g., 0x4>);
boolean result = bool1 & bool2; // Is supposed to be true
The value of 'result' is supposed to be true. However, because values
returned by Unsafe.getBoolean() are currently not normalized by the VM,
'value' can be either true or false. The value of 'result' depends on
(1) whether the code above is interpreted or compiled and (2) also on
the platform where the test is executed.
The same problem can be triggered with on-heap accesses as well. Here is
a second test, UnsafeOnHeapBooleanTest.java [2] that does that. I'd
like to, therefore, propose that the VM normalizes not only off-heap
data read by Unsafe.getBoolean() (as originally proposed by John), but
also on-heap data returned by Unsafe.getBoolean().
Maybe the UnsafeOnHeapBooleanTest.java test mis-uses the Unsafe API in
the way it reads data from the heap. But as any user of the Unsafe API
could do what the test does, maybe it would be good to "protect" the VM
from the problem of having to deal with many different 'true' values
(e.g., 0x2 and 0x4). Also, Unsafe.getBoolean() is declared as a native,
so maybe it's not too bad if the method respects JNI conventions.
Solution: Normalize the result returned by Unsafe.getBoolean in
- src/share/vm/prims/unsafe.cpp (used by the interpreter and by compiled
code if the compiler intrinsics for Unsafe.getBoolean() are disabled)
- the C1 and C2 intrinsics for Unsafe.getBoolean().
Webrev:
http://cr.openjdk.java.net/~zmajo/8161720/webrev.00/
Testing:
- JPRT (incl. Unsafe[On|Off]HeapBooleanTest.java);
- RBT testing with all hotspot tests both w/ -Xmixed and -Xcomp (no new
problems have showed up).
Thank you!
Best regards,
Zoltan
[1]
http://cr.openjdk.java.net/~zmajo/8161720/webrev.00/test/compiler/unsafe/UnsafeOffHeapBooleanTest.java.html
[2]
http://cr.openjdk.java.net/~zmajo/8161720/webrev.00/test/compiler/unsafe/UnsafeOnHeapBooleanTest.java.html
More information about the hotspot-compiler-dev
mailing list