1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 /**
  25  * @test TestClassLoaderLeak
  26  * @summary Test OOME in due to classloader leak
  27  * @key gc
  28  * @requires vm.gc.Shenandoah
  29  * @library /test/lib
  30  * @run main TestClassLoaderLeak
  31  */
  32 
  33 import java.util.*;
  34 import java.io.*;
  35 import java.nio.*;
  36 import java.nio.file.*;
  37 
  38 import jdk.test.lib.process.OutputAnalyzer;
  39 import jdk.test.lib.process.ProcessTools;
  40 
  41 public class TestClassLoaderLeak {
  42 
  43     static final int SIZE = 1 * 1024 * 1024;
  44     static final int COUNT = 128;
  45 
  46     static volatile Object sink;
  47 
  48     static class Dummy {
  49         static final int[] PAYLOAD = new int[SIZE];
  50     }
  51 
  52     static class MyClassLoader extends ClassLoader {
  53         final String path;
  54 
  55         MyClassLoader(String path) {
  56             this.path = path;
  57         }
  58 
  59         public Class<?> loadClass(String name) throws ClassNotFoundException {
  60             try {
  61                 File f = new File(path, name + ".class");
  62                 if (!f.exists()) {
  63                     return super.loadClass(name);
  64                 }
  65 
  66                 Path path = Paths.get(f.getAbsolutePath());
  67                 byte[] cls = Files.readAllBytes(path);
  68                 return defineClass(name, cls, 0, cls.length, null);
  69             } catch (IOException e) {
  70                 throw new ClassNotFoundException(name);
  71             }
  72         }
  73     }
  74 
  75     static void load(String path) throws Exception {
  76         ClassLoader cl = new MyClassLoader(path);
  77         Class<Dummy> c = (Class<Dummy>) Class.forName("TestClassLoaderLeak$Dummy", true, cl);
  78         if (c.getClassLoader() != cl) {
  79             throw new IllegalStateException("Should have loaded by target loader");
  80         }
  81         sink = c;
  82     }
  83 
  84     public static void passWith(String... args) throws Exception {
  85         testWith(true, args);
  86     }
  87 
  88     public static void failWith(String... args) throws Exception {
  89         testWith(false, args);
  90     }
  91 
  92     public static void testWith(boolean shouldPass, String... args) throws Exception {
  93         List<String> pbArgs = new ArrayList<>();
  94         pbArgs.add("-Xmx128m");
  95         pbArgs.add("-XX:+UnlockExperimentalVMOptions");
  96         pbArgs.add("-XX:+UnlockDiagnosticVMOptions");
  97         pbArgs.add("-XX:+UseShenandoahGC");
  98         pbArgs.addAll(Arrays.asList(args));
  99         pbArgs.add(TestClassLoaderLeak.class.getName());
 100         pbArgs.add("test");
 101 
 102         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(pbArgs.toArray(new String[0]));
 103 
 104         OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());
 105 
 106         if (shouldPass) {
 107             analyzer.shouldHaveExitValue(0);
 108             analyzer.shouldNotContain("java.lang.OutOfMemoryError");
 109             analyzer.shouldContain("All good");
 110         } else {
 111             analyzer.shouldHaveExitValue(1);
 112             analyzer.shouldContain("java.lang.OutOfMemoryError");
 113             analyzer.shouldNotContain("All good");
 114         }
 115     }
 116 
 117     public static void main(String[] args) throws Exception {
 118         if (args.length > 0) {
 119             String classDir = TestClassLoaderLeak.class.getProtectionDomain().getCodeSource().getLocation().getPath();
 120             for (int c = 0; c < COUNT; c++) {
 121                 load(classDir);
 122             }
 123             System.out.println("All good");
 124             return;
 125         }
 126 
 127         String[] heuristics = new String[] {
 128                 "adaptive",
 129                 "compact",
 130                 "static",
 131                 "traversal",
 132                 "aggressive",
 133                 "passive",
 134         };
 135 
 136         for (String h : heuristics) {
 137             // Forceful enabling should work
 138             passWith("-XX:ShenandoahGCHeuristics=" + h, "-XX:+ClassUnloading");
 139             passWith("-XX:ShenandoahGCHeuristics=" + h, "-XX:+ClassUnloadingWithConcurrentMark");
 140 
 141             // Even when concurrent unloading is disabled, Full GC has to recover
 142             passWith("-XX:ShenandoahGCHeuristics=" + h, "-XX:+ClassUnloading", "-XX:-ClassUnloadingWithConcurrentMark");
 143             passWith("-XX:ShenandoahGCHeuristics=" + h, "-XX:+ClassUnloading", "-XX:-ClassUnloadingWithConcurrentMark", "-XX:ShenandoahUnloadClassesFrequency=0");
 144             passWith("-XX:ShenandoahGCHeuristics=" + h, "-XX:+ClassUnloading", "-XX:+ClassUnloadingWithConcurrentMark", "-XX:ShenandoahUnloadClassesFrequency=0");
 145 
 146             // Should OOME when unloading forcefully disabled, even if local flags try to enable it back
 147             failWith("-XX:ShenandoahGCHeuristics=" + h, "-XX:-ClassUnloading");
 148             failWith("-XX:ShenandoahGCHeuristics=" + h, "-XX:-ClassUnloading", "-XX:+ClassUnloadingWithConcurrentMark");
 149             failWith("-XX:ShenandoahGCHeuristics=" + h, "-XX:-ClassUnloading", "-XX:+ClassUnloadingWithConcurrentMark", "-XX:ShenandoahUnloadClassesFrequency=1");
 150             failWith("-XX:ShenandoahGCHeuristics=" + h, "-XX:-ClassUnloading", "-XX:-ClassUnloadingWithConcurrentMark", "-XX:ShenandoahUnloadClassesFrequency=1");
 151         }
 152     }
 153 }