Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/ClassLoader/forNameLeak/ClassForNameLeak.java
41152 views
1
/*
2
* Copyright (c) 2016, 2018, 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 8151486
27
* @summary Call Class.forName() on the system classloader from a class loaded
28
* from a custom classloader.
29
* @library /test/lib
30
* @build jdk.test.lib.Utils
31
* jdk.test.lib.util.JarUtils
32
* @build ClassForName ClassForNameLeak
33
* @run main/othervm/policy=test.policy -Djava.security.manager ClassForNameLeak
34
*/
35
36
import java.io.IOException;
37
import java.lang.ref.PhantomReference;
38
import java.lang.ref.Reference;
39
import java.lang.ref.ReferenceQueue;
40
import java.net.MalformedURLException;
41
import java.net.URL;
42
import java.net.URLClassLoader;
43
import java.nio.file.Path;
44
import java.nio.file.Paths;
45
import java.util.List;
46
import java.util.concurrent.Callable;
47
import java.util.concurrent.ExecutorService;
48
import java.util.concurrent.Executors;
49
import java.util.concurrent.Future;
50
import java.util.stream.Collectors;
51
import java.util.stream.Stream;
52
53
import jdk.test.lib.Utils;
54
import jdk.test.lib.util.JarUtils;
55
56
/*
57
* Create .jar, load ClassForName from .jar using a URLClassLoader
58
*/
59
public class ClassForNameLeak {
60
private static final long TIMEOUT = (long)(5000.0 * Utils.TIMEOUT_FACTOR);
61
private static final int THREADS = 10;
62
private static final Path jarFilePath = Paths.get("cfn.jar");
63
private static final ReferenceQueue<ClassLoader> rq = new ReferenceQueue<>();
64
65
static class TestLoader {
66
private final PhantomReference<ClassLoader> ref;
67
TestLoader() {
68
this.ref = loadAndRun();
69
}
70
71
// Use a new classloader to load the ClassForName class, then run its
72
// Runnable.
73
PhantomReference<ClassLoader> loadAndRun() {
74
try {
75
ClassLoader classLoader =
76
new URLClassLoader("LeakedClassLoader",
77
new URL[]{jarFilePath.toUri().toURL()},
78
ClassLoader.getPlatformClassLoader());
79
80
Class<?> loadClass = Class.forName("ClassForName", true, classLoader);
81
((Runnable) loadClass.newInstance()).run();
82
83
return new PhantomReference<>(classLoader, rq);
84
} catch (MalformedURLException|ReflectiveOperationException e) {
85
throw new RuntimeException(e);
86
}
87
}
88
89
PhantomReference<ClassLoader> getRef() {
90
return ref;
91
}
92
}
93
94
public static void main(String... args) throws Exception {
95
// create the JAR file
96
setup();
97
98
// Make simultaneous calls to the test method, to stress things a bit
99
ExecutorService es = Executors.newFixedThreadPool(THREADS);
100
101
List<Callable<TestLoader>> callables =
102
Stream.generate(() -> {
103
Callable<TestLoader> cprcl = TestLoader::new;
104
return cprcl;
105
}).limit(THREADS).collect(Collectors.toList());
106
107
List<Future<TestLoader>> futures = es.invokeAll(callables);
108
109
// Give the GC a chance to enqueue the PhantomReferences
110
for (int i = 0; i < 10; i++) {
111
System.gc();
112
}
113
114
// Make sure all PhantomReferences to the leaked classloader are enqueued
115
for (int j = 0; j < futures.size(); j++) {
116
Reference rmRef = rq.remove(TIMEOUT);
117
if (rmRef == null) {
118
throw new RuntimeException("ClassLoader was never enqueued!");
119
} else {
120
System.out.println("Enqueued " + rmRef);
121
}
122
}
123
es.shutdown();
124
System.out.println("All ClassLoaders successfully enqueued");
125
}
126
127
private static final String CLASSFILENAME = "ClassForName.class";
128
private static void setup() throws IOException {
129
String testclasses = System.getProperty("test.classes", ".");
130
131
// Create a temporary .jar file containing ClassForName.class
132
Path testClassesDir = Paths.get(testclasses);
133
JarUtils.createJarFile(jarFilePath, testClassesDir, CLASSFILENAME);
134
}
135
}
136
137