Path: blob/master/test/hotspot/jtreg/compiler/profiling/spectrapredefineclass_classloaders/Agent.java
41154 views
/*1* Copyright (c) 2014, 2020, 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*/2223package compiler.profiling.spectrapredefineclass_classloaders;2425import com.sun.tools.attach.VirtualMachine;26import jdk.test.lib.Utils;2728import java.lang.instrument.ClassFileTransformer;29import java.lang.instrument.Instrumentation;30import java.lang.reflect.Method;31import java.net.MalformedURLException;32import java.net.URL;33import java.net.URLClassLoader;34import java.nio.file.Path;35import java.nio.file.Paths;36import java.security.ProtectionDomain;3738public class Agent implements ClassFileTransformer {39public static final String AGENT_JAR = Paths.get(Utils.TEST_CLASSES, "agent.jar").toString();40public static ClassLoader newClassLoader() {41try {42return new URLClassLoader(new URL[] {43Paths.get(Utils.TEST_CLASSES).toUri().toURL(),44}, null);45} catch (MalformedURLException e){46throw new RuntimeException("Unexpected URL conversion failure", e);47}48}4950static public Class Test_class;5152static public void main(String[] args) throws Exception {5354// loader2 must be first on the list so loader 1 must be used first55ClassLoader loader1 = newClassLoader();56String packageName = Agent.class.getPackage().getName();57Class dummy = loader1.loadClass(packageName + ".Test");5859ClassLoader loader2 = newClassLoader();6061Test_class = loader2.loadClass(packageName + ".Test");62Method m3 = Test_class.getMethod("m3", ClassLoader.class);63// Add speculative trap in m2() (loaded by loader1) that64// references m4() (loaded by loader2).65m3.invoke(Test_class.newInstance(), loader1);6667String pid = Long.toString(ProcessHandle.current().pid());6869// Make the nmethod go away70for (int i = 0; i < 10; i++) {71System.gc();72}7374// Redefine class Test loaded by loader275for (int i = 0; i < 2; i++) {76try {77VirtualMachine vm = VirtualMachine.attach(pid);78vm.loadAgent(AGENT_JAR, "");79vm.detach();80} catch (Exception e) {81throw new RuntimeException(e);82}83}84// Will process loader2 first, find m4() is redefined and85// needs to be freed then process loader1, check the86// speculative trap in m2() and try to access m4() which was87// freed already.88for (int i = 0; i < 10; i++) {89System.gc();90}91}9293public synchronized byte[] transform(final ClassLoader classLoader,94final String className,95Class<?> classBeingRedefined,96ProtectionDomain protectionDomain,97byte[] classfileBuffer) {98System.out.println("Transforming class " + className + " "+ classLoader);99return classfileBuffer;100}101102public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) {103104try {105instrumentation.retransformClasses(to_redefine);106} catch (Exception e) {107e.printStackTrace();108}109110}111112public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception {113Agent transformer = new Agent();114instrumentation.addTransformer(transformer, true);115116redefine(agentArgs, instrumentation, Test_class);117}118}119120121