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                 while (true) {
  68                     try {
  69                         System.loadLibrary("LoadLibraryClass");
  70                         // Able to load the library with this class's class loader
  71                         // so it must have been unloaded by myLoader.
  72                         break;
  73                     } catch(java.lang.UnsatisfiedLinkError e) {
  74                         if (e.getMessage().contains("already loaded in another classloader")) {
  75                             // Library has not been unloaded yet, so wait a little and check again.
  76                             Thread.sleep(10);
  77                         } else {
  78                             throw new RuntimeException(
  79                                 "Unexpected UnsatisfiedLinkError: " + e.getMessage());
  80                         }
  81                     }
  82                 }
  83             }
  84         }
  85 
  86 
  87         public static class MyClassLoader extends ClassLoader {
  88 
  89             static ByteBuffer readClassFile(String name) {
  90                 File f = new File(testClasses, name);
  91                 try (FileInputStream fin = new FileInputStream(f);
  92                      FileChannel fc = fin.getChannel())
  93                 {
  94                     return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
  95                 } catch (IOException e) {
  96                     throw new RuntimeException("Can't open file: " + name +
  97                                                ", exception: " + e.toString());
  98                 }
  99             }
 100 
 101             protected Class<?> loadClass(String name, boolean resolve)
 102                 throws ClassNotFoundException {
 103                 Class<?> c;
 104                 if (!CLASS_NAME.equals(name)) {
 105                     c = super.loadClass(name, resolve);
 106                 } else {
 107                     // should not delegate to the system class loader
 108                     c = findClass(name);
 109                     if (resolve) {
 110                         resolveClass(c);
 111                     }
 112                 }
 113                 return c;
 114             }
 115 
 116             protected Class<?> findClass(String name) throws ClassNotFoundException {
 117                 if (!CLASS_NAME.equals(name)) {
 118                     throw new ClassNotFoundException("Unexpected class: " + name);
 119                 }
 120                 return defineClass(name, readClassFile(name + ".class"), null);
 121             }
 122         } // MyClassLoader
 123     }
 124 
 125 
 126     static class LoadLibraryClass {
 127         static {
 128             System.loadLibrary("LoadLibraryClass");
 129             nTest();
 130         }
 131         native static void nTest();
 132     }
 133 
 134 
 135     public static void main(String... args) throws Exception {
 136         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 137             "-Xbootclasspath/a:.", "-XX:+UnlockDiagnosticVMOptions",
 138             "-XX:+WhiteBoxAPI", "-Xmn8m", "-Xlog:library=info",
 139             "-Djava.library.path=" + System.getProperty("java.library.path"),
 140             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
 141             "LoadLibraryTest$LoadLibrary", System.getProperty("test.classes"));
 142 
 143         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 144         output.shouldHaveExitValue(0);
 145         output.shouldContain("Loaded library");
 146         output.shouldContain("Found Java_LoadLibraryTest_00024LoadLibraryClass_nTest__ in library");
 147         if (output.getOutput().contains("Class LoadLibraryClass was unloaded")) {
 148             output.shouldContain("Unloaded library with handle");
 149         } else {
 150             throw new SkippedException(
 151                 "Skipping check for library unloading logging because no unloading occurred");
 152         }
 153     }
 154 }