Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/rmi/transport/DGCImpl.java
38831 views
1
/*
2
* Copyright (c) 1996, 2016, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package sun.rmi.transport;
26
27
import java.net.SocketPermission;
28
import java.rmi.Remote;
29
import java.rmi.RemoteException;
30
import java.rmi.dgc.DGC;
31
import java.rmi.dgc.Lease;
32
import java.rmi.dgc.VMID;
33
import java.rmi.server.LogStream;
34
import java.rmi.server.ObjID;
35
import java.rmi.server.RemoteServer;
36
import java.rmi.server.ServerNotActiveException;
37
import java.rmi.server.UID;
38
import java.security.AccessControlContext;
39
import java.security.AccessController;
40
import java.security.Permissions;
41
import java.security.PrivilegedAction;
42
import java.security.ProtectionDomain;
43
import java.security.Security;
44
import java.util.ArrayList;
45
import java.util.HashSet;
46
import java.util.HashMap;
47
import java.util.Iterator;
48
import java.util.List;
49
import java.util.Map;
50
import java.util.Set;
51
import java.util.concurrent.Future;
52
import java.util.concurrent.ScheduledExecutorService;
53
import java.util.concurrent.TimeUnit;
54
55
import sun.misc.ObjectInputFilter;
56
57
import sun.rmi.runtime.Log;
58
import sun.rmi.runtime.RuntimeUtil;
59
import sun.rmi.server.UnicastRef;
60
import sun.rmi.server.UnicastServerRef;
61
import sun.rmi.server.Util;
62
import sun.security.action.GetLongAction;
63
import sun.security.action.GetPropertyAction;
64
65
/**
66
* This class implements the guts of the server-side distributed GC
67
* algorithm
68
*
69
* @author Ann Wollrath
70
*/
71
@SuppressWarnings("deprecation")
72
final class DGCImpl implements DGC {
73
74
/* dgc system log */
75
static final Log dgcLog = Log.getLog("sun.rmi.dgc", "dgc",
76
LogStream.parseLevel(AccessController.doPrivileged(
77
new GetPropertyAction("sun.rmi.dgc.logLevel"))));
78
79
/** lease duration to grant to clients */
80
private static final long leaseValue = // default 10 minutes
81
AccessController.doPrivileged(
82
new GetLongAction("java.rmi.dgc.leaseValue", 600000));
83
84
/** lease check interval; default is half of lease grant duration */
85
private static final long leaseCheckInterval =
86
AccessController.doPrivileged(
87
new GetLongAction("sun.rmi.dgc.checkInterval", leaseValue / 2));
88
89
/** thread pool for scheduling delayed tasks */
90
private static final ScheduledExecutorService scheduler =
91
AccessController.doPrivileged(
92
new RuntimeUtil.GetInstanceAction()).getScheduler();
93
94
/** remote implementation of DGC interface for this VM */
95
private static DGCImpl dgc;
96
/** table that maps VMID to LeaseInfo */
97
private Map<VMID,LeaseInfo> leaseTable = new HashMap<>();
98
/** checks for lease expiration */
99
private Future<?> checker = null;
100
101
/**
102
* Return the remote implementation of the DGC interface for
103
* this VM.
104
*/
105
static DGCImpl getDGCImpl() {
106
return dgc;
107
}
108
109
/**
110
* Property name of the DGC serial filter to augment
111
* the built-in list of allowed types.
112
* Setting the property in the {@code lib/security/java.security} file
113
* will enable the augmented filter.
114
*/
115
private static final String DGC_FILTER_PROPNAME = "sun.rmi.transport.dgcFilter";
116
117
/** Registry max depth of remote invocations. **/
118
private static int DGC_MAX_DEPTH = 5;
119
120
/** Registry maximum array size in remote invocations. **/
121
private static int DGC_MAX_ARRAY_SIZE = 10000;
122
/**
123
* The dgcFilter created from the value of the {@code "sun.rmi.transport.dgcFilter"}
124
* property.
125
*/
126
private static final ObjectInputFilter dgcFilter =
127
AccessController.doPrivileged((PrivilegedAction<ObjectInputFilter>)DGCImpl::initDgcFilter);
128
129
/**
130
* Initialize the dgcFilter from the security properties or system property; if any
131
* @return an ObjectInputFilter, or null
132
*/
133
private static ObjectInputFilter initDgcFilter() {
134
ObjectInputFilter filter = null;
135
String props = System.getProperty(DGC_FILTER_PROPNAME);
136
if (props == null) {
137
props = Security.getProperty(DGC_FILTER_PROPNAME);
138
}
139
if (props != null) {
140
filter = ObjectInputFilter.Config.createFilter(props);
141
if (dgcLog.isLoggable(Log.BRIEF)) {
142
dgcLog.log(Log.BRIEF, "dgcFilter = " + filter);
143
}
144
}
145
return filter;
146
}
147
148
/**
149
* Construct a new server-side remote object collector at
150
* a particular port. Disallow construction from outside.
151
*/
152
private DGCImpl() {}
153
154
/**
155
* The dirty call adds the VMID "vmid" to the set of clients
156
* that hold references to the object associated with the ObjID
157
* id. The long "sequenceNum" is used to detect late dirty calls. If
158
* the VMID "vmid" is null, a VMID will be generated on the
159
* server (for use by the client in subsequent calls) and
160
* returned.
161
*
162
* The client must call the "dirty" method to renew the lease
163
* before the "lease" time expires or all references to remote
164
* objects in this VM that the client holds are considered
165
* "unreferenced".
166
*/
167
public Lease dirty(ObjID[] ids, long sequenceNum, Lease lease) {
168
VMID vmid = lease.getVMID();
169
/*
170
* The server specifies the lease value; the client has
171
* no say in the matter.
172
*/
173
long duration = leaseValue;
174
175
if (dgcLog.isLoggable(Log.VERBOSE)) {
176
dgcLog.log(Log.VERBOSE, "vmid = " + vmid);
177
}
178
179
// create a VMID if one wasn't supplied
180
if (vmid == null) {
181
vmid = new VMID();
182
183
if (dgcLog.isLoggable(Log.BRIEF)) {
184
String clientHost;
185
try {
186
clientHost = RemoteServer.getClientHost();
187
} catch (ServerNotActiveException e) {
188
clientHost = "<unknown host>";
189
}
190
dgcLog.log(Log.BRIEF, " assigning vmid " + vmid +
191
" to client " + clientHost);
192
}
193
}
194
195
lease = new Lease(vmid, duration);
196
// record lease information
197
synchronized (leaseTable) {
198
LeaseInfo info = leaseTable.get(vmid);
199
if (info == null) {
200
leaseTable.put(vmid, new LeaseInfo(vmid, duration));
201
if (checker == null) {
202
checker = scheduler.scheduleWithFixedDelay(
203
new Runnable() {
204
public void run() {
205
checkLeases();
206
}
207
},
208
leaseCheckInterval,
209
leaseCheckInterval, TimeUnit.MILLISECONDS);
210
}
211
} else {
212
info.renew(duration);
213
}
214
}
215
216
for (ObjID id : ids) {
217
if (dgcLog.isLoggable(Log.VERBOSE)) {
218
dgcLog.log(Log.VERBOSE, "id = " + id +
219
", vmid = " + vmid + ", duration = " + duration);
220
}
221
222
ObjectTable.referenced(id, sequenceNum, vmid);
223
}
224
225
// return the VMID used
226
return lease;
227
}
228
229
/**
230
* The clean call removes the VMID from the set of clients
231
* that hold references to the object associated with the LiveRef
232
* ref. The sequence number is used to detect late clean calls. If the
233
* argument "strong" is true, then the clean call is a result of a
234
* failed "dirty" call, thus the sequence number for the VMID needs
235
* to be remembered until the client goes away.
236
*/
237
public void clean(ObjID[] ids, long sequenceNum, VMID vmid, boolean strong)
238
{
239
for (ObjID id : ids) {
240
if (dgcLog.isLoggable(Log.VERBOSE)) {
241
dgcLog.log(Log.VERBOSE, "id = " + id +
242
", vmid = " + vmid + ", strong = " + strong);
243
}
244
245
ObjectTable.unreferenced(id, sequenceNum, vmid, strong);
246
}
247
}
248
249
/**
250
* Register interest in receiving a callback when this VMID
251
* becomes inaccessible.
252
*/
253
void registerTarget(VMID vmid, Target target) {
254
synchronized (leaseTable) {
255
LeaseInfo info = leaseTable.get(vmid);
256
if (info == null) {
257
target.vmidDead(vmid);
258
} else {
259
info.notifySet.add(target);
260
}
261
}
262
}
263
264
/**
265
* Remove notification request.
266
*/
267
void unregisterTarget(VMID vmid, Target target) {
268
synchronized (leaseTable) {
269
LeaseInfo info = leaseTable.get(vmid);
270
if (info != null) {
271
info.notifySet.remove(target);
272
}
273
}
274
}
275
276
/**
277
* Check if leases have expired. If a lease has expired, remove
278
* it from the table and notify all interested parties that the
279
* VMID is essentially "dead".
280
*
281
* @return if true, there are leases outstanding; otherwise leases
282
* no longer need to be checked
283
*/
284
private void checkLeases() {
285
long time = System.currentTimeMillis();
286
287
/* List of vmids that need to be removed from the leaseTable */
288
List<LeaseInfo> toUnregister = new ArrayList<>();
289
290
/* Build a list of leaseInfo objects that need to have
291
* targets removed from their notifySet. Remove expired
292
* leases from leaseTable.
293
*/
294
synchronized (leaseTable) {
295
Iterator<LeaseInfo> iter = leaseTable.values().iterator();
296
while (iter.hasNext()) {
297
LeaseInfo info = iter.next();
298
if (info.expired(time)) {
299
toUnregister.add(info);
300
iter.remove();
301
}
302
}
303
304
if (leaseTable.isEmpty()) {
305
checker.cancel(false);
306
checker = null;
307
}
308
}
309
310
/* Notify and unegister targets without holding the lock on
311
* the leaseTable so we avoid deadlock.
312
*/
313
for (LeaseInfo info : toUnregister) {
314
for (Target target : info.notifySet) {
315
target.vmidDead(info.vmid);
316
}
317
}
318
}
319
320
static {
321
/*
322
* "Export" the singleton DGCImpl in a context isolated from
323
* the arbitrary current thread context.
324
*/
325
AccessController.doPrivileged(new PrivilegedAction<Void>() {
326
public Void run() {
327
ClassLoader savedCcl =
328
Thread.currentThread().getContextClassLoader();
329
try {
330
Thread.currentThread().setContextClassLoader(
331
ClassLoader.getSystemClassLoader());
332
333
/*
334
* Put remote collector object in table by hand to prevent
335
* listen on port. (UnicastServerRef.exportObject would
336
* cause transport to listen.)
337
*/
338
try {
339
dgc = new DGCImpl();
340
ObjID dgcID = new ObjID(ObjID.DGC_ID);
341
LiveRef ref = new LiveRef(dgcID, 0);
342
UnicastServerRef disp = new UnicastServerRef(ref,
343
DGCImpl::checkInput);
344
Remote stub =
345
Util.createProxy(DGCImpl.class,
346
new UnicastRef(ref), true);
347
disp.setSkeleton(dgc);
348
349
Permissions perms = new Permissions();
350
perms.add(new SocketPermission("*", "accept,resolve"));
351
ProtectionDomain[] pd = { new ProtectionDomain(null, perms) };
352
AccessControlContext acceptAcc = new AccessControlContext(pd);
353
354
Target target = AccessController.doPrivileged(
355
new PrivilegedAction<Target>() {
356
public Target run() {
357
return new Target(dgc, disp, stub, dgcID, true);
358
}
359
}, acceptAcc);
360
361
ObjectTable.putTarget(target);
362
} catch (RemoteException e) {
363
throw new Error(
364
"exception initializing server-side DGC", e);
365
}
366
} finally {
367
Thread.currentThread().setContextClassLoader(savedCcl);
368
}
369
return null;
370
}
371
});
372
}
373
374
/**
375
* ObjectInputFilter to filter DGC input objects.
376
* The list of acceptable classes is very short and explicit.
377
* The depth and array sizes are limited.
378
*
379
* @param filterInfo access to class, arrayLength, etc.
380
* @return {@link ObjectInputFilter.Status#ALLOWED} if allowed,
381
* {@link ObjectInputFilter.Status#REJECTED} if rejected,
382
* otherwise {@link ObjectInputFilter.Status#UNDECIDED}
383
*/
384
private static ObjectInputFilter.Status checkInput(ObjectInputFilter.FilterInfo filterInfo) {
385
if (dgcFilter != null) {
386
ObjectInputFilter.Status status = dgcFilter.checkInput(filterInfo);
387
if (status != ObjectInputFilter.Status.UNDECIDED) {
388
// The DGC filter can override the built-in white-list
389
return status;
390
}
391
}
392
393
if (filterInfo.depth() > DGC_MAX_DEPTH) {
394
return ObjectInputFilter.Status.REJECTED;
395
}
396
Class<?> clazz = filterInfo.serialClass();
397
if (clazz != null) {
398
while (clazz.isArray()) {
399
if (filterInfo.arrayLength() >= 0 && filterInfo.arrayLength() > DGC_MAX_ARRAY_SIZE) {
400
return ObjectInputFilter.Status.REJECTED;
401
}
402
// Arrays are allowed depending on the component type
403
clazz = clazz.getComponentType();
404
}
405
if (clazz.isPrimitive()) {
406
// Arrays of primitives are allowed
407
return ObjectInputFilter.Status.ALLOWED;
408
}
409
return (clazz == ObjID.class ||
410
clazz == UID.class ||
411
clazz == VMID.class ||
412
clazz == Lease.class)
413
? ObjectInputFilter.Status.ALLOWED
414
: ObjectInputFilter.Status.REJECTED;
415
}
416
// Not a class, not size limited
417
return ObjectInputFilter.Status.UNDECIDED;
418
}
419
420
421
private static class LeaseInfo {
422
VMID vmid;
423
long expiration;
424
Set<Target> notifySet = new HashSet<>();
425
426
LeaseInfo(VMID vmid, long lease) {
427
this.vmid = vmid;
428
expiration = System.currentTimeMillis() + lease;
429
}
430
431
synchronized void renew(long lease) {
432
long newExpiration = System.currentTimeMillis() + lease;
433
if (newExpiration > expiration)
434
expiration = newExpiration;
435
}
436
437
boolean expired(long time) {
438
if (expiration < time) {
439
if (dgcLog.isLoggable(Log.BRIEF)) {
440
dgcLog.log(Log.BRIEF, vmid.toString());
441
}
442
return true;
443
} else {
444
return false;
445
}
446
}
447
}
448
}
449
450