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