1 /*
   2  * Copyright (c) 2014, 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 import sun.hotspot.WhiteBox;
  25 
  26 import java.lang.reflect.Method;
  27 import java.net.URL;
  28 import java.net.URLClassLoader;
  29 
  30 /*
  31  * @test MethodUnloadingTest
  32  * @bug 8029443
  33  * @summary "Tests the unloading of methods to to class unloading"
  34  * @modules java.base/jdk.internal.misc
  35  * @library /testlibrary /test/lib
  36  * @build TestMethodUnloading
  37  * @build WorkerClass
  38  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  39  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  40  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-BackgroundCompilation -XX:-UseCompressedOops -XX:CompileOnly=TestMethodUnloading::doWork TestMethodUnloading
  41  */
  42 public class TestMethodUnloading {
  43     private static final String workerClassName = "WorkerClass";
  44     private static int work = -1;
  45 
  46     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  47     private static int COMP_LEVEL_SIMPLE = 1;
  48     private static int COMP_LEVEL_FULL_OPTIMIZATION = 4;
  49 
  50     /**
  51      * Does some work by either using the workerClass or locally producing values.
  52      * @param workerClass Class performing some work (will be unloaded)
  53      * @param useWorker If true the workerClass is used
  54      */
  55     static private void doWork(Class<?> workerClass, boolean useWorker) throws InstantiationException, IllegalAccessException {
  56         if (useWorker) {
  57             // Create a new instance
  58             Object worker = workerClass.newInstance();
  59             // We would like to call a method of WorkerClass here but we cannot cast to WorkerClass
  60             // because the class was loaded by a different class loader. One solution would be to use
  61             // reflection but since we want C2 to implement the call as an optimized IC we call
  62             // Object::hashCode() here which actually calls WorkerClass::hashCode().
  63             // C2 will then implement this call as an optimized IC that points to a to-interpreter stub
  64             // referencing the Method* for WorkerClass::hashCode().
  65             work = worker.hashCode();
  66             if (work != 42) {
  67                 new RuntimeException("Work not done");
  68             }
  69         } else {
  70             // Do some important work here
  71             work = 1;
  72         }
  73     }
  74 
  75     /**
  76      * Makes sure that method is compiled by forcing compilation if not yet compiled.
  77      * @param m Method to be checked
  78      */
  79     static private void makeSureIsCompiled(Method m) {
  80         // Make sure background compilation is disabled
  81         if (WHITE_BOX.getBooleanVMFlag("BackgroundCompilation")) {
  82             throw new RuntimeException("Background compilation enabled");
  83         }
  84 
  85         // Check if already compiled
  86         if (!WHITE_BOX.isMethodCompiled(m)) {
  87             // If not, try to compile it with C2
  88             if(!WHITE_BOX.enqueueMethodForCompilation(m, COMP_LEVEL_FULL_OPTIMIZATION)) {
  89                 // C2 compiler not available, try to compile with C1
  90                 WHITE_BOX.enqueueMethodForCompilation(m, COMP_LEVEL_SIMPLE);
  91             }
  92             // Because background compilation is disabled, method should now be compiled
  93             if(!WHITE_BOX.isMethodCompiled(m)) {
  94                 throw new RuntimeException(m + " not compiled");
  95             }
  96         }
  97     }
  98 
  99     /**
 100      * This test creates stale Method* metadata in a to-interpreter stub of an optimized IC.
 101      *
 102      * The following steps are performed:
 103      * (1) A workerClass is loaded by a custom class loader
 104      * (2) The method doWork that calls a method of the workerClass is compiled. The call
 105      *     is implemented as an optimized IC calling a to-interpreted stub. The to-interpreter
 106      *     stub contains a Method* to a workerClass method.
 107      * (3) Unloading of the workerClass is enforced. The to-interpreter stub now contains a dead Method*.
 108      * (4) Depending on the implementation of the IC, the compiled version of doWork should still be
 109      *     valid. We call it again without using the workerClass.
 110      */
 111     static public void main(String[] args) throws Exception {
 112         // (1) Create a custom class loader with no parent class loader
 113         URL url = TestMethodUnloading.class.getProtectionDomain().getCodeSource().getLocation();
 114         URLClassLoader loader = new URLClassLoader(new URL[] {url}, null);
 115 
 116         // Load worker class with custom class loader
 117         Class<?> workerClass = Class.forName(workerClassName, true, loader);
 118 
 119         // (2) Make sure all paths of doWork are profiled and compiled
 120         for (int i = 0; i < 100000; ++i) {
 121             doWork(workerClass, true);
 122             doWork(workerClass, false);
 123         }
 124 
 125         // Make sure doWork is compiled now
 126         Method doWork = TestMethodUnloading.class.getDeclaredMethod("doWork", Class.class, boolean.class);
 127         makeSureIsCompiled(doWork);
 128 
 129         // (3) Throw away class loader and reference to workerClass to allow unloading
 130         loader.close();
 131         loader = null;
 132         workerClass = null;
 133 
 134         // Force garbage collection to trigger unloading of workerClass
 135         // Dead reference to WorkerClass::hashCode triggers JDK-8029443
 136         WHITE_BOX.fullGC();
 137 
 138         // (4) Depending on the implementation of the IC, the compiled version of doWork
 139         // may still be valid here. Execute it without a workerClass.
 140         doWork(null, false);
 141         if (work != 1) {
 142             throw new RuntimeException("Work not done");
 143         }
 144 
 145         doWork(Object.class, false);
 146     }
 147 }