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 import sun.misc.Unsafe; 26 import sun.misc.IOUtils; 27 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 /testlibrary/whitebox 37 * @compile TestAnonymousClassUnloading.java 38 * @run main ClassFileInstaller TestAnonymousClassUnloading sun.hotspot.WhiteBox 39 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-BackgroundCompilation TestAnonymousClassUnloading 40 */ 41 public class TestAnonymousClassUnloading { 42 private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox(); 43 private static final Unsafe UNSAFE = Unsafe.getUnsafe(); 44 private static int COMP_LEVEL_SIMPLE = 1; 45 private static int COMP_LEVEL_FULL_OPTIMIZATION = 4; 46 47 /** 48 * We override hashCode here to be able to access this implementation 49 * via an Object reference (we cannot cast to TestAnonymousClassUnloading). 50 */ 51 @Override 52 public int hashCode() { 53 return 42; 54 } 55 56 /** 57 * Does some work by using the anonymousClass. 58 * @param anonymousClass Class performing some work (will be unloaded) 59 */ 60 static private void doWork(Class<?> anonymousClass) throws InstantiationException, IllegalAccessException { 61 // Create a new instance 62 Object anon = anonymousClass.newInstance(); 63 // We would like to call a method of anonymousClass here but we cannot cast because the class 64 // was loaded by a different class loader. One solution would be to use reflection but since 65 // we want C2 to implement the call as an IC we call Object::hashCode() here which actually 66 // calls anonymousClass::hashCode(). C2 will then implement this call as an IC. 67 if (anon.hashCode() != 42) { 68 new RuntimeException("Work not done"); 69 } 70 } 71 72 /** 73 * Makes sure that method is compiled by forcing compilation if not yet compiled. 74 * @param m Method to be checked 75 */ 76 static private void makeSureIsCompiled(Method m) { 77 // Make sure background compilation is disabled 78 if (WHITE_BOX.getBooleanVMFlag("BackgroundCompilation")) { 79 throw new RuntimeException("Background compilation enabled"); 80 } 81 82 // Check if already compiled 83 if (!WHITE_BOX.isMethodCompiled(m)) { 84 // If not, try to compile it with C2 85 if(!WHITE_BOX.enqueueMethodForCompilation(m, COMP_LEVEL_FULL_OPTIMIZATION)) { 86 // C2 compiler not available, try to compile with C1 87 WHITE_BOX.enqueueMethodForCompilation(m, COMP_LEVEL_SIMPLE); 88 } 89 // Because background compilation is disabled, method should now be compiled 90 if(!WHITE_BOX.isMethodCompiled(m)) { 91 throw new RuntimeException(m + " not compiled"); 92 } 93 } 94 } 95 96 /** 97 * This test creates stale Klass* metadata referenced by a compiled IC. 98 * 99 * The following steps are performed: 100 * (1) An anonymous version of TestAnonymousClassUnloading is loaded by a custom class loader 101 * (2) The method doWork that calls a method of the anonymous class is compiled. The call 102 * is implemented as an IC referencing Klass* metadata of the anonymous class. 103 * (3) Unloading of the anonymous class is enforced. The IC now references dead metadata. 104 */ 105 static public void main(String[] args) throws Exception { 106 // (1) Load an anonymous version of this class using the corresponding Unsafe method 107 URL classUrl = TestAnonymousClassUnloading.class.getResource("TestAnonymousClassUnloading.class"); 108 URLConnection connection = classUrl.openConnection(); 109 byte[] classBytes = IOUtils.readFully(connection.getInputStream(), connection.getContentLength(), true); 110 Class<?> anonymousClass = UNSAFE.defineAnonymousClass(TestAnonymousClassUnloading.class, classBytes, null); 111 112 // (2) Make sure all paths of doWork are profiled and compiled 113 for (int i = 0; i < 100000; ++i) { 114 doWork(anonymousClass); 115 } 116 117 // Make sure doWork is compiled now 118 Method doWork = TestAnonymousClassUnloading.class.getDeclaredMethod("doWork", Class.class); 119 makeSureIsCompiled(doWork); 120 121 // (3) Throw away reference to anonymousClass to allow unloading 122 anonymousClass = null; 123 124 // Force garbage collection to trigger unloading of anonymousClass 125 // Dead metadata reference to anonymousClass triggers JDK-8054402 126 WHITE_BOX.fullGC(); 127 } 128 }