|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, 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 | + * @summary Test correct pinning of loads from flat arrays. |
| 27 | + * @bug 8384405 |
| 28 | + * @requires vm.compiler2.enabled |
| 29 | + * @enablePreview |
| 30 | + * @modules java.base/jdk.internal.value |
| 31 | + * java.base/jdk.internal.vm.annotation |
| 32 | + * @run main ${test.main.class} |
| 33 | + * @run main/othervm -Xcomp -XX:CompileCommand=compileonly,${test.main.class}::test |
| 34 | + * -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM |
| 35 | + * ${test.main.class} |
| 36 | + */ |
| 37 | + |
| 38 | +package compiler.valhalla.inlinetypes; |
| 39 | + |
| 40 | +import jdk.internal.value.ValueClass; |
| 41 | +import jdk.internal.vm.annotation.LooselyConsistentValue; |
| 42 | + |
| 43 | +public class TestFlatArrayLoadPinning { |
| 44 | + |
| 45 | + @LooselyConsistentValue |
| 46 | + static value class MyValueWithInts { |
| 47 | + int i1; |
| 48 | + int i2; |
| 49 | + |
| 50 | + MyValueWithInts(int i) { |
| 51 | + i1 = i; |
| 52 | + i2 = i; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + static int test(MyValueWithInts[] array, int limit) { |
| 57 | + int res = 0; |
| 58 | + for (int i = 0; i < limit; ++i) { |
| 59 | + int index = (i == 7) ? Integer.MAX_VALUE : i; |
| 60 | + // If below accesses are not pinned to both the flat-array layout check, |
| 61 | + // as well as the range check, they can float above and access out-of-bounds. |
| 62 | + MyValueWithInts val = array[index]; |
| 63 | + res += val.i1 + val.i2; |
| 64 | + } |
| 65 | + return res; |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + public static void main(String[] args) { |
| 70 | + MyValueWithInts[] array = (MyValueWithInts[])ValueClass.newNullRestrictedAtomicArray(MyValueWithInts.class, 10, new MyValueWithInts(42)); |
| 71 | + for (int i = 0; i < 10; ++i) { |
| 72 | + try { |
| 73 | + test(array, 10); |
| 74 | + throw new RuntimeException("No ArrayIndexOutOfBoundsException thrown!"); |
| 75 | + } catch (ArrayIndexOutOfBoundsException expected) { |
| 76 | + // Expected |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
0 commit comments