1 /* 2 * Copyright (c) 2014, 2015, 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 * @requires vm.flavor != "minimal" 30 * @modules java.base/jdk.internal.org.objectweb.asm 31 * java.compiler 32 * java.instrument 33 * java.management 34 * 35 * @run main/othervm 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 javax.tools.ToolProvider; 47 import java.io.FileOutputStream; 48 import java.io.IOException; 49 import java.io.InputStream; 50 import java.lang.instrument.ClassFileTransformer; 51 import java.lang.instrument.IllegalClassFormatException; 52 import java.lang.instrument.Instrumentation; 53 import java.lang.invoke.MethodHandle; 54 import java.lang.invoke.MethodHandles; 55 import java.lang.invoke.MethodHandles.Lookup; 56 import java.lang.management.ManagementFactory; 57 import java.lang.reflect.Method; 58 import java.nio.file.Files; 59 import java.nio.file.Path; 60 import java.security.ProtectionDomain; 61 import java.util.jar.Attributes; 62 import java.util.jar.JarEntry; 63 import java.util.jar.JarOutputStream; 64 import java.util.jar.Manifest; 65 66 public class RedefineMethodUsedByMultipleMethodHandles { 67 68 static class Foo { 69 public static Object getName() { 70 return "foo"; 71 } 72 } 73 74 public static void main(String[] args) throws Throwable { 75 76 Lookup lookup = MethodHandles.lookup(); 77 Method fooMethod = Foo.class.getDeclaredMethod("getName"); 78 79 // fooMH2 displaces fooMH1 from the MemberNamesTable 80 MethodHandle fooMH1 = lookup.unreflect(fooMethod); 81 MethodHandle fooMH2 = lookup.unreflect(fooMethod); 82 83 System.out.println("fooMH1.invoke = " + fooMH1.invokeExact()); 84 System.out.println("fooMH2.invoke = " + fooMH2.invokeExact()); 85 86 // Redefining Foo.getName() causes vmtarget to be updated 87 // in fooMH2 but not fooMH1 88 redefineFoo(); 89 90 // Full GC causes fooMH1.vmtarget to be deallocated 91 System.gc(); 92 93 // Calling fooMH1.vmtarget crashes the VM 94 System.out.println("fooMH1.invoke = " + fooMH1.invokeExact()); 95 } 96 97 /** 98 * Adds the class file bytes for {@code c} to {@code jar}. 99 */ 100 static void add(JarOutputStream jar, Class<?> c) throws IOException { 101 String classAsPath = c.getName().replace('.', '/') + ".class"; 102 jar.putNextEntry(new JarEntry(classAsPath)); 103 InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath); 104 105 int b; 106 while ((b = stream.read()) != -1) { 107 jar.write(b); 108 } 109 } 110 111 static void redefineFoo() throws Exception { 112 Manifest manifest = new Manifest(); 113 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); 114 Attributes mainAttrs = manifest.getMainAttributes(); 115 mainAttrs.putValue("Agent-Class", FooAgent.class.getName()); 116 mainAttrs.putValue("Can-Redefine-Classes", "true"); 117 mainAttrs.putValue("Can-Retransform-Classes", "true"); 118 119 Path jar = Files.createTempFile("myagent", ".jar"); 120 try { 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 } finally { 127 Files.deleteIfExists(jar); 128 } 129 } 130 131 public static void runAgent(Path agent) throws Exception { 132 String vmName = ManagementFactory.getRuntimeMXBean().getName(); 133 int p = vmName.indexOf('@'); 134 assert p != -1 : "VM name not in <pid>@<host> format: " + vmName; 135 String pid = vmName.substring(0, p); 136 ClassLoader cl = ToolProvider.getSystemToolClassLoader(); 137 Class<?> c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl); 138 Method attach = c.getDeclaredMethod("attach", String.class); 139 Method loadAgent = c.getDeclaredMethod("loadAgent", String.class); 140 Method detach = c.getDeclaredMethod("detach"); 141 Object vm = attach.invoke(null, pid); 142 loadAgent.invoke(vm, agent.toString()); 143 detach.invoke(vm); 144 } 145 146 public static class FooAgent { 147 148 public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception { 149 assert inst.isRedefineClassesSupported(); 150 assert inst.isRetransformClassesSupported(); 151 inst.addTransformer(new FooTransformer(), true); 152 Class<?>[] classes = inst.getAllLoadedClasses(); 153 for (int i = 0; i < classes.length; i++) { 154 Class<?> c = classes[i]; 155 if (c == Foo.class) { 156 inst.retransformClasses(new Class[]{c}); 157 } 158 } 159 } 160 } 161 162 static class FooTransformer implements ClassFileTransformer { 163 164 @Override 165 public byte[] transform(ClassLoader cl, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { 166 if (Foo.class.equals(classBeingRedefined)) { 167 System.out.println("redefining " + classBeingRedefined); 168 ClassReader cr = new ClassReader(classfileBuffer); 169 ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES); 170 ClassVisitor adapter = new ClassVisitor(Opcodes.ASM5, cw) { 171 @Override 172 public MethodVisitor visitMethod(int access, String base, String desc, String signature, String[] exceptions) { 173 MethodVisitor mv = cv.visitMethod(access, base, desc, signature, exceptions); 174 if (mv != null) { 175 mv = new MethodVisitor(Opcodes.ASM5, mv) { 176 @Override 177 public void visitLdcInsn(Object cst) { 178 System.out.println("replacing \"" + cst + "\" with \"bar\""); 179 mv.visitLdcInsn("bar"); 180 } 181 }; 182 } 183 return mv; 184 } 185 }; 186 187 cr.accept(adapter, ClassReader.SKIP_FRAMES); 188 cw.visitEnd(); 189 return cw.toByteArray(); 190 } 191 return classfileBuffer; 192 } 193 } 194 }