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 import java.io.File; 68 import java.io.IOException; 69 import java.nio.file.Files; 70 import java.nio.file.Path; 71 import java.nio.file.Paths; 72 73 import sun.hotspot.WhiteBox; 74 75 import static jdk.test.lib.Asserts.*; 76 77 public class TestClassUnloadingDisabled { 78 public static void main(String args[]) throws Exception { 79 final WhiteBox wb = WhiteBox.getWhiteBox(); 80 // Fetch the dir where the test class and the class 81 // to be loaded resides. 82 String classDir = TestClassUnloadingDisabled.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 83 String className = "ClassToLoadUnload"; 84 85 assertFalse(wb.isClassAlive(className), "Should not be loaded yet"); 86 87 // The NoPDClassLoader handles loading classes in the test directory 88 // and loads them without a protection domain, which in some cases 89 // keeps the class live regardless of marking state. 90 NoPDClassLoader nopd = new NoPDClassLoader(classDir); 91 nopd.loadClass(className); 92 93 assertTrue(wb.isClassAlive(className), "Class should be loaded"); 94 95 // Clear the class-loader, class and object references to make 96 // class unloading possible. 97 nopd = null; 98 99 System.gc(); 100 assertTrue(wb.isClassAlive(className), "Class should not have ben unloaded"); 101 } 102 } 103 104 class NoPDClassLoader extends ClassLoader { 105 String path; 106 107 NoPDClassLoader(String path) { 108 this.path = path; 109 } 110 111 public Class<?> loadClass(String name) throws ClassNotFoundException { 112 byte[] cls = null; 113 File f = new File(path,name + ".class"); 114 115 // Delegate class loading if class not present in the given 116 // directory. 117 if (!f.exists()) { 118 return super.loadClass(name); 119 } 120 121 try { 122 Path path = Paths.get(f.getAbsolutePath()); 123 cls = Files.readAllBytes(path); 124 } catch (IOException e) { 125 throw new ClassNotFoundException(name); 126 } 127 128 // Define class with no protection domain and resolve it. 129 return defineClass(name, cls, 0, cls.length, null); 130 } 131 } 132 133 class ClassToLoadUnload { 134 }