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 package compiler.profiling.spectrapredefineclass_classloaders; 25 26 import com.sun.tools.attach.VirtualMachine; 27 28 import java.lang.instrument.ClassFileTransformer; 29 import java.lang.instrument.Instrumentation; 30 import java.lang.management.ManagementFactory; 31 import java.lang.reflect.Method; 32 import java.net.MalformedURLException; 33 import java.net.URL; 34 import java.net.URLClassLoader; 35 import java.nio.file.Paths; 36 import java.security.ProtectionDomain; 37 38 public class Agent implements ClassFileTransformer { 39 public static ClassLoader newClassLoader() { 40 try { 41 return new URLClassLoader(new URL[] { 42 Paths.get(System.getProperty("test.classes",".")).toUri().toURL(), 43 }, null); 44 } catch (MalformedURLException e){ 45 throw new RuntimeException("Unexpected URL conversion failure", e); 46 } 47 } 48 49 static public Class Test_class; 50 51 static public void main(String[] args) throws Exception { 52 53 // loader2 must be first on the list so loader 1 must be used first 54 ClassLoader loader1 = newClassLoader(); 55 String packageName = Agent.class.getPackage().getName(); 56 Class dummy = loader1.loadClass(packageName + ".Test"); 57 58 ClassLoader loader2 = newClassLoader(); 59 60 Test_class = loader2.loadClass(packageName + ".Test"); 61 Method m3 = Test_class.getMethod("m3", ClassLoader.class); 62 // Add speculative trap in m2() (loaded by loader1) that 63 // references m4() (loaded by loader2). 64 m3.invoke(Test_class.newInstance(), loader1); 65 66 String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName(); 67 int p = nameOfRunningVM.indexOf('@'); 68 String pid = nameOfRunningVM.substring(0, p); 69 70 // Make the nmethod go away 71 for (int i = 0; i < 10; i++) { 72 System.gc(); 73 } 74 75 // Redefine class Test loaded by loader2 76 for (int i = 0; i < 2; i++) { 77 try { 78 VirtualMachine vm = VirtualMachine.attach(pid); 79 vm.loadAgent(System.getProperty("test.classes",".") + "/agent.jar", ""); 80 vm.detach(); 81 } catch (Exception e) { 82 throw new RuntimeException(e); 83 } 84 } 85 // Will process loader2 first, find m4() is redefined and 86 // needs to be freed then process loader1, check the 87 // speculative trap in m2() and try to access m4() which was 88 // freed already. 89 for (int i = 0; i < 10; i++) { 90 System.gc(); 91 } 92 } 93 94 public synchronized byte[] transform(final ClassLoader classLoader, 95 final String className, 96 Class<?> classBeingRedefined, 97 ProtectionDomain protectionDomain, 98 byte[] classfileBuffer) { 99 System.out.println("Transforming class " + className + " "+ classLoader); 100 return classfileBuffer; 101 } 102 103 public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) { 104 105 try { 106 instrumentation.retransformClasses(to_redefine); 107 } catch (Exception e) { 108 e.printStackTrace(); 109 } 110 111 } 112 113 public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception { 114 Agent transformer = new Agent(); 115 instrumentation.addTransformer(transformer, true); 116 117 redefine(agentArgs, instrumentation, Test_class); 118 } 119 }