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