Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/rmi/transport/runtimeThreadInheritanceLeak/RuntimeThreadInheritanceLeak.java
41152 views
1
/*
2
* Copyright (c) 2001, 2012, 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
/* @test
25
* @bug 4404702 8216528
26
* @summary When the RMI runtime (lazily) spawns system threads that could
27
* outlive the application context in which they were (happened to be)
28
* created, such threads should not inherit (thread local) data specific to
29
* such an application context for various isolation reasons (see 4219095).
30
* While there is not yet a practical means for a general solution to this
31
* problem, the particular problem documented in 4404702-- the inheritance
32
* of the parent thread's context class loader, preventing that loader from
33
* being garbage collected in the future-- can be easily fixed. This test
34
* verifies that the context class loader in effect when the first remote
35
* object is exported (and thus when some long-lived RMI daemon threads are
36
* created) can be garbage collected after the remote object has been
37
* unexported. [Note that this test is somewhat at the mercy of other J2SE
38
* subsystems also not holding on to the loader in their daemon threads.]
39
* @author Peter Jones
40
*
41
* @build RuntimeThreadInheritanceLeak_Stub
42
* @run main/othervm RuntimeThreadInheritanceLeak
43
*/
44
45
import java.lang.ref.Reference;
46
import java.lang.ref.ReferenceQueue;
47
import java.lang.ref.WeakReference;
48
import java.net.URL;
49
import java.net.URLClassLoader;
50
import java.rmi.Remote;
51
import java.rmi.RemoteException;
52
import java.rmi.server.UnicastRemoteObject;
53
import java.util.Iterator;
54
import java.util.Map;
55
56
public class RuntimeThreadInheritanceLeak implements Remote {
57
58
private static final int TIMEOUT = 20000;
59
60
public static void main(String[] args) {
61
62
System.err.println("\nRegression test for bug 4404702\n");
63
64
/*
65
* HACK: Work around the fact that java.util.logging.LogManager's
66
* (singleton) construction also has this bug-- it will register a
67
* "shutdown hook", i.e. a thread, which will inherit and pin the
68
* current thread's context class loader for the lifetime of the VM--
69
* by causing the LogManager to be initialized now, instead of by
70
* RMI when our special context class loader is set.
71
*/
72
java.util.logging.LogManager.getLogManager();
73
74
/*
75
* HACK: Work around the fact that the non-native, thread-based
76
* SecureRandom seed generator (ThreadedSeedGenerator) seems to
77
* have this bug too (which had been causing this test to fail
78
* when run with jtreg on Windows XP-- see 4910382).
79
*/
80
(new java.security.SecureRandom()).nextInt();
81
82
RuntimeThreadInheritanceLeak obj = new RuntimeThreadInheritanceLeak();
83
84
try {
85
ClassLoader loader = URLClassLoader.newInstance(new URL[0]);
86
ReferenceQueue refQueue = new ReferenceQueue();
87
Reference loaderRef = new WeakReference(loader, refQueue);
88
System.err.println("created loader: " + loader);
89
90
Thread.currentThread().setContextClassLoader(loader);
91
UnicastRemoteObject.exportObject(obj);
92
Thread.currentThread().setContextClassLoader(
93
ClassLoader.getSystemClassLoader());
94
System.err.println(
95
"exported remote object with loader as context class loader");
96
97
loader = null;
98
System.err.println("nulled strong reference to loader");
99
100
UnicastRemoteObject.unexportObject(obj, true);
101
System.err.println("unexported remote object");
102
103
/*
104
* HACK: Work around the fact that the sun.misc.GC daemon thread
105
* also has this bug-- it will have inherited our loader as its
106
* context class loader-- by giving it a chance to pass away.
107
*/
108
Thread.sleep(2000);
109
while (loaderRef.get() != null) {
110
System.gc();
111
Thread.sleep(100);
112
}
113
114
System.err.println(
115
"waiting to be notified of loader being weakly reachable...");
116
Reference dequeued = refQueue.remove(TIMEOUT);
117
if (dequeued == null) {
118
System.err.println(
119
"TEST FAILED: loader not deteced weakly reachable");
120
dumpThreads();
121
throw new RuntimeException(
122
"TEST FAILED: loader not detected weakly reachable");
123
}
124
125
System.err.println(
126
"TEST PASSED: loader detected weakly reachable");
127
dumpThreads();
128
129
} catch (RuntimeException e) {
130
throw e;
131
} catch (Exception e) {
132
throw new RuntimeException("TEST FAILED: unexpected exception", e);
133
} finally {
134
try {
135
UnicastRemoteObject.unexportObject(obj, true);
136
} catch (RemoteException e) {
137
}
138
}
139
}
140
141
/**
142
* Dumps information about all live threads to System.err,
143
* including their context class loaders.
144
**/
145
private static void dumpThreads() {
146
System.err.println(
147
"current live threads and their context class loaders:");
148
Map threads = Thread.getAllStackTraces();
149
for (Iterator iter = threads.entrySet().iterator(); iter.hasNext();) {
150
Map.Entry e = (Map.Entry) iter.next();
151
Thread t = (Thread) e.getKey();
152
System.err.println(" thread: " + t);
153
System.err.println(" context class loader: " +
154
t.getContextClassLoader());
155
StackTraceElement[] trace = (StackTraceElement[]) e.getValue();
156
for (int i = 0; i < trace.length; i++) {
157
System.err.println(" " + trace[i]);
158
}
159
}
160
}
161
}
162
163