1 /* 2 * Copyright (c) 2017 SAP SE. 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 * @test 26 * @bug 8173743 27 * @summary Failures during class definition can lead to memory leaks in metaspace 28 * @library /test/lib 29 * @run main/othervm test.DefineClass defineClass 30 * @run main/othervm test.DefineClass defineSystemClass 31 * @run main/othervm -XX:+UnlockDiagnosticVMOptions 32 -XX:+UnsyncloadClass -XX:+AllowParallelDefineClass 33 test.DefineClass defineClassParallel 34 * @run main/othervm -XX:+UnlockDiagnosticVMOptions 35 -XX:+UnsyncloadClass -XX:-AllowParallelDefineClass 36 test.DefineClass defineClassParallel 37 * @run main/othervm -XX:+UnlockDiagnosticVMOptions 38 -XX:-UnsyncloadClass -XX:+AllowParallelDefineClass 39 test.DefineClass defineClassParallel 40 * @run main/othervm -XX:+UnlockDiagnosticVMOptions 41 -XX:-UnsyncloadClass -XX:-AllowParallelDefineClass 42 test.DefineClass defineClassParallel 43 * @run main/othervm test.DefineClass redefineClass 44 * @run main/othervm test.DefineClass redefineClassWithError 45 * @author volker.simonis@gmail.com 46 */ 47 48 package test; 49 50 import java.io.ByteArrayOutputStream; 51 import java.io.File; 52 import java.io.FileOutputStream; 53 import java.io.InputStream; 54 import java.lang.instrument.ClassDefinition; 55 import java.lang.instrument.Instrumentation; 56 import java.lang.management.ManagementFactory; 57 import java.util.Scanner; 58 import java.util.concurrent.CountDownLatch; 59 import java.util.jar.Attributes; 60 import java.util.jar.JarEntry; 61 import java.util.jar.JarOutputStream; 62 import java.util.jar.Manifest; 63 64 import javax.management.MBeanServer; 65 import javax.management.ObjectName; 66 67 import com.sun.tools.attach.VirtualMachine; 68 69 import jdk.test.lib.process.ProcessTools; 70 71 public class DefineClass { 72 73 private static Instrumentation instrumentation; 74 75 public void getID(CountDownLatch start, CountDownLatch stop) { 76 String id = "AAAAAAAA"; 77 System.out.println(id); 78 try { 79 // Signal that we've entered the activation.. 80 start.countDown(); 81 //..and wait until we can leave it. 82 stop.await(); 83 } catch (InterruptedException e) { 84 e.printStackTrace(); 85 } 86 System.out.println(id); 87 return; 88 } 89 90 private static class MyThread extends Thread { 91 private DefineClass dc; 92 private CountDownLatch start, stop; 93 94 public MyThread(DefineClass dc, CountDownLatch start, CountDownLatch stop) { 95 this.dc = dc; 96 this.start = start; 97 this.stop = stop; 98 } 99 100 public void run() { 101 dc.getID(start, stop); 102 } 103 } 104 105 private static class ParallelLoadingThread extends Thread { 106 private MyParallelClassLoader pcl; 107 private CountDownLatch stop; 108 private byte[] buf; 109 110 public ParallelLoadingThread(MyParallelClassLoader pcl, byte[] buf, CountDownLatch stop) { 111 this.pcl = pcl; 112 this.stop = stop; 113 this.buf = buf; 114 } 115 116 public void run() { 117 try { 118 stop.await(); 119 } catch (InterruptedException e) { 120 e.printStackTrace(); 121 } 122 try { 123 @SuppressWarnings("unchecked") 124 Class<DefineClass> dc = (Class<DefineClass>) pcl.myDefineClass(DefineClass.class.getName(), buf, 0, buf.length); 125 } 126 catch (LinkageError jle) { 127 // Expected with a parallel capable class loader and 128 // -XX:+UnsyncloadClass or -XX:+AllowParallelDefineClass 129 pcl.incrementLinkageErrors(); 130 } 131 132 } 133 } 134 135 static private class MyClassLoader extends ClassLoader { 136 public Class<?> myDefineClass(String name, byte[] b, int off, int len) throws ClassFormatError { 137 return defineClass(name, b, off, len, null); 138 } 139 } 140 141 static private class MyParallelClassLoader extends ClassLoader { 142 static { 143 System.out.println("parallelCapable : " + registerAsParallelCapable()); 144 } 145 public Class<?> myDefineClass(String name, byte[] b, int off, int len) throws ClassFormatError { 146 return defineClass(name, b, off, len, null); 147 } 148 public synchronized void incrementLinkageErrors() { 149 linkageErrors++; 150 } 151 public int getLinkageErrors() { 152 return linkageErrors; 153 } 154 private volatile int linkageErrors; 155 } 156 157 public static void agentmain(String args, Instrumentation inst) { 158 System.out.println("Loading Java Agent."); 159 instrumentation = inst; 160 } 161 162 163 private static void loadInstrumentationAgent(String myName, byte[] buf) throws Exception { 164 // Create agent jar file on the fly 165 Manifest m = new Manifest(); 166 m.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); 167 m.getMainAttributes().put(new Attributes.Name("Agent-Class"), myName); 168 m.getMainAttributes().put(new Attributes.Name("Can-Redefine-Classes"), "true"); 169 File jarFile = File.createTempFile("agent", ".jar"); 170 jarFile.deleteOnExit(); 171 JarOutputStream jar = new JarOutputStream(new FileOutputStream(jarFile), m); 172 jar.putNextEntry(new JarEntry(myName.replace('.', '/') + ".class")); 173 jar.write(buf); 174 jar.close(); 175 String pid = Long.toString(ProcessTools.getProcessId()); 176 System.out.println("Our pid is = " + pid); 177 VirtualMachine vm = VirtualMachine.attach(pid); 178 vm.loadAgent(jarFile.getAbsolutePath()); 179 } 180 181 private static byte[] getBytecodes(String myName) throws Exception { 182 InputStream is = DefineClass.class.getResourceAsStream(myName + ".class"); 183 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 184 byte[] buf = new byte[4096]; 185 int len; 186 while ((len = is.read(buf)) != -1) baos.write(buf, 0, len); 187 buf = baos.toByteArray(); 188 System.out.println("sizeof(" + myName + ".class) == " + buf.length); 189 return buf; 190 } 191 192 private static int getStringIndex(String needle, byte[] buf) { 193 return getStringIndex(needle, buf, 0); 194 } 195 196 private static int getStringIndex(String needle, byte[] buf, int offset) { 197 outer: 198 for (int i = offset; i < buf.length - offset - needle.length(); i++) { 199 for (int j = 0; j < needle.length(); j++) { 200 if (buf[i + j] != (byte)needle.charAt(j)) continue outer; 201 } 202 return i; 203 } 204 return 0; 205 } 206 207 private static void replaceString(byte[] buf, String name, int index) { 208 for (int i = index; i < index + name.length(); i++) { 209 buf[i] = (byte)name.charAt(i - index); 210 } 211 } 212 213 private static MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer(); 214 215 private static int getClassStats(String pattern) { 216 try { 217 ObjectName diagCmd = new ObjectName("com.sun.management:type=DiagnosticCommand"); 218 219 String result = (String)mbserver.invoke(diagCmd , "gcClassStats" , new Object[] { null }, new String[] {String[].class.getName()}); 220 int count = 0; 221 try (Scanner s = new Scanner(result)) { 222 if (s.hasNextLine()) { 223 System.out.println(s.nextLine()); 224 } 225 while (s.hasNextLine()) { 226 String l = s.nextLine(); 227 if (l.endsWith(pattern)) { 228 count++; 229 System.out.println(l); 230 } 231 } 232 } 233 return count; 234 } 235 catch (Exception e) { 236 throw new RuntimeException("Test failed because we can't read the class statistics!", e); 237 } 238 } 239 240 private static void printClassStats(int expectedCount, boolean reportError) { 241 int count = getClassStats("DefineClass"); 242 String res = "Should have " + expectedCount + 243 " DefineClass instances and we have: " + count; 244 System.out.println(res); 245 if (reportError && count != expectedCount) { 246 throw new RuntimeException(res); 247 } 248 } 249 250 public static final int ITERATIONS = 10; 251 252 public static void main(String[] args) throws Exception { 253 254 ObjectName diagCmd = new ObjectName("com.sun.management:type=DiagnosticCommand"); 255 String result = (String)mbserver.invoke(diagCmd , "vmCommandLine" , 256 new Object[] { null }, new String[] {String[].class.getName()}); 257 if (result.contains("-Xcomp") || result.contains("-XX:-UseInterpreter")) { 258 System.out.println("This test is not executed in in -Xcomp mode!"); 259 return; 260 } 261 262 String myName = DefineClass.class.getName(); 263 byte[] buf = getBytecodes(myName.substring(myName.lastIndexOf(".") + 1)); 264 int iterations = (args.length > 1 ? Integer.parseInt(args[1]) : ITERATIONS); 265 266 if (args.length == 0 || "defineClass".equals(args[0])) { 267 MyClassLoader cl = new MyClassLoader(); 268 for (int i = 0; i < iterations; i++) { 269 try { 270 @SuppressWarnings("unchecked") 271 Class<DefineClass> dc = (Class<DefineClass>) cl.myDefineClass(myName, buf, 0, buf.length); 272 System.out.println(dc); 273 } 274 catch (LinkageError jle) { 275 // Can only define once! 276 if (i == 0) throw new Exception("Should succeed the first time."); 277 } 278 } 279 // We expect to have two instances of DefineClass here: the initial version in which we are 280 // executing and another version which was loaded into our own classloader 'MyClassLoader'. 281 // All the subsequent attempts to reload DefineClass into our 'MyClassLoader' should have failed. 282 printClassStats(2, false); 283 System.gc(); 284 System.out.println("System.gc()"); 285 // At least after System.gc() the failed loading attempts should leave no instances around! 286 printClassStats(2, true); 287 } 288 else if ("defineSystemClass".equals(args[0])) { 289 MyClassLoader cl = new MyClassLoader(); 290 int index = getStringIndex("test/DefineClass", buf); 291 replaceString(buf, "java/DefineClass", index); 292 while ((index = getStringIndex("Ltest/DefineClass;", buf, index + 1)) != 0) { 293 replaceString(buf, "Ljava/DefineClass;", index); 294 } 295 index = getStringIndex("test.DefineClass", buf); 296 replaceString(buf, "java.DefineClass", index); 297 298 for (int i = 0; i < iterations; i++) { 299 try { 300 @SuppressWarnings("unchecked") 301 Class<DefineClass> dc = (Class<DefineClass>) cl.myDefineClass(null, buf, 0, buf.length); 302 throw new RuntimeException("Defining a class in the 'java' package should fail!"); 303 } 304 catch (java.lang.SecurityException jlse) { 305 // Expected, because we're not allowed to define a class in the 'java' package 306 } 307 } 308 // We expect to stay with one (the initial) instances of DefineClass. 309 // All the subsequent attempts to reload DefineClass into the 'java' package should have failed. 310 printClassStats(1, false); 311 System.gc(); 312 System.out.println("System.gc()"); 313 // At least after System.gc() the failed loading attempts should leave no instances around! 314 printClassStats(1, true); 315 } 316 else if ("defineClassParallel".equals(args[0])) { 317 MyParallelClassLoader pcl = new MyParallelClassLoader(); 318 CountDownLatch stop = new CountDownLatch(1); 319 320 Thread[] threads = new Thread[iterations]; 321 for (int i = 0; i < iterations; i++) { 322 (threads[i] = new ParallelLoadingThread(pcl, buf, stop)).start(); 323 } 324 stop.countDown(); // start parallel class loading.. 325 // ..and wait until all threads loaded the class 326 for (int i = 0; i < iterations; i++) { 327 threads[i].join(); 328 } 329 System.out.print("Counted " + pcl.getLinkageErrors() + " LinkageErrors "); 330 System.out.println(pcl.getLinkageErrors() == 0 ? 331 "" : "(use -XX:+UnsyncloadClass and/or -XX:+AllowParallelDefineClass to avoid this)"); 332 System.gc(); 333 System.out.println("System.gc()"); 334 // After System.gc() we expect to remain with two instances: one is the initial version which is 335 // kept alive by this main method and another one in the parallel class loader. 336 printClassStats(2, true); 337 } 338 else if ("redefineClass".equals(args[0])) { 339 loadInstrumentationAgent(myName, buf); 340 int index = getStringIndex("AAAAAAAA", buf); 341 CountDownLatch stop = new CountDownLatch(1); 342 343 Thread[] threads = new Thread[iterations]; 344 for (int i = 0; i < iterations; i++) { 345 buf[index] = (byte) ('A' + i + 1); // Change string constant in getID() which is legal in redefinition 346 instrumentation.redefineClasses(new ClassDefinition(DefineClass.class, buf)); 347 DefineClass dc = DefineClass.class.newInstance(); 348 CountDownLatch start = new CountDownLatch(1); 349 (threads[i] = new MyThread(dc, start, stop)).start(); 350 start.await(); // Wait until the new thread entered the getID() method 351 } 352 // We expect to have one instance for each redefinition because they are all kept alive by an activation 353 // plus the initial version which is kept active by this main method. 354 printClassStats(iterations + 1, false); 355 stop.countDown(); // Let all threads leave the DefineClass.getID() activation.. 356 // ..and wait until really all of them returned from DefineClass.getID() 357 for (int i = 0; i < iterations; i++) { 358 threads[i].join(); 359 } 360 System.gc(); 361 System.out.println("System.gc()"); 362 // After System.gc() we expect to remain with two instances: one is the initial version which is 363 // kept alive by this main method and another one which is the latest redefined version. 364 printClassStats(2, true); 365 } 366 else if ("redefineClassWithError".equals(args[0])) { 367 loadInstrumentationAgent(myName, buf); 368 int index = getStringIndex("getID", buf); 369 370 for (int i = 0; i < iterations; i++) { 371 buf[index] = (byte) 'X'; // Change getID() to XetID() which is illegal in redefinition 372 try { 373 instrumentation.redefineClasses(new ClassDefinition(DefineClass.class, buf)); 374 throw new RuntimeException("Class redefinition isn't allowed to change method names!"); 375 } 376 catch (UnsupportedOperationException uoe) { 377 // Expected because redefinition can't change the name of methods 378 } 379 } 380 // We expect just a single DefineClass instance because failed redefinitions should 381 // leave no garbage around. 382 printClassStats(1, false); 383 System.gc(); 384 System.out.println("System.gc()"); 385 // At least after a System.gc() we should definitely stay with a single instance! 386 printClassStats(1, true); 387 } 388 } 389 }