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/invoke/AccessControlTest.java
47209 views
1
/*
2
* Copyright (c) 2012, 2013, 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
* @summary test access checking by java.lang.invoke.MethodHandles.Lookup
26
* @compile AccessControlTest.java AccessControlTest_subpkg/Acquaintance_remote.java
27
* @run testng/othervm test.java.lang.invoke.AccessControlTest
28
*/
29
30
package test.java.lang.invoke;
31
32
import java.lang.invoke.*;
33
import java.lang.reflect.*;
34
import java.util.*;
35
import org.testng.*;
36
import org.testng.annotations.*;
37
38
import static java.lang.invoke.MethodHandles.*;
39
import static java.lang.invoke.MethodHandles.Lookup.*;
40
import static java.lang.invoke.MethodType.*;
41
import static org.testng.Assert.*;
42
43
import test.java.lang.invoke.AccessControlTest_subpkg.Acquaintance_remote;
44
45
46
/**
47
* Test many combinations of Lookup access and cross-class lookupStatic.
48
* @author jrose
49
*/
50
public class AccessControlTest {
51
static final Class<?> THIS_CLASS = AccessControlTest.class;
52
// How much output?
53
static int verbosity = 0;
54
static {
55
String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbosity");
56
if (vstr == null)
57
vstr = System.getProperty(THIS_CLASS.getName()+".verbosity");
58
if (vstr != null) verbosity = Integer.parseInt(vstr);
59
}
60
61
private class LookupCase implements Comparable<LookupCase> {
62
final Lookup lookup;
63
final Class<?> lookupClass;
64
final int lookupModes;
65
public LookupCase(Lookup lookup) {
66
this.lookup = lookup;
67
this.lookupClass = lookup.lookupClass();
68
this.lookupModes = lookup.lookupModes();
69
assert(lookupString().equals(lookup.toString()));
70
numberOf(lookupClass().getClassLoader()); // assign CL#
71
}
72
public LookupCase(Class<?> lookupClass, int lookupModes) {
73
this.lookup = null;
74
this.lookupClass = lookupClass;
75
this.lookupModes = lookupModes;
76
numberOf(lookupClass().getClassLoader()); // assign CL#
77
}
78
79
public final Class<?> lookupClass() { return lookupClass; }
80
public final int lookupModes() { return lookupModes; }
81
82
public Lookup lookup() { lookup.getClass(); return lookup; }
83
84
@Override
85
public int compareTo(LookupCase that) {
86
Class<?> c1 = this.lookupClass();
87
Class<?> c2 = that.lookupClass();
88
if (c1 != c2) {
89
int cmp = c1.getName().compareTo(c2.getName());
90
if (cmp != 0) return cmp;
91
cmp = numberOf(c1.getClassLoader()) - numberOf(c2.getClassLoader());
92
assert(cmp != 0);
93
return cmp;
94
}
95
return -(this.lookupModes() - that.lookupModes());
96
}
97
98
@Override
99
public boolean equals(Object that) {
100
return (that instanceof LookupCase && equals((LookupCase)that));
101
}
102
public boolean equals(LookupCase that) {
103
return (this.lookupClass() == that.lookupClass() &&
104
this.lookupModes() == that.lookupModes());
105
}
106
107
@Override
108
public int hashCode() {
109
return lookupClass().hashCode() + (lookupModes() * 31);
110
}
111
112
/** Simulate all assertions in the spec. for Lookup.toString. */
113
private String lookupString() {
114
String name = lookupClass.getName();
115
String suffix = "";
116
if (lookupModes == 0)
117
suffix = "/noaccess";
118
else if (lookupModes == PUBLIC)
119
suffix = "/public";
120
else if (lookupModes == (PUBLIC|PACKAGE))
121
suffix = "/package";
122
else if (lookupModes == (PUBLIC|PACKAGE|PRIVATE))
123
suffix = "/private";
124
else if (lookupModes == (PUBLIC|PACKAGE|PRIVATE|PROTECTED))
125
suffix = "";
126
else
127
suffix = "/#"+Integer.toHexString(lookupModes);
128
return name+suffix;
129
}
130
131
/** Simulate all assertions from the spec. for Lookup.in:
132
* <hr>
133
* Creates a lookup on the specified new lookup class.
134
* [A1] The resulting object will report the specified
135
* class as its own {@link #lookupClass lookupClass}.
136
* <p>
137
* [A2] However, the resulting {@code Lookup} object is guaranteed
138
* to have no more access capabilities than the original.
139
* In particular, access capabilities can be lost as follows:<ul>
140
* <li>[A3] If the new lookup class differs from the old one,
141
* protected members will not be accessible by virtue of inheritance.
142
* (Protected members may continue to be accessible because of package sharing.)
143
* <li>[A4] If the new lookup class is in a different package
144
* than the old one, protected and default (package) members will not be accessible.
145
* <li>[A5] If the new lookup class is not within the same package member
146
* as the old one, private members will not be accessible.
147
* <li>[A6] If the new lookup class is not accessible to the old lookup class,
148
* using the original access modes,
149
* then no members, not even public members, will be accessible.
150
* [A7] (In all other cases, public members will continue to be accessible.)
151
* </ul>
152
* Other than the above cases, the new lookup will have the same
153
* access capabilities as the original. [A8]
154
* <hr>
155
*/
156
public LookupCase in(Class<?> c2) {
157
Class<?> c1 = lookupClass();
158
int m1 = lookupModes();
159
int changed = 0;
160
boolean samePackage = (c1.getClassLoader() == c2.getClassLoader() &&
161
packagePrefix(c1).equals(packagePrefix(c2)));
162
boolean sameTopLevel = (topLevelClass(c1) == topLevelClass(c2));
163
boolean sameClass = (c1 == c2);
164
assert(samePackage || !sameTopLevel);
165
assert(sameTopLevel || !sameClass);
166
boolean accessible = sameClass; // [A6]
167
if ((m1 & PACKAGE) != 0) accessible |= samePackage;
168
if ((m1 & PUBLIC ) != 0) accessible |= (c2.getModifiers() & PUBLIC) != 0;
169
if (!accessible) {
170
// Different package and no access to c2; lose all access.
171
changed |= (PUBLIC|PACKAGE|PRIVATE|PROTECTED); // [A6]
172
}
173
if (!samePackage) {
174
// Different package; lose PACKAGE and lower access.
175
changed |= (PACKAGE|PRIVATE|PROTECTED); // [A4]
176
}
177
if (!sameTopLevel) {
178
// Different top-level class. Lose PRIVATE and lower access.
179
changed |= (PRIVATE|PROTECTED); // [A5]
180
}
181
if (!sameClass) {
182
changed |= (PROTECTED); // [A3]
183
} else {
184
assert(changed == 0); // [A8] (no deprivation if same class)
185
}
186
if (accessible) assert((changed & PUBLIC) == 0); // [A7]
187
int m2 = m1 & ~changed;
188
LookupCase l2 = new LookupCase(c2, m2);
189
assert(l2.lookupClass() == c2); // [A1]
190
assert((m1 | m2) == m1); // [A2] (no elevation of access)
191
return l2;
192
}
193
194
@Override
195
public String toString() {
196
String s = lookupClass().getSimpleName();
197
String lstr = lookupString();
198
int sl = lstr.indexOf('/');
199
if (sl >= 0) s += lstr.substring(sl);
200
ClassLoader cld = lookupClass().getClassLoader();
201
if (cld != THIS_LOADER) s += "/loader#"+numberOf(cld);
202
return s;
203
}
204
205
/** Predict the success or failure of accessing this method. */
206
public boolean willAccess(Method m) {
207
Class<?> c1 = lookupClass();
208
Class<?> c2 = m.getDeclaringClass();
209
LookupCase lc = this.in(c2);
210
int m1 = lc.lookupModes();
211
int m2 = fixMods(m.getModifiers());
212
// privacy is strictly enforced on lookups
213
if (c1 != c2) m1 &= ~PRIVATE;
214
// protected access is sometimes allowed
215
if ((m2 & PROTECTED) != 0) {
216
int prev = m2;
217
m2 |= PACKAGE; // it acts like a package method also
218
if ((lookupModes() & PROTECTED) != 0 &&
219
c2.isAssignableFrom(c1))
220
m2 |= PUBLIC; // from a subclass, it acts like a public method also
221
}
222
if (verbosity >= 2)
223
System.out.println(this+" willAccess "+lc+" m1="+m1+" m2="+m2+" => "+((m2 & m1) != 0));
224
return (m2 & m1) != 0;
225
}
226
}
227
228
private static Class<?> topLevelClass(Class<?> cls) {
229
Class<?> c = cls;
230
for (Class<?> ec; (ec = c.getEnclosingClass()) != null; )
231
c = ec;
232
assert(c.getEnclosingClass() == null);
233
assert(c == cls || cls.getEnclosingClass() != null);
234
return c;
235
}
236
237
private static String packagePrefix(Class<?> c) {
238
while (c.isArray()) c = c.getComponentType();
239
String s = c.getName();
240
assert(s.indexOf('/') < 0);
241
return s.substring(0, s.lastIndexOf('.')+1);
242
}
243
244
245
private final TreeSet<LookupCase> CASES = new TreeSet<>();
246
private final TreeMap<LookupCase,TreeSet<LookupCase>> CASE_EDGES = new TreeMap<>();
247
private final ArrayList<ClassLoader> LOADERS = new ArrayList<>();
248
private final ClassLoader THIS_LOADER = this.getClass().getClassLoader();
249
{ if (THIS_LOADER != null) LOADERS.add(THIS_LOADER); } // #1
250
251
private LookupCase lookupCase(String name) {
252
for (LookupCase lc : CASES) {
253
if (lc.toString().equals(name))
254
return lc;
255
}
256
throw new AssertionError(name);
257
}
258
259
private int numberOf(ClassLoader cl) {
260
if (cl == null) return 0;
261
int i = LOADERS.indexOf(cl);
262
if (i < 0) {
263
i = LOADERS.size();
264
LOADERS.add(cl);
265
}
266
return i+1;
267
}
268
269
private void addLookupEdge(LookupCase l1, Class<?> c2, LookupCase l2) {
270
TreeSet<LookupCase> edges = CASE_EDGES.get(l2);
271
if (edges == null) CASE_EDGES.put(l2, edges = new TreeSet<>());
272
if (edges.add(l1)) {
273
Class<?> c1 = l1.lookupClass();
274
assert(l2.lookupClass() == c2); // [A1]
275
int m1 = l1.lookupModes();
276
int m2 = l2.lookupModes();
277
assert((m1 | m2) == m1); // [A2] (no elevation of access)
278
LookupCase expect = l1.in(c2);
279
if (!expect.equals(l2))
280
System.out.println("*** expect "+l1+" => "+expect+" but got "+l2);
281
assertEquals(expect, l2);
282
}
283
}
284
285
private void makeCases(Lookup[] originalLookups) {
286
// make initial set of lookup test cases
287
CASES.clear(); LOADERS.clear(); CASE_EDGES.clear();
288
ArrayList<Class<?>> classes = new ArrayList<>();
289
for (Lookup l : originalLookups) {
290
CASES.add(new LookupCase(l));
291
classes.remove(l.lookupClass()); // no dups please
292
classes.add(l.lookupClass());
293
}
294
System.out.println("loaders = "+LOADERS);
295
int rounds = 0;
296
for (int lastCount = -1; lastCount != CASES.size(); ) {
297
lastCount = CASES.size(); // if CASES grow in the loop we go round again
298
for (LookupCase lc1 : CASES.toArray(new LookupCase[0])) {
299
for (Class<?> c2 : classes) {
300
LookupCase lc2 = new LookupCase(lc1.lookup().in(c2));
301
addLookupEdge(lc1, c2, lc2);
302
CASES.add(lc2);
303
}
304
}
305
rounds++;
306
}
307
System.out.println("filled in "+CASES.size()+" cases from "+originalLookups.length+" original cases in "+rounds+" rounds");
308
if (false) {
309
System.out.println("CASES: {");
310
for (LookupCase lc : CASES) {
311
System.out.println(lc);
312
Set<LookupCase> edges = CASE_EDGES.get(lc);
313
if (edges != null)
314
for (LookupCase prev : edges) {
315
System.out.println("\t"+prev);
316
}
317
}
318
System.out.println("}");
319
}
320
}
321
322
@Test public void test() {
323
makeCases(lookups());
324
if (verbosity > 0) {
325
verbosity += 9;
326
Method pro_in_self = targetMethod(THIS_CLASS, PROTECTED, methodType(void.class));
327
testOneAccess(lookupCase("AccessControlTest/public"), pro_in_self, "find");
328
testOneAccess(lookupCase("Remote_subclass/public"), pro_in_self, "find");
329
testOneAccess(lookupCase("Remote_subclass"), pro_in_self, "find");
330
verbosity -= 9;
331
}
332
Set<Class<?>> targetClassesDone = new HashSet<>();
333
for (LookupCase targetCase : CASES) {
334
Class<?> targetClass = targetCase.lookupClass();
335
if (!targetClassesDone.add(targetClass)) continue; // already saw this one
336
String targetPlace = placeName(targetClass);
337
if (targetPlace == null) continue; // Object, String, not a target
338
for (int targetAccess : ACCESS_CASES) {
339
MethodType methodType = methodType(void.class);
340
Method method = targetMethod(targetClass, targetAccess, methodType);
341
// Try to access target method from various contexts.
342
for (LookupCase sourceCase : CASES) {
343
testOneAccess(sourceCase, method, "find");
344
testOneAccess(sourceCase, method, "unreflect");
345
}
346
}
347
}
348
System.out.println("tested "+testCount+" access scenarios; "+testCountFails+" accesses were denied");
349
}
350
351
private int testCount, testCountFails;
352
353
private void testOneAccess(LookupCase sourceCase, Method method, String kind) {
354
Class<?> targetClass = method.getDeclaringClass();
355
String methodName = method.getName();
356
MethodType methodType = methodType(method.getReturnType(), method.getParameterTypes());
357
boolean willAccess = sourceCase.willAccess(method);
358
boolean didAccess = false;
359
ReflectiveOperationException accessError = null;
360
try {
361
switch (kind) {
362
case "find":
363
if ((method.getModifiers() & Modifier.STATIC) != 0)
364
sourceCase.lookup().findStatic(targetClass, methodName, methodType);
365
else
366
sourceCase.lookup().findVirtual(targetClass, methodName, methodType);
367
break;
368
case "unreflect":
369
sourceCase.lookup().unreflect(method);
370
break;
371
default:
372
throw new AssertionError(kind);
373
}
374
didAccess = true;
375
} catch (ReflectiveOperationException ex) {
376
accessError = ex;
377
}
378
if (willAccess != didAccess) {
379
System.out.println(sourceCase+" => "+targetClass.getSimpleName()+"."+methodName+methodType);
380
System.out.println("fail on "+method+" ex="+accessError);
381
assertEquals(willAccess, didAccess);
382
}
383
testCount++;
384
if (!didAccess) testCountFails++;
385
}
386
387
static Method targetMethod(Class<?> targetClass, int targetAccess, MethodType methodType) {
388
String methodName = accessName(targetAccess)+placeName(targetClass);
389
if (verbosity >= 2)
390
System.out.println(targetClass.getSimpleName()+"."+methodName+methodType);
391
try {
392
Method method = targetClass.getDeclaredMethod(methodName, methodType.parameterArray());
393
assertEquals(method.getReturnType(), methodType.returnType());
394
int haveMods = method.getModifiers();
395
assert(Modifier.isStatic(haveMods));
396
assert(targetAccess == fixMods(haveMods));
397
return method;
398
} catch (NoSuchMethodException ex) {
399
throw new AssertionError(methodName, ex);
400
}
401
}
402
403
static String placeName(Class<?> cls) {
404
// return "self", "sibling", "nestmate", etc.
405
if (cls == AccessControlTest.class) return "self";
406
String cln = cls.getSimpleName();
407
int under = cln.lastIndexOf('_');
408
if (under < 0) return null;
409
return cln.substring(under+1);
410
}
411
static String accessName(int acc) {
412
switch (acc) {
413
case PUBLIC: return "pub_in_";
414
case PROTECTED: return "pro_in_";
415
case PACKAGE: return "pkg_in_";
416
case PRIVATE: return "pri_in_";
417
}
418
assert(false);
419
return "?";
420
}
421
private static final int[] ACCESS_CASES = {
422
PUBLIC, PACKAGE, PRIVATE, PROTECTED
423
};
424
/** Return one of the ACCESS_CASES. */
425
static int fixMods(int mods) {
426
mods &= (PUBLIC|PRIVATE|PROTECTED);
427
switch (mods) {
428
case PUBLIC: case PRIVATE: case PROTECTED: return mods;
429
case 0: return PACKAGE;
430
}
431
throw new AssertionError(mods);
432
}
433
434
static Lookup[] lookups() {
435
ArrayList<Lookup> tem = new ArrayList<>();
436
Collections.addAll(tem,
437
AccessControlTest.lookup_in_self(),
438
Inner_nestmate.lookup_in_nestmate(),
439
AccessControlTest_sibling.lookup_in_sibling());
440
if (true) {
441
Collections.addAll(tem,Acquaintance_remote.lookups());
442
} else {
443
try {
444
Class<?> remc = Class.forName("test.java.lang.invoke.AccessControlTest_subpkg.Acquaintance_remote");
445
Lookup[] remls = (Lookup[]) remc.getMethod("lookups").invoke(null);
446
Collections.addAll(tem, remls);
447
} catch (ReflectiveOperationException ex) {
448
throw new LinkageError("reflection failed", ex);
449
}
450
}
451
tem.add(publicLookup());
452
tem.add(publicLookup().in(String.class));
453
tem.add(publicLookup().in(List.class));
454
return tem.toArray(new Lookup[0]);
455
}
456
457
static Lookup lookup_in_self() {
458
return MethodHandles.lookup();
459
}
460
static public void pub_in_self() { }
461
static protected void pro_in_self() { }
462
static /*package*/ void pkg_in_self() { }
463
static private void pri_in_self() { }
464
465
static class Inner_nestmate {
466
static Lookup lookup_in_nestmate() {
467
return MethodHandles.lookup();
468
}
469
static public void pub_in_nestmate() { }
470
static protected void pro_in_nestmate() { }
471
static /*package*/ void pkg_in_nestmate() { }
472
static private void pri_in_nestmate() { }
473
}
474
}
475
class AccessControlTest_sibling {
476
static Lookup lookup_in_sibling() {
477
return MethodHandles.lookup();
478
}
479
static public void pub_in_sibling() { }
480
static protected void pro_in_sibling() { }
481
static /*package*/ void pkg_in_sibling() { }
482
static private void pri_in_sibling() { }
483
}
484
485
// This guy tests access from outside the package:
486
/*
487
package test.java.lang.invoke.AccessControlTest_subpkg;
488
public class Acquaintance_remote {
489
public static Lookup[] lookups() { ...
490
}
491
...
492
}
493
*/
494
495