Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/profiling/spectrapredefineclass/Agent.java
41153 views
1
/*
2
* Copyright (c) 2014, 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
package compiler.profiling.spectrapredefineclass;
25
26
import com.sun.tools.attach.VirtualMachine;
27
import jdk.test.lib.Utils;
28
29
import java.lang.instrument.ClassFileTransformer;
30
import java.lang.instrument.Instrumentation;
31
import java.nio.file.Paths;
32
import java.security.ProtectionDomain;
33
34
class A {
35
void m() {
36
}
37
}
38
39
class B extends A {
40
void m() {
41
}
42
}
43
44
class C extends A {
45
void m() {
46
}
47
}
48
49
class Test {
50
51
static public void m() throws Exception {
52
for (int i = 0; i < 20000; i++) {
53
m1(a);
54
}
55
for (int i = 0; i < 4; i++) {
56
m1(b);
57
}
58
}
59
60
static boolean m1(A a) {
61
boolean res = Agent.m2(a);
62
return res;
63
}
64
65
static public A a = new A();
66
static public B b = new B();
67
static public C c = new C();
68
}
69
70
public class Agent implements ClassFileTransformer {
71
public static final String AGENT_JAR = Paths.get(Utils.TEST_CLASSES, "agent.jar").toString();
72
static public boolean m2(A a) {
73
boolean res = false;
74
if (a.getClass() == B.class) {
75
a.m();
76
} else {
77
res = true;
78
}
79
return res;
80
}
81
82
static public void main(String[] args) throws Exception {
83
// Create speculative trap entries
84
Test.m();
85
86
String pid = Long.toString(ProcessHandle.current().pid());
87
88
// Make the nmethod go away
89
for (int i = 0; i < 10; i++) {
90
System.gc();
91
}
92
93
// Redefine class
94
try {
95
VirtualMachine vm = VirtualMachine.attach(pid);
96
vm.loadAgent(AGENT_JAR, "");
97
vm.detach();
98
} catch (Exception e) {
99
throw new RuntimeException(e);
100
}
101
102
Test.m();
103
// GC will hit dead method pointer
104
for (int i = 0; i < 10; i++) {
105
System.gc();
106
}
107
}
108
109
public synchronized byte[] transform(final ClassLoader classLoader,
110
final String className,
111
Class<?> classBeingRedefined,
112
ProtectionDomain protectionDomain,
113
byte[] classfileBuffer) {
114
System.out.println("Transforming class " + className);
115
return classfileBuffer;
116
}
117
118
public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) {
119
120
try {
121
instrumentation.retransformClasses(to_redefine);
122
} catch (Exception e) {
123
e.printStackTrace();
124
}
125
126
}
127
128
public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception {
129
Agent transformer = new Agent();
130
instrumentation.addTransformer(transformer, true);
131
132
redefine(agentArgs, instrumentation, Test.class);
133
}
134
}
135
136