Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/rmi/transport/checkLeaseInfoLeak/CheckLeaseLeak.java
41155 views
1
/*
2
* Copyright (c) 1998, 2017, 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 4116437
26
* @summary Distributed Garbage Collector Memory Leak
27
*
28
* @author Laird Dornin
29
*
30
* @library ../../testlibrary
31
* @modules java.rmi/sun.rmi.registry
32
* java.rmi/sun.rmi.server
33
* java.rmi/sun.rmi.transport:open
34
* java.rmi/sun.rmi.transport
35
* java.rmi/sun.rmi.transport.tcp
36
* @build TestLibrary CheckLeaseLeak_Stub LeaseLeakClient LeaseLeak
37
* @run main/othervm/timeout=240 CheckLeaseLeak
38
*
39
*/
40
41
/**
42
* A bug in sun.rmi.transport.DGCImp.checkLeases() results in memory
43
* leak of LeaseInfo objects.
44
*
45
* In order to verify that this problem no longer exists, we create a
46
* remote object and a serveral clients in different VMs. The clients
47
* call a remote method on an exported object. This will cause the rmi
48
* runtime to create several references (all with different vmids) to
49
* the remote object. Each vmid needs a seperate LeaseInfo object in
50
* the object table target DGCImpl.leaseTable. If the leak is fixed,
51
* the leaseTable field will contain no objects. We use reflection to
52
* find the number of objects contained in this table.
53
*/
54
55
import java.rmi.*;
56
import java.rmi.server.*;
57
import sun.rmi.transport.*;
58
import sun.rmi.*;
59
import java.util.Map;
60
import java.io.*;
61
import java.lang.reflect.*;
62
import java.rmi.registry.*;
63
64
public class CheckLeaseLeak extends UnicastRemoteObject implements LeaseLeak {
65
public CheckLeaseLeak() throws RemoteException { }
66
public void ping () throws RemoteException { }
67
68
/**
69
* Id to fake the DGC_ID, so we can later get a reference to the
70
* DGCImpl in the object table.
71
*/
72
private final static int DGC_ID = 2;
73
74
private final static int ITERATIONS = 10;
75
private final static int numberPingCalls = 0;
76
private final static int CHECK_INTERVAL = 400;
77
private final static int LEASE_VALUE = 20;
78
79
public static void main (String[] args) {
80
CheckLeaseLeak leakServer = null;
81
int numLeft =0;
82
83
/*
84
* we want DGC to collect leases *quickly*
85
* decrease the lease check interval
86
*/
87
TestLibrary.setInteger("sun.rmi.dgc.checkInterval",
88
CHECK_INTERVAL);
89
TestLibrary.setInteger("java.rmi.dgc.leaseValue",
90
LEASE_VALUE);
91
92
try {
93
Registry registry =
94
TestLibrary.createRegistryOnEphemeralPort();
95
int registryPort = TestLibrary.getRegistryPort(registry);
96
97
leakServer = new CheckLeaseLeak();
98
registry.rebind("/LeaseLeak", leakServer);
99
100
/* create a bunch of clients in a *different* vm */
101
for (int i = 0 ; i < ITERATIONS ; i ++ ) {
102
System.err.println("Created client: " + i);
103
104
JavaVM jvm = new JavaVM("LeaseLeakClient",
105
" -Djava.security.policy=" +
106
TestParams.defaultPolicy +
107
" -Drmi.registry.port=" +
108
registryPort,
109
"");
110
111
try {
112
if (jvm.execute() != 0) {
113
TestLibrary.bomb("Client process failed");
114
}
115
} finally {
116
jvm.destroy();
117
}
118
}
119
numLeft = getDGCLeaseTableSize();
120
Thread.sleep(3000);
121
122
} catch(Exception e) {
123
TestLibrary.bomb("CheckLeaseLeak Error: ", e);
124
} finally {
125
if (leakServer != null) {
126
TestLibrary.unexport(leakServer);
127
leakServer = null;
128
}
129
}
130
131
/* numLeft should be 2 - if 11 there is a problem. */
132
if (numLeft > 2) {
133
TestLibrary.bomb("Too many objects in DGCImpl.leaseTable: "+
134
numLeft);
135
} else {
136
System.err.println("Check leaseInfo leak passed with " +
137
numLeft
138
+ " object(s) in the leaseTable");
139
}
140
}
141
142
/**
143
* Obtain a reference to the main DGCImpl via reflection. Extract
144
* the DGCImpl using the ObjectTable and the well known ID of the
145
* DGCImpl.
146
*/
147
private static int getDGCLeaseTableSize () {
148
int numLeaseInfosLeft = 0;
149
150
/**
151
* Will eventually be set to point at the leaseTable inside
152
* DGCImpl.
153
*/
154
Map leaseTable = null;
155
final Remote[] dgcImpl = new Remote[1];
156
Field f;
157
158
try {
159
f = (Field) java.security.AccessController.doPrivileged
160
(new java.security.PrivilegedExceptionAction() {
161
public Object run() throws Exception {
162
163
ObjID dgcID = new ObjID(DGC_ID);
164
165
/*
166
* Construct an ObjectEndpoint containing DGC's
167
* ObjID.
168
*/
169
Class oeClass =
170
Class.forName("sun.rmi.transport.ObjectEndpoint");
171
Class[] constrParams =
172
new Class[]{ ObjID.class, Transport.class };
173
Constructor oeConstructor =
174
oeClass.getDeclaredConstructor(constrParams);
175
oeConstructor.setAccessible(true);
176
Object oe =
177
oeConstructor.newInstance(
178
new Object[]{ dgcID, null });
179
180
/*
181
* Get Target that contains DGCImpl in ObjectTable
182
*/
183
Class objTableClass =
184
Class.forName("sun.rmi.transport.ObjectTable");
185
Class getTargetParams[] = new Class[] { oeClass };
186
Method objTableGetTarget =
187
objTableClass.getDeclaredMethod("getTarget",
188
getTargetParams);
189
objTableGetTarget.setAccessible(true);
190
Target dgcTarget = (Target)
191
objTableGetTarget.invoke(null, new Object[]{ oe });
192
193
/* get the DGCImpl from its Target */
194
Method targetGetImpl =
195
dgcTarget.getClass().getDeclaredMethod
196
("getImpl", null);
197
targetGetImpl.setAccessible(true);
198
dgcImpl[0] =
199
(Remote) targetGetImpl.invoke(dgcTarget, null);
200
201
/* Get the lease table from the DGCImpl. */
202
Field reflectedLeaseTable =
203
dgcImpl[0].getClass().getDeclaredField
204
("leaseTable");
205
reflectedLeaseTable.setAccessible(true);
206
207
return reflectedLeaseTable;
208
}
209
});
210
211
/**
212
* This is the leaseTable that will fill up with LeaseInfo
213
* objects if the LeaseInfo memory leak is not fixed.
214
*/
215
leaseTable = (Map) f.get(dgcImpl[0]);
216
217
numLeaseInfosLeft = leaseTable.size();
218
219
} catch(Exception e) {
220
if (e instanceof java.security.PrivilegedActionException)
221
e = ((java.security.PrivilegedActionException) e).
222
getException();
223
TestLibrary.bomb(e);
224
}
225
226
return numLeaseInfosLeft;
227
}
228
}
229
230