Skip to content

Commit

Permalink
8297936: Use reachabilityFence to manage liveness in ClassUnload tests
Browse files Browse the repository at this point in the history
Reviewed-by: coleenp, dholmes
  • Loading branch information
afshin-zafari authored and JesperIRL committed Mar 3, 2023
1 parent 379f206 commit 5085bd5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -71,8 +72,8 @@ static void test() throws Throwable {
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 {
Expand Down
8 changes: 4 additions & 4 deletions test/hotspot/jtreg/runtime/ClassUnload/KeepAliveObject.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -66,8 +65,9 @@ public static void main(String... args) throws Exception {

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();

{
Expand Down
17 changes: 10 additions & 7 deletions test/hotspot/jtreg/runtime/ClassUnload/UnloadTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -36,6 +36,7 @@
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.
Expand All @@ -48,12 +49,10 @@
* 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 {
Expand All @@ -63,7 +62,7 @@ private static void test_unload_instance_klass() throws Exception {
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");

Expand All @@ -76,6 +75,8 @@ private static void test_unload_instance_klass() throws Exception {

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();

Expand All @@ -91,7 +92,7 @@ private static void test_unload_obj_array_klass() throws Exception {
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");
Expand All @@ -104,6 +105,8 @@ private static void test_unload_obj_array_klass() throws Exception {
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();

Expand Down

1 comment on commit 5085bd5

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.