Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/RedefineRunningMethodsWithResolutionErrors.java
41153 views
/*1* Copyright (c) 2014, 2019, 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 807611026* @summary Redefine running methods that have cached resolution errors27* @requires vm.jvmti28* @library /test/lib29* @modules java.base/jdk.internal.misc30* @modules java.base/jdk.internal.org.objectweb.asm31* java.instrument32* jdk.jartool/sun.tools.jar33* @run main RedefineClassHelper34* @run main/othervm -javaagent:redefineagent.jar -Xlog:redefine+class+iklass+add=trace,redefine+class+iklass+purge=trace RedefineRunningMethodsWithResolutionErrors35*/3637import jdk.internal.org.objectweb.asm.ClassWriter;38import jdk.internal.org.objectweb.asm.Label;39import jdk.internal.org.objectweb.asm.MethodVisitor;40import jdk.internal.org.objectweb.asm.Opcodes;4142import java.lang.reflect.InvocationTargetException;4344public class RedefineRunningMethodsWithResolutionErrors extends ClassLoader implements Opcodes {4546@Override47protected Class<?> findClass(String name) throws ClassNotFoundException {48if (name.equals("C")) {49byte[] b = loadC(false);50return defineClass(name, b, 0, b.length);51} else {52return super.findClass(name);53}54}5556private static byte[] loadC(boolean redefine) {57ClassWriter cw = new ClassWriter(0);5859cw.visit(52, ACC_SUPER | ACC_PUBLIC, "C", null, "java/lang/Object", null);60{61MethodVisitor mv;6263mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "m", "()V", null, null);64mv.visitCode();6566// First time we run we will:67// 1) Cache resolution errors68// 2) Redefine the class / method69// 3) Try to read the resolution errors that were cached70//71// The redefined method will never run, throw error to be sure72if (redefine) {73createThrowRuntimeExceptionCode(mv, "The redefined method was called");74} else {75createMethodBody(mv);76}77mv.visitMaxs(3, 0);78mv.visitEnd();79}80cw.visitEnd();81return cw.toByteArray();82}8384private static void createMethodBody(MethodVisitor mv) {85Label classExists = new Label();8687// Cache resolution errors88createLoadNonExistentClassCode(mv, classExists);8990// Redefine our own class and method91mv.visitMethodInsn(INVOKESTATIC, "RedefineRunningMethodsWithResolutionErrors", "redefine", "()V");9293// Provoke the same error again to make sure the resolution error cache works94createLoadNonExistentClassCode(mv, classExists);9596// Test passed97mv.visitInsn(RETURN);9899mv.visitFrame(F_SAME, 0, new Object[0], 0, new Object[0]);100mv.visitLabel(classExists);101102createThrowRuntimeExceptionCode(mv, "Loaded class that shouldn't exist (\"NonExistentClass\")");103}104105private static void createLoadNonExistentClassCode(MethodVisitor mv, Label classExists) {106Label tryLoadBegin = new Label();107Label tryLoadEnd = new Label();108Label catchLoadBlock = new Label();109mv.visitTryCatchBlock(tryLoadBegin, tryLoadEnd, catchLoadBlock, "java/lang/NoClassDefFoundError");110111// Try to load a class that does not exist to provoke resolution errors112mv.visitLabel(tryLoadBegin);113mv.visitMethodInsn(INVOKESTATIC, "NonExistentClass", "nonExistentMethod", "()V");114mv.visitLabel(tryLoadEnd);115116// No NoClassDefFoundError means NonExistentClass existed, which shouldn't happen117mv.visitJumpInsn(GOTO, classExists);118119mv.visitFrame(F_SAME1, 0, new Object[0], 1, new Object[] { "java/lang/NoClassDefFoundError" });120mv.visitLabel(catchLoadBlock);121122// Ignore the expected NoClassDefFoundError123mv.visitInsn(POP);124}125126private static void createThrowRuntimeExceptionCode(MethodVisitor mv, String msg) {127mv.visitTypeInsn(NEW, "java/lang/RuntimeException");128mv.visitInsn(DUP);129mv.visitLdcInsn(msg);130mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V");131mv.visitInsn(ATHROW);132}133134private static Class<?> c;135136public static void redefine() throws Exception {137RedefineClassHelper.redefineClass(c, loadC(true));138}139140public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {141c = Class.forName("C", true, new RedefineRunningMethodsWithResolutionErrors());142c.getMethod("m").invoke(null);143}144}145146147