1 /*
   2  * Copyright (c) 2017, 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 8164512 8191360
  27  * @requires vm.compMode != "Xcomp"
  28  * @comment Under musl, dlclose is a no-op. The static variable 'count' in libnative.c
  29  * keeps its value across a GC and the check in Test.java fails.
  30  * @requires !vm.musl
  31  * @summary verify if the native library is unloaded when the class loader is GC'ed
  32  * @build p.Test
  33  * @run main/othervm/native -Xcheck:jni NativeLibraryTest
  34  */
  35 
  36 import java.io.IOException;
  37 import java.net.MalformedURLException;
  38 import java.net.URL;
  39 import java.net.URLClassLoader;
  40 import java.nio.file.Files;
  41 import java.nio.file.Path;
  42 import java.nio.file.Paths;
  43 
  44 public class NativeLibraryTest {
  45     static final Path CLASSES = Paths.get("classes");
  46     static int unloadedCount = 0;
  47 
  48     /*
  49      * Called by JNI_OnUnload when the native library is unloaded
  50      */
  51     static void nativeLibraryUnloaded() {
  52         unloadedCount++;
  53     }
  54 
  55     public static void main(String... args) throws Exception {
  56         setup();
  57 
  58         for (int count=1; count <= 5; count++) {
  59             // create a class loader and load a native library
  60             runTest();
  61             // unloading the class loader and native library
  62             System.gc();
  63             // give Cleaner thread a chance to unload the native library
  64             Thread.sleep(100);
  65 
  66             // unloadedCount is incremented when the native library is unloaded
  67             if (count != unloadedCount) {
  68                 throw new RuntimeException("Expected unloaded=" + count +
  69                     " but got=" + unloadedCount);
  70             }
  71         }
  72     }
  73 
  74     /*
  75      * Loads p.Test class with a new class loader and its static initializer
  76      * will load a native library.
  77      *
  78      * The class loader becomes unreachable when this method returns and
  79      * the native library should be unloaded at some point after the class
  80      * loader is garbage collected.
  81      */
  82     static void runTest() throws Exception {
  83         // invoke p.Test.run() that loads the native library
  84         Runnable r = newTestRunnable();
  85         r.run();
  86 
  87         // reload the native library by the same class loader
  88         r.run();
  89 
  90         // load the native library by another class loader
  91         Runnable r1 = newTestRunnable();
  92         try {
  93             r1.run();
  94             throw new RuntimeException("should fail to load the native library" +
  95                     " by another class loader");
  96         } catch (UnsatisfiedLinkError e) {}
  97     }
  98 
  99     /*
 100      * Loads p.Test class with a new class loader and returns
 101      * a Runnable instance.
 102      */
 103     static Runnable newTestRunnable() throws Exception {
 104         TestLoader loader = new TestLoader();
 105         Class<?> c = Class.forName("p.Test", true, loader);
 106         return (Runnable) c.newInstance();
 107     }
 108 
 109     static class TestLoader extends URLClassLoader {
 110         static URL[] toURLs() {
 111             try {
 112                 return new URL[] { CLASSES.toUri().toURL() };
 113             } catch (MalformedURLException e) {
 114                 throw new Error(e);
 115             }
 116         }
 117 
 118         TestLoader() {
 119             super("testloader", toURLs(), ClassLoader.getSystemClassLoader());
 120         }
 121     }
 122 
 123     /*
 124      * move p/Test.class out from classpath to the scratch directory
 125      */
 126     static void setup() throws IOException {
 127         String dir = System.getProperty("test.classes", ".");
 128         Path file = Paths.get("p", "Test.class");
 129         Files.createDirectories(CLASSES.resolve("p"));
 130         Files.move(Paths.get(dir).resolve(file),
 131                    CLASSES.resolve("p").resolve("Test.class"));
 132     }
 133 }