Skip to content

Commit 7966955

Browse files
committed
JDK-8384405
1 parent b13d505 commit 7966955

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

src/hotspot/share/opto/inlinetypenode.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,8 @@ InlineTypeNode* InlineTypeNode::make_from_flat_impl(GraphKit* kit, ciInlineKlass
14561456
InlineTypeNode* InlineTypeNode::make_from_flat_array(GraphKit* kit, ciInlineKlass* vk, Node* base, Node* idx) {
14571457
assert(vk->maybe_flat_in_array(), "element type %s cannot be flat in array", vk->name()->as_utf8());
14581458
PhaseGVN& gvn = kit->gvn();
1459-
DecoratorSet decorators = IN_HEAP | IS_ARRAY | MO_UNORDERED | C2_CONTROL_DEPENDENT_LOAD;
1459+
// The flat field loads are dependent on both the array layout checks as well as the range check.
1460+
DecoratorSet decorators = IN_HEAP | IS_ARRAY | MO_UNORDERED | C2_CONTROL_DEPENDENT_LOAD | C2_UNKNOWN_CONTROL_LOAD;
14601461
kit->C->set_flat_accesses();
14611462
InlineTypeNode* vt_nullable = nullptr;
14621463
InlineTypeNode* vt_null_free = nullptr;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)