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 * @library /test/lib 27 * @modules java.instrument 28 * jdk.compiler 29 * @build jdk.test.lib.compiler.CompilerUtils 30 * jdk.test.lib.Utils * 31 * @run shell ../MakeJAR3.sh HiddenClassAgent 'Can-Retransform-Classes: true' 32 * @run main/othervm/native -javaagent:HiddenClassAgent.jar HiddenClassApp 33 */ 34 35 import java.lang.instrument.*; 36 37 /* Test that Instrumentation getAllLoadedClasses includes 38 * hidden classes but getInitiatedClasses does not. 39 * Check that all hidden classes are non-retransformable. 40 */ 41 public class HiddenClassAgent extends Thread { 42 private static volatile boolean completed = false; 43 private static volatile boolean failed = false; 44 private static volatile boolean hiddenClassLoaded = false; 45 46 private static Instrumentation instr = null; 47 private static Object monitor = new Object(); 48 49 static void log(String str) { System.err.println(str); } 50 public static boolean failed() { return failed; } 51 52 public static 53 boolean checkWaitForCompleteness() { 54 try { 55 synchronized (monitor) { 56 while (!completed) { 57 monitor.wait(100); 58 } 59 } 60 } catch (InterruptedException ex) { 61 ex.printStackTrace(); 62 log("HiddenClassAgent: waitCheckForCompletness: Caught InterruptedException: " + ex); 63 } 64 return completed; 65 } 66 67 public static 68 void setHiddenClassLoaded() { 69 synchronized (monitor) { 70 hiddenClassLoaded = true; 71 monitor.notifyAll(); 72 } 73 } 74 75 private static 76 void waitForHiddenClassLoad() { 77 try { 78 synchronized (monitor) { 79 while (!hiddenClassLoaded) { 80 monitor.wait(100); 81 } 82 } 83 } catch (InterruptedException ex) { 84 ex.printStackTrace(); 85 log("HiddenClassAgent: waitForHiddenClassLoad: Caught InterruptedException: " + ex); 86 } 87 } 88 89 private static 90 ClassLoader testGetAllLoadedClasses() { 91 boolean hiddenClassFound = false; 92 ClassLoader loader = null; 93 Class<?>[] classes = instr.getAllLoadedClasses(); 94 95 for (int i = 0; i < classes.length; i++) { 96 Class klass = classes[i]; 97 98 if (!klass.isHidden() || !klass.getName().contains("HiddenClass/")) { 99 continue; 100 } 101 log("HiddenClassAgent: getAllLoadedClasses returned hidden class: " + klass); 102 hiddenClassFound = true; 103 loader = klass.getClassLoader(); 104 log("HiddenClassAgent: class loader of hidden class: " + loader); 105 106 try { 107 instr.retransformClasses(klass); 108 log("HiddenClassAgent: FAIL: hidden class is retransformable: " + klass); 109 failed = true; 110 } catch (UnmodifiableClassException e) { 111 log("HiddenClassAgent: Got expected UnmodifiableClassException for class: " + klass); 112 } catch (Throwable e) { 113 log("HiddenClassAgent: FAIL: unexpected throwable in hidden class retransform: " + klass); 114 log("HiddenClassAgent: got Throwable" + e); 115 failed = true; 116 } 117 } 118 if (!hiddenClassFound) { 119 log("HiddenClassAgent: FAIL: a hidden class is not found in getAllLoadedClasses list"); 120 failed = true; 121 } 122 return loader; 123 } 124 125 private static 126 void testGetInitiatedClasses(ClassLoader loader) { 127 Class<?>[] classes = instr.getInitiatedClasses(loader); 128 for (int i = 0; i < classes.length; i++) { 129 Class klass = classes[i]; 130 131 if (klass.isHidden()) { 132 log("HiddenClassAgent: FAIL: getInitiatedClasses returned hidden class: " + klass); 133 failed = true; 134 return; 135 } 136 } 137 log("HiddenClassAgent: getInitiatedClasses returned no hidden classes as expected"); 138 } 139 140 public static void 141 premain(String agentArgs, Instrumentation instrumentation) { 142 instr = instrumentation; 143 Thread agentThread = new HiddenClassAgent(); 144 agentThread.start(); 145 } 146 147 public void run () { 148 log("HiddenClassAgent: started"); 149 waitForHiddenClassLoad(); 150 151 // Test getAllLoadedClasses 152 ClassLoader loader = testGetAllLoadedClasses(); 153 154 // Test getInitiatedClasses 155 testGetInitiatedClasses(null); 156 if (loader != null) { 157 testGetInitiatedClasses(loader); 158 } 159 160 synchronized (monitor) { 161 completed = true; 162 monitor.notifyAll(); 163 } 164 log("HiddenClassAgent: finished"); 165 } 166 }