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