1 /* 2 * Copyright (c) 2005, 2024, 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 import java.io.PrintStream; 26 import java.nio.file.Path; 27 28 import jdk.test.lib.process.OutputAnalyzer; 29 import jdk.test.lib.process.ProcessTools; 30 import jdk.test.lib.helpers.ClassFileInstaller; 31 32 /* 33 * @test 34 * @bug 6274264 6274241 5070281 35 * @summary test retransformClasses 36 * 37 * @modules java.instrument 38 * @library /test/lib 39 * @build jdk.test.lib.process.ProcessTools 40 * @build RetransformAgent asmlib.Instrumentor 41 * @enablePreview 42 * @comment The test uses asmlib/Instrumentor.java which relies on ClassFile API PreviewFeature. 43 * @run driver/timeout=240 RetransformApp roleDriver 44 * @comment The test uses a higher timeout to prevent test timeouts noted in JDK-6528548 45 */ 46 public class RetransformApp { 47 48 public static void main(String[] args) throws Exception { 49 if (args.length == 1) { 50 if (!"roleDriver".equals(args[0])) { 51 throw new Exception("unexpected program argument: " + args[0]); 52 } 53 // launch the RetransformApp java process after creating the necessary 54 // infrastructure 55 System.out.println("creating agent jar"); 56 final Path agentJar = createAgentJar(); 57 System.out.println("launching app, with javaagent jar: " + agentJar); 58 launchApp(agentJar); 59 } else { 60 System.err.println("running app"); 61 new RetransformApp().run(System.out); 62 } 63 } 64 65 private static Path createAgentJar() throws Exception { 66 Path agentJar = Path.of("RetransformAgent.jar"); 67 final String manifest = """ 68 Manifest-Version: 1.0 69 Premain-Class: RetransformAgent 70 Can-Retransform-Classes: true 71 """; 72 System.out.println("Manifest is:\n" + manifest); 73 ClassFileInstaller.writeJar(agentJar.getFileName().toString(), 74 ClassFileInstaller.Manifest.fromString(manifest), 75 "RetransformAgent", 76 "asmlib.Instrumentor"); 77 return agentJar; 78 } 79 80 private static void launchApp(final Path agentJar) throws Exception { 81 final OutputAnalyzer oa = ProcessTools.executeTestJava( 82 "--enable-preview", // due to usage of ClassFile API PreviewFeature in the agent 83 "-javaagent:" + agentJar.toString(), 84 RetransformApp.class.getName()); 85 oa.shouldHaveExitValue(0); 86 // make available stdout/stderr in the logs, even in case of successful completion 87 oa.reportDiagnosticSummary(); 88 } 89 90 int foo(int x) { 91 return x * x; 92 } 93 94 public void run(PrintStream out) throws Exception { 95 out.println("start"); 96 for (int i = 0; i < 4; i++) { 97 if (foo(3) != 9) { 98 throw new Exception("ERROR: unexpected application behavior"); 99 } 100 } 101 out.println("undo"); 102 RetransformAgent.undo(); 103 for (int i = 0; i < 4; i++) { 104 if (foo(3) != 9) { 105 throw new Exception("ERROR: unexpected application behavior"); 106 } 107 } 108 out.println("end"); 109 if (RetransformAgent.succeeded()) { 110 out.println("Instrumentation succeeded."); 111 } else { 112 throw new Exception("ERROR: Instrumentation failed."); 113 } 114 } 115 }