1 /*
   2  * Copyright (c) 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 8072008
  27  * @library /testlibrary /test/lib
  28  * @compile -XDignore.symbol.file RedefineTest.java Agent.java
  29  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  30  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  31  *                              java.lang.invoke.RedefineTest
  32  *                              Agent
  33  * @run main Agent agent.jar java.lang.invoke.RedefineTest
  34  * @run main/othervm -Xbootclasspath/a:. -javaagent:agent.jar
  35  *                   -XX:+IgnoreUnrecognizedVMOptions
  36  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  37  *                   -Xbatch -XX:-TieredCompilation -XX:CICompilerCount=1
  38  *                      java.lang.invoke.RedefineTest
  39  */
  40 package java.lang.invoke;
  41 
  42 import sun.hotspot.WhiteBox;
  43 import sun.misc.Unsafe;
  44 import jdk.internal.org.objectweb.asm.*;
  45 import jdk.internal.vm.annotation.DontInline;
  46 import java.lang.instrument.ClassDefinition;
  47 import java.lang.instrument.Instrumentation;
  48 
  49 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  50 
  51 public class RedefineTest {
  52     static final MethodHandles.Lookup LOOKUP = MethodHandles.Lookup.IMPL_LOOKUP;
  53     static final Unsafe UNSAFE = Unsafe.getUnsafe();
  54 
  55     static final String NAME = "java/lang/invoke/RedefineTest$T";
  56 
  57     static Class<?> getClass(int r) {
  58         byte[] classFile = getClassFile(r);
  59         return UNSAFE.defineClass(NAME, classFile, 0, classFile.length, null, null);
  60     }
  61 
  62     /**
  63      * Generates a class of the following shape:
  64      *     static class T {
  65      *         @DontInline public static int f() { return $r; }
  66      *     }
  67      */
  68     static byte[] getClassFile(int r) {
  69         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
  70         MethodVisitor mv;
  71         cw.visit(52, ACC_PUBLIC | ACC_SUPER, NAME, null, "java/lang/Object", null);
  72         {
  73             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "f", "()I", null, null);
  74             mv.visitAnnotation("Ljdk/internal/vm/annotation/DontInline;", true);
  75             mv.visitCode();
  76             mv.visitLdcInsn(r);
  77             mv.visitInsn(IRETURN);
  78             mv.visitMaxs(0, 0);
  79             mv.visitEnd();
  80         }
  81         cw.visitEnd();
  82         return cw.toByteArray();
  83     }
  84 
  85     static final MethodHandle mh;
  86     static final Class<?> CLS = getClass(0);
  87     static {
  88         try {
  89             mh = LOOKUP.findStatic(CLS, "f", MethodType.methodType(int.class));
  90         } catch (Exception e) {
  91             throw new Error(e);
  92         }
  93     }
  94 
  95     static final WhiteBox WB = WhiteBox.getWhiteBox();
  96 
  97     @DontInline
  98     static int invokeBasic() {
  99         try {
 100             return (int)mh.invokeExact();
 101         } catch (Throwable e) {
 102             throw new Error(e);
 103         }
 104     }
 105 
 106     static Instrumentation instr;
 107     public static void premain(String args, Instrumentation instr) {
 108         RedefineTest.instr = instr;
 109     }
 110 
 111 
 112     public static void main(String[] args) throws Exception {
 113         for (int i = 0; i < 20_000; i++) {
 114             int r = invokeBasic();
 115             if (r != 0) {
 116                 throw new Error(r + " != 0");
 117             }
 118         }
 119         // WB.ensureCompiled();
 120 
 121         redefine();
 122 
 123         int exp = (instr != null) ? 1 : 0;
 124 
 125         for (int i = 0; i < 20_000; i++) {
 126             if (invokeBasic() != exp) {
 127                 throw new Error();
 128             }
 129         }
 130 
 131         WB.clearInlineCaches();
 132 
 133         for (int i = 0; i < 20_000; i++) {
 134             if (invokeBasic() != exp) {
 135                 throw new Error();
 136             }
 137         }
 138 
 139         // WB.ensureCompiled();
 140     }
 141 
 142     static void redefine() {
 143         if (instr == null) {
 144             System.out.println("NOT REDEFINED");
 145             return;
 146         }
 147         ClassDefinition cd = new ClassDefinition(CLS, getClassFile(1));
 148         try {
 149             instr.redefineClasses(cd);
 150         } catch (Exception e) {
 151             throw new Error(e);
 152         }
 153         System.out.println("REDEFINED");
 154     }
 155 }