Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/RedefineRunningMethodsWithResolutionErrors.java
41153 views
1
/*
2
* Copyright (c) 2014, 2019, 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
* @bug 8076110
27
* @summary Redefine running methods that have cached resolution errors
28
* @requires vm.jvmti
29
* @library /test/lib
30
* @modules java.base/jdk.internal.misc
31
* @modules java.base/jdk.internal.org.objectweb.asm
32
* java.instrument
33
* jdk.jartool/sun.tools.jar
34
* @run main RedefineClassHelper
35
* @run main/othervm -javaagent:redefineagent.jar -Xlog:redefine+class+iklass+add=trace,redefine+class+iklass+purge=trace RedefineRunningMethodsWithResolutionErrors
36
*/
37
38
import jdk.internal.org.objectweb.asm.ClassWriter;
39
import jdk.internal.org.objectweb.asm.Label;
40
import jdk.internal.org.objectweb.asm.MethodVisitor;
41
import jdk.internal.org.objectweb.asm.Opcodes;
42
43
import java.lang.reflect.InvocationTargetException;
44
45
public class RedefineRunningMethodsWithResolutionErrors extends ClassLoader implements Opcodes {
46
47
@Override
48
protected Class<?> findClass(String name) throws ClassNotFoundException {
49
if (name.equals("C")) {
50
byte[] b = loadC(false);
51
return defineClass(name, b, 0, b.length);
52
} else {
53
return super.findClass(name);
54
}
55
}
56
57
private static byte[] loadC(boolean redefine) {
58
ClassWriter cw = new ClassWriter(0);
59
60
cw.visit(52, ACC_SUPER | ACC_PUBLIC, "C", null, "java/lang/Object", null);
61
{
62
MethodVisitor mv;
63
64
mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "m", "()V", null, null);
65
mv.visitCode();
66
67
// First time we run we will:
68
// 1) Cache resolution errors
69
// 2) Redefine the class / method
70
// 3) Try to read the resolution errors that were cached
71
//
72
// The redefined method will never run, throw error to be sure
73
if (redefine) {
74
createThrowRuntimeExceptionCode(mv, "The redefined method was called");
75
} else {
76
createMethodBody(mv);
77
}
78
mv.visitMaxs(3, 0);
79
mv.visitEnd();
80
}
81
cw.visitEnd();
82
return cw.toByteArray();
83
}
84
85
private static void createMethodBody(MethodVisitor mv) {
86
Label classExists = new Label();
87
88
// Cache resolution errors
89
createLoadNonExistentClassCode(mv, classExists);
90
91
// Redefine our own class and method
92
mv.visitMethodInsn(INVOKESTATIC, "RedefineRunningMethodsWithResolutionErrors", "redefine", "()V");
93
94
// Provoke the same error again to make sure the resolution error cache works
95
createLoadNonExistentClassCode(mv, classExists);
96
97
// Test passed
98
mv.visitInsn(RETURN);
99
100
mv.visitFrame(F_SAME, 0, new Object[0], 0, new Object[0]);
101
mv.visitLabel(classExists);
102
103
createThrowRuntimeExceptionCode(mv, "Loaded class that shouldn't exist (\"NonExistentClass\")");
104
}
105
106
private static void createLoadNonExistentClassCode(MethodVisitor mv, Label classExists) {
107
Label tryLoadBegin = new Label();
108
Label tryLoadEnd = new Label();
109
Label catchLoadBlock = new Label();
110
mv.visitTryCatchBlock(tryLoadBegin, tryLoadEnd, catchLoadBlock, "java/lang/NoClassDefFoundError");
111
112
// Try to load a class that does not exist to provoke resolution errors
113
mv.visitLabel(tryLoadBegin);
114
mv.visitMethodInsn(INVOKESTATIC, "NonExistentClass", "nonExistentMethod", "()V");
115
mv.visitLabel(tryLoadEnd);
116
117
// No NoClassDefFoundError means NonExistentClass existed, which shouldn't happen
118
mv.visitJumpInsn(GOTO, classExists);
119
120
mv.visitFrame(F_SAME1, 0, new Object[0], 1, new Object[] { "java/lang/NoClassDefFoundError" });
121
mv.visitLabel(catchLoadBlock);
122
123
// Ignore the expected NoClassDefFoundError
124
mv.visitInsn(POP);
125
}
126
127
private static void createThrowRuntimeExceptionCode(MethodVisitor mv, String msg) {
128
mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
129
mv.visitInsn(DUP);
130
mv.visitLdcInsn(msg);
131
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V");
132
mv.visitInsn(ATHROW);
133
}
134
135
private static Class<?> c;
136
137
public static void redefine() throws Exception {
138
RedefineClassHelper.redefineClass(c, loadC(true));
139
}
140
141
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
142
c = Class.forName("C", true, new RedefineRunningMethodsWithResolutionErrors());
143
c.getMethod("m").invoke(null);
144
}
145
}
146
147