coleenp@7391: /* coleenp@7391: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. coleenp@7391: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. coleenp@7391: * coleenp@7391: * This code is free software; you can redistribute it and/or modify it coleenp@7391: * under the terms of the GNU General Public License version 2 only, as coleenp@7391: * published by the Free Software Foundation. coleenp@7391: * coleenp@7391: * This code is distributed in the hope that it will be useful, but WITHOUT coleenp@7391: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or coleenp@7391: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License coleenp@7391: * version 2 for more details (a copy is included in the LICENSE file that coleenp@7391: * accompanied this code). coleenp@7391: * coleenp@7391: * You should have received a copy of the GNU General Public License version coleenp@7391: * 2 along with this work; if not, write to the Free Software Foundation, coleenp@7391: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. coleenp@7391: * coleenp@7391: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA coleenp@7391: * or visit www.oracle.com if you need additional information or have any coleenp@7391: * questions. coleenp@7391: */ coleenp@7391: coleenp@7391: /** coleenp@7391: * @test coleenp@7391: * @bug 8042235 coleenp@7391: * @summary redefining method used by multiple MethodHandles crashes VM coleenp@7391: * @compile -XDignore.symbol.file RedefineMethodUsedByMultipleMethodHandles.java coleenp@7391: * @run main RedefineMethodUsedByMultipleMethodHandles coleenp@7391: */ coleenp@7391: coleenp@7391: import java.io.*; coleenp@7391: import java.lang.instrument.*; coleenp@7391: import java.lang.invoke.*; coleenp@7391: import java.lang.invoke.MethodHandles.Lookup; coleenp@7391: import java.lang.management.*; coleenp@7391: import java.lang.reflect.*; coleenp@7391: import java.nio.file.*; coleenp@7391: import java.security.*; coleenp@7391: import java.util.jar.*; coleenp@7391: coleenp@7391: import javax.tools.*; coleenp@7391: coleenp@7391: import jdk.internal.org.objectweb.asm.*; coleenp@7391: coleenp@7391: public class RedefineMethodUsedByMultipleMethodHandles { coleenp@7391: coleenp@7391: static class Foo { coleenp@7391: public static Object getName() { coleenp@7391: return "foo"; coleenp@7391: } coleenp@7391: } coleenp@7391: coleenp@7391: public static void main(String[] args) throws Throwable { coleenp@7391: coleenp@7391: Lookup lookup = MethodHandles.lookup(); coleenp@7391: Method fooMethod = Foo.class.getDeclaredMethod("getName"); coleenp@7391: coleenp@7391: // fooMH2 displaces fooMH1 from the MemberNamesTable coleenp@7391: MethodHandle fooMH1 = lookup.unreflect(fooMethod); coleenp@7391: MethodHandle fooMH2 = lookup.unreflect(fooMethod); coleenp@7391: coleenp@7391: System.out.println("fooMH1.invoke = " + fooMH1.invokeExact()); coleenp@7391: System.out.println("fooMH2.invoke = " + fooMH2.invokeExact()); coleenp@7391: coleenp@7391: // Redefining Foo.getName() causes vmtarget to be updated coleenp@7391: // in fooMH2 but not fooMH1 coleenp@7391: redefineFoo(); coleenp@7391: coleenp@7391: // Full GC causes fooMH1.vmtarget to be deallocated coleenp@7391: System.gc(); coleenp@7391: coleenp@7391: // Calling fooMH1.vmtarget crashes the VM coleenp@7391: System.out.println("fooMH1.invoke = " + fooMH1.invokeExact()); coleenp@7391: } coleenp@7391: coleenp@7391: /** coleenp@7391: * Adds the class file bytes for {@code c} to {@code jar}. coleenp@7391: */ coleenp@7391: static void add(JarOutputStream jar, Class c) throws IOException { coleenp@7391: String classAsPath = c.getName().replace('.', '/') + ".class"; coleenp@7391: jar.putNextEntry(new JarEntry(classAsPath)); coleenp@7391: InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath); coleenp@7391: coleenp@7391: int b; coleenp@7391: while ((b = stream.read()) != -1) { coleenp@7391: jar.write(b); coleenp@7391: } coleenp@7391: } coleenp@7391: coleenp@7391: static void redefineFoo() throws Exception { coleenp@7391: Manifest manifest = new Manifest(); coleenp@7391: manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); coleenp@7391: Attributes mainAttrs = manifest.getMainAttributes(); coleenp@7391: mainAttrs.putValue("Agent-Class", FooAgent.class.getName()); coleenp@7391: mainAttrs.putValue("Can-Redefine-Classes", "true"); coleenp@7391: mainAttrs.putValue("Can-Retransform-Classes", "true"); coleenp@7391: coleenp@7391: Path jar = Files.createTempFile("myagent", ".jar"); coleenp@7391: try { coleenp@7391: JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jar.toFile()), manifest); coleenp@7391: add(jarStream, FooAgent.class); coleenp@7391: add(jarStream, FooTransformer.class); coleenp@7391: jarStream.close(); coleenp@7391: runAgent(jar); coleenp@7391: } finally { coleenp@7391: Files.deleteIfExists(jar); coleenp@7391: } coleenp@7391: } coleenp@7391: coleenp@7391: public static void runAgent(Path agent) throws Exception { coleenp@7391: String vmName = ManagementFactory.getRuntimeMXBean().getName(); coleenp@7391: int p = vmName.indexOf('@'); coleenp@7391: assert p != -1 : "VM name not in @ format: " + vmName; coleenp@7391: String pid = vmName.substring(0, p); coleenp@7391: ClassLoader cl = ToolProvider.getSystemToolClassLoader(); coleenp@7391: Class c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl); coleenp@7391: Method attach = c.getDeclaredMethod("attach", String.class); coleenp@7391: Method loadAgent = c.getDeclaredMethod("loadAgent", String.class); coleenp@7391: Method detach = c.getDeclaredMethod("detach"); coleenp@7391: Object vm = attach.invoke(null, pid); coleenp@7391: loadAgent.invoke(vm, agent.toString()); coleenp@7391: detach.invoke(vm); coleenp@7391: } coleenp@7391: coleenp@7391: public static class FooAgent { coleenp@7391: coleenp@7391: public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception { coleenp@7391: assert inst.isRedefineClassesSupported(); coleenp@7391: assert inst.isRetransformClassesSupported(); coleenp@7391: inst.addTransformer(new FooTransformer(), true); coleenp@7391: Class[] classes = inst.getAllLoadedClasses(); coleenp@7391: for (int i = 0; i < classes.length; i++) { coleenp@7391: Class c = classes[i]; coleenp@7391: if (c == Foo.class) { coleenp@7391: inst.retransformClasses(new Class[]{c}); coleenp@7391: } coleenp@7391: } coleenp@7391: } coleenp@7391: } coleenp@7391: coleenp@7391: static class FooTransformer implements ClassFileTransformer { coleenp@7391: coleenp@7391: @Override coleenp@7391: public byte[] transform(ClassLoader cl, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { coleenp@7391: if (Foo.class.equals(classBeingRedefined)) { coleenp@7391: System.out.println("redefining " + classBeingRedefined); coleenp@7391: ClassReader cr = new ClassReader(classfileBuffer); coleenp@7391: ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES); coleenp@7391: ClassVisitor adapter = new ClassVisitor(Opcodes.ASM5, cw) { coleenp@7391: @Override coleenp@7391: public MethodVisitor visitMethod(int access, String base, String desc, String signature, String[] exceptions) { coleenp@7391: MethodVisitor mv = cv.visitMethod(access, base, desc, signature, exceptions); coleenp@7391: if (mv != null) { coleenp@7391: mv = new MethodVisitor(Opcodes.ASM5, mv) { coleenp@7391: @Override coleenp@7391: public void visitLdcInsn(Object cst) { coleenp@7391: System.out.println("replacing \"" + cst + "\" with \"bar\""); coleenp@7391: mv.visitLdcInsn("bar"); coleenp@7391: } coleenp@7391: }; coleenp@7391: } coleenp@7391: return mv; coleenp@7391: } coleenp@7391: }; coleenp@7391: coleenp@7391: cr.accept(adapter, ClassReader.SKIP_FRAMES); coleenp@7391: cw.visitEnd(); coleenp@7391: return cw.toByteArray(); coleenp@7391: } coleenp@7391: return classfileBuffer; coleenp@7391: } coleenp@7391: } coleenp@7391: }