1 /*
 2  * Copyright (c) 2024, 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  * @key stress randomness
27  * @bug 8333889
28  * @summary Test that speculative array access checks do not cause a load to be wrongly hoisted before its range check.
29  * @run main/othervm -XX:CompileCommand=dontinline,*::* -XX:CompileCommand=compileonly,*TestSpeculateArrayAccess::test
30  *                   -Xbatch -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:StressSeed=1202682944
31  *                   compiler.valhalla.inlinetypes.TestSpeculateArrayAccess
32  * @run main/othervm -XX:CompileCommand=dontinline,*::* -XX:CompileCommand=compileonly,*TestSpeculateArrayAccess::test
33  *                   -Xbatch -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM
34  *                   compiler.valhalla.inlinetypes.TestSpeculateArrayAccess
35  */
36 
37 package compiler.valhalla.inlinetypes;
38 
39 public class TestSpeculateArrayAccess {
40     static Object[] oArr = new Object[100];
41     static int iFld = 100;
42 
43     public static void main(String[] args) {
44         for (int i = 0; i < 100; i++) {
45             oArr[i] = new Object();
46         }
47         iFld = 1;
48         for (int i = 0; i < 1000; i++) {
49             test();
50         }
51         iFld = -1;
52 
53         try {
54             test();
55         } catch (ArrayIndexOutOfBoundsException e) {
56             // Expected.
57         }
58     }
59 
60     static void test() {
61         Object[] oA = oArr; // Load here to avoid G1 barriers being expanded inside loop which prevents Loop Predication.
62         for (float i = 0; i < 100; i++) {
63             // RangeCheck -> If-Speculative-Array-Type -> CastII
64             //
65             // At Loop Predication:
66             // - RangeCheck not hoisted because loop dependent
67             // - If-Speculative-Array-Type hoisted and CastII and LoadN ends up before loop
68             //
69             // Running with -XX:+StressGCM: We could execute the LoadN before entering the loop.
70             // This crashes when iFld = -1 because we then access an out-of-bounds element.
71             Object o = oA[(int)i*iFld];
72             o.toString(); // Use the object with its speculated type.
73         }
74     }
75 }
76