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