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