1 /*
2 * Copyright (c) 2014, 2017, 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.lang.instrument.ClassDefinition;
26 import java.lang.instrument.Instrumentation;
27 import java.lang.instrument.UnmodifiableClassException;
28 import java.net.URL;
29 import java.net.URLClassLoader;
30 import java.io.File;
31 import java.security.CodeSigner;
32 import java.security.CodeSource;
33 import java.security.ProtectionDomain;
34 import sun.hotspot.WhiteBox;
35
36 public class InstrumentationApp {
37 static WhiteBox wb = WhiteBox.getWhiteBox();
38
39 public static final String COO_CLASS_NAME = "InstrumentationApp$Coo";
40
41 public static interface Intf { // Loaded from Boot class loader (-Xbootclasspath/a).
42 public String get();
43 }
44 public static class Bar implements Intf { // Loaded from Boot class loader.
45 public String get() {
46 // The initial transform:
47 // change "buzz" -> "fuzz"
48 // The re-transform:
49 // change "buzz" -> "guzz"
50 return "buzz";
123 waitAttach(flagFile);
124
125 instrumentation = InstrumentationRegisterClassFileTransformer.getInstrumentation();
126 System.out.println("INFO: instrumentation = " + instrumentation);
127
128 testBootstrapCDS("Bootstrap Loader", bootJar);
129 testAppCDSv1("Application Loader", appJar);
130
131 if (isAppCDSV2Enabled()) {
132 testAppCDSv2("Custom Loader (unregistered)", custJar);
133 }
134
135 if (failed > 0) {
136 throw new RuntimeException("FINAL RESULT: " + failed + " out of " + numTests + " test case(s) have failed");
137 } else {
138 System.out.println("FINAL RESULT: All " + numTests + " test case(s) have passed!");
139 }
140 }
141
142 static void waitAttach(String flagFile) throws Throwable {
143 if (!flagFile.equals("noattach")) {
144 File f = new File(flagFile);
145 long start = System.currentTimeMillis();
146 while (f.exists()) {
147 long elapsed = System.currentTimeMillis() - start;
148 System.out.println(".... (" + elapsed + ") waiting for deletion of " + f);
149 Thread.sleep(1000);
150 }
151 System.out.println("Attach succeeded (child)");
152 isAttachingAgent = true;
153 }
154 }
155
156 static void testBootstrapCDS(String group, File jar) throws Throwable {
157 doTest(group, new Bar(), jar);
158 }
159
160 static void testAppCDSv1(String group, File jar) throws Throwable {
161 doTest(group, new Foo(), jar);
162 }
163
164 static void testAppCDSv2(String group, File jar) throws Throwable {
|
1 /*
2 * Copyright (c) 2014, 2019, 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.lang.instrument.ClassDefinition;
26 import java.lang.instrument.Instrumentation;
27 import java.lang.instrument.UnmodifiableClassException;
28 import java.net.URL;
29 import java.net.URLClassLoader;
30 import java.io.File;
31 import java.io.FileWriter;
32 import java.security.CodeSigner;
33 import java.security.CodeSource;
34 import java.security.ProtectionDomain;
35 import sun.hotspot.WhiteBox;
36
37 public class InstrumentationApp {
38 static WhiteBox wb = WhiteBox.getWhiteBox();
39
40 public static final String COO_CLASS_NAME = "InstrumentationApp$Coo";
41
42 public static interface Intf { // Loaded from Boot class loader (-Xbootclasspath/a).
43 public String get();
44 }
45 public static class Bar implements Intf { // Loaded from Boot class loader.
46 public String get() {
47 // The initial transform:
48 // change "buzz" -> "fuzz"
49 // The re-transform:
50 // change "buzz" -> "guzz"
51 return "buzz";
124 waitAttach(flagFile);
125
126 instrumentation = InstrumentationRegisterClassFileTransformer.getInstrumentation();
127 System.out.println("INFO: instrumentation = " + instrumentation);
128
129 testBootstrapCDS("Bootstrap Loader", bootJar);
130 testAppCDSv1("Application Loader", appJar);
131
132 if (isAppCDSV2Enabled()) {
133 testAppCDSv2("Custom Loader (unregistered)", custJar);
134 }
135
136 if (failed > 0) {
137 throw new RuntimeException("FINAL RESULT: " + failed + " out of " + numTests + " test case(s) have failed");
138 } else {
139 System.out.println("FINAL RESULT: All " + numTests + " test case(s) have passed!");
140 }
141 }
142
143 static void waitAttach(String flagFile) throws Throwable {
144 // See InstrumentationTest.java for the hand-shake protocol.
145 if (!flagFile.equals("noattach")) {
146 File f = new File(flagFile);
147 try (FileWriter fw = new FileWriter(f)) {
148 long pid = ProcessHandle.current().pid();
149 System.out.println("my pid = " + pid);
150 fw.write(Long.toString(pid));
151 fw.write("\n");
152 for (int i=0; i<10; i++) {
153 // Parent process waits until we have written more than 100 bytes, so it won't
154 // read a partial pid
155 fw.write("==========");
156 }
157 fw.close();
158 }
159
160 long start = System.currentTimeMillis();
161 while (f.exists()) {
162 long elapsed = System.currentTimeMillis() - start;
163 System.out.println(".... (" + elapsed + ") waiting for deletion of " + f);
164 Thread.sleep(1000);
165 }
166 System.out.println("Attach succeeded (child)");
167 isAttachingAgent = true;
168 }
169 }
170
171 static void testBootstrapCDS(String group, File jar) throws Throwable {
172 doTest(group, new Bar(), jar);
173 }
174
175 static void testAppCDSv1(String group, File jar) throws Throwable {
176 doTest(group, new Foo(), jar);
177 }
178
179 static void testAppCDSv2(String group, File jar) throws Throwable {
|