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.IOUtils;
26 import sun.misc.Unsafe;
27 
28 import java.io.IOException;
29 import java.lang.reflect.Method;
30 import java.net.URL;
31 import java.net.URLConnection;
32 
33 /*
34  * @test TestAnonymousClassUnloading
35  * @bug 8054402
36  * @summary "Tests unloading of anonymous classes."
37  * @library /testlibrary /testlibrary/whitebox
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
hashCode()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      */
doWork(Class<?> anonymousClass)63     static private void doWork(Class<?> anonymousClass) throws InstantiationException, IllegalAccessException {
64         // Create a new instance
65         Object anon = anonymousClass.newInstance();
66         // We would like to call a method of anonymousClass here but we cannot cast because the class
67         // was loaded by a different class loader. One solution would be to use reflection but since
68         // we want C2 to implement the call as an IC we call Object::hashCode() here which actually
69         // calls anonymousClass::hashCode(). C2 will then implement this call as an IC.
70         if (anon.hashCode() != 42) {
71             new RuntimeException("Work not done");
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      */
makeSureIsCompiled(Method m)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 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      */
main(String[] args)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 = IOUtils.readAllBytes(connection.getInputStream());
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
131         anonymousClass = null;
132 
133         // Force garbage collection to trigger unloading of anonymousClass
134         // Dead metadata reference to anonymousClass triggers JDK-8054402
135         WHITE_BOX.fullGC();
136     }
137 }
138