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