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 /* 26 * @test 27 * @summary Exercise the java.lang.instrument.Instrumentation APIs on classes archived 28 * using CDS/AppCDSv1/AppCDSv2. 29 * @library /test/lib /test/hotspot/jtreg/runtime/appcds /test/hotspot/jtreg/runtime/appcds/test-classes 30 * @requires (vm.opt.UseCompressedOops == null) | (vm.opt.UseCompressedOops == true) 31 * @requires vm.flavor != "minimal" 32 * @modules java.base/jdk.internal.misc 33 * jdk.jartool/sun.tools.jar 34 * java.management 35 * @build sun.hotspot.WhiteBox 36 * InstrumentationApp 37 * InstrumentationClassFileTransformer 38 * InstrumentationRegisterClassFileTransformer 39 * @run main/othervm InstrumentationTest 40 */ 41 42 // Note: TestCommon is from /test/hotspot/jtreg/runtime/appcds/TestCommon.java 43 // Note: Util is from /test/hotspot/jtreg/runtime/appcds/test-classes/TestCommon.java 44 45 import com.sun.tools.attach.VirtualMachine; 46 import com.sun.tools.attach.VirtualMachineDescriptor; 47 import java.io.File; 48 import java.io.FileOutputStream; 49 import java.util.List; 50 import jdk.test.lib.Asserts; 51 import jdk.test.lib.cds.CDSOptions; 52 import jdk.test.lib.process.OutputAnalyzer; 53 import jdk.test.lib.process.ProcessTools; 54 55 public class InstrumentationTest { 56 public static String bootClasses[] = { 57 "InstrumentationApp$Intf", 58 "InstrumentationApp$Bar", 59 "sun.hotspot.WhiteBox", 60 }; 61 public static String appClasses[] = { 62 "InstrumentationApp", 63 "InstrumentationApp$Foo", 64 "InstrumentationApp$MyLoader", 65 }; 66 public static String custClasses[] = { 67 "InstrumentationApp$Coo", 68 }; 69 public static String sharedClasses[] = TestCommon.concat(bootClasses, appClasses); 70 71 public static String agentClasses[] = { 72 "InstrumentationClassFileTransformer", 73 "InstrumentationRegisterClassFileTransformer", 74 "Util", 75 }; 76 77 public static void main(String[] args) throws Throwable { 78 runTest(false); 79 runTest(true); 80 } 81 82 public static void runTest(boolean attachAgent) throws Throwable { 83 String bootJar = 84 ClassFileInstaller.writeJar("InstrumentationBoot.jar", bootClasses); 85 String appJar = 86 ClassFileInstaller.writeJar("InstrumentationApp.jar", 87 TestCommon.concat(appClasses, 88 "InstrumentationApp$ArchivedIfAppCDSv2Enabled")); 89 String custJar = 90 ClassFileInstaller.writeJar("InstrumentationCust.jar", custClasses); 91 String agentJar = 92 ClassFileInstaller.writeJar("InstrumentationAgent.jar", 93 ClassFileInstaller.Manifest.fromSourceFile("InstrumentationAgent.mf"), 94 agentClasses); 95 96 String bootCP = "-Xbootclasspath/a:" + bootJar; 97 98 System.out.println(""); 99 System.out.println("============================================================"); 100 System.out.println("CDS: NO, attachAgent: " + (attachAgent ? "YES" : "NO")); 101 System.out.println("============================================================"); 102 System.out.println(""); 103 104 String agentCmdArg, flagFile; 105 if (attachAgent) { 106 // we will attach the agent, so don't specify -javaagent in the command line. We'll use 107 // something harmless like -showversion to make it easier to construct the command line 108 agentCmdArg = "-showversion"; 109 } else { 110 agentCmdArg = "-javaagent:" + agentJar; 111 } 112 113 // First, run the test class directly, w/o sharing, as a baseline reference 114 flagFile = getFlagFile(attachAgent); 115 AgentAttachThread t = doAttach(attachAgent, flagFile, agentJar); 116 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 117 bootCP, 118 "-cp", appJar, 119 "-XX:+UnlockDiagnosticVMOptions", 120 "-XX:+WhiteBoxAPI", 121 "-Xshare:off", 122 agentCmdArg, 123 "InstrumentationApp", bootJar, appJar, custJar, flagFile); 124 TestCommon.executeAndLog(pb, "no-sharing").shouldHaveExitValue(0); 125 checkAttach(t); 126 127 // Dump the AppCDS archive. On some platforms AppCDSv2 may not be enabled, so we 128 // first try the v2 classlist, and if that fails, revert to the v1 classlist. 129 // Note that the InstrumentationApp$ArchivedIfAppCDSv2Enabled class is archived 130 // only if V2 is enabled. This is tested by InstrumentationApp.isAppCDSV2Enabled(). 131 String[] v2Classes = { 132 "InstrumentationApp$ArchivedIfAppCDSv2Enabled", 133 "java/lang/Object id: 0", 134 "InstrumentationApp$Intf id: 1", 135 "InstrumentationApp$Coo id: 2 super: 0 interfaces: 1 source: " + custJar, 136 }; 137 String[] sharedClassesWithV2 = TestCommon.concat(v2Classes, sharedClasses); 138 OutputAnalyzer out = TestCommon.dump(appJar, sharedClassesWithV2, bootCP); 139 if (out.getExitValue() != 0) { 140 System.out.println("Redumping with AppCDSv2 disabled"); 141 TestCommon.testDump(appJar, sharedClasses, bootCP); 142 } 143 144 // Run with AppCDS. 145 System.out.println(""); 146 System.out.println("============================================================"); 147 System.out.println("CDS: YES, attachAgent: " + (attachAgent ? "YES" : "NO")); 148 System.out.println("============================================================"); 149 System.out.println(""); 150 151 flagFile = getFlagFile(attachAgent); 152 t = doAttach(attachAgent, flagFile, agentJar); 153 out = TestCommon.execAuto("-cp", appJar, 154 bootCP, 155 "-XX:+UnlockDiagnosticVMOptions", 156 "-XX:+WhiteBoxAPI", 157 agentCmdArg, 158 "InstrumentationApp", bootJar, appJar, custJar, flagFile); 159 160 CDSOptions opts = (new CDSOptions()).setXShareMode("auto"); 161 TestCommon.checkExec(out, opts); 162 checkAttach(t); 163 } 164 165 static int flagFileSerial = 1; 166 static private String getFlagFile(boolean attachAgent) { 167 if (attachAgent) { 168 // Do not reuse the same file name as Windows may fail to 169 // delete the file. 170 return "attach.flag." + ProcessHandle.current().pid() + 171 "." + (flagFileSerial++) + "." + System.currentTimeMillis(); 172 } else { 173 return "noattach"; 174 } 175 } 176 177 static AgentAttachThread doAttach(boolean attachAgent, String flagFile, String agentJar) throws Throwable { 178 if (!attachAgent) { 179 return null; 180 } 181 182 // We use the flagFile to prevent the child process to make progress, until we have 183 // attached to it. 184 File f = new File(flagFile); 185 try (FileOutputStream o = new FileOutputStream(f)) { 186 o.write(1); 187 } 188 if (!f.exists()) { 189 throw new RuntimeException("Failed to create " + f); 190 } 191 192 // At this point, the child process is not yet launched. Note that 193 // TestCommon.exec() and OutputAnalyzer.OutputAnalyzer() both block 194 // until the child process has finished. 195 // 196 // So, we will launch a AgentAttachThread which will poll the system 197 // until the child process is launched, and then do the attachment. 198 // The child process is uniquely identified by having flagFile in its 199 // command-line -- see AgentAttachThread.getPid(). 200 AgentAttachThread t = new AgentAttachThread(flagFile, agentJar); 201 t.start(); 202 return t; 203 } 204 205 static void checkAttach(AgentAttachThread thread) throws Throwable { 206 if (thread != null) { 207 thread.check(); 208 } 209 } 210 211 static class AgentAttachThread extends Thread { 212 String flagFile; 213 String agentJar; 214 volatile boolean succeeded; 215 216 AgentAttachThread(String flagFile, String agentJar) { 217 this.flagFile = flagFile; 218 this.agentJar = agentJar; 219 this.succeeded = false; 220 } 221 222 static String getPid(String flagFile) throws Throwable { 223 while (true) { 224 // Keep polling until the child process has been launched. If for some 225 // reason the child process fails to launch, this test will be terminated 226 // by JTREG's time-out mechanism. 227 Thread.sleep(100); 228 List<VirtualMachineDescriptor> vmds = VirtualMachine.list(); 229 for (VirtualMachineDescriptor vmd : vmds) { 230 if (vmd.displayName().contains(flagFile) && vmd.displayName().contains("InstrumentationApp")) { 231 // We use flagFile (which has the PID of this process) as a unique identifier 232 // to ident the child process, which we want to attach to. 233 System.out.println("Process found: " + vmd.id() + " " + vmd.displayName()); 234 return vmd.id(); 235 } 236 } 237 } 238 } 239 240 public void run() { 241 try { 242 String pid = getPid(flagFile); 243 VirtualMachine vm = VirtualMachine.attach(pid); 244 System.out.println(agentJar); 245 vm.loadAgent(agentJar); 246 } catch (Throwable t) { 247 t.printStackTrace(); 248 throw new RuntimeException(t); 249 } 250 251 // Delete the flagFile to indicate to the child process that we 252 // have attached to it, so it should proceed. 253 File f = new File(flagFile); 254 for (int i=0; i<5; i++) { 255 // The detele may fail on Windows if the child JVM is checking 256 // f.exists() at exactly the same time?? Let's do a little 257 // dance. 258 f.delete(); 259 try { 260 Thread.sleep(10); 261 } catch (Throwable t) {;} 262 } 263 if (f.exists()) { 264 throw new RuntimeException("Failed to delete " + f); 265 } 266 System.out.println("Attach succeeded (parent)"); 267 succeeded = true; 268 } 269 270 void check() throws Throwable { 271 super.join(); 272 if (!succeeded) { 273 throw new RuntimeException("Attaching agent to child VM failed"); 274 } 275 } 276 } 277 } 278