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