1 /*
   2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. 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 package gc.class_unloading;
  25 
  26 /*
  27  * @test
  28  * @key gc
  29  * @bug 8114823
  30  * @requires vm.gc == null
  31  * @requires vm.opt.ExplicitGCInvokesConcurrent != true
  32  * @requires vm.opt.ClassUnloading != true
  33  * @library /test/lib
  34  * @modules java.base/jdk.internal.misc
  35  *          java.management
  36  * @build sun.hotspot.WhiteBox
  37  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  38  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  39  *
  40  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  41  *                   -XX:-ClassUnloading -XX:+UseG1GC gc.class_unloading.TestClassUnloadingDisabled
  42  *
  43  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  44  *                   -XX:-ClassUnloading -XX:+UseSerialGC gc.class_unloading.TestClassUnloadingDisabled
  45  *
  46  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  47  *                   -XX:-ClassUnloading -XX:+UseParallelGC gc.class_unloading.TestClassUnloadingDisabled
  48  *
  49  */
  50 
  51 /*
  52  * @test
  53  * @key gc
  54  * @bug 8114823
  55  * @comment Graal does not support CMS
  56  * @requires vm.gc=="null" & !vm.graal.enabled
  57  * @requires vm.opt.ExplicitGCInvokesConcurrent != true
  58  * @requires vm.opt.ClassUnloading != true
  59  * @library /test/lib
  60  * @modules java.base/jdk.internal.misc
  61  *          java.management
  62  * @build sun.hotspot.WhiteBox
  63  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  64  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  65  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  66  *                   -XX:-ClassUnloading -XX:+UseConcMarkSweepGC gc.class_unloading.TestClassUnloadingDisabled
  67  */
  68 
  69 /*
  70  * @test TestClassUnloadingDisabledShenandoah
  71  * @key gc
  72  * @bug 8114823
  73  * @comment Graal does not support Shenandoah
  74  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  75  * @requires vm.opt.ExplicitGCInvokesConcurrent != true
  76  * @requires vm.opt.ClassUnloading != true
  77  * @library /test/lib
  78  * @modules java.base/jdk.internal.misc
  79  *          java.management
  80  * @build sun.hotspot.WhiteBox
  81  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  82  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  83  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  84  *                   -XX:-ClassUnloading -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC gc.class_unloading.TestClassUnloadingDisabled
  85  */
  86 
  87 import java.io.File;
  88 import java.io.IOException;
  89 import java.nio.file.Files;
  90 import java.nio.file.Path;
  91 import java.nio.file.Paths;
  92 
  93 import sun.hotspot.WhiteBox;
  94 
  95 import static jdk.test.lib.Asserts.*;
  96 
  97 public class TestClassUnloadingDisabled {
  98     public static void main(String args[]) throws Exception {
  99         final WhiteBox wb = WhiteBox.getWhiteBox();
 100         // Fetch the dir where the test class and the class
 101         // to be loaded resides.
 102         String classDir = TestClassUnloadingDisabled.class.getProtectionDomain().getCodeSource().getLocation().getPath();
 103         String className = "gc.class_unloading.ClassToLoadUnload"; // can not use ClassToLoadUnload.class.getName() as that would load the class
 104 
 105         assertFalse(wb.isClassAlive(className), "Should not be loaded yet");
 106 
 107         // The NoPDClassLoader handles loading classes in the test directory
 108         // and loads them without a protection domain, which in some cases
 109         // keeps the class live regardless of marking state.
 110         NoPDClassLoader nopd = new NoPDClassLoader(classDir);
 111         nopd.loadClass(className);
 112 
 113         assertTrue(wb.isClassAlive(className), "Class should be loaded");
 114 
 115         // Clear the class-loader, class and object references to make
 116         // class unloading possible.
 117         nopd = null;
 118 
 119         System.gc();
 120         assertTrue(wb.isClassAlive(className), "Class should not have ben unloaded");
 121     }
 122 }
 123 
 124 class NoPDClassLoader extends ClassLoader {
 125     String path;
 126 
 127     NoPDClassLoader(String path) {
 128         this.path = path;
 129     }
 130 
 131     public Class<?> loadClass(String name) throws ClassNotFoundException {
 132         byte[] cls = null;
 133         File f = new File(path,name + ".class");
 134 
 135         // Delegate class loading if class not present in the given
 136         // directory.
 137         if (!f.exists()) {
 138             return super.loadClass(name);
 139         }
 140 
 141         try {
 142             Path path = Paths.get(f.getAbsolutePath());
 143             cls = Files.readAllBytes(path);
 144         } catch (IOException e) {
 145             throw new ClassNotFoundException(name);
 146         }
 147 
 148         // Define class with no protection domain and resolve it.
 149         return defineClass(name, cls, 0, cls.length, null);
 150     }
 151 }
 152 
 153 class ClassToLoadUnload {
 154 }