8382815: C2: Reference intrinsics are broken by shadow "referent" field

Reviewed-by: kvn, vlivanov, qamai
This commit is contained in:
Aleksey Shipilev 2026-04-23 17:59:03 +00:00
parent 989b1882d4
commit e1d889ee53
3 changed files with 281 additions and 36 deletions

View File

@ -6932,7 +6932,8 @@ bool LibraryCallKit::inline_reference_get0() {
DecoratorSet decorators = IN_HEAP | ON_WEAK_OOP_REF;
Node* result = load_field_from_object(reference_obj, "referent", "Ljava/lang/Object;",
decorators, /*is_static*/ false, nullptr);
decorators, /*is_static*/ false,
env()->Reference_klass());
if (result == nullptr) return false;
// Add memory barrier to prevent commoning reads from this field
@ -6955,7 +6956,8 @@ bool LibraryCallKit::inline_reference_refersTo0(bool is_phantom) {
DecoratorSet decorators = IN_HEAP | AS_NO_KEEPALIVE;
decorators |= (is_phantom ? ON_PHANTOM_OOP_REF : ON_WEAK_OOP_REF);
Node* referent = load_field_from_object(reference_obj, "referent", "Ljava/lang/Object;",
decorators, /*is_static*/ false, nullptr);
decorators, /*is_static*/ false,
env()->Reference_klass());
if (referent == nullptr) return false;
// Add memory barrier to prevent commoning reads from this field
@ -7042,8 +7044,6 @@ Node* LibraryCallKit::load_field_from_object(Node* fromObj, const char* fieldNam
assert(tinst != nullptr, "obj is null");
assert(tinst->is_loaded(), "obj is not loaded");
fromKls = tinst->instance_klass();
} else {
assert(is_static, "only for static field access");
}
ciField* field = fromKls->get_field_by_name(ciSymbol::make(fieldName),
ciSymbol::make(fieldTypeString),

View File

@ -0,0 +1,175 @@
/*
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8382815
* @run main/othervm -XX:CompileCommand=dontinline,${test.main.class}::test_* ${test.main.class}
*/
package compiler.intrinsics;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.PhantomReference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
public class TestReferenceGet {
private static final void fail(String msg) throws Exception {
throw new RuntimeException(msg);
}
private static final void test0(Reference ref,
Object expectedValue,
Object unexpectedValue,
String kind) throws Exception {
if ((expectedValue != null) && ref.get() == null) {
fail(kind + " refers to null");
}
if (ref.get() != expectedValue) {
fail(kind + " doesn't refer to expected value");
}
if (ref.get() == unexpectedValue) {
fail(kind + " refers to unexpected value");
}
}
private static final void test_phantom0(PhantomReference ref,
String kind) throws Exception {
if (ref.get() != null) {
fail(kind + " does not refer to null");
}
}
// Entry points to the test, important to push down type information to
// individual test methods.
private static final void test_phantom(PhantomReference ref) throws Exception {
test_phantom0(ref, "phantom");
}
private static final void test_phantom_shadow(ShadowPhantomReference ref) throws Exception {
test_phantom0(ref, "phantom shadow");
}
private static final void test_weak(WeakReference ref,
Object expectedValue,
Object unexpectedValue) throws Exception {
test0(ref, expectedValue, unexpectedValue, "weak");
}
private static final void test_weak_shadow(ShadowWeakReference ref,
Object expectedValue,
Object unexpectedValue) throws Exception {
test0(ref, expectedValue, unexpectedValue, "weak shadow");
}
private static final void test_soft(SoftReference ref,
Object expectedValue,
Object unexpectedValue) throws Exception {
test0(ref, expectedValue, unexpectedValue, "soft");
}
private static final void test_soft_shadow(ShadowSoftReference ref,
Object expectedValue,
Object unexpectedValue) throws Exception {
test0(ref, expectedValue, unexpectedValue, "soft shadow");
}
static Object unexpected = new Object();
static Object obj0 = new Object();
static Object obj1 = new Object();
static Object obj2 = new Object();
static Object obj3 = new Object();
static Object obj4 = new Object();
static Object obj5 = new Object();
public static void main(String[] args) throws Exception {
var queue = new ReferenceQueue<Object>();
// It is important to do all test methods in the loop, so that we
// exercise all paths in intrinsics.
for (int i = 0; i < 100000; i++) {
System.out.println("Create");
var pref = new PhantomReference(obj0, queue);
var wref = new WeakReference(obj1);
var sref = new SoftReference(obj2);
var psref = new ShadowPhantomReference<>(obj3, queue);
var wsref = new ShadowWeakReference<>(obj4);
var ssref = new ShadowSoftReference<>(obj5);
System.out.println("After creation");
test_phantom(pref);
test_weak(wref, obj1, unexpected);
test_soft(sref, obj2, unexpected);
test_phantom_shadow(psref);
test_weak_shadow(wsref, obj4, unexpected);
test_soft_shadow(ssref, obj5, unexpected);
System.out.println("Cleaning references");
pref.clear();
wref.clear();
sref.clear();
psref.clear();
wsref.clear();
ssref.clear();
System.out.println("Testing after cleaning");
test_phantom(pref);
test_weak(wref, null, unexpected);
test_soft(sref, null, unexpected);
test_phantom_shadow(psref);
test_weak_shadow(wsref, null, unexpected);
test_soft_shadow(ssref, null, unexpected);
}
}
// References that have their own "shadow" referent. Check that intrinsics
// hit the right referent.
static class ShadowSoftReference<T> extends SoftReference<T> {
T referent;
public ShadowSoftReference(T ref) {
super(ref);
referent = ref;
}
}
static class ShadowWeakReference<T> extends WeakReference<T> {
T referent;
public ShadowWeakReference(T ref) {
super(ref);
referent = ref;
}
}
static class ShadowPhantomReference<T> extends PhantomReference<T> {
T referent;
public ShadowPhantomReference(T ref, ReferenceQueue<Object> q) {
super(ref, q);
referent = ref;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,10 +23,13 @@
/*
* @test
* @bug 8256377
* @bug 8256377 8382815
* @summary Based on test/jdk/java/lang/ref/ReferenceRefersTo.java.
* @run main/othervm -XX:CompileCommand=dontinline,${test.main.class}::test_* ${test.main.class}
*/
package compiler.intrinsics;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.PhantomReference;
@ -39,7 +42,7 @@ public class TestReferenceRefersTo {
}
// Test java.lang.ref.Reference::refersTo0 intrinsic.
private static final void test(Reference ref,
private static final void test0(Reference ref,
Object expectedValue,
Object unexpectedValue,
String kind) throws Exception {
@ -55,10 +58,10 @@ public class TestReferenceRefersTo {
}
// Test java.lang.ref.PhantomReference::refersTo0 intrinsic.
private static final void test_phantom(PhantomReference ref,
private static final void test_phantom0(PhantomReference ref,
Object expectedValue,
Object unexpectedValue) throws Exception {
String kind = "phantom";
Object unexpectedValue,
String kind) throws Exception {
if ((expectedValue != null) && ref.refersTo(null)) {
fail(kind + " refers to null");
}
@ -68,53 +71,120 @@ public class TestReferenceRefersTo {
if (ref.refersTo(unexpectedValue)) {
fail(kind + " refers to unexpected value");
}
}
// Entry points to the test, important to push down type information to
// individual test methods.
private static final void test_phantom(PhantomReference ref,
Object expectedValue,
Object unexpectedValue) throws Exception {
test_phantom0(ref, expectedValue, unexpectedValue, "phantom");
}
private static final void test_phantom_shadow(ShadowPhantomReference ref,
Object expectedValue,
Object unexpectedValue) throws Exception {
test_phantom0(ref, expectedValue, unexpectedValue, "phantom shadow");
}
private static final void test_weak(WeakReference ref,
Object expectedValue,
Object unexpectedValue) throws Exception {
test(ref, expectedValue, unexpectedValue, "weak");
test0(ref, expectedValue, unexpectedValue, "weak");
}
private static final void test_weak_shadow(ShadowWeakReference ref,
Object expectedValue,
Object unexpectedValue) throws Exception {
test0(ref, expectedValue, unexpectedValue, "weak shadow");
}
private static final void test_soft(SoftReference ref,
Object expectedValue,
Object unexpectedValue) throws Exception {
test(ref, expectedValue, unexpectedValue, "soft");
test0(ref, expectedValue, unexpectedValue, "soft");
}
private static final void test_soft_shadow(ShadowSoftReference ref,
Object expectedValue,
Object unexpectedValue) throws Exception {
test0(ref, expectedValue, unexpectedValue, "soft shadow");
}
static Object unexpected = new Object();
static Object obj0 = new Object();
static Object obj1 = new Object();
static Object obj2 = new Object();
static Object obj3 = new Object();
static Object obj4 = new Object();
static Object obj5 = new Object();
public static void main(String[] args) throws Exception {
var queue = new ReferenceQueue<Object>();
var obj0 = new Object();
var obj1 = new Object();
var obj2 = new Object();
var obj3 = new Object();
// It is important to do all test methods in the loop, so that we
// exercise all paths in intrinsics.
for (int i = 0; i < 100000; i++) {
System.out.println("Create");
var pref = new PhantomReference(obj0, queue);
var wref = new WeakReference(obj1);
var sref = new SoftReference(obj2);
var psref = new ShadowPhantomReference<>(obj3, queue);
var wsref = new ShadowWeakReference<>(obj4);
var ssref = new ShadowSoftReference<>(obj5);
var pref = new PhantomReference(obj0, queue);
var wref = new WeakReference(obj1);
var sref = new SoftReference(obj2);
System.out.println("After creation");
test_phantom(pref, obj0, unexpected);
test_weak(wref, obj1, unexpected);
test_soft(sref, obj2, unexpected);
test_phantom_shadow(psref, obj3, unexpected);
test_weak_shadow(wsref, obj4, unexpected);
test_soft_shadow(ssref, obj5, unexpected);
System.out.println("Warmup");
for (int i = 0; i < 10000; i++) {
test_phantom(pref, obj0, obj3);
test_weak(wref, obj1, obj3);
test_soft(sref, obj2, obj3);
System.out.println("Cleaning references");
pref.clear();
wref.clear();
sref.clear();
psref.clear();
wsref.clear();
ssref.clear();
System.out.println("Testing after cleaning");
test_phantom(pref, null, unexpected);
test_weak(wref, null, unexpected);
test_soft(sref, null, unexpected);
test_phantom_shadow(psref, null, unexpected);
test_weak_shadow(wsref, null, unexpected);
test_soft_shadow(ssref, null, unexpected);
}
}
System.out.println("Testing starts");
test_phantom(pref, obj0, obj3);
test_weak(wref, obj1, obj3);
test_soft(sref, obj2, obj3);
// References that have their own "shadow" referent. Check that intrinsics
// hit the right referent.
System.out.println("Cleaning references");
pref.clear();
wref.clear();
sref.clear();
static class ShadowSoftReference<T> extends SoftReference<T> {
T referent;
public ShadowSoftReference(T ref) {
super(ref);
referent = ref;
}
}
System.out.println("Testing after cleaning");
test_phantom(pref, null, obj3);
test_weak(wref, null, obj3);
test_soft(sref, null, obj3);
static class ShadowWeakReference<T> extends WeakReference<T> {
T referent;
public ShadowWeakReference(T ref) {
super(ref);
referent = ref;
}
}
static class ShadowPhantomReference<T> extends PhantomReference<T> {
T referent;
public ShadowPhantomReference(T ref, ReferenceQueue<Object> q) {
super(ref, q);
referent = ref;
}
}
}