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