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