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