Fri, 20 Jul 2018 03:07:49 -0400
8114823: G1 doesn't honor request to disable class unloading
Reviewed-by: tschatzl
fmatte@9358 | 1 | /* |
fmatte@9358 | 2 | * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
fmatte@9358 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
fmatte@9358 | 4 | * |
fmatte@9358 | 5 | * This code is free software; you can redistribute it and/or modify it |
fmatte@9358 | 6 | * under the terms of the GNU General Public License version 2 only, as |
fmatte@9358 | 7 | * published by the Free Software Foundation. |
fmatte@9358 | 8 | * |
fmatte@9358 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
fmatte@9358 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
fmatte@9358 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
fmatte@9358 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
fmatte@9358 | 13 | * accompanied this code). |
fmatte@9358 | 14 | * |
fmatte@9358 | 15 | * You should have received a copy of the GNU General Public License version |
fmatte@9358 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
fmatte@9358 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
fmatte@9358 | 18 | * |
fmatte@9358 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
fmatte@9358 | 20 | * or visit www.oracle.com if you need additional information or have any |
fmatte@9358 | 21 | * questions. |
fmatte@9358 | 22 | */ |
fmatte@9358 | 23 | |
fmatte@9358 | 24 | /* |
fmatte@9358 | 25 | * @test |
fmatte@9358 | 26 | * @key gc |
fmatte@9358 | 27 | * @bug 8114823 |
fmatte@9358 | 28 | * @requires vm.gc == null |
fmatte@9358 | 29 | * @requires vm.opt.ExplicitGCInvokesConcurrent != true |
fmatte@9358 | 30 | * @requires vm.opt.ClassUnloading != true |
fmatte@9358 | 31 | * @library /testlibrary /testlibrary/whitebox |
fmatte@9358 | 32 | * @build sun.hotspot.WhiteBox |
fmatte@9358 | 33 | * @run main ClassFileInstaller sun.hotspot.WhiteBox |
fmatte@9358 | 34 | * sun.hotspot.WhiteBox$WhiteBoxPermission |
fmatte@9358 | 35 | * |
fmatte@9358 | 36 | * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI |
fmatte@9358 | 37 | * -XX:-ClassUnloading -XX:+UseG1GC TestClassUnloadingDisabled |
fmatte@9358 | 38 | * |
fmatte@9358 | 39 | * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI |
fmatte@9358 | 40 | * -XX:-ClassUnloading -XX:+UseSerialGC TestClassUnloadingDisabled |
fmatte@9358 | 41 | * |
fmatte@9358 | 42 | * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI |
fmatte@9358 | 43 | * -XX:-ClassUnloading -XX:+UseParallelGC TestClassUnloadingDisabled |
fmatte@9358 | 44 | * |
fmatte@9358 | 45 | * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI |
fmatte@9358 | 46 | * -XX:-ClassUnloading -XX:+UseConcMarkSweepGC TestClassUnloadingDisabled |
fmatte@9358 | 47 | */ |
fmatte@9358 | 48 | |
fmatte@9358 | 49 | import java.io.File; |
fmatte@9358 | 50 | import java.io.IOException; |
fmatte@9358 | 51 | import java.nio.file.Files; |
fmatte@9358 | 52 | import java.nio.file.Path; |
fmatte@9358 | 53 | import java.nio.file.Paths; |
fmatte@9358 | 54 | |
fmatte@9358 | 55 | import sun.hotspot.WhiteBox; |
fmatte@9358 | 56 | |
fmatte@9358 | 57 | import com.oracle.java.testlibrary.Asserts; |
fmatte@9358 | 58 | |
fmatte@9358 | 59 | public class TestClassUnloadingDisabled { |
fmatte@9358 | 60 | public static void main(String args[]) throws Exception { |
fmatte@9358 | 61 | final WhiteBox wb = WhiteBox.getWhiteBox(); |
fmatte@9358 | 62 | // Fetch the dir where the test class and the class |
fmatte@9358 | 63 | // to be loaded resides. |
fmatte@9358 | 64 | String classDir = TestClassUnloadingDisabled.class.getProtectionDomain().getCodeSource().getLocation().getPath(); |
fmatte@9358 | 65 | String className = "ClassToLoadUnload"; |
fmatte@9358 | 66 | |
fmatte@9358 | 67 | Asserts.assertFalse(wb.isClassAlive(className), "Should not be loaded yet"); |
fmatte@9358 | 68 | |
fmatte@9358 | 69 | // The NoPDClassLoader handles loading classes in the test directory |
fmatte@9358 | 70 | // and loads them without a protection domain, which in some cases |
fmatte@9358 | 71 | // keeps the class live regardless of marking state. |
fmatte@9358 | 72 | NoPDClassLoader nopd = new NoPDClassLoader(classDir); |
fmatte@9358 | 73 | nopd.loadClass(className); |
fmatte@9358 | 74 | |
fmatte@9358 | 75 | Asserts.assertTrue(wb.isClassAlive(className), "Class should be loaded"); |
fmatte@9358 | 76 | |
fmatte@9358 | 77 | // Clear the class-loader, class and object references to make |
fmatte@9358 | 78 | // class unloading possible. |
fmatte@9358 | 79 | nopd = null; |
fmatte@9358 | 80 | |
fmatte@9358 | 81 | System.gc(); |
fmatte@9358 | 82 | Asserts.assertTrue(wb.isClassAlive(className), "Class should not have ben unloaded"); |
fmatte@9358 | 83 | } |
fmatte@9358 | 84 | } |
fmatte@9358 | 85 | |
fmatte@9358 | 86 | class NoPDClassLoader extends ClassLoader { |
fmatte@9358 | 87 | String path; |
fmatte@9358 | 88 | |
fmatte@9358 | 89 | NoPDClassLoader(String path) { |
fmatte@9358 | 90 | this.path = path; |
fmatte@9358 | 91 | } |
fmatte@9358 | 92 | |
fmatte@9358 | 93 | public Class<?> loadClass(String name) throws ClassNotFoundException { |
fmatte@9358 | 94 | byte[] cls = null; |
fmatte@9358 | 95 | File f = new File(path,name + ".class"); |
fmatte@9358 | 96 | |
fmatte@9358 | 97 | // Delegate class loading if class not present in the given |
fmatte@9358 | 98 | // directory. |
fmatte@9358 | 99 | if (!f.exists()) { |
fmatte@9358 | 100 | return super.loadClass(name); |
fmatte@9358 | 101 | } |
fmatte@9358 | 102 | |
fmatte@9358 | 103 | try { |
fmatte@9358 | 104 | Path path = Paths.get(f.getAbsolutePath()); |
fmatte@9358 | 105 | cls = Files.readAllBytes(path); |
fmatte@9358 | 106 | } catch (IOException e) { |
fmatte@9358 | 107 | throw new ClassNotFoundException(name); |
fmatte@9358 | 108 | } |
fmatte@9358 | 109 | |
fmatte@9358 | 110 | // Define class with no protection domain and resolve it. |
fmatte@9358 | 111 | return defineClass(name, cls, 0, cls.length, null); |
fmatte@9358 | 112 | } |
fmatte@9358 | 113 | } |
fmatte@9358 | 114 | |
fmatte@9358 | 115 | class ClassToLoadUnload { |
fmatte@9358 | 116 | } |