JDK-8335390 : C2 MergeStores: wrong result with Unsafe
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 23,24
  • Priority: P2
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2024-07-01
  • Updated: 2024-07-01
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 23 JDK 24
23Unresolved 24Unresolved
Related Reports
Relates :  
Relates :  
Description
With Unsafe, you can create arbitrary array pointers, and some of them seem to lead to wrong results with the MergeStores optimization JDK-8318446.

The easiest way for now is to disable Unsafe accesses, and then redesign the ArrayPointer class from the ground up, to be more powerful and safe in all cases (incl proofs). JDK-8335392

./java --add-modules java.base --add-exports java.base/jdk.internal.misc=ALL-UNNAMED --add-exports java.base/jdk.internal.util=ALL-UNNAMED -XX:CompileCommand=compileonly,TestMergeStores*::test* -XX:CompileCommand=printcompilation,TestMergeStores*::test* -Xbatch -XX:+TraceMergeStores -XX:+MergeStores TestMergeStoresUnsafeArrayPointer.java
CompileCommand: compileonly TestMergeStores*.test* bool compileonly = true
CompileCommand: PrintCompilation TestMergeStores*.test* bool PrintCompilation = true
Allocate big array of SIZE = 1073741924
test1
7583   99    b  3       TestMergeStoresUnsafeArrayPointer::testClear (72 bytes)
7585  100    b  3       TestMergeStoresUnsafeArrayPointer::testSum (84 bytes)
7586  101 %  b  4       TestMergeStoresUnsafeArrayPointer::testClear @ 2 (72 bytes)
7612  102    b  4       TestMergeStoresUnsafeArrayPointer::testClear (72 bytes)
7651  103 %  b  4       TestMergeStoresUnsafeArrayPointer::testSum @ 4 (84 bytes)
7674  104    b  4       TestMergeStoresUnsafeArrayPointer::testSum (84 bytes)
7695  105    b  3       TestMergeStoresUnsafeArrayPointer::test1 (38 bytes)
7696  106    b  4       TestMergeStoresUnsafeArrayPointer::test1 (38 bytes)
[TraceMergeStores]: Replace
 54  StoreI  === 41 7 52 32  [[ 63 ]]  @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):exact+any *, idx=4; unsafe  Memory: @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):NotNull:exact+any *, idx=4; !jvms: TestMergeStoresUnsafeArrayPointer::test1 @ bci:20 (line 81)
 63  StoreI  === 41 54 61 58  [[ 18 ]]  @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):exact+any *, idx=4; unsafe  Memory: @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):NotNull:exact+any *, idx=4; !jvms: TestMergeStoresUnsafeArrayPointer::test1 @ bci:34 (line 82)
[TraceMergeStores]: with
 66  ConL  === 0  [[ 67 ]]  #long:7378697628877472322
 67  StoreL  === 41 7 52 66  [[ ]]  @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):NotNull:exact+any *, idx=4; mismatched  Memory: @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):NotNull:exact+any *, idx=4;
test2
7720  107    b  3       TestMergeStoresUnsafeArrayPointer::test2 (55 bytes)
7722  108    b  4       TestMergeStoresUnsafeArrayPointer::test2 (55 bytes)
[TraceMergeStores]: Replace
 63  StoreI  === 51 7 61 42  [[ 78 ]]  @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):exact+any *, idx=5; unsafe  Memory: @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):NotNull:exact+any *, idx=5; !jvms: TestMergeStoresUnsafeArrayPointer::test2 @ bci:30 (line 87)
 78  StoreI  === 51 63 76 73  [[ 18 ]]  @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):exact+any *, idx=5; unsafe  Memory: @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):NotNull:exact+any *, idx=5; !jvms: TestMergeStoresUnsafeArrayPointer::test2 @ bci:51 (line 88)
[TraceMergeStores]: with
 81  ConL  === 0  [[ 82 ]]  #long:7378697628877472322
 82  StoreL  === 51 7 61 81  [[ ]]  @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):NotNull:exact+any *, idx=5; mismatched  Memory: @int[int:>=0] (java/lang/Cloneable,java/io/Serializable):NotNull:exact+any *, idx=5;
ERROR: test2 had wrong value: 216504268185 != 217578010009
Exception in thread "main" java.lang.RuntimeException: ERRORS: 1
	at TestMergeStoresUnsafeArrayPointer.main(TestMergeStoresUnsafeArrayPointer.java:60)
Comments
ILW = Incorrect result of C2 compiled code, with Unsafe accesses, -XX:-MergeStores = HML = P2
01-07-2024

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/19964 Date: 2024-07-01 08:38:26 +0000
01-07-2024