Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/jvmci/SecurityRestrictionsTest.java
41149 views
1
/*
2
* Copyright (c) 2015, 2019, 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 8136421
27
* @requires vm.jvmci
28
* @library /test/lib /
29
* @library common/patches
30
* @modules java.base/jdk.internal.misc
31
* @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot
32
* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
33
* @run main/othervm -XX:+UnlockExperimentalVMOptions
34
* -XX:+EnableJVMCI
35
* compiler.jvmci.SecurityRestrictionsTest
36
* NO_SEC_MAN
37
* @run main/othervm -XX:+UnlockExperimentalVMOptions
38
* -XX:+EnableJVMCI
39
* -Djava.security.manager=allow
40
* compiler.jvmci.SecurityRestrictionsTest
41
* NO_PERM
42
* @run main/othervm -XX:+UnlockExperimentalVMOptions
43
* -XX:+EnableJVMCI
44
* -Djava.security.manager=allow
45
* compiler.jvmci.SecurityRestrictionsTest
46
* ALL_PERM
47
* @run main/othervm -XX:+UnlockExperimentalVMOptions
48
* -XX:+EnableJVMCI -XX:-UseJVMCICompiler
49
* -Djava.security.manager=allow
50
* compiler.jvmci.SecurityRestrictionsTest
51
* NO_JVMCI_ACCESS_PERM
52
* @run main/othervm -XX:+UnlockExperimentalVMOptions
53
* -XX:-EnableJVMCI -XX:-UseJVMCICompiler
54
* compiler.jvmci.SecurityRestrictionsTest
55
* NO_JVMCI
56
*/
57
58
package compiler.jvmci;
59
60
import jdk.test.lib.Utils;
61
62
import java.security.AccessControlException;
63
import java.security.Permission;
64
import java.util.PropertyPermission;
65
import java.util.function.Consumer;
66
67
public class SecurityRestrictionsTest {
68
69
public static void main(String[] args) {
70
try {
71
// to init Utils before call SecurityManager
72
Class.forName(Utils.class.getName(), true,
73
Utils.class.getClassLoader());
74
} catch (ClassNotFoundException e) {
75
throw new Error("[TEST BUG]: jdk.test.lib.Utils not found", e);
76
}
77
try {
78
TestCase mode = TestCase.valueOf(args[0]);
79
mode.run();
80
} catch (IllegalArgumentException e) {
81
throw new Error("[TEST BUG]: Unknown mode " + args[0], e);
82
}
83
}
84
85
private enum TestCase {
86
NO_SEC_MAN,
87
NO_JVMCI {
88
@Override
89
public Class<? extends Throwable> getExpectedException() {
90
return Error.class;
91
}
92
},
93
ALL_PERM {
94
@Override
95
public SecurityManager getSecurityManager() {
96
return new SecurityManager() {
97
@Override
98
public void checkPermission(Permission perm) {
99
}
100
};
101
}
102
},
103
NO_PERM {
104
@Override
105
public SecurityManager getSecurityManager() {
106
return new SecurityManager();
107
}
108
109
@Override
110
public Class<? extends Throwable> getExpectedException() {
111
return AccessControlException.class;
112
}
113
},
114
NO_JVMCI_ACCESS_PERM {
115
@Override
116
public SecurityManager getSecurityManager() {
117
return new SecurityManager() {
118
@Override
119
public void checkPermission(Permission perm) {
120
if (isJvmciPermission(perm)) {
121
super.checkPermission(perm);
122
}
123
}
124
125
@Override
126
public void checkPropertyAccess(String key) {
127
if (key.startsWith(JVMCI_PROP_START)) {
128
super.checkPropertyAccess(key);
129
}
130
}
131
};
132
}
133
134
private boolean isJvmciPermission(Permission perm) {
135
String name = perm.getName();
136
boolean isJvmciRuntime = perm instanceof RuntimePermission
137
&& (JVMCI_SERVICES.equals(name)
138
|| name.startsWith(JVMCI_RT_PERM_START));
139
boolean isJvmciProperty = perm instanceof PropertyPermission
140
&& name.startsWith(JVMCI_PROP_START);
141
return isJvmciRuntime || isJvmciProperty;
142
}
143
144
@Override
145
public Class<? extends Throwable> getExpectedException() {
146
return AccessControlException.class;
147
}
148
};
149
150
public void run() {
151
System.setSecurityManager(getSecurityManager());
152
Consumer<Throwable> exceptionCheck = e -> {
153
if (e == null) {
154
if (getExpectedException() != null) {
155
String message = name() + ": Didn't get expected exception "
156
+ getExpectedException();
157
throw new AssertionError(message);
158
}
159
} else {
160
String message = name() + ": Got unexpected exception "
161
+ e.getClass().getSimpleName();
162
if (getExpectedException() == null){
163
throw new AssertionError(message, e);
164
}
165
166
Throwable t = e;
167
while (t.getCause() != null) {
168
t = t.getCause();
169
}
170
if (!getExpectedException().isAssignableFrom(t.getClass())) {
171
message += " instead of " + getExpectedException()
172
.getSimpleName();
173
throw new AssertionError(message, e);
174
}
175
}
176
};
177
Utils.runAndCheckException(() -> {
178
// CompilerToVM::<cinit> provokes CompilerToVM::<init>
179
Class.forName("jdk.vm.ci.hotspot.CompilerToVMHelper");
180
}, exceptionCheck);
181
}
182
183
public SecurityManager getSecurityManager() {
184
return null;
185
}
186
187
public Class<? extends Throwable> getExpectedException() {
188
return null;
189
}
190
191
private static final String JVMCI_RT_PERM_START
192
= "accessClassInPackage.jdk.vm.ci";
193
private static final String JVMCI_SERVICES = "jvmciServices";
194
private static final String JVMCI_PROP_START = "jvmci.";
195
196
}
197
}
198
199