Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/RedefineTests/RedefineRunningMethodsWithResolutionErrors.java
32284 views
/*1* Copyright (c) 2014, 2015, 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* @library /testlibrary28* @modules java.instrument29* java.base/jdk.internal.org.objectweb.asm30* @build RedefineClassHelper31* @run main RedefineClassHelper32* @run main/othervm -javaagent:redefineagent.jar RedefineRunningMethodsWithResolutionErrors33*/3435import jdk.internal.org.objectweb.asm.ClassWriter;36import jdk.internal.org.objectweb.asm.Label;37import jdk.internal.org.objectweb.asm.MethodVisitor;38import jdk.internal.org.objectweb.asm.Opcodes;3940import java.lang.reflect.InvocationTargetException;4142public class RedefineRunningMethodsWithResolutionErrors extends ClassLoader implements Opcodes {4344@Override45protected Class<?> findClass(String name) throws ClassNotFoundException {46if (name.equals("C")) {47byte[] b = loadC(false);48return defineClass(name, b, 0, b.length);49} else {50return super.findClass(name);51}52}5354private static byte[] loadC(boolean redefine) {55ClassWriter cw = new ClassWriter(0);5657cw.visit(52, ACC_SUPER | ACC_PUBLIC, "C", null, "java/lang/Object", null);58{59MethodVisitor mv;6061mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "m", "()V", null, null);62mv.visitCode();6364// First time we run we will:65// 1) Cache resolution errors66// 2) Redefine the class / method67// 3) Try to read the resolution errors that were cached68//69// The redefined method will never run, throw error to be sure70if (redefine) {71createThrowRuntimeExceptionCode(mv, "The redefined method was called");72} else {73createMethodBody(mv);74}75mv.visitMaxs(3, 0);76mv.visitEnd();77}78cw.visitEnd();79return cw.toByteArray();80}8182private static void createMethodBody(MethodVisitor mv) {83Label classExists = new Label();8485// Cache resolution errors86createLoadNonExistentClassCode(mv, classExists);8788// Redefine our own class and method89mv.visitMethodInsn(INVOKESTATIC, "RedefineRunningMethodsWithResolutionErrors", "redefine", "()V");9091// Provoke the same error again to make sure the resolution error cache works92createLoadNonExistentClassCode(mv, classExists);9394// Test passed95mv.visitInsn(RETURN);9697mv.visitFrame(F_SAME, 0, new Object[0], 0, new Object[0]);98mv.visitLabel(classExists);99100createThrowRuntimeExceptionCode(mv, "Loaded class that shouldn't exist (\"NonExistentClass\")");101}102103private static void createLoadNonExistentClassCode(MethodVisitor mv, Label classExists) {104Label tryLoadBegin = new Label();105Label tryLoadEnd = new Label();106Label catchLoadBlock = new Label();107mv.visitTryCatchBlock(tryLoadBegin, tryLoadEnd, catchLoadBlock, "java/lang/NoClassDefFoundError");108109// Try to load a class that does not exist to provoke resolution errors110mv.visitLabel(tryLoadBegin);111mv.visitMethodInsn(INVOKESTATIC, "NonExistentClass", "nonExistentMethod", "()V");112mv.visitLabel(tryLoadEnd);113114// No NoClassDefFoundError means NonExistentClass existed, which shouldn't happen115mv.visitJumpInsn(GOTO, classExists);116117mv.visitFrame(F_SAME1, 0, new Object[0], 1, new Object[] { "java/lang/NoClassDefFoundError" });118mv.visitLabel(catchLoadBlock);119120// Ignore the expected NoClassDefFoundError121mv.visitInsn(POP);122}123124private static void createThrowRuntimeExceptionCode(MethodVisitor mv, String msg) {125mv.visitTypeInsn(NEW, "java/lang/RuntimeException");126mv.visitInsn(DUP);127mv.visitLdcInsn(msg);128mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V");129mv.visitInsn(ATHROW);130}131132private static Class<?> c;133134public static void redefine() throws Exception {135RedefineClassHelper.redefineClass(c, loadC(true));136}137138public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {139c = Class.forName("C", true, new RedefineRunningMethodsWithResolutionErrors());140c.getMethod("m").invoke(null);141}142}143144145