test/compiler/jsr292/RedefineMethodUsedByMultipleMethodHandles.java

changeset 7391
fe34c5ab0b35
child 9629
8119983116f6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/compiler/jsr292/RedefineMethodUsedByMultipleMethodHandles.java	Wed Nov 19 13:02:11 2014 -0500
     1.3 @@ -0,0 +1,174 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * @test
    1.29 + * @bug 8042235
    1.30 + * @summary redefining method used by multiple MethodHandles crashes VM
    1.31 + * @compile -XDignore.symbol.file RedefineMethodUsedByMultipleMethodHandles.java
    1.32 + * @run main RedefineMethodUsedByMultipleMethodHandles
    1.33 + */
    1.34 +
    1.35 +import java.io.*;
    1.36 +import java.lang.instrument.*;
    1.37 +import java.lang.invoke.*;
    1.38 +import java.lang.invoke.MethodHandles.Lookup;
    1.39 +import java.lang.management.*;
    1.40 +import java.lang.reflect.*;
    1.41 +import java.nio.file.*;
    1.42 +import java.security.*;
    1.43 +import java.util.jar.*;
    1.44 +
    1.45 +import javax.tools.*;
    1.46 +
    1.47 +import jdk.internal.org.objectweb.asm.*;
    1.48 +
    1.49 +public class RedefineMethodUsedByMultipleMethodHandles {
    1.50 +
    1.51 +    static class Foo {
    1.52 +        public static Object getName() {
    1.53 +            return "foo";
    1.54 +        }
    1.55 +    }
    1.56 +
    1.57 +    public static void main(String[] args) throws Throwable {
    1.58 +
    1.59 +        Lookup lookup = MethodHandles.lookup();
    1.60 +        Method fooMethod = Foo.class.getDeclaredMethod("getName");
    1.61 +
    1.62 +        // fooMH2 displaces fooMH1 from the MemberNamesTable
    1.63 +        MethodHandle fooMH1 = lookup.unreflect(fooMethod);
    1.64 +        MethodHandle fooMH2 = lookup.unreflect(fooMethod);
    1.65 +
    1.66 +        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
    1.67 +        System.out.println("fooMH2.invoke = " + fooMH2.invokeExact());
    1.68 +
    1.69 +        // Redefining Foo.getName() causes vmtarget to be updated
    1.70 +        // in fooMH2 but not fooMH1
    1.71 +        redefineFoo();
    1.72 +
    1.73 +        // Full GC causes fooMH1.vmtarget to be deallocated
    1.74 +        System.gc();
    1.75 +
    1.76 +        // Calling fooMH1.vmtarget crashes the VM
    1.77 +        System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * Adds the class file bytes for {@code c} to {@code jar}.
    1.82 +     */
    1.83 +    static void add(JarOutputStream jar, Class<?> c) throws IOException {
    1.84 +        String classAsPath = c.getName().replace('.', '/') + ".class";
    1.85 +        jar.putNextEntry(new JarEntry(classAsPath));
    1.86 +        InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath);
    1.87 +
    1.88 +        int b;
    1.89 +        while ((b = stream.read()) != -1) {
    1.90 +            jar.write(b);
    1.91 +        }
    1.92 +    }
    1.93 +
    1.94 +    static void redefineFoo() throws Exception {
    1.95 +        Manifest manifest = new Manifest();
    1.96 +        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    1.97 +        Attributes mainAttrs = manifest.getMainAttributes();
    1.98 +        mainAttrs.putValue("Agent-Class", FooAgent.class.getName());
    1.99 +        mainAttrs.putValue("Can-Redefine-Classes", "true");
   1.100 +        mainAttrs.putValue("Can-Retransform-Classes", "true");
   1.101 +
   1.102 +        Path jar = Files.createTempFile("myagent", ".jar");
   1.103 +        try {
   1.104 +            JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jar.toFile()), manifest);
   1.105 +            add(jarStream, FooAgent.class);
   1.106 +            add(jarStream, FooTransformer.class);
   1.107 +            jarStream.close();
   1.108 +            runAgent(jar);
   1.109 +        } finally {
   1.110 +            Files.deleteIfExists(jar);
   1.111 +        }
   1.112 +    }
   1.113 +
   1.114 +    public static void runAgent(Path agent) throws Exception {
   1.115 +        String vmName = ManagementFactory.getRuntimeMXBean().getName();
   1.116 +        int p = vmName.indexOf('@');
   1.117 +        assert p != -1 : "VM name not in <pid>@<host> format: " + vmName;
   1.118 +        String pid = vmName.substring(0, p);
   1.119 +        ClassLoader cl = ToolProvider.getSystemToolClassLoader();
   1.120 +        Class<?> c = Class.forName("com.sun.tools.attach.VirtualMachine", true, cl);
   1.121 +        Method attach = c.getDeclaredMethod("attach", String.class);
   1.122 +        Method loadAgent = c.getDeclaredMethod("loadAgent", String.class);
   1.123 +        Method detach = c.getDeclaredMethod("detach");
   1.124 +        Object vm = attach.invoke(null, pid);
   1.125 +        loadAgent.invoke(vm, agent.toString());
   1.126 +        detach.invoke(vm);
   1.127 +    }
   1.128 +
   1.129 +    public static class FooAgent {
   1.130 +
   1.131 +        public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception {
   1.132 +            assert inst.isRedefineClassesSupported();
   1.133 +            assert inst.isRetransformClassesSupported();
   1.134 +            inst.addTransformer(new FooTransformer(), true);
   1.135 +            Class<?>[] classes = inst.getAllLoadedClasses();
   1.136 +            for (int i = 0; i < classes.length; i++) {
   1.137 +                Class<?> c = classes[i];
   1.138 +                if (c == Foo.class) {
   1.139 +                    inst.retransformClasses(new Class[]{c});
   1.140 +                }
   1.141 +            }
   1.142 +        }
   1.143 +    }
   1.144 +
   1.145 +    static class FooTransformer implements ClassFileTransformer {
   1.146 +
   1.147 +        @Override
   1.148 +        public byte[] transform(ClassLoader cl, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
   1.149 +            if (Foo.class.equals(classBeingRedefined)) {
   1.150 +                System.out.println("redefining " + classBeingRedefined);
   1.151 +                ClassReader cr = new ClassReader(classfileBuffer);
   1.152 +                ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);
   1.153 +                ClassVisitor adapter = new ClassVisitor(Opcodes.ASM5, cw) {
   1.154 +                    @Override
   1.155 +                    public MethodVisitor visitMethod(int access, String base, String desc, String signature, String[] exceptions) {
   1.156 +                        MethodVisitor mv = cv.visitMethod(access, base, desc, signature, exceptions);
   1.157 +                        if (mv != null) {
   1.158 +                            mv = new MethodVisitor(Opcodes.ASM5, mv) {
   1.159 +                                @Override
   1.160 +                                public void visitLdcInsn(Object cst) {
   1.161 +                                    System.out.println("replacing \"" + cst + "\" with \"bar\"");
   1.162 +                                    mv.visitLdcInsn("bar");
   1.163 +                                }
   1.164 +                            };
   1.165 +                        }
   1.166 +                        return mv;
   1.167 +                    }
   1.168 +                };
   1.169 +
   1.170 +                cr.accept(adapter, ClassReader.SKIP_FRAMES);
   1.171 +                cw.visitEnd();
   1.172 +                return cw.toByteArray();
   1.173 +            }
   1.174 +            return classfileBuffer;
   1.175 +        }
   1.176 +    }
   1.177 +}

mercurial