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