Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/ModifyAnonymous.java
41153 views
1
/*
2
* Copyright (c) 2016, 2021, 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
* @summary Test that retransforming and redefining anonymous classes gets UnmodifiableClassException
28
* @requires vm.jvmti
29
* @modules java.base/jdk.internal.misc
30
* @modules java.instrument
31
* jdk.jartool/sun.tools.jar
32
* @run main ModifyAnonymous buildagent
33
* @run main/othervm -javaagent:redefineagent.jar ModifyAnonymous
34
*/
35
36
import java.io.File;
37
import java.io.FileNotFoundException;
38
import java.io.FileOutputStream;
39
import java.io.PrintWriter;
40
import java.lang.RuntimeException;
41
import java.lang.instrument.ClassDefinition;
42
import java.lang.instrument.ClassFileTransformer;
43
import java.lang.instrument.IllegalClassFormatException;
44
import java.lang.instrument.Instrumentation;
45
import java.security.ProtectionDomain;
46
47
import jdk.test.lib.compiler.InMemoryJavaCompiler;
48
import jdk.test.lib.helpers.ClassFileInstaller;
49
50
public class ModifyAnonymous {
51
52
public static class LambdaTransformer implements ClassFileTransformer {
53
@Override
54
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
55
ProtectionDomain protectionDomain, byte[] classfileBuffer)
56
throws IllegalClassFormatException {
57
return null;
58
}
59
}
60
61
static Instrumentation inst = null;
62
static volatile boolean done = false;
63
64
public static void premain(String args, Instrumentation instrumentation) {
65
66
inst = instrumentation;
67
System.out.println("javaagent in da house!");
68
instrumentation.addTransformer(new LambdaTransformer());
69
}
70
71
private static void buildAgent() {
72
try {
73
ClassFileInstaller.main("ModifyAnonymous");
74
} catch (Exception e) {
75
throw new RuntimeException("Could not write agent classfile", e);
76
}
77
78
try {
79
PrintWriter pw = new PrintWriter("MANIFEST.MF");
80
pw.println("Premain-Class: ModifyAnonymous");
81
pw.println("Agent-Class: ModifyAnonymous");
82
pw.println("Can-Retransform-Classes: true");
83
pw.println("Can-Redefine-Classes: true");
84
pw.close();
85
} catch (FileNotFoundException e) {
86
throw new RuntimeException("Could not write manifest file for the agent", e);
87
}
88
89
sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");
90
if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "ModifyAnonymous.class" })) {
91
throw new RuntimeException("Could not write the agent jar file");
92
}
93
}
94
95
public static class InstanceMethodCallSiteApp {
96
97
public static void test() throws InterruptedException {
98
for (int i = 0; i < 2; i++) {
99
InstanceMethodCallSiteApp app = new InstanceMethodCallSiteApp();
100
Runnable r = app::doWork; // this creates an anonymous class
101
while (!done) {
102
r.run();
103
Thread.sleep(10);
104
}
105
}
106
}
107
108
public void doWork() {
109
System.out.print(".");
110
}
111
}
112
113
static void runTest() {
114
PrintWriter pw;
115
String logName = System.getProperty("test.classes") +
116
File.separator + "loadedClasses.log";
117
// Create a log file to capture the names of the classes in the
118
// allLoadedClasses array. The log file is for assisting in debugging
119
// in case a null class is encountered in the allLoadedClasses array.
120
try {
121
pw = new PrintWriter(new FileOutputStream(
122
new File(logName), true));
123
} catch (FileNotFoundException e) {
124
throw new RuntimeException("Could not write loaded classes to log", e);
125
}
126
while (!done) {
127
Class[] allLoadedClasses = inst.getAllLoadedClasses();
128
int len = allLoadedClasses.length;
129
pw.println(" allLoadedClasses length: " + len);
130
for (int idx = 0; idx < len; idx++) {
131
Class cls = allLoadedClasses[idx];
132
pw.println(" " + idx + " " +
133
((cls != null) ? cls.getName() : "null"));
134
}
135
for (int idx = 0; idx < len; idx++) {
136
Class clazz = allLoadedClasses[idx];
137
if (clazz == null) {
138
pw.flush();
139
pw.close();
140
throw new RuntimeException("null class encountered");
141
}
142
final String name = clazz.getName();
143
if (name.contains("$$Lambda$") && name.contains("App")) {
144
if (inst.isModifiableClass(clazz)) {
145
pw.flush();
146
pw.close();
147
throw new RuntimeException ("Class should not be modifiable");
148
}
149
// Try to modify them anyway.
150
try {
151
System.out.println("retransform called for " + name);
152
inst.retransformClasses(clazz);
153
} catch(java.lang.instrument.UnmodifiableClassException t) {
154
System.out.println("PASSED: expecting UnmodifiableClassException");
155
t.printStackTrace();
156
}
157
try {
158
System.out.println("redefine called for " + name);
159
String newclass = "class Dummy {}";
160
byte[] bytecode = InMemoryJavaCompiler.compile("Dummy", newclass);
161
ClassDefinition cld = new ClassDefinition(clazz, bytecode);
162
inst.redefineClasses(new ClassDefinition[] { cld });
163
} catch(java.lang.instrument.UnmodifiableClassException t) {
164
System.out.println("PASSED: expecting UnmodifiableClassException");
165
t.printStackTrace();
166
} catch(java.lang.ClassNotFoundException e) {
167
pw.flush();
168
pw.close();
169
throw new RuntimeException ("ClassNotFoundException thrown");
170
}
171
done = true;
172
}
173
}
174
}
175
pw.flush();
176
pw.close();
177
}
178
179
public static void main(String argv[]) throws InterruptedException, RuntimeException {
180
if (argv.length == 1 && argv[0].equals("buildagent")) {
181
buildAgent();
182
return;
183
}
184
185
if (inst == null) {
186
throw new RuntimeException("Instrumentation object was null");
187
}
188
189
new Thread() {
190
public void run() {
191
runTest();
192
}
193
}.start();
194
195
// Test that NCDFE is not thrown for anonymous class:
196
// ModifyAnonymous$InstanceMethodCallSiteApp$$Lambda$18
197
try {
198
ModifyAnonymous test = new ModifyAnonymous();
199
InstanceMethodCallSiteApp.test();
200
} catch (NoClassDefFoundError e) {
201
throw new RuntimeException("FAILED: NoClassDefFoundError thrown for " + e.getMessage());
202
}
203
System.out.println("PASSED: NoClassDefFound error not thrown");
204
}
205
}
206
207