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