1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary Test unloading of hidden classes.
  27  * @library /test/lib /
  28  * @build sun.hotspot.WhiteBox
  29  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  30  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  31  *
  32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  33  *      -XX:-BackgroundCompilation
  34  *      compiler.classUnloading.hiddenClass.TestHiddenClassUnloading
  35  */
  36 
  37 package compiler.classUnloading.hiddenClass;
  38 
  39 import sun.hotspot.WhiteBox;
  40 
  41 import java.io.IOException;
  42 import java.lang.invoke.MethodHandles;
  43 import java.lang.invoke.MethodHandles.Lookup;
  44 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;
  45 import java.lang.reflect.Method;
  46 import java.net.URL;
  47 import java.net.URLConnection;
  48 import compiler.whitebox.CompilerWhiteBoxTest;
  49 
  50 // This is based on test compiler/classUnloading/anonymousClass/TestAnonymousClassUnloading.java
  51 public class TestHiddenClassUnloading {
  52     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  53 
  54     /**
  55      * We override hashCode here to be able to access this implementation
  56      * via an Object reference (we cannot cast to TestHiddenClassUnloading).
  57      */
  58     @Override
  59     public int hashCode() {
  60         return 42;
  61     }
  62 
  63     /**
  64      * Does some work by using the hiddenClass.
  65      * @param hiddenClass Class performing some work (will be unloaded)
  66      */
  67     static private void doWork(Class<?> hiddenClass) throws InstantiationException, IllegalAccessException {
  68         // Create a new instance
  69         Object anon = hiddenClass.newInstance();
  70         // We would like to call a method of hiddenClass here but we cannot cast because the class
  71         // was loaded by a different class loader. One solution would be to use reflection but since
  72         // we want C2 to implement the call as an IC we call Object::hashCode() here which actually
  73         // calls hiddenClass::hashCode(). C2 will then implement this call as an IC.
  74         if (anon.hashCode() != 42) {
  75             new RuntimeException("Work not done");
  76         }
  77     }
  78 
  79     /**
  80      * Makes sure that method is compiled by forcing compilation if not yet compiled.
  81      * @param m Method to be checked
  82      */
  83     static private void makeSureIsCompiled(Method m) {
  84         // Make sure background compilation is disabled
  85         if (WHITE_BOX.getBooleanVMFlag("BackgroundCompilation")) {
  86             throw new RuntimeException("Background compilation enabled");
  87         }
  88 
  89         // Check if already compiled
  90         if (!WHITE_BOX.isMethodCompiled(m)) {
  91             // If not, try to compile it with C2
  92             if(!WHITE_BOX.enqueueMethodForCompilation(m, CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION)) {
  93                 // C2 compiler not available, try to compile with C1
  94                 WHITE_BOX.enqueueMethodForCompilation(m, CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE);
  95             }
  96             // Because background compilation is disabled, method should now be compiled
  97             if(!WHITE_BOX.isMethodCompiled(m)) {
  98                 throw new RuntimeException(m + " not compiled");
  99             }
 100         }
 101     }
 102 
 103     /**
 104      * This test creates stale Klass* metadata referenced by a compiled IC.
 105      *
 106      * The following steps are performed:
 107      * (1) A hidden version of TestHiddenClassUnloading is loaded by a custom class loader
 108      * (2) The method doWork that calls a method of the hidden class is compiled. The call
 109      *     is implemented as an IC referencing Klass* metadata of the hidden class.
 110      * (3) Unloading of the hidden class is enforced. The IC now references dead metadata.
 111      */
 112     static public void main(String[] args) throws Exception {
 113         // (1) Load a hidden version of this class using method lookup.defineHiddenClass().
 114         String rn = TestHiddenClassUnloading.class.getSimpleName() + ".class";
 115         URL classUrl = TestHiddenClassUnloading.class.getResource(rn);
 116         URLConnection connection = classUrl.openConnection();
 117 
 118         int length = connection.getContentLength();
 119         byte[] classBytes = connection.getInputStream().readAllBytes();
 120         if (length != -1 && classBytes.length != length) {
 121             throw new IOException("Expected:" + length + ", actual: " + classBytes.length);
 122         }
 123 
 124         Lookup lookup = MethodHandles.lookup();
 125         Class<?> hiddenClass = lookup.defineHiddenClass(classBytes, true, NESTMATE, WEAK).lookupClass();
 126 
 127         // (2) Make sure all paths of doWork are profiled and compiled
 128         for (int i = 0; i < 100000; ++i) {
 129             doWork(hiddenClass);
 130         }
 131 
 132         // Make sure doWork is compiled now
 133         Method doWork = TestHiddenClassUnloading.class.getDeclaredMethod("doWork", Class.class);
 134         makeSureIsCompiled(doWork);
 135 
 136         // (3) Throw away reference to hiddenClass to allow unloading
 137         hiddenClass = null;
 138 
 139         // Force garbage collection to trigger unloading of hiddenClass
 140         WHITE_BOX.fullGC();
 141     }
 142 }