Path: blob/master/test/hotspot/jtreg/compiler/jsr292/NonInlinedCall/RedefineTest.java
41153 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 807200826* @modules java.base/jdk.internal.org.objectweb.asm27* java.base/jdk.internal.misc28* java.base/jdk.internal.vm.annotation29* @library /test/lib / ../patches30* @requires vm.jvmti31*32* @build sun.hotspot.WhiteBox33* java.base/java.lang.invoke.MethodHandleHelper34* compiler.jsr292.NonInlinedCall.RedefineTest35* @run driver compiler.jsr292.NonInlinedCall.Agent36* agent.jar37* compiler.jsr292.NonInlinedCall.RedefineTest38* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox39* compiler.jsr292.NonInlinedCall.RedefineTest40* @run main/bootclasspath/othervm -javaagent:agent.jar41* -XX:+IgnoreUnrecognizedVMOptions42* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI43* -Xbatch -XX:-TieredCompilation -XX:CICompilerCount=144* compiler.jsr292.NonInlinedCall.RedefineTest45*/4647package compiler.jsr292.NonInlinedCall;4849import jdk.internal.org.objectweb.asm.ClassWriter;50import jdk.internal.org.objectweb.asm.MethodVisitor;51import jdk.internal.vm.annotation.DontInline;52import sun.hotspot.WhiteBox;5354import java.lang.instrument.ClassDefinition;55import java.lang.instrument.Instrumentation;56import java.lang.invoke.MethodHandle;57import java.lang.invoke.MethodHandleHelper;58import java.lang.invoke.MethodHandles;59import java.lang.invoke.MethodType;6061import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;62import static jdk.internal.org.objectweb.asm.Opcodes.ACC_STATIC;63import static jdk.internal.org.objectweb.asm.Opcodes.ACC_SUPER;64import static jdk.internal.org.objectweb.asm.Opcodes.IRETURN;6566public class RedefineTest {67static final MethodHandles.Lookup LOOKUP = MethodHandleHelper.IMPL_LOOKUP;68static final String NAME = "compiler/jsr292/NonInlinedCall/RedefineTest$T";6970static Class<?> getClass(int r) {71byte[] classFile = getClassFile(r);72try {73return MethodHandles.lookup().defineClass(classFile);74} catch (IllegalAccessException e) {75throw new Error(e);76}77}7879/**80* Generates a class of the following shape:81* static class T {82* @DontInline public static int f() { return $r; }83* }84*/85static byte[] getClassFile(int r) {86ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);87MethodVisitor mv;88cw.visit(52, ACC_PUBLIC | ACC_SUPER, NAME, null, "java/lang/Object", null);89{90mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "f", "()I", null, null);91mv.visitAnnotation("Ljdk/internal/vm/annotation/DontInline;", true);92mv.visitCode();93mv.visitLdcInsn(r);94mv.visitInsn(IRETURN);95mv.visitMaxs(0, 0);96mv.visitEnd();97}98cw.visitEnd();99return cw.toByteArray();100}101102static final MethodHandle mh;103static final Class<?> CLS = getClass(0);104static {105try {106mh = LOOKUP.findStatic(CLS, "f", MethodType.methodType(int.class));107} catch (Exception e) {108throw new Error(e);109}110}111112static final WhiteBox WB = WhiteBox.getWhiteBox();113114@DontInline115static int invokeExact() {116try {117return (int)mh.invokeExact();118} catch (Throwable e) {119throw new Error(e);120}121}122123static Instrumentation instr;124public static void premain(String args, Instrumentation instr) {125RedefineTest.instr = instr;126}127128129public static void main(String[] args) throws Exception {130for (int i = 0; i < 20_000; i++) {131int r = invokeExact();132if (r != 0) {133throw new Error(r + " != 0");134}135}136// WB.ensureCompiled();137138redefine();139140int exp = (instr != null) ? 1 : 0;141142for (int i = 0; i < 20_000; i++) {143if (invokeExact() != exp) {144throw new Error();145}146}147148WB.clearInlineCaches();149150for (int i = 0; i < 20_000; i++) {151if (invokeExact() != exp) {152throw new Error();153}154}155156// WB.ensureCompiled();157}158159static void redefine() {160if (instr == null) {161System.out.println("NOT REDEFINED");162return;163}164ClassDefinition cd = new ClassDefinition(CLS, getClassFile(1));165try {166instr.redefineClasses(cd);167} catch (Exception e) {168throw new Error(e);169}170System.out.println("REDEFINED");171}172}173174175