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