1 /* 2 * Copyright (c) 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 /* 25 * @test 26 * @bug 8187305 27 * @summary Tests logging of shared library loads and unloads. 28 * @library /runtime/testlibrary /test/lib 29 * @build sun.hotspot.WhiteBox 30 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 31 * @run main LoadLibraryTest 32 */ 33 34 import java.io.File; 35 import java.io.FileInputStream; 36 import java.io.IOException; 37 import java.nio.ByteBuffer; 38 import java.nio.channels.FileChannel; 39 40 import jdk.test.lib.process.ProcessTools; 41 import jdk.test.lib.process.OutputAnalyzer; 42 import jtreg.SkippedException; 43 44 import sun.hotspot.WhiteBox; 45 46 public class LoadLibraryTest { 47 48 public static final String CLASS_NAME = "LoadLibraryTest$LoadLibraryClass"; 49 50 private static class LoadLibrary { 51 52 static String testClasses; 53 54 public static void runTest() throws Exception { 55 // create a classloader and load a class that loads a library. 56 MyClassLoader myLoader = new MyClassLoader(); 57 Class<?> c = Class.forName(CLASS_NAME, true, myLoader); 58 } 59 60 public static void main(String[] args) throws Exception { 61 testClasses = args[0]; 62 runTest(); 63 ClassUnloadCommon.triggerUnloading(); 64 WhiteBox wb = WhiteBox.getWhiteBox(); 65 if (!wb.isClassAlive(CLASS_NAME)) { 66 System.out.println("Class LoadLibraryClass was unloaded"); 67 } 68 } 69 70 71 public static class MyClassLoader extends ClassLoader { 72 73 static ByteBuffer readClassFile(String name) { 74 File f = new File(testClasses, name); 75 try (FileInputStream fin = new FileInputStream(f); 76 FileChannel fc = fin.getChannel()) 77 { 78 return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); 79 } catch (IOException e) { 80 throw new RuntimeException("Can't open file: " + name + 81 ", exception: " + e.toString()); 82 } 83 } 84 85 protected Class<?> loadClass(String name, boolean resolve) 86 throws ClassNotFoundException { 87 Class<?> c; 88 if (!CLASS_NAME.equals(name)) { 89 c = super.loadClass(name, resolve); 90 } else { 91 // should not delegate to the system class loader 92 c = findClass(name); 93 if (resolve) { 94 resolveClass(c); 95 } 96 } 97 return c; 98 } 99 100 protected Class<?> findClass(String name) throws ClassNotFoundException { 101 if (!CLASS_NAME.equals(name)) { 102 throw new ClassNotFoundException("Unexpected class: " + name); 103 } 104 return defineClass(name, readClassFile(name + ".class"), null); 105 } 106 } // MyClassLoader 107 } 108 109 110 static class LoadLibraryClass { 111 static { 112 System.loadLibrary("LoadLibraryClass"); 113 nTest(); 114 } 115 native static void nTest(); 116 } 117 118 119 public static void main(String... args) throws Exception { 120 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 121 "-Xbootclasspath/a:.", "-XX:+UnlockDiagnosticVMOptions", 122 "-XX:+WhiteBoxAPI", "-Xmn8m", "-Xlog:library=info", 123 "-Djava.library.path=" + System.getProperty("java.library.path"), 124 "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", 125 "LoadLibraryTest$LoadLibrary", System.getProperty("test.classes")); 126 127 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 128 output.shouldHaveExitValue(0); 129 output.shouldContain("Loaded library"); 130 output.shouldContain("Found Java_LoadLibraryTest_00024LoadLibraryClass_nTest__ in library"); 131 if (output.getOutput().contains("Class LoadLibraryClass was unloaded")) { 132 output.shouldContain("Unloaded library with handle"); 133 } else { 134 throw new SkippedException( 135 "Skipping check for library unloading logging because no unloading occurred"); 136 } 137 } 138 }