Skip to content

Commit 0a21e38

Browse files
Fix review feedback
1 parent 4d663eb commit 0a21e38

File tree

4 files changed

+37
-49
lines changed

4 files changed

+37
-49
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/ProcessPropertiesSupport.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -40,18 +40,13 @@
4040
*/
4141
package org.graalvm.nativeimage.impl;
4242

43-
import java.net.URL;
4443
import java.nio.file.Path;
4544

4645
import org.graalvm.nativeimage.c.function.CEntryPointLiteral;
4746

4847
public interface ProcessPropertiesSupport {
4948
String getExecutableName();
5049

51-
default URL getExecutableURL() {
52-
return null;
53-
}
54-
5550
long getProcessID();
5651

5752
long getProcessID(Process process);

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/BaseProcessPropertiesSupport.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,23 +27,7 @@
2727

2828
import org.graalvm.nativeimage.impl.ProcessPropertiesSupport;
2929

30-
import java.io.File;
31-
import java.net.MalformedURLException;
32-
import java.net.URL;
33-
3430
public abstract class BaseProcessPropertiesSupport implements ProcessPropertiesSupport {
35-
36-
@Override
37-
public URL getExecutableURL() {
38-
try {
39-
return new File(getExecutableName()).toURI().toURL();
40-
} catch (MalformedURLException ex) {
41-
// This should not really happen; the file is cannonicalized, absolute, so it should
42-
// always have file:// URL.
43-
return null;
44-
}
45-
}
46-
4731
@Override
4832
public int getArgumentVectorBlockSize() {
4933
return JavaMainWrapper.getCRuntimeArgumentBlockLength();

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
//Checkstyle: allow reflection
2828

29+
import java.io.File;
2930
import java.io.InputStream;
3031
import java.io.Serializable;
3132
import java.lang.annotation.Annotation;
@@ -42,6 +43,7 @@
4243
import java.lang.reflect.Modifier;
4344
import java.lang.reflect.Type;
4445
import java.lang.reflect.TypeVariable;
46+
import java.net.MalformedURLException;
4547
import java.net.URL;
4648
import java.security.CodeSource;
4749
import java.security.ProtectionDomain;
@@ -53,12 +55,14 @@
5355
import java.util.Set;
5456
import java.util.StringJoiner;
5557

58+
import com.oracle.svm.core.BaseProcessPropertiesSupport;
5659
import org.graalvm.compiler.core.common.NumUtil;
5760
import org.graalvm.compiler.core.common.SuppressFBWarnings;
5861
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
5962
import org.graalvm.nativeimage.ImageSingletons;
6063
import org.graalvm.nativeimage.Platform;
6164
import org.graalvm.nativeimage.Platforms;
65+
import org.graalvm.nativeimage.ProcessProperties;
6266
import org.graalvm.nativeimage.c.function.CFunctionPointer;
6367
import org.graalvm.nativeimage.impl.ProcessPropertiesSupport;
6468
import org.graalvm.util.DirectAnnotationAccess;
@@ -327,19 +331,26 @@ public void setModule(Object module) {
327331
}
328332

329333
/**
330-
* Final fields in subsituted classes are treated as implicitly RecomputeFieldValue even when
334+
* Final fields in substituted classes are treated as implicitly RecomputeFieldValue even when
331335
* not annotated with @RecomputeFieldValue. Their name must not match a field in the original
332336
* class, i.e., allPermDomain.
333337
*/
334338
static final LazyFinalReference<java.security.ProtectionDomain> allPermDomainReference = new LazyFinalReference<>(() -> {
335339
java.security.Permissions perms = new java.security.Permissions();
336340
perms.add(SecurityConstants.ALL_PERMISSION);
337-
CodeSource cs;
338-
339-
// Try to use executable image's name as code source for the class.
340-
// The file location can be used by Java code to determine its location on disk, similar
341-
// to argv[0].
342-
cs = new CodeSource(ImageSingletons.lookup(ProcessPropertiesSupport.class).getExecutableURL(), (Certificate[]) null);
341+
CodeSource cs = null;
342+
343+
if (ImageSingletons.lookup(ProcessPropertiesSupport.class) instanceof BaseProcessPropertiesSupport) {
344+
// Try to use executable image's name as code source for the class.
345+
// The file location can be used by Java code to determine its location on disk, similar
346+
// to argv[0].
347+
try {
348+
cs = new CodeSource(new File(ProcessProperties.getExecutableName()).toURI().toURL(), (Certificate[]) null);
349+
} catch (MalformedURLException e) {
350+
// This should not really happen; the file is cannonicalized, absolute, so it should
351+
// always have file:// URL.
352+
}
353+
}
343354

344355
return new java.security.ProtectionDomain(cs, perms);
345356
});

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SecuritySubstitutions.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import java.security.Provider;
4141
import java.security.SecureRandom;
4242
import java.util.ArrayDeque;
43-
import java.util.ArrayList;
43+
import java.util.HashMap;
4444
import java.util.List;
4545
import java.util.Map;
4646
import java.util.Objects;
@@ -49,7 +49,6 @@
4949

5050
import jdk.vm.ci.meta.MetaAccessProvider;
5151
import jdk.vm.ci.meta.ResolvedJavaField;
52-
import org.graalvm.collections.Pair;
5352
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
5453
import org.graalvm.nativeimage.Platform;
5554
import org.graalvm.nativeimage.Platforms;
@@ -135,16 +134,11 @@ private static ProtectionDomain getProtectionDomain(final Class<?> caller) {
135134

136135
@Substitute
137136
@TargetElement(onlyWith = JDK14OrLater.class)
138-
@SuppressWarnings("deprecation") // deprecated starting JDK 17
139137
static <T> T executePrivileged(PrivilegedExceptionAction<T> action, AccessControlContext context, Class<?> caller) throws Throwable {
140138
if (action == null) {
141139
throw new NullPointerException("Null action");
142140
}
143141

144-
if (context != null && context.equals(AccessControllerUtil.NO_CONTEXT_SINGLETON)) {
145-
VMError.shouldNotReachHere("Invoked AccessControlContext was replaced at build time but wasn't reinitialized at run time.");
146-
}
147-
148142
AccessControllerUtil.PrivilegedStack.push(context, caller);
149143
try {
150144
return action.run();
@@ -157,17 +151,11 @@ static <T> T executePrivileged(PrivilegedExceptionAction<T> action, AccessContro
157151

158152
@Substitute
159153
@TargetElement(onlyWith = JDK14OrLater.class)
160-
@SuppressWarnings("deprecation") // deprecated starting JDK 17
161154
static <T> T executePrivileged(PrivilegedAction<T> action, AccessControlContext context, Class<?> caller) throws Throwable {
162155
if (action == null) {
163156
throw new NullPointerException("Null action");
164157
}
165158

166-
if (context != null && context.equals(AccessControllerUtil.NO_CONTEXT_SINGLETON)) {
167-
VMError.shouldNotReachHere("Invoked AccessControlContext was replaced at build time but wasn't reinitialized at run time.\n" +
168-
"This might be an indicator of improper build time initialization, or of a non-compatible JDK version.");
169-
}
170-
171159
AccessControllerUtil.PrivilegedStack.push(context, caller);
172160
try {
173161
return action.run();
@@ -180,11 +168,21 @@ static <T> T executePrivileged(PrivilegedAction<T> action, AccessControlContext
180168

181169
@Substitute
182170
@TargetElement(onlyWith = JDK14OrLater.class)
183-
@SuppressWarnings("unused")
171+
@SuppressWarnings({"unused", "deprecation"})
184172
static AccessControlContext checkContext(AccessControlContext context, Class<?> caller) {
173+
174+
if (context != null && context.equals(AccessControllerUtil.NO_CONTEXT_SINGLETON)) {
175+
VMError.shouldNotReachHere("Non-allowed AccessControlContext that was replaced with a blank one at build time was invoked without being reinitialized at run time.\n" +
176+
"This might be an indicator of improper build time initialization, or of a non-compatible JDK version.\n" +
177+
"In order to fix this you can either:\n" +
178+
" * Annotate the offending context's field with @RecomputeFieldValue\n" +
179+
" * Implement a custom runtime accessor and annotate said field with @InjectAccessors\n" +
180+
" * If this context originates from the JDK, and it doesn't leak sensitive info, you can allow it in 'AccessControlContextFeature.duringSetup'");
181+
}
182+
185183
// check if caller is authorized to create context
186184
if (System.getSecurityManager() != null) {
187-
throw VMError.shouldNotReachHere("Needs to be implemented when SecurityManager is supported");
185+
throw VMError.unsupportedFeature("SecurityManager isn't supported");
188186
}
189187
return context;
190188
}
@@ -224,7 +222,7 @@ public Class<?> getCaller() {
224222
}
225223
}
226224

227-
@SuppressWarnings("rawtypes") private static final FastThreadLocalObject<ArrayDeque> stack = FastThreadLocalFactory.createObject(ArrayDeque.class, "accStack");
225+
@SuppressWarnings("rawtypes") private static final FastThreadLocalObject<ArrayDeque> stack = FastThreadLocalFactory.createObject(ArrayDeque.class, "AccessControlContextStack");
228226

229227
@SuppressWarnings("unchecked")
230228
private static ArrayDeque<StackElement> getStack() {
@@ -270,7 +268,7 @@ static Throwable wrapCheckedException(Throwable ex) {
270268
@SuppressWarnings({"unused"})
271269
class AccessControlContextFeature implements Feature {
272270

273-
static List<Pair<String, AccessControlContext>> allowedContexts = new ArrayList<>();
271+
static Map<String, AccessControlContext> allowedContexts = new HashMap<>();
274272

275273
static void allowContextIfExists(String className, String fieldName) {
276274
try {
@@ -280,7 +278,7 @@ static void allowContextIfExists(String className, String fieldName) {
280278
String description = className + "." + fieldName;
281279
try {
282280
AccessControlContext acc = ReflectionUtil.readStaticField(clazz, fieldName);
283-
allowedContexts.add(Pair.create(description, acc));
281+
allowedContexts.put(description, acc);
284282
} catch (ReflectionUtil.ReflectionUtilError e) {
285283
VMError.shouldNotReachHere("Following field isn't present in JDK" + JavaVersionUtil.JAVA_SPEC + ": " + description);
286284
}
@@ -323,7 +321,7 @@ public void duringSetup(DuringSetupAccess access) {
323321

324322
private static Object replaceAccessControlContext(Object obj) {
325323
if (obj instanceof AccessControlContext && obj != AccessControllerUtil.NO_CONTEXT_SINGLETON) {
326-
if (allowedContexts.stream().anyMatch((e) -> obj.equals(e.getRight()))) {
324+
if (allowedContexts.containsValue(obj)) {
327325
return obj;
328326
} else {
329327
return AccessControllerUtil.NO_CONTEXT_SINGLETON;

0 commit comments

Comments
 (0)