Path: blob/master/test/hotspot/jtreg/compiler/classUnloading/methodUnloading/TestMethodUnloading.java
41153 views
/*1* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test MethodUnloadingTest25* @bug 802944326* @summary Tests the unloading of methods to to class unloading27* @modules java.base/jdk.internal.misc28* @library /test/lib /29*30* @build sun.hotspot.WhiteBox31* compiler.classUnloading.methodUnloading.WorkerClass32* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox33* @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions34* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI35* -XX:-BackgroundCompilation -XX:-UseCompressedOops36* -XX:CompileCommand=compileonly,compiler.classUnloading.methodUnloading.TestMethodUnloading::doWork37* compiler.classUnloading.methodUnloading.TestMethodUnloading38*/3940package compiler.classUnloading.methodUnloading;4142import sun.hotspot.WhiteBox;4344import java.lang.reflect.Method;45import java.net.URL;46import java.net.URLClassLoader;47import compiler.whitebox.CompilerWhiteBoxTest;4849public class TestMethodUnloading {50private static final String workerClassName = "compiler.classUnloading.methodUnloading.WorkerClass";51private static int work = -1;5253private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();5455/**56* Does some work by either using the workerClass or locally producing values.57* @param workerClass Class performing some work (will be unloaded)58* @param useWorker If true the workerClass is used59*/60static private void doWork(Class<?> workerClass, boolean useWorker) throws InstantiationException, IllegalAccessException {61if (useWorker) {62// Create a new instance63Object worker = workerClass.newInstance();64// We would like to call a method of WorkerClass here but we cannot cast to WorkerClass65// because the class was loaded by a different class loader. One solution would be to use66// reflection but since we want C2 to implement the call as an optimized IC we call67// Object::hashCode() here which actually calls WorkerClass::hashCode().68// C2 will then implement this call as an optimized IC that points to a to-interpreter stub69// referencing the Method* for WorkerClass::hashCode().70work = worker.hashCode();71if (work != 42) {72new RuntimeException("Work not done");73}74} else {75// Do some important work here76work = 1;77}78}7980/**81* Makes sure that method is compiled by forcing compilation if not yet compiled.82* @param m Method to be checked83*/84static private void makeSureIsCompiled(Method m) {85// Make sure background compilation is disabled86if (WHITE_BOX.getBooleanVMFlag("BackgroundCompilation")) {87throw new RuntimeException("Background compilation enabled");88}8990// Check if already compiled91if (!WHITE_BOX.isMethodCompiled(m)) {92// If not, try to compile it with C293if(!WHITE_BOX.enqueueMethodForCompilation(m, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION)) {94// C2 compiler not available, try to compile with C195WHITE_BOX.enqueueMethodForCompilation(m, CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE);96}97// Because background compilation is disabled, method should now be compiled98if(!WHITE_BOX.isMethodCompiled(m)) {99throw new RuntimeException(m + " not compiled");100}101}102}103104/**105* This test creates stale Method* metadata in a to-interpreter stub of an optimized IC.106*107* The following steps are performed:108* (1) A workerClass is loaded by a custom class loader109* (2) The method doWork that calls a method of the workerClass is compiled. The call110* is implemented as an optimized IC calling a to-interpreted stub. The to-interpreter111* stub contains a Method* to a workerClass method.112* (3) Unloading of the workerClass is enforced. The to-interpreter stub now contains a dead Method*.113* (4) Depending on the implementation of the IC, the compiled version of doWork should still be114* valid. We call it again without using the workerClass.115*/116static public void main(String[] args) throws Exception {117// (1) Create a custom class loader with no parent class loader118URL url = TestMethodUnloading.class.getProtectionDomain().getCodeSource().getLocation();119URLClassLoader loader = new URLClassLoader(new URL[] {url}, null);120121// Load worker class with custom class loader122Class<?> workerClass = Class.forName(workerClassName, true, loader);123124// (2) Make sure all paths of doWork are profiled and compiled125for (int i = 0; i < 100000; ++i) {126doWork(workerClass, true);127doWork(workerClass, false);128}129130// Make sure doWork is compiled now131Method doWork = TestMethodUnloading.class.getDeclaredMethod("doWork", Class.class, boolean.class);132makeSureIsCompiled(doWork);133134// (3) Throw away class loader and reference to workerClass to allow unloading135loader.close();136loader = null;137workerClass = null;138139// Force garbage collection to trigger unloading of workerClass140// Dead reference to WorkerClass::hashCode triggers JDK-8029443141WHITE_BOX.fullGC();142143// (4) Depending on the implementation of the IC, the compiled version of doWork144// may still be valid here. Execute it without a workerClass.145doWork(null, false);146if (work != 1) {147throw new RuntimeException("Work not done");148}149150doWork(Object.class, false);151}152}153154155