Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/jsr292/RedefineMethodUsedByMultipleMethodHandles.java
41152 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
/**
25
* @test
26
* @bug 8042235
27
* @summary redefining method used by multiple MethodHandles crashes VM
28
* @library /
29
* @modules java.base/jdk.internal.org.objectweb.asm
30
* java.compiler
31
* java.instrument
32
* jdk.attach
33
* @requires vm.jvmti
34
*
35
* @run main/othervm -Djdk.attach.allowAttachSelf compiler.jsr292.RedefineMethodUsedByMultipleMethodHandles
36
*/
37
38
package compiler.jsr292;
39
40
import jdk.internal.org.objectweb.asm.ClassReader;
41
import jdk.internal.org.objectweb.asm.ClassVisitor;
42
import jdk.internal.org.objectweb.asm.ClassWriter;
43
import jdk.internal.org.objectweb.asm.MethodVisitor;
44
import jdk.internal.org.objectweb.asm.Opcodes;
45
46
import java.io.FileOutputStream;
47
import java.io.IOException;
48
import java.io.InputStream;
49
import java.lang.instrument.ClassFileTransformer;
50
import java.lang.instrument.IllegalClassFormatException;
51
import java.lang.instrument.Instrumentation;
52
import java.lang.invoke.MethodHandle;
53
import java.lang.invoke.MethodHandles;
54
import java.lang.invoke.MethodHandles.Lookup;
55
import java.lang.reflect.Method;
56
import java.nio.file.Files;
57
import java.nio.file.Path;
58
import java.security.ProtectionDomain;
59
import java.util.jar.Attributes;
60
import java.util.jar.JarEntry;
61
import java.util.jar.JarOutputStream;
62
import java.util.jar.Manifest;
63
64
public class RedefineMethodUsedByMultipleMethodHandles {
65
66
static class Foo {
67
public static Object getName() {
68
return "foo";
69
}
70
}
71
72
public static void main(String[] args) throws Throwable {
73
74
Lookup lookup = MethodHandles.lookup();
75
Method fooMethod = Foo.class.getDeclaredMethod("getName");
76
77
// fooMH2 displaces fooMH1 from the MemberNamesTable
78
MethodHandle fooMH1 = lookup.unreflect(fooMethod);
79
MethodHandle fooMH2 = lookup.unreflect(fooMethod);
80
81
System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
82
System.out.println("fooMH2.invoke = " + fooMH2.invokeExact());
83
84
// Redefining Foo.getName() causes vmtarget to be updated
85
// in fooMH2 but not fooMH1
86
redefineFoo();
87
88
// Full GC causes fooMH1.vmtarget to be deallocated
89
System.gc();
90
91
// Calling fooMH1.vmtarget crashes the VM
92
System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
93
}
94
95
/**
96
* Adds the class file bytes for {@code c} to {@code jar}.
97
*/
98
static void add(JarOutputStream jar, Class<?> c) throws IOException {
99
String classAsPath = c.getName().replace('.', '/') + ".class";
100
jar.putNextEntry(new JarEntry(classAsPath));
101
InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath);
102
103
int b;
104
while ((b = stream.read()) != -1) {
105
jar.write(b);
106
}
107
}
108
109
static void redefineFoo() throws Exception {
110
Manifest manifest = new Manifest();
111
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
112
Attributes mainAttrs = manifest.getMainAttributes();
113
mainAttrs.putValue("Agent-Class", FooAgent.class.getName());
114
mainAttrs.putValue("Can-Redefine-Classes", "true");
115
mainAttrs.putValue("Can-Retransform-Classes", "true");
116
117
// The jar file will be added to the system classloader search path. It is not safe
118
// to delete it while the JVM is running, so make sure to create it in the test
119
// directory so it will be cleaned up by the test harness.
120
Path jar = Files.createTempFile(Path.of(""), "myagent", ".jar");
121
JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jar.toFile()), manifest);
122
add(jarStream, FooAgent.class);
123
add(jarStream, FooTransformer.class);
124
jarStream.close();
125
runAgent(jar);
126
}
127
128
public static void runAgent(Path agent) throws Exception {
129
String pid = Long.toString(ProcessHandle.current().pid());
130
ClassLoader cl = ClassLoader.getSystemClassLoader();
131
Class<?> c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl);
132
Method attach = c.getDeclaredMethod("attach", String.class);
133
Method loadAgent = c.getDeclaredMethod("loadAgent", String.class);
134
Method detach = c.getDeclaredMethod("detach");
135
Object vm = attach.invoke(null, pid);
136
loadAgent.invoke(vm, agent.toString());
137
detach.invoke(vm);
138
}
139
140
public static class FooAgent {
141
142
public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception {
143
assert inst.isRedefineClassesSupported();
144
assert inst.isRetransformClassesSupported();
145
inst.addTransformer(new FooTransformer(), true);
146
Class<?>[] classes = inst.getAllLoadedClasses();
147
for (int i = 0; i < classes.length; i++) {
148
Class<?> c = classes[i];
149
if (c == Foo.class) {
150
inst.retransformClasses(new Class[]{c});
151
}
152
}
153
}
154
}
155
156
static class FooTransformer implements ClassFileTransformer {
157
158
@Override
159
public byte[] transform(ClassLoader cl, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
160
if (Foo.class.equals(classBeingRedefined)) {
161
System.out.println("redefining " + classBeingRedefined);
162
ClassReader cr = new ClassReader(classfileBuffer);
163
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);
164
ClassVisitor adapter = new ClassVisitor(Opcodes.ASM5, cw) {
165
@Override
166
public MethodVisitor visitMethod(int access, String base, String desc, String signature, String[] exceptions) {
167
MethodVisitor mv = cv.visitMethod(access, base, desc, signature, exceptions);
168
if (mv != null) {
169
mv = new MethodVisitor(Opcodes.ASM5, mv) {
170
@Override
171
public void visitLdcInsn(Object cst) {
172
System.out.println("replacing \"" + cst + "\" with \"bar\"");
173
mv.visitLdcInsn("bar");
174
}
175
};
176
}
177
return mv;
178
}
179
};
180
181
cr.accept(adapter, ClassReader.SKIP_FRAMES);
182
cw.visitEnd();
183
return cw.toByteArray();
184
}
185
return classfileBuffer;
186
}
187
}
188
}
189
190