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  * @comment Graal does not support Shenandoah
  81  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  82  * @requires vm.opt.ExplicitGCInvokesConcurrent != true
  83  * @requires vm.opt.ClassUnloading != true
  84  * @library /test/lib
  85  * @modules java.base/jdk.internal.misc
  86  *          java.management
  87  * @build sun.hotspot.WhiteBox
  88  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  89  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  90  *                   -XX:-ClassUnloading -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC gc.class_unloading.TestClassUnloadingDisabled
  91  */
  92 
  93 import java.io.File;
  94 import java.io.IOException;
  95 import java.nio.file.Files;
  96 import java.nio.file.Path;
  97 import java.nio.file.Paths;
  98 
  99 import sun.hotspot.WhiteBox;
 100 
 101 import static jdk.test.lib.Asserts.*;
 102 
 103 public class TestClassUnloadingDisabled {
 104     public static void main(String args[]) throws Exception {
 105         final WhiteBox wb = WhiteBox.getWhiteBox();
 106         // Fetch the dir where the test class and the class
 107         // to be loaded resides.
 108         String classDir = TestClassUnloadingDisabled.class.getProtectionDomain().getCodeSource().getLocation().getPath();
 109         String className = "gc.class_unloading.ClassToLoadUnload"; // can not use ClassToLoadUnload.class.getName() as that would load the class
 110 
 111         assertFalse(wb.isClassAlive(className), "Should not be loaded yet");
 112 
 113         // The NoPDClassLoader handles loading classes in the test directory
 114         // and loads them without a protection domain, which in some cases
 115         // keeps the class live regardless of marking state.
 116         NoPDClassLoader nopd = new NoPDClassLoader(classDir);
 117         nopd.loadClass(className);
 118 
 119         assertTrue(wb.isClassAlive(className), "Class should be loaded");
 120 
 121         // Clear the class-loader, class and object references to make
 122         // class unloading possible.
 123         nopd = null;
 124 
 125         System.gc();
 126         assertTrue(wb.isClassAlive(className), "Class should not have ben unloaded");
 127     }
 128 }
 129 
 130 class NoPDClassLoader extends ClassLoader {
 131     String path;
 132 
 133     NoPDClassLoader(String path) {
 134         this.path = path;
 135     }
 136 
 137     public Class<?> loadClass(String name) throws ClassNotFoundException {
 138         byte[] cls = null;
 139         File f = new File(path,name + ".class");
 140 
 141         // Delegate class loading if class not present in the given
 142         // directory.
 143         if (!f.exists()) {
 144             return super.loadClass(name);
 145         }
 146 
 147         try {
 148             Path path = Paths.get(f.getAbsolutePath());
 149             cls = Files.readAllBytes(path);
 150         } catch (IOException e) {
 151             throw new ClassNotFoundException(name);
 152         }
 153 
 154         // Define class with no protection domain and resolve it.
 155         return defineClass(name, cls, 0, cls.length, null);
 156     }
 157 }
 158 
 159 class ClassToLoadUnload {
 160 }