Path: blob/master/test/jdk/java/lang/ClassLoader/nativeLibrary/NativeLibraryTest.java
41153 views
/*1* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8164512 819136026* @requires vm.compMode != "Xcomp"27* @comment Under musl, dlclose is a no-op. The static variable 'count' in libnative.c28* keeps its value across a GC and the check in Test.java fails.29* @requires !vm.musl30* @summary verify if the native library is unloaded when the class loader is GC'ed31* @library /test/lib/32* @build jdk.test.lib.util.ForceGC33* @build p.Test34* @run main/othervm/native -Xcheck:jni NativeLibraryTest35*/3637import java.io.IOException;38import java.net.MalformedURLException;39import java.net.URL;40import java.net.URLClassLoader;41import java.nio.file.Files;42import java.nio.file.Path;43import java.nio.file.Paths;4445import jdk.test.lib.util.ForceGC;4647public class NativeLibraryTest {48static final Path CLASSES = Paths.get("classes");49static int unloadedCount = 0;5051/*52* Called by JNI_OnUnload when the native library is unloaded53*/54static void nativeLibraryUnloaded() {55unloadedCount++;56}5758public static void main(String... args) throws Exception {59setup();6061for (int count=1; count <= 5; count++) {62System.out.println("count: " + count);63// create a class loader and load a native library64runTest();65// Unload the class loader and native library, and give the Cleaner66// thread a chance to unload the native library.67// unloadedCount is incremented when the native library is unloaded.68ForceGC gc = new ForceGC();69final int finalCount = count;70if (!gc.await(() -> finalCount == unloadedCount)) {71throw new RuntimeException("Expected unloaded=" + count +72" but got=" + unloadedCount);73}74}75}7677/*78* Loads p.Test class with a new class loader and its static initializer79* will load a native library.80*81* The class loader becomes unreachable when this method returns and82* the native library should be unloaded at some point after the class83* loader is garbage collected.84*/85static void runTest() throws Exception {86// invoke p.Test.run() that loads the native library87Runnable r = newTestRunnable();88r.run();8990// reload the native library by the same class loader91r.run();9293// load the native library by another class loader94Runnable r1 = newTestRunnable();95try {96r1.run();97throw new RuntimeException("should fail to load the native library" +98" by another class loader");99} catch (UnsatisfiedLinkError e) {}100}101102/*103* Loads p.Test class with a new class loader and returns104* a Runnable instance.105*/106static Runnable newTestRunnable() throws Exception {107TestLoader loader = new TestLoader();108Class<?> c = Class.forName("p.Test", true, loader);109return (Runnable) c.newInstance();110}111112static class TestLoader extends URLClassLoader {113static URL[] toURLs() {114try {115return new URL[] { CLASSES.toUri().toURL() };116} catch (MalformedURLException e) {117throw new Error(e);118}119}120121TestLoader() {122super("testloader", toURLs(), ClassLoader.getSystemClassLoader());123}124}125126/*127* move p/Test.class out from classpath to the scratch directory128*/129static void setup() throws IOException {130String dir = System.getProperty("test.classes", ".");131Path file = Paths.get("p", "Test.class");132Files.createDirectories(CLASSES.resolve("p"));133Files.move(Paths.get(dir).resolve(file),134CLASSES.resolve("p").resolve("Test.class"));135}136}137138139