< prev index next >

test/compiler/classUnloading/anonymousClass/TestAnonymousClassUnloading.java

Print this page
rev 11557 : 8132919: use package in compiler tests
Reviewed-by: duke


   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 import jdk.internal.misc.Unsafe;

  26 
  27 import java.io.IOException;
  28 import java.lang.reflect.Method;
  29 import java.net.URL;
  30 import java.net.URLConnection;
  31 
  32 /*
  33  * @test TestAnonymousClassUnloading
  34  * @bug 8054402
  35  * @summary "Tests unloading of anonymous classes."
  36  * @library /testlibrary /test/lib
  37  * @modules java.base/jdk.internal.misc
  38  * @compile TestAnonymousClassUnloading.java
  39  * @run main ClassFileInstaller TestAnonymousClassUnloading
  40  *                              sun.hotspot.WhiteBox
  41  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  42  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-BackgroundCompilation TestAnonymousClassUnloading
  43  */
  44 public class TestAnonymousClassUnloading {
  45     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  46     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
  47     private static int COMP_LEVEL_SIMPLE = 1;
  48     private static int COMP_LEVEL_FULL_OPTIMIZATION = 4;
  49 
  50     /**
  51      * We override hashCode here to be able to access this implementation
  52      * via an Object reference (we cannot cast to TestAnonymousClassUnloading).
  53      */
  54     @Override
  55     public int hashCode() {
  56         return 42;
  57     }
  58 
  59     /**
  60      * Does some work by using the anonymousClass.
  61      * @param anonymousClass Class performing some work (will be unloaded)
  62      */
  63     static private void doWork(Class<?> anonymousClass) throws InstantiationException, IllegalAccessException {


  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 Klass* metadata referenced by a compiled IC.
 101      *
 102      * The following steps are performed:
 103      * (1) An anonymous version of TestAnonymousClassUnloading is loaded by a custom class loader
 104      * (2) The method doWork that calls a method of the anonymous class is compiled. The call
 105      *     is implemented as an IC referencing Klass* metadata of the anonymous class.
 106      * (3) Unloading of the anonymous class is enforced. The IC now references dead metadata.
 107      */
 108     static public void main(String[] args) throws Exception {
 109         // (1) Load an anonymous version of this class using the corresponding Unsafe method
 110         URL classUrl = TestAnonymousClassUnloading.class.getResource("TestAnonymousClassUnloading.class");

 111         URLConnection connection = classUrl.openConnection();
 112 
 113         int length = connection.getContentLength();
 114         byte[] classBytes = connection.getInputStream().readAllBytes();
 115         if (length != -1 && classBytes.length != length) {
 116             throw new IOException("Expected:" + length + ", actual: " + classBytes.length);
 117         }
 118 
 119         Class<?> anonymousClass = UNSAFE.defineAnonymousClass(TestAnonymousClassUnloading.class, classBytes, null);
 120 
 121         // (2) Make sure all paths of doWork are profiled and compiled
 122         for (int i = 0; i < 100000; ++i) {
 123             doWork(anonymousClass);
 124         }
 125 
 126         // Make sure doWork is compiled now
 127         Method doWork = TestAnonymousClassUnloading.class.getDeclaredMethod("doWork", Class.class);
 128         makeSureIsCompiled(doWork);
 129 
 130         // (3) Throw away reference to anonymousClass to allow unloading


   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 TestAnonymousClassUnloading
  26  * @bug 8054402
  27  * @summary "Tests unloading of anonymous classes."
  28  * @library /testlibrary /test/lib /
  29  * @modules java.base/jdk.internal.misc
  30  *
  31  * @run main/othervm/bootclasspath -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  32  *      -XX:-BackgroundCompilation
  33  *      compiler.classUnloading.anonymousClass.TestAnonymousClassUnloading
  34  */
  35 
  36 package compiler.classUnloading.anonymousClass;
  37 
  38 import jdk.internal.misc.Unsafe;
  39 import sun.hotspot.WhiteBox;
  40 
  41 import java.io.IOException;
  42 import java.lang.reflect.Method;
  43 import java.net.URL;
  44 import java.net.URLConnection;
  45 












  46 public class TestAnonymousClassUnloading {
  47     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  48     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
  49     private static int COMP_LEVEL_SIMPLE = 1;
  50     private static int COMP_LEVEL_FULL_OPTIMIZATION = 4;
  51 
  52     /**
  53      * We override hashCode here to be able to access this implementation
  54      * via an Object reference (we cannot cast to TestAnonymousClassUnloading).
  55      */
  56     @Override
  57     public int hashCode() {
  58         return 42;
  59     }
  60 
  61     /**
  62      * Does some work by using the anonymousClass.
  63      * @param anonymousClass Class performing some work (will be unloaded)
  64      */
  65     static private void doWork(Class<?> anonymousClass) throws InstantiationException, IllegalAccessException {


  92                 WHITE_BOX.enqueueMethodForCompilation(m, COMP_LEVEL_SIMPLE);
  93             }
  94             // Because background compilation is disabled, method should now be compiled
  95             if(!WHITE_BOX.isMethodCompiled(m)) {
  96                 throw new RuntimeException(m + " not compiled");
  97             }
  98         }
  99     }
 100 
 101     /**
 102      * This test creates stale Klass* metadata referenced by a compiled IC.
 103      *
 104      * The following steps are performed:
 105      * (1) An anonymous version of TestAnonymousClassUnloading is loaded by a custom class loader
 106      * (2) The method doWork that calls a method of the anonymous class is compiled. The call
 107      *     is implemented as an IC referencing Klass* metadata of the anonymous class.
 108      * (3) Unloading of the anonymous class is enforced. The IC now references dead metadata.
 109      */
 110     static public void main(String[] args) throws Exception {
 111         // (1) Load an anonymous version of this class using the corresponding Unsafe method
 112         URL classUrl = TestAnonymousClassUnloading.class.getResource(
 113                 TestAnonymousClassUnloading.class.getName().replace('.', '/') + ".class");
 114         URLConnection connection = classUrl.openConnection();
 115 
 116         int length = connection.getContentLength();
 117         byte[] classBytes = connection.getInputStream().readAllBytes();
 118         if (length != -1 && classBytes.length != length) {
 119             throw new IOException("Expected:" + length + ", actual: " + classBytes.length);
 120         }
 121 
 122         Class<?> anonymousClass = UNSAFE.defineAnonymousClass(TestAnonymousClassUnloading.class, classBytes, null);
 123 
 124         // (2) Make sure all paths of doWork are profiled and compiled
 125         for (int i = 0; i < 100000; ++i) {
 126             doWork(anonymousClass);
 127         }
 128 
 129         // Make sure doWork is compiled now
 130         Method doWork = TestAnonymousClassUnloading.class.getDeclaredMethod("doWork", Class.class);
 131         makeSureIsCompiled(doWork);
 132 
 133         // (3) Throw away reference to anonymousClass to allow unloading
< prev index next >