On bytecode level booleans are represented as ints and HotSpot JVM normalizes boolean values on memory accesses. It unconditionally applies normalization on boolean stores, but trusts on-heap boolean locations to hold normalized values. Normalization is applied on loads for off-heap and mismatched unsafe accesses .
There are 2 normalization procedures used: (1) cast int to byte and test it against zero; and (2) truncation to least-significant bit. Truncation is preferred (due to performance considerations), but JNI mandates testing against zero and, historically, `#1` was used for off-heap unsafe accesses as well. It complicated the implementation (leading to subtle bugs) and introduced divergence in behavior at runtime (depending on execution mode and JIT-compilation peculiarities). The fix uses truncation uniformly across all execution modes. It simplifies implementation and eliminates possible divergence in behavior between execution modes. Also, it drastically simplifies future Unsafe API refactorings. There's one scenario left when it's possible to observe non-normalized values: when mismatched access pollutes the Java heap with a bogus boolean value, but then the value is read with a well-typed boolean access. Testing: hs-tier1 - hs-tier6 - [x] I confirm that I make this contribution in accordance with the [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai). ------------- Commit messages: - Truncate boolean values Changes: https://git.openjdk.org/jdk/pull/31249/files Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=31249&range=00 Issue: https://bugs.openjdk.org/browse/JDK-8385119 Stats: 311 lines in 7 files changed: 272 ins; 28 del; 11 mod Patch: https://git.openjdk.org/jdk/pull/31249.diff Fetch: git fetch https://git.openjdk.org/jdk.git pull/31249/head:pull/31249 PR: https://git.openjdk.org/jdk/pull/31249
