Skip to content

Commit b53b049

Browse files
marc-chevalierTobiHartmann
authored andcommitted
8365978: [lworld] C2: assert(vk->maybe_flat_in_array()) when using compareAndSetFlatValue with -XX:-UseArrayFlattening
Reviewed-by: thartmann
1 parent 3620108 commit b53b049

File tree

2 files changed

+101
-10
lines changed

2 files changed

+101
-10
lines changed

src/hotspot/share/opto/library_call.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,16 +2825,22 @@ bool LibraryCallKit::inline_unsafe_flat_access(bool is_store, AccessKind kind) {
28252825
}
28262826
ptr = basic_plus_adr(base, ConvL2X(offset));
28272827
} else {
2828-
// Flat array must have an exact type
2829-
bool is_null_free = layout != LayoutKind::NULLABLE_ATOMIC_FLAT;
2830-
bool is_atomic = layout != LayoutKind::NON_ATOMIC_FLAT;
2831-
Node* new_base = cast_to_flat_array(base, value_klass, is_null_free, !is_null_free, is_atomic);
2832-
replace_in_map(base, new_base);
2833-
base = new_base;
2834-
ptr = basic_plus_adr(base, ConvL2X(offset));
2835-
const TypeAryPtr* ptr_type = _gvn.type(ptr)->is_aryptr();
2836-
if (ptr_type->field_offset().get() != 0) {
2837-
ptr = _gvn.transform(new CastPPNode(control(), ptr, ptr_type->with_field_offset(0), ConstraintCastNode::StrongDependency));
2828+
if (UseArrayFlattening) {
2829+
// Flat array must have an exact type
2830+
bool is_null_free = layout != LayoutKind::NULLABLE_ATOMIC_FLAT;
2831+
bool is_atomic = layout != LayoutKind::NON_ATOMIC_FLAT;
2832+
Node* new_base = cast_to_flat_array(base, value_klass, is_null_free, !is_null_free, is_atomic);
2833+
replace_in_map(base, new_base);
2834+
base = new_base;
2835+
ptr = basic_plus_adr(base, ConvL2X(offset));
2836+
const TypeAryPtr* ptr_type = _gvn.type(ptr)->is_aryptr();
2837+
if (ptr_type->field_offset().get() != 0) {
2838+
ptr = _gvn.transform(new CastPPNode(control(), ptr, ptr_type->with_field_offset(0), ConstraintCastNode::StrongDependency));
2839+
}
2840+
} else {
2841+
uncommon_trap(Deoptimization::Reason_intrinsic,
2842+
Deoptimization::Action_none);
2843+
return true;
28382844
}
28392845
}
28402846
} else {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2025, 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 8365978
27+
* @summary Unsafe::compareAndSetFlatValue crashes with -XX:-UseArrayFlattening
28+
* @enablePreview
29+
* @library /test/lib
30+
* @modules java.base/jdk.internal.misc
31+
* @run main/othervm -XX:CompileCommand=compileonly,compiler.valhalla.inlinetypes.PutFlatValueWithoutUseArrayFlattening::test
32+
* -XX:-TieredCompilation -Xcomp
33+
* -XX:-UseArrayFlattening -XX:+UseFieldFlattening
34+
* compiler.valhalla.inlinetypes.PutFlatValueWithoutUseArrayFlattening
35+
* @run main/othervm -XX:+UseFieldFlattening
36+
* compiler.valhalla.inlinetypes.PutFlatValueWithoutUseArrayFlattening
37+
*/
38+
39+
package compiler.valhalla.inlinetypes;
40+
41+
import java.lang.reflect.Field;
42+
import jdk.internal.misc.Unsafe;
43+
import jdk.test.lib.Asserts;
44+
45+
public class PutFlatValueWithoutUseArrayFlattening {
46+
static public value class SmallValue {
47+
byte a;
48+
byte b;
49+
SmallValue(int a, int b) {
50+
this.a = (byte)a;
51+
this.b = (byte)b;
52+
}
53+
}
54+
55+
SmallValue f;
56+
private static final long OFFSET;
57+
private static final boolean IS_FLATTENED;
58+
private static final int LAYOUT;
59+
static private final Unsafe U = Unsafe.getUnsafe();
60+
static {
61+
try {
62+
Field f = PutFlatValueWithoutUseArrayFlattening.class.getDeclaredField("f");
63+
OFFSET = U.objectFieldOffset(f);
64+
IS_FLATTENED = U.isFlatField(f);
65+
Asserts.assertTrue(IS_FLATTENED, "Field f should be flat, the test makes no sense otherwise. And why isn't it?!");
66+
LAYOUT = U.fieldLayout(f);
67+
} catch (Exception e) {
68+
throw new RuntimeException(e);
69+
}
70+
}
71+
72+
public void test(boolean flag) {
73+
var newVal = new SmallValue(1, 1);
74+
var oldVal = new SmallValue(0, 0);
75+
f = oldVal;
76+
if (flag) {
77+
U.compareAndSetFlatValue(this, OFFSET, LAYOUT, SmallValue.class, oldVal, newVal);
78+
}
79+
}
80+
81+
static public void main(String args[]) {
82+
new SmallValue(0, 0);
83+
new PutFlatValueWithoutUseArrayFlattening().test(false);
84+
}
85+
}

0 commit comments

Comments
 (0)