Mercurial > hg > truffle
comparison test/compiler/jsr292/RedefineMethodUsedByMultipleMethodHandles.java @ 20677:fe34c5ab0b35
8042235: redefining method used by multiple MethodHandles crashes VM
Summary: note all MemberNames created on internal list for adjusting method entries.
Reviewed-by: sspitsyn, dcubed, lfoltan
author | coleenp |
---|---|
date | Wed, 19 Nov 2014 13:02:11 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
20666:bee8095780db | 20677:fe34c5ab0b35 |
---|---|
1 /* | |
2 * Copyright (c) 2014, 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 * @compile -XDignore.symbol.file RedefineMethodUsedByMultipleMethodHandles.java | |
29 * @run main RedefineMethodUsedByMultipleMethodHandles | |
30 */ | |
31 | |
32 import java.io.*; | |
33 import java.lang.instrument.*; | |
34 import java.lang.invoke.*; | |
35 import java.lang.invoke.MethodHandles.Lookup; | |
36 import java.lang.management.*; | |
37 import java.lang.reflect.*; | |
38 import java.nio.file.*; | |
39 import java.security.*; | |
40 import java.util.jar.*; | |
41 | |
42 import javax.tools.*; | |
43 | |
44 import jdk.internal.org.objectweb.asm.*; | |
45 | |
46 public class RedefineMethodUsedByMultipleMethodHandles { | |
47 | |
48 static class Foo { | |
49 public static Object getName() { | |
50 return "foo"; | |
51 } | |
52 } | |
53 | |
54 public static void main(String[] args) throws Throwable { | |
55 | |
56 Lookup lookup = MethodHandles.lookup(); | |
57 Method fooMethod = Foo.class.getDeclaredMethod("getName"); | |
58 | |
59 // fooMH2 displaces fooMH1 from the MemberNamesTable | |
60 MethodHandle fooMH1 = lookup.unreflect(fooMethod); | |
61 MethodHandle fooMH2 = lookup.unreflect(fooMethod); | |
62 | |
63 System.out.println("fooMH1.invoke = " + fooMH1.invokeExact()); | |
64 System.out.println("fooMH2.invoke = " + fooMH2.invokeExact()); | |
65 | |
66 // Redefining Foo.getName() causes vmtarget to be updated | |
67 // in fooMH2 but not fooMH1 | |
68 redefineFoo(); | |
69 | |
70 // Full GC causes fooMH1.vmtarget to be deallocated | |
71 System.gc(); | |
72 | |
73 // Calling fooMH1.vmtarget crashes the VM | |
74 System.out.println("fooMH1.invoke = " + fooMH1.invokeExact()); | |
75 } | |
76 | |
77 /** | |
78 * Adds the class file bytes for {@code c} to {@code jar}. | |
79 */ | |
80 static void add(JarOutputStream jar, Class<?> c) throws IOException { | |
81 String classAsPath = c.getName().replace('.', '/') + ".class"; | |
82 jar.putNextEntry(new JarEntry(classAsPath)); | |
83 InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath); | |
84 | |
85 int b; | |
86 while ((b = stream.read()) != -1) { | |
87 jar.write(b); | |
88 } | |
89 } | |
90 | |
91 static void redefineFoo() throws Exception { | |
92 Manifest manifest = new Manifest(); | |
93 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); | |
94 Attributes mainAttrs = manifest.getMainAttributes(); | |
95 mainAttrs.putValue("Agent-Class", FooAgent.class.getName()); | |
96 mainAttrs.putValue("Can-Redefine-Classes", "true"); | |
97 mainAttrs.putValue("Can-Retransform-Classes", "true"); | |
98 | |
99 Path jar = Files.createTempFile("myagent", ".jar"); | |
100 try { | |
101 JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jar.toFile()), manifest); | |
102 add(jarStream, FooAgent.class); | |
103 add(jarStream, FooTransformer.class); | |
104 jarStream.close(); | |
105 runAgent(jar); | |
106 } finally { | |
107 Files.deleteIfExists(jar); | |
108 } | |
109 } | |
110 | |
111 public static void runAgent(Path agent) throws Exception { | |
112 String vmName = ManagementFactory.getRuntimeMXBean().getName(); | |
113 int p = vmName.indexOf('@'); | |
114 assert p != -1 : "VM name not in <pid>@<host> format: " + vmName; | |
115 String pid = vmName.substring(0, p); | |
116 ClassLoader cl = ToolProvider.getSystemToolClassLoader(); | |
117 Class<?> c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl); | |
118 Method attach = c.getDeclaredMethod("attach", String.class); | |
119 Method loadAgent = c.getDeclaredMethod("loadAgent", String.class); | |
120 Method detach = c.getDeclaredMethod("detach"); | |
121 Object vm = attach.invoke(null, pid); | |
122 loadAgent.invoke(vm, agent.toString()); | |
123 detach.invoke(vm); | |
124 } | |
125 | |
126 public static class FooAgent { | |
127 | |
128 public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception { | |
129 assert inst.isRedefineClassesSupported(); | |
130 assert inst.isRetransformClassesSupported(); | |
131 inst.addTransformer(new FooTransformer(), true); | |
132 Class<?>[] classes = inst.getAllLoadedClasses(); | |
133 for (int i = 0; i < classes.length; i++) { | |
134 Class<?> c = classes[i]; | |
135 if (c == Foo.class) { | |
136 inst.retransformClasses(new Class[]{c}); | |
137 } | |
138 } | |
139 } | |
140 } | |
141 | |
142 static class FooTransformer implements ClassFileTransformer { | |
143 | |
144 @Override | |
145 public byte[] transform(ClassLoader cl, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { | |
146 if (Foo.class.equals(classBeingRedefined)) { | |
147 System.out.println("redefining " + classBeingRedefined); | |
148 ClassReader cr = new ClassReader(classfileBuffer); | |
149 ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES); | |
150 ClassVisitor adapter = new ClassVisitor(Opcodes.ASM5, cw) { | |
151 @Override | |
152 public MethodVisitor visitMethod(int access, String base, String desc, String signature, String[] exceptions) { | |
153 MethodVisitor mv = cv.visitMethod(access, base, desc, signature, exceptions); | |
154 if (mv != null) { | |
155 mv = new MethodVisitor(Opcodes.ASM5, mv) { | |
156 @Override | |
157 public void visitLdcInsn(Object cst) { | |
158 System.out.println("replacing \"" + cst + "\" with \"bar\""); | |
159 mv.visitLdcInsn("bar"); | |
160 } | |
161 }; | |
162 } | |
163 return mv; | |
164 } | |
165 }; | |
166 | |
167 cr.accept(adapter, ClassReader.SKIP_FRAMES); | |
168 cw.visitEnd(); | |
169 return cw.toByteArray(); | |
170 } | |
171 return classfileBuffer; | |
172 } | |
173 } | |
174 } |