author | amlu |
Tue, 30 Jun 2015 10:00:46 +0200 | |
changeset 31427 | 42740247adcf |
parent 24310 | 1cc31246af03 |
child 40059 | c2304140ed64 |
permissions | -rw-r--r-- |
23845
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
1 |
/* |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
4 |
* |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
8 |
* |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
13 |
* accompanied this code). |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
14 |
* |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
18 |
* |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
21 |
* questions. |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
22 |
*/ |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
23 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
24 |
import java.security.*; |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
25 |
import java.lang.instrument.*; |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
26 |
import java.lang.reflect.*; |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
27 |
import java.lang.management.ManagementFactory; |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
28 |
import com.sun.tools.attach.VirtualMachine; |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
29 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
30 |
class A { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
31 |
void m() { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
32 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
33 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
34 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
35 |
class B extends A { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
36 |
void m() { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
37 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
38 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
39 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
40 |
class C extends A { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
41 |
void m() { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
42 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
43 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
44 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
45 |
class Test { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
46 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
47 |
static public void m() throws Exception { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
48 |
for (int i = 0; i < 20000; i++) { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
49 |
m1(a); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
50 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
51 |
for (int i = 0; i < 4; i++) { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
52 |
m1(b); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
53 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
54 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
55 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
56 |
static boolean m1(A a) { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
57 |
boolean res = Agent.m2(a); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
58 |
return res; |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
59 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
60 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
61 |
static public A a = new A(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
62 |
static public B b = new B(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
63 |
static public C c = new C(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
64 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
65 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
66 |
public class Agent implements ClassFileTransformer { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
67 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
68 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
69 |
static public boolean m2(A a) { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
70 |
boolean res = false; |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
71 |
if (a.getClass() == B.class) { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
72 |
a.m(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
73 |
} else { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
74 |
res = true; |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
75 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
76 |
return res; |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
77 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
78 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
79 |
static public void main(String[] args) throws Exception { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
80 |
// Create speculative trap entries |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
81 |
Test.m(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
82 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
83 |
String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
84 |
int p = nameOfRunningVM.indexOf('@'); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
85 |
String pid = nameOfRunningVM.substring(0, p); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
86 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
87 |
// Make the nmethod go away |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
88 |
for (int i = 0; i < 10; i++) { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
89 |
System.gc(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
90 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
91 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
92 |
// Redefine class |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
93 |
try { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
94 |
VirtualMachine vm = VirtualMachine.attach(pid); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
95 |
vm.loadAgent(System.getProperty("test.classes",".") + "/agent.jar", ""); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
96 |
vm.detach(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
97 |
} catch (Exception e) { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
98 |
throw new RuntimeException(e); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
99 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
100 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
101 |
Test.m(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
102 |
// GC will hit dead method pointer |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
103 |
for (int i = 0; i < 10; i++) { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
104 |
System.gc(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
105 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
106 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
107 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
108 |
public synchronized byte[] transform(final ClassLoader classLoader, |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
109 |
final String className, |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
110 |
Class<?> classBeingRedefined, |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
111 |
ProtectionDomain protectionDomain, |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
112 |
byte[] classfileBuffer) { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
113 |
System.out.println("Transforming class " + className); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
114 |
return classfileBuffer; |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
115 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
116 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
117 |
public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
118 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
119 |
try { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
120 |
instrumentation.retransformClasses(to_redefine); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
121 |
} catch (Exception e) { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
122 |
e.printStackTrace(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
123 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
124 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
125 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
126 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
127 |
public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception { |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
128 |
Agent transformer = new Agent(); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
129 |
instrumentation.addTransformer(transformer, true); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
130 |
|
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
131 |
redefine(agentArgs, instrumentation, Test.class); |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
132 |
} |
ebd1fafcc362
8038636: speculative traps break when classes are redefined
roland
parents:
diff
changeset
|
133 |
} |