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 System.out.println(e); 237 System.out.println(e.getCause()); 238 e.getCause().printStackTrace(); 239 throw new RuntimeException("Test failed because we can't read the class statistics!"); 240 } 241 } 242 243 private static void printClassStats(int expectedCount, boolean reportError) { 244 int count = getClassStats("DefineClass"); 245 String res = "Should have " + expectedCount + 246 " DefineClass instances and we have: " + count; 247 System.out.println(res); 248 if (reportError && count != expectedCount) { 249 throw new RuntimeException(res); 250 } 251 } 252 253 public static final int ITERATIONS = 10; 254 255 public static void main(String[] args) throws Exception { 256 String myName = DefineClass.class.getName(); 257 byte[] buf = getBytecodes(myName.substring(myName.lastIndexOf(".") + 1)); 258 int iterations = (args.length > 1 ? Integer.parseInt(args[1]) : ITERATIONS); 259 260 if (args.length == 0 || "defineClass".equals(args[0])) { 261 MyClassLoader cl = new MyClassLoader(); 262 for (int i = 0; i < iterations; i++) { 263 try { 264 @SuppressWarnings("unchecked") 265 Class<DefineClass> dc = (Class<DefineClass>) cl.myDefineClass(myName, buf, 0, buf.length); 266 System.out.println(dc); 267 } 268 catch (LinkageError jle) { 269 // Can only define once! 270 if (i == 0) throw new Exception("Should succeed the first time."); 271 } 272 } 273 // We expect to have two instances of DefineClass here: the initial version in which we are 274 // executing and another version which was loaded into our own classloader 'MyClassLoader'. 275 // All the subsequent attempts to reload DefineClass into our 'MyClassLoader' should have failed. 276 printClassStats(2, false); 277 System.gc(); 278 System.out.println("System.gc()"); 279 // At least after System.gc() the failed loading attempts should leave no instances around! 280 printClassStats(2, true); 281 } 282 else if ("defineSystemClass".equals(args[0])) { 283 MyClassLoader cl = new MyClassLoader(); 284 int index = getStringIndex("test/DefineClass", buf); 285 replaceString(buf, "java/DefineClass", index); 286 while ((index = getStringIndex("Ltest/DefineClass;", buf, index + 1)) != 0) { 287 replaceString(buf, "Ljava/DefineClass;", index); 288 } 289 index = getStringIndex("test.DefineClass", buf); 290 replaceString(buf, "java.DefineClass", index); 291 292 for (int i = 0; i < iterations; i++) { 293 try { 294 @SuppressWarnings("unchecked") 295 Class<DefineClass> dc = (Class<DefineClass>) cl.myDefineClass(null, buf, 0, buf.length); 296 throw new RuntimeException("Defining a class in the 'java' package should fail!"); 297 } 298 catch (java.lang.SecurityException jlse) { 299 // Expected, because we're not allowed to define a class in the 'java' package 300 } 301 } 302 // We expect to stay with one (the initial) instances of DefineClass. 303 // All the subsequent attempts to reload DefineClass into the 'java' package should have failed. 304 printClassStats(1, false); 305 System.gc(); 306 System.out.println("System.gc()"); 307 // At least after System.gc() the failed loading attempts should leave no instances around! 308 printClassStats(1, true); 309 } 310 else if ("defineClassParallel".equals(args[0])) { 311 MyParallelClassLoader pcl = new MyParallelClassLoader(); 312 CountDownLatch stop = new CountDownLatch(1); 313 314 Thread[] threads = new Thread[iterations]; 315 for (int i = 0; i < iterations; i++) { 316 (threads[i] = new ParallelLoadingThread(pcl, buf, stop)).start(); 317 } 318 stop.countDown(); // start parallel class loading.. 319 // ..and wait until all threads loaded the class 320 for (int i = 0; i < iterations; i++) { 321 threads[i].join(); 322 } 323 System.out.print("Counted " + pcl.getLinkageErrors() + " LinkageErrors "); 324 System.out.println(pcl.getLinkageErrors() == 0 ? 325 "" : "(use -XX:+UnsyncloadClass and/or -XX:+AllowParallelDefineClass to avoid this)"); 326 System.gc(); 327 System.out.println("System.gc()"); 328 // After System.gc() we expect to remain with two instances: one is the initial version which is 329 // kept alive by this main method and another one in the parallel class loader. 330 printClassStats(2, true); 331 } 332 else if ("redefineClass".equals(args[0])) { 333 loadInstrumentationAgent(myName, buf); 334 int index = getStringIndex("AAAAAAAA", buf); 335 CountDownLatch stop = new CountDownLatch(1); 336 337 Thread[] threads = new Thread[iterations]; 338 for (int i = 0; i < iterations; i++) { 339 buf[index] = (byte) ('A' + i + 1); // Change string constant in getID() which is legal in redefinition 340 instrumentation.redefineClasses(new ClassDefinition(DefineClass.class, buf)); 341 DefineClass dc = DefineClass.class.newInstance(); 342 CountDownLatch start = new CountDownLatch(1); 343 (threads[i] = new MyThread(dc, start, stop)).start(); 344 start.await(); // Wait until the new thread entered the getID() method 345 } 346 // We expect to have one instance for each redefinition because they are all kept alive by an activation 347 // plus the initial version which is kept active by this main method. 348 printClassStats(iterations + 1, false); 349 stop.countDown(); // Let all threads leave the DefineClass.getID() activation.. 350 // ..and wait until really all of them returned from DefineClass.getID() 351 for (int i = 0; i < iterations; i++) { 352 threads[i].join(); 353 } 354 System.gc(); 355 System.out.println("System.gc()"); 356 // After System.gc() we expect to remain with two instances: one is the initial version which is 357 // kept alive by this main method and another one which is the latest redefined version. 358 printClassStats(2, true); 359 } 360 else if ("redefineClassWithError".equals(args[0])) { 361 loadInstrumentationAgent(myName, buf); 362 int index = getStringIndex("getID", buf); 363 364 for (int i = 0; i < iterations; i++) { 365 buf[index] = (byte) 'X'; // Change getID() to XetID() which is illegal in redefinition 366 try { 367 instrumentation.redefineClasses(new ClassDefinition(DefineClass.class, buf)); 368 throw new RuntimeException("Class redifinition isn't allowed to change method names!"); 369 } 370 catch (UnsupportedOperationException uoe) { 371 // Expected because redefinition can't change the name of methods 372 } 373 } 374 // We expect just a single DefineClass instance because failed redefinitions should 375 // leave no garbage around. 376 printClassStats(1, false); 377 System.gc(); 378 System.out.println("System.gc()"); 379 // At least after a System.gc() we should definitely stay with a single instance! 380 printClassStats(1, true); 381 } 382 } 383 }