1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8236140
27  * @requires vm.gc.Serial
28  * @summary Tests proper rehashing of a captured volatile field StoreL node when completing it.
29  * @run main/othervm -Xbatch -XX:+UseSerialGC -XX:CompileCommand=compileonly,compiler.macronodes.TestCompleteVolatileStore::test
30  *                   compiler.macronodes.TestCompleteVolatileStore
31  */
32 
33 package compiler.macronodes;
34 
35 public class TestCompleteVolatileStore {
36     int i1 = 4;
37 
test()38     public void test() {
39         /*
40          * The store to the volatile field 'l1' (StoreL) of 'a' is captured in the Initialize node of 'a'
41          * (i.e. additional input to it) and completed in InitializeNode::complete_stores.
42          * Since 'l1' is volatile, the hash of the StoreL is non-zero triggering the hash assertion failure.
43          */
44         A a = new A();
45 
46         // Make sure that the CheckCastPP node of 'a' is used in the input chain of the Intialize node of 'b'
47         B b = new B(a);
48 
49         // Make sure 'b' is non-scalar-replacable to avoid eliminating all allocations
50         B[] arr = new B[i1];
51         arr[i1-3] = b;
52     }
53 
main(String[] strArr)54     public static void main(String[] strArr) {
55         TestCompleteVolatileStore _instance = new TestCompleteVolatileStore();
56         for (int i = 0; i < 10_000; i++ ) {
57             _instance.test();
58         }
59     }
60 }
61 
62 class A {
63     // Needs to be volatile to have a non-zero hash for the StoreL node.
64     volatile long l1;
65 
A()66     A() {
67         // StoreL gets captured and is later processed in InitializeNode::complete_stores while expanding the allocation node.
68         this.l1 = 256;
69     }
70 }
71 
72 class B {
73     A a;
74 
B(A a)75     B(A a) {
76         this.a = a;
77     }
78 }
79 
80