Path: blob/master/test/hotspot/jtreg/runtime/HiddenClasses/TestHiddenClassUnloading.java
40942 views
/*1* Copyright (c) 2020, 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* @test25* @summary Test unloading of hidden classes.26* @modules java.management27* @library /test/lib /28* @build sun.hotspot.WhiteBox29* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox30*31* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI32* -XX:-BackgroundCompilation33* compiler.classUnloading.hiddenClass.TestHiddenClassUnloading34*/3536package compiler.classUnloading.hiddenClass;3738import sun.hotspot.WhiteBox;3940import java.io.IOException;41import java.lang.invoke.MethodHandles;42import java.lang.invoke.MethodHandles.Lookup;43import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;44import java.lang.reflect.Method;45import java.net.URL;46import java.net.URLConnection;47import compiler.whitebox.CompilerWhiteBoxTest;4849public class TestHiddenClassUnloading {50private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();5152/**53* We override hashCode here to be able to access this implementation54* via an Object reference (we cannot cast to TestHiddenClassUnloading).55*/56@Override57public int hashCode() {58return 42;59}6061/**62* Does some work by using the hiddenClass.63* @param hiddenClass Class performing some work (will be unloaded)64*/65static private void doWork(Class<?> hiddenClass) throws InstantiationException, IllegalAccessException {66// Create a new instance67Object anon = hiddenClass.newInstance();68// We would like to call a method of hiddenClass here but we cannot cast because the class69// was loaded by a different class loader. One solution would be to use reflection but since70// we want C2 to implement the call as an IC we call Object::hashCode() here which actually71// calls hiddenClass::hashCode(). C2 will then implement this call as an IC.72if (anon.hashCode() != 42) {73new RuntimeException("Work not done");74}75}7677/**78* Makes sure that method is compiled by forcing compilation if not yet compiled.79* @param m Method to be checked80*/81static private void makeSureIsCompiled(Method m) {82// Make sure background compilation is disabled83if (WHITE_BOX.getBooleanVMFlag("BackgroundCompilation")) {84throw new RuntimeException("Background compilation enabled");85}8687// Check if already compiled88if (!WHITE_BOX.isMethodCompiled(m)) {89// If not, try to compile it with C290if(!WHITE_BOX.enqueueMethodForCompilation(m, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION)) {91// C2 compiler not available, try to compile with C192WHITE_BOX.enqueueMethodForCompilation(m, CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE);93}94// Because background compilation is disabled, method should now be compiled95if(!WHITE_BOX.isMethodCompiled(m)) {96throw new RuntimeException(m + " not compiled");97}98}99}100101/**102* This test creates stale Klass* metadata referenced by a compiled IC.103*104* The following steps are performed:105* (1) A hidden version of TestHiddenClassUnloading is loaded by a custom class loader106* (2) The method doWork that calls a method of the hidden class is compiled. The call107* is implemented as an IC referencing Klass* metadata of the hidden class.108* (3) Unloading of the hidden class is enforced. The IC now references dead metadata.109*/110static public void main(String[] args) throws Exception {111// (1) Load a hidden version of this class using method lookup.defineHiddenClass().112String rn = TestHiddenClassUnloading.class.getSimpleName() + ".class";113URL classUrl = TestHiddenClassUnloading.class.getResource(rn);114URLConnection connection = classUrl.openConnection();115116int length = connection.getContentLength();117byte[] classBytes = connection.getInputStream().readAllBytes();118if (length != -1 && classBytes.length != length) {119throw new IOException("Expected:" + length + ", actual: " + classBytes.length);120}121122Lookup lookup = MethodHandles.lookup();123Class<?> hiddenClass = lookup.defineHiddenClass(classBytes, true, NESTMATE).lookupClass();124125// (2) Make sure all paths of doWork are profiled and compiled126for (int i = 0; i < 100000; ++i) {127doWork(hiddenClass);128}129130// Make sure doWork is compiled now131Method doWork = TestHiddenClassUnloading.class.getDeclaredMethod("doWork", Class.class);132makeSureIsCompiled(doWork);133134// (3) Throw away reference to hiddenClass to allow unloading135hiddenClass = null;136137// Force garbage collection to trigger unloading of hiddenClass138WHITE_BOX.fullGC();139}140}141142143