Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/cha/Utils.java
41149 views
1
/*
2
* Copyright (c) 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
package compiler.cha;
24
25
import jdk.internal.misc.Unsafe;
26
import jdk.internal.org.objectweb.asm.ClassWriter;
27
import jdk.internal.org.objectweb.asm.MethodVisitor;
28
import jdk.internal.vm.annotation.DontInline;
29
import sun.hotspot.WhiteBox;
30
import sun.hotspot.code.NMethod;
31
32
import java.io.IOException;
33
import java.lang.annotation.Retention;
34
import java.lang.annotation.RetentionPolicy;
35
import java.lang.invoke.MethodHandle;
36
import java.lang.invoke.MethodHandles;
37
import java.lang.reflect.Method;
38
import java.util.HashMap;
39
import java.util.concurrent.Callable;
40
41
import static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_FRAMES;
42
import static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_MAXS;
43
import static jdk.internal.org.objectweb.asm.Opcodes.*;
44
import static jdk.test.lib.Asserts.assertTrue;
45
46
public class Utils {
47
public static final Unsafe U = Unsafe.getUnsafe();
48
49
interface Test<T> {
50
void call(T o);
51
T receiver(int id);
52
53
default Runnable monomophic() {
54
return () -> {
55
call(receiver(0)); // 100%
56
};
57
}
58
59
default Runnable bimorphic() {
60
return () -> {
61
call(receiver(0)); // 50%
62
call(receiver(1)); // 50%
63
};
64
}
65
66
default Runnable polymorphic() {
67
return () -> {
68
for (int i = 0; i < 23; i++) {
69
call(receiver(0)); // 92%
70
}
71
call(receiver(1)); // 4%
72
call(receiver(2)); // 4%
73
};
74
}
75
76
default Runnable megamorphic() {
77
return () -> {
78
call(receiver(0)); // 33%
79
call(receiver(1)); // 33%
80
call(receiver(2)); // 33%
81
};
82
}
83
84
default void load(Class<?>... cs) {
85
// nothing to do
86
}
87
88
default void initialize(Class<?>... cs) {
89
for (Class<?> c : cs) {
90
U.ensureClassInitialized(c);
91
}
92
}
93
94
default void repeat(int cnt, Runnable r) {
95
for (int i = 0; i < cnt; i++) {
96
r.run();
97
}
98
}
99
}
100
101
public static abstract class ATest<T> implements Test<T> {
102
public static final WhiteBox WB = WhiteBox.getWhiteBox();
103
104
public static final Object CORRECT = new Object();
105
public static final Object WRONG = new Object();
106
107
final Method TEST;
108
private final Class<T> declared;
109
private final Class<?> receiver;
110
111
private final HashMap<Integer, T> receivers = new HashMap<>();
112
113
public ATest(Class<T> declared, Class<?> receiver) {
114
this.declared = declared;
115
this.receiver = receiver;
116
TEST = compute(() -> this.getClass().getDeclaredMethod("test", declared));
117
}
118
119
@DontInline
120
public abstract Object test(T i);
121
122
public abstract void checkInvalidReceiver();
123
124
public T receiver(int id) {
125
return receivers.computeIfAbsent(id, (i -> {
126
try {
127
MyClassLoader cl = (MyClassLoader) receiver.getClassLoader();
128
Class<?> sub = cl.subclass(receiver, i);
129
return (T)sub.getDeclaredConstructor().newInstance();
130
} catch (Exception e) {
131
throw new Error(e);
132
}
133
}));
134
}
135
136
137
public void compile(Runnable r) {
138
while (!WB.isMethodCompiled(TEST)) {
139
for (int i = 0; i < 100; i++) {
140
r.run();
141
}
142
}
143
assertCompiled(); // record nmethod info
144
}
145
146
private NMethod prevNM = null;
147
148
public void assertNotCompiled() {
149
NMethod curNM = NMethod.get(TEST, false);
150
assertTrue(prevNM != null); // was previously compiled
151
assertTrue(curNM == null || prevNM.compile_id != curNM.compile_id); // either no nmethod present or recompiled
152
prevNM = curNM; // update nmethod info
153
}
154
155
public void assertCompiled() {
156
NMethod curNM = NMethod.get(TEST, false);
157
assertTrue(curNM != null); // nmethod is present
158
assertTrue(prevNM == null || prevNM.compile_id == curNM.compile_id); // no recompilations if nmethod present
159
prevNM = curNM; // update nmethod info
160
}
161
162
@Override
163
public void call(T i) {
164
assertTrue(test(i) != WRONG);
165
}
166
}
167
168
@Retention(value = RetentionPolicy.RUNTIME)
169
public @interface TestCase {}
170
171
static void run(Class<?> test) {
172
try {
173
for (Method m : test.getDeclaredMethods()) {
174
if (m.isAnnotationPresent(TestCase.class)) {
175
System.out.println(m.toString());
176
ClassLoader cl = new MyClassLoader(test);
177
Class<?> c = cl.loadClass(test.getName());
178
c.getMethod(m.getName()).invoke(c.getDeclaredConstructor().newInstance());
179
}
180
}
181
} catch (Exception e) {
182
throw new Error(e);
183
}
184
}
185
186
static class ObjectToStringHelper {
187
static Object testHelper(Object o) {
188
throw new Error("not used");
189
}
190
}
191
static class ObjectHashCodeHelper {
192
static int testHelper(Object o) {
193
throw new Error("not used");
194
}
195
}
196
197
static final class MyClassLoader extends ClassLoader {
198
private final Class<?> test;
199
200
MyClassLoader(Class<?> test) {
201
this.test = test;
202
}
203
204
static String intl(String s) {
205
return s.replace('.', '/');
206
}
207
208
Class<?> subclass(Class<?> c, int id) {
209
String name = c.getName() + id;
210
Class<?> sub = findLoadedClass(name);
211
if (sub == null) {
212
ClassWriter cw = new ClassWriter(COMPUTE_MAXS | COMPUTE_FRAMES);
213
cw.visit(52, ACC_PUBLIC | ACC_SUPER, intl(name), null, intl(c.getName()), null);
214
215
{ // Default constructor: <init>()V
216
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
217
mv.visitCode();
218
mv.visitVarInsn(ALOAD, 0);
219
mv.visitMethodInsn(INVOKESPECIAL, intl(c.getName()), "<init>", "()V", false);
220
mv.visitInsn(RETURN);
221
mv.visitMaxs(0, 0);
222
mv.visitEnd();
223
}
224
225
byte[] classFile = cw.toByteArray();
226
return defineClass(name, classFile, 0, classFile.length);
227
}
228
return sub;
229
}
230
231
protected Class<?> loadClass(String name, boolean resolve)
232
throws ClassNotFoundException
233
{
234
// First, check if the class has already been loaded
235
Class<?> c = findLoadedClass(name);
236
if (c == null) {
237
try {
238
c = getParent().loadClass(name);
239
if (name.endsWith("ObjectToStringHelper")) {
240
ClassWriter cw = new ClassWriter(COMPUTE_MAXS | COMPUTE_FRAMES);
241
cw.visit(52, ACC_PUBLIC | ACC_SUPER, intl(name), null, "java/lang/Object", null);
242
243
{
244
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "testHelper", "(Ljava/lang/Object;)Ljava/lang/Object;", null, null);
245
mv.visitCode();
246
mv.visitVarInsn(ALOAD, 0);
247
mv.visitMethodInsn(INVOKEINTERFACE, intl(test.getName()) + "$I", "toString", "()Ljava/lang/String;", true);
248
mv.visitInsn(ARETURN);
249
mv.visitMaxs(0, 0);
250
mv.visitEnd();
251
}
252
253
byte[] classFile = cw.toByteArray();
254
return defineClass(name, classFile, 0, classFile.length);
255
} else if (name.endsWith("ObjectHashCodeHelper")) {
256
ClassWriter cw = new ClassWriter(COMPUTE_MAXS | COMPUTE_FRAMES);
257
cw.visit(52, ACC_PUBLIC | ACC_SUPER, intl(name), null, "java/lang/Object", null);
258
259
{
260
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "testHelper", "(Ljava/lang/Object;)I", null, null);
261
mv.visitCode();
262
mv.visitVarInsn(ALOAD, 0);
263
mv.visitMethodInsn(INVOKEINTERFACE, intl(test.getName()) + "$I", "hashCode", "()I", true);
264
mv.visitInsn(IRETURN);
265
mv.visitMaxs(0, 0);
266
mv.visitEnd();
267
}
268
269
byte[] classFile = cw.toByteArray();
270
return defineClass(name, classFile, 0, classFile.length);
271
} else if (c == test || name.startsWith(test.getName())) {
272
try {
273
String path = name.replace('.', '/') + ".class";
274
byte[] classFile = getParent().getResourceAsStream(path).readAllBytes();
275
return defineClass(name, classFile, 0, classFile.length);
276
} catch (IOException e) {
277
throw new Error(e);
278
}
279
}
280
} catch (ClassNotFoundException e) {
281
// ClassNotFoundException thrown if class not found
282
// from the non-null parent class loader
283
}
284
285
if (c == null) {
286
// If still not found, then invoke findClass in order
287
// to find the class.
288
c = findClass(name);
289
}
290
}
291
if (resolve) {
292
resolveClass(c);
293
}
294
return c;
295
}
296
}
297
298
public interface RunnableWithException {
299
void run() throws Throwable;
300
}
301
302
public static void shouldThrow(Class<? extends Throwable> expectedException, RunnableWithException r) {
303
try {
304
r.run();
305
throw new AssertionError("Exception not thrown: " + expectedException.getName());
306
} catch(Throwable e) {
307
if (expectedException == e.getClass()) {
308
// success: proper exception is thrown
309
} else {
310
throw new Error(expectedException.getName() + " is expected", e);
311
}
312
}
313
}
314
315
public static MethodHandle unsafeCastMH(Class<?> cls) {
316
try {
317
MethodHandle mh = MethodHandles.identity(Object.class);
318
return MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(cls));
319
} catch (Throwable e) {
320
throw new Error(e);
321
}
322
}
323
324
static <T> T compute(Callable<T> c) {
325
try {
326
return c.call();
327
} catch (Exception e) {
328
throw new Error(e);
329
}
330
}
331
}
332
333