Skip to content

Commit 8b08668

Browse files
SendaoYanPaul Hohensee
authored andcommitted
8250825: C2 crashes with assert(field != __null) failed: missing field
Reviewed-by: phh Backport-of: e03ca73dc122af84d4a5456120e5cf5fac7aed31
1 parent bfd4d8b commit 8b08668

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

hotspot/src/share/vm/opto/type.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,7 +2540,7 @@ TypeOopPtr::TypeOopPtr(TYPES t, PTR ptr, ciKlass* k, bool xk, ciObject* o, int o
25402540
} else if (_offset == OffsetBot || _offset == OffsetTop) {
25412541
// unsafe access
25422542
_is_ptr_to_narrowoop = UseCompressedOops;
2543-
} else { // exclude unsafe ops
2543+
} else {
25442544
assert(this->isa_instptr(), "must be an instance ptr.");
25452545

25462546
if (klass() == ciEnv::current()->Class_klass() &&
@@ -2555,10 +2555,14 @@ TypeOopPtr::TypeOopPtr(TYPES t, PTR ptr, ciKlass* k, bool xk, ciObject* o, int o
25552555
assert(o != NULL, "must be constant");
25562556
ciInstanceKlass* k = o->as_instance()->java_lang_Class_klass()->as_instance_klass();
25572557
ciField* field = k->get_field_by_offset(_offset, true);
2558-
assert(field != NULL, "missing field");
2559-
BasicType basic_elem_type = field->layout_type();
2560-
_is_ptr_to_narrowoop = UseCompressedOops && (basic_elem_type == T_OBJECT ||
2558+
if (field != NULL) {
2559+
BasicType basic_elem_type = field->layout_type();
2560+
_is_ptr_to_narrowoop = UseCompressedOops && (basic_elem_type == T_OBJECT ||
25612561
basic_elem_type == T_ARRAY);
2562+
} else {
2563+
// unsafe access
2564+
_is_ptr_to_narrowoop = UseCompressedOops;
2565+
}
25622566
} else {
25632567
// Instance fields which contains a compressed oop references.
25642568
field = ik->get_field_by_offset(_offset, false);
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (C) 2020 THL A29 Limited, a Tencent company. 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 8250825
27+
* @summary "assert(field != __null) failed: missing field" in TypeOopPtr::TypeOopPt(...) with misaligned unsafe accesses
28+
* @library /test/lib
29+
* @modules java.base/jdk.internal.misc
30+
* @run main/othervm -XX:-TieredCompilation -Xcomp
31+
* -XX:CompileCommand=compileonly,TestMisalignedUnsafeAccess::test* TestMisalignedUnsafeAccess
32+
*/
33+
34+
import java.lang.reflect.Field;
35+
import sun.misc.Unsafe;
36+
import jdk.test.lib.Asserts;
37+
38+
public class TestMisalignedUnsafeAccess {
39+
40+
private static final Unsafe UNSAFE;
41+
static {
42+
Field f = null;
43+
try {
44+
f = Unsafe.class.getDeclaredField("theUnsafe");
45+
f.setAccessible(true);
46+
UNSAFE = (Unsafe) f.get(null);
47+
} catch (ReflectiveOperationException e) {
48+
throw new Error(e);
49+
}
50+
}
51+
52+
private static short onHeapStaticMemory; // For static field testing
53+
private static final Object onHeapStaticMemoryBase;
54+
private static final long onHeapStaticMemoryOffset;
55+
56+
private short onHeapInstanceMemory; // For instance field testing
57+
private static final long onHeapInstanceMemoryOffset;
58+
59+
static {
60+
try {
61+
Field staticField = TestMisalignedUnsafeAccess.class.getDeclaredField("onHeapStaticMemory");
62+
onHeapStaticMemoryBase = UNSAFE.staticFieldBase(staticField);
63+
onHeapStaticMemoryOffset = UNSAFE.staticFieldOffset(staticField);
64+
65+
Field instanceField = TestMisalignedUnsafeAccess.class.getDeclaredField("onHeapInstanceMemory");
66+
onHeapInstanceMemoryOffset = UNSAFE.objectFieldOffset(instanceField);
67+
} catch (Exception e) {
68+
throw new RuntimeException(e);
69+
}
70+
}
71+
72+
public static void testStaticField() {
73+
byte b1 = 0x01;
74+
byte b2 = 0x02;
75+
76+
UNSAFE.putByte(onHeapStaticMemoryBase, onHeapStaticMemoryOffset, b1);
77+
UNSAFE.putByte(onHeapStaticMemoryBase, onHeapStaticMemoryOffset + 1, b2);
78+
79+
Asserts.assertEquals(b1, UNSAFE.getByte(onHeapStaticMemoryBase, onHeapStaticMemoryOffset));
80+
Asserts.assertEquals(b2, UNSAFE.getByte(onHeapStaticMemoryBase, onHeapStaticMemoryOffset + 1));
81+
}
82+
83+
public static void testInstanceField() {
84+
byte b1 = 0x03;
85+
byte b2 = 0x04;
86+
TestMisalignedUnsafeAccess obj = new TestMisalignedUnsafeAccess();
87+
88+
UNSAFE.putByte(obj, onHeapInstanceMemoryOffset, b1);
89+
UNSAFE.putByte(obj, onHeapInstanceMemoryOffset + 1, b2);
90+
91+
Asserts.assertEquals(b1, UNSAFE.getByte(obj, onHeapInstanceMemoryOffset));
92+
Asserts.assertEquals(b2, UNSAFE.getByte(obj, onHeapInstanceMemoryOffset + 1));
93+
}
94+
95+
public static void main(String[] args) {
96+
testStaticField();
97+
testInstanceField();
98+
}
99+
}

0 commit comments

Comments
 (0)