1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/gc/class_unloading/TestClassUnloadingDisabled.java Fri Jul 20 03:07:49 2018 -0400 1.3 @@ -0,0 +1,116 @@ 1.4 +/* 1.5 + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @key gc 1.30 + * @bug 8114823 1.31 + * @requires vm.gc == null 1.32 + * @requires vm.opt.ExplicitGCInvokesConcurrent != true 1.33 + * @requires vm.opt.ClassUnloading != true 1.34 + * @library /testlibrary /testlibrary/whitebox 1.35 + * @build sun.hotspot.WhiteBox 1.36 + * @run main ClassFileInstaller sun.hotspot.WhiteBox 1.37 + * sun.hotspot.WhiteBox$WhiteBoxPermission 1.38 + * 1.39 + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 1.40 + * -XX:-ClassUnloading -XX:+UseG1GC TestClassUnloadingDisabled 1.41 + * 1.42 + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 1.43 + * -XX:-ClassUnloading -XX:+UseSerialGC TestClassUnloadingDisabled 1.44 + * 1.45 + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 1.46 + * -XX:-ClassUnloading -XX:+UseParallelGC TestClassUnloadingDisabled 1.47 + * 1.48 + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 1.49 + * -XX:-ClassUnloading -XX:+UseConcMarkSweepGC TestClassUnloadingDisabled 1.50 + */ 1.51 + 1.52 +import java.io.File; 1.53 +import java.io.IOException; 1.54 +import java.nio.file.Files; 1.55 +import java.nio.file.Path; 1.56 +import java.nio.file.Paths; 1.57 + 1.58 +import sun.hotspot.WhiteBox; 1.59 + 1.60 +import com.oracle.java.testlibrary.Asserts; 1.61 + 1.62 +public class TestClassUnloadingDisabled { 1.63 + public static void main(String args[]) throws Exception { 1.64 + final WhiteBox wb = WhiteBox.getWhiteBox(); 1.65 + // Fetch the dir where the test class and the class 1.66 + // to be loaded resides. 1.67 + String classDir = TestClassUnloadingDisabled.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 1.68 + String className = "ClassToLoadUnload"; 1.69 + 1.70 + Asserts.assertFalse(wb.isClassAlive(className), "Should not be loaded yet"); 1.71 + 1.72 + // The NoPDClassLoader handles loading classes in the test directory 1.73 + // and loads them without a protection domain, which in some cases 1.74 + // keeps the class live regardless of marking state. 1.75 + NoPDClassLoader nopd = new NoPDClassLoader(classDir); 1.76 + nopd.loadClass(className); 1.77 + 1.78 + Asserts.assertTrue(wb.isClassAlive(className), "Class should be loaded"); 1.79 + 1.80 + // Clear the class-loader, class and object references to make 1.81 + // class unloading possible. 1.82 + nopd = null; 1.83 + 1.84 + System.gc(); 1.85 + Asserts.assertTrue(wb.isClassAlive(className), "Class should not have ben unloaded"); 1.86 + } 1.87 +} 1.88 + 1.89 +class NoPDClassLoader extends ClassLoader { 1.90 + String path; 1.91 + 1.92 + NoPDClassLoader(String path) { 1.93 + this.path = path; 1.94 + } 1.95 + 1.96 + public Class<?> loadClass(String name) throws ClassNotFoundException { 1.97 + byte[] cls = null; 1.98 + File f = new File(path,name + ".class"); 1.99 + 1.100 + // Delegate class loading if class not present in the given 1.101 + // directory. 1.102 + if (!f.exists()) { 1.103 + return super.loadClass(name); 1.104 + } 1.105 + 1.106 + try { 1.107 + Path path = Paths.get(f.getAbsolutePath()); 1.108 + cls = Files.readAllBytes(path); 1.109 + } catch (IOException e) { 1.110 + throw new ClassNotFoundException(name); 1.111 + } 1.112 + 1.113 + // Define class with no protection domain and resolve it. 1.114 + return defineClass(name, cls, 0, cls.length, null); 1.115 + } 1.116 +} 1.117 + 1.118 +class ClassToLoadUnload { 1.119 +}