8297936: Use reachabilityFence to manage liveness in ClassUnload tests

Reviewed-by: coleenp, dholmes
This commit is contained in:
Afshin Zafari 2023-03-03 16:44:27 +00:00 committed by Jesper Wilhelmsson
parent 379f2061aa
commit 5085bd5f05
3 changed files with 18 additions and 14 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2023, 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
@ -38,6 +38,7 @@
import jdk.test.whitebox.WhiteBox;
import jdk.test.lib.classloader.ClassUnloadCommon;
import java.lang.ref.Reference;
public class ConstantPoolDependsTest {
public static WhiteBox wb = WhiteBox.getWhiteBox();
public static final String MY_TEST = "ConstantPoolDependsTest$c1c";
@ -71,8 +72,8 @@ public class ConstantPoolDependsTest {
ClassUnloadCommon.triggerUnloading(); // should not unload anything
ClassUnloadCommon.failIf(!wb.isClassAlive(MY_TEST), "should not be unloaded");
ClassUnloadCommon.failIf(!wb.isClassAlive("p2.c2"), "should not be unloaded");
// Unless MyTest_class is referenced here, the compiler can unload it.
System.out.println("Should not unload anything before here because " + MyTest_class + " is still alive.");
// Should not unload anything before here because MyTest_class is kept alive by the following fence.
Reference.reachabilityFence(MyTest_class);
}
public static void main(String args[]) throws Throwable {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, 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
@ -36,20 +36,19 @@
import jdk.test.whitebox.WhiteBox;
import jdk.test.lib.classloader.ClassUnloadCommon;
import java.lang.ref.Reference;
/**
* Test that verifies that classes are not unloaded when specific types of references are kept to them.
*/
public class KeepAliveObject {
private static final String className = "test.Empty";
private static final WhiteBox wb = WhiteBox.getWhiteBox();
public static Object escape = null;
public static void main(String... args) throws Exception {
ClassLoader cl = ClassUnloadCommon.newClassLoader();
Class<?> c = cl.loadClass(className);
Object o = c.newInstance();
cl = null; c = null;
escape = o;
{
boolean isAlive = wb.isClassAlive(className);
@ -66,8 +65,9 @@ public class KeepAliveObject {
ClassUnloadCommon.failIf(!isAlive, "should be alive");
}
// Don't let `o` get prematurely reclaimed by the GC.
Reference.reachabilityFence(o);
o = null;
escape = null;
ClassUnloadCommon.triggerUnloading();
{

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, 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
@ -36,6 +36,7 @@ import jdk.test.whitebox.WhiteBox;
import jdk.test.lib.classloader.ClassUnloadCommon;
import java.lang.reflect.Array;
import java.lang.ref.Reference;
/**
* Test that verifies that liveness of classes is correctly tracked.
@ -48,12 +49,10 @@ import java.lang.reflect.Array;
* and verifies the class is unloaded.
*/
public class UnloadTest {
// Using a global static field to keep the object live in -Xcomp mode.
private static Object o;
public static void main(String... args) throws Exception {
test_unload_instance_klass();
test_unload_obj_array_klass();
test_unload_instance_klass();
test_unload_obj_array_klass();
}
private static void test_unload_instance_klass() throws Exception {
@ -63,7 +62,7 @@ public class UnloadTest {
ClassUnloadCommon.failIf(wb.isClassAlive(className), "is not expected to be alive yet");
ClassLoader cl = ClassUnloadCommon.newClassLoader();
o = cl.loadClass(className).newInstance();
Object o = cl.loadClass(className).newInstance();
ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should be live here");
@ -76,6 +75,8 @@ public class UnloadTest {
ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should still be live");
// Don't let `o` get prematurely reclaimed by the GC.
Reference.reachabilityFence(o);
o = null;
ClassUnloadCommon.triggerUnloading();
@ -91,7 +92,7 @@ public class UnloadTest {
final WhiteBox wb = WhiteBox.getWhiteBox();
ClassLoader cl = ClassUnloadCommon.newClassLoader();
o = Array.newInstance(cl.loadClass("test.Empty"), 1);
Object o = Array.newInstance(cl.loadClass("test.Empty"), 1);
final String className = o.getClass().getName();
ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should be live here");
@ -104,6 +105,8 @@ public class UnloadTest {
ClassUnloadCommon.triggerUnloading();
ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should still be live");
// Don't let `o` get prematurely reclaimed by the GC.
Reference.reachabilityFence(o);
o = null;
ClassUnloadCommon.triggerUnloading();