Skip to content

Commit a349076

Browse files
committed
[GR-42823] Add native-image API for defining built-in system-properties for image-runtime #5557.
PullRequest: graal/13313
2 parents 3027251 + c434bfe commit a349076

File tree

13 files changed

+163
-37
lines changed

13 files changed

+163
-37
lines changed

sdk/src/org.graalvm.nativeimage/snapshot.sigtest

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,10 @@ meth public static void registerLambdaCapturingClass(java.lang.Class<?>)
10711071
meth public static void registerWithTargetConstructorClass(java.lang.Class<?>,java.lang.Class<?>)
10721072
supr java.lang.Object
10731073

1074+
CLSS public final org.graalvm.nativeimage.hosted.RuntimeSystemProperties
1075+
meth public static void register(java.lang.String,java.lang.String)
1076+
supr java.lang.Object
1077+
10741078
CLSS public abstract interface org.graalvm.nativeimage.impl.InternalPlatform
10751079
innr public abstract interface static NATIVE_ONLY
10761080
innr public abstract interface static PLATFORM_JNI
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.graalvm.nativeimage.hosted;
42+
43+
import java.util.Objects;
44+
45+
import org.graalvm.nativeimage.ImageSingletons;
46+
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
47+
48+
public final class RuntimeSystemProperties {
49+
50+
/**
51+
* Define a system property setting for image runtime. This ensures a given key has the given
52+
* value at runtime even if no system property is set via command line of the image execution.
53+
*
54+
* @since 23.0
55+
*/
56+
public static void register(String key, String value) {
57+
Objects.requireNonNull(key);
58+
Objects.requireNonNull(value);
59+
ImageSingletons.lookup(RuntimeSystemPropertiesSupport.class).initializeProperty(key, value);
60+
}
61+
62+
private RuntimeSystemProperties() {
63+
}
64+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.graalvm.nativeimage.impl;
42+
43+
public interface RuntimeSystemPropertiesSupport {
44+
void initializeProperty(String key, String value);
45+
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSystemPropertiesSupport.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.graalvm.nativeimage.c.type.CCharPointer;
3131
import org.graalvm.nativeimage.c.type.CTypeConversion;
3232
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
33+
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
3334
import org.graalvm.word.UnsignedWord;
3435
import org.graalvm.word.WordFactory;
3536

@@ -130,6 +131,8 @@ protected String osVersionValue() {
130131
class DarwinSystemPropertiesFeature implements InternalFeature {
131132
@Override
132133
public void duringSetup(DuringSetupAccess access) {
133-
ImageSingletons.add(SystemPropertiesSupport.class, new DarwinSystemPropertiesSupport());
134+
ImageSingletons.add(RuntimeSystemPropertiesSupport.class, new DarwinSystemPropertiesSupport());
135+
/* GR-42971 - Remove once SystemPropertiesSupport.class ImageSingletons use is gone. */
136+
ImageSingletons.add(SystemPropertiesSupport.class, (SystemPropertiesSupport) ImageSingletons.lookup(RuntimeSystemPropertiesSupport.class));
134137
}
135138
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/linux/LinuxSystemPropertiesSupport.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.graalvm.nativeimage.c.type.CCharPointer;
2929
import org.graalvm.nativeimage.c.type.CTypeConversion;
3030
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
31+
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
3132

3233
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
3334
import com.oracle.svm.core.feature.InternalFeature;
@@ -92,6 +93,8 @@ class LinuxSystemPropertiesFeature implements InternalFeature {
9293

9394
@Override
9495
public void duringSetup(DuringSetupAccess access) {
95-
ImageSingletons.add(SystemPropertiesSupport.class, new LinuxSystemPropertiesSupport());
96+
ImageSingletons.add(RuntimeSystemPropertiesSupport.class, new LinuxSystemPropertiesSupport());
97+
/* GR-42971 - Remove once SystemPropertiesSupport.class ImageSingletons use is gone. */
98+
ImageSingletons.add(SystemPropertiesSupport.class, (SystemPropertiesSupport) ImageSingletons.lookup(RuntimeSystemPropertiesSupport.class));
9699
}
97100
}

substratevm/src/com.oracle.svm.core.windows/src/com/oracle/svm/core/windows/WindowsSystemPropertiesSupport.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.graalvm.nativeimage.c.type.CTypeConversion;
3838
import org.graalvm.nativeimage.c.type.VoidPointer;
3939
import org.graalvm.nativeimage.c.type.WordPointer;
40+
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
4041
import org.graalvm.word.UnsignedWord;
4142
import org.graalvm.word.WordFactory;
4243

@@ -390,6 +391,8 @@ public Pair<String, String> getOsNameAndVersion() {
390391
class WindowsSystemPropertiesFeature implements InternalFeature {
391392
@Override
392393
public void duringSetup(DuringSetupAccess access) {
393-
ImageSingletons.add(SystemPropertiesSupport.class, new WindowsSystemPropertiesSupport());
394+
ImageSingletons.add(RuntimeSystemPropertiesSupport.class, new WindowsSystemPropertiesSupport());
395+
/* GR-42971 - Remove once SystemPropertiesSupport.class ImageSingletons use is gone. */
396+
ImageSingletons.add(SystemPropertiesSupport.class, (SystemPropertiesSupport) ImageSingletons.lookup(RuntimeSystemPropertiesSupport.class));
394397
}
395398
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ private static synchronized void reinitialize(Target_sun_nio_fs_WindowsFileSyste
379379
return;
380380
}
381381
that.needsReinitialization = NeedsReinitializationProvider.STATUS_IN_REINITIALIZATION;
382-
that.originalConstructor(that.provider, ImageSingletons.lookup(SystemPropertiesSupport.class).userDir());
382+
that.originalConstructor(that.provider, SystemPropertiesSupport.singleton().userDir());
383383
that.needsReinitialization = NeedsReinitializationProvider.STATUS_REINITIALIZED;
384384
}
385385
}
@@ -433,7 +433,7 @@ static String getUserDir(Target_java_io_FileSystem that) {
433433
*/
434434
return Platform.includedIn(Platform.WINDOWS.class)
435435
? that.normalize(System.getProperty("user.dir"))
436-
: ImageSingletons.lookup(SystemPropertiesSupport.class).userDir();
436+
: SystemPropertiesSupport.singleton().userDir();
437437
}
438438
}
439439

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Map;
2828
import java.util.function.BooleanSupplier;
2929

30-
import org.graalvm.nativeimage.ImageSingletons;
3130
import org.graalvm.nativeimage.Platform;
3231
import org.graalvm.nativeimage.Platforms;
3332

@@ -54,7 +53,7 @@ public boolean getAsBoolean() {
5453
final class Target_jdk_vm_ci_services_Services {
5554
@Substitute
5655
public static Map<String, String> getSavedProperties() {
57-
return ImageSingletons.lookup(SystemPropertiesSupport.class).getSavedProperties();
56+
return SystemPropertiesSupport.singleton().getSavedProperties();
5857
}
5958
}
6059

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,30 +305,30 @@ private static int identityHashCode(Object obj) {
305305

306306
@Substitute
307307
private static Properties getProperties() {
308-
return ImageSingletons.lookup(SystemPropertiesSupport.class).getProperties();
308+
return SystemPropertiesSupport.singleton().getProperties();
309309
}
310310

311311
@Substitute
312312
private static void setProperties(Properties props) {
313-
ImageSingletons.lookup(SystemPropertiesSupport.class).setProperties(props);
313+
SystemPropertiesSupport.singleton().setProperties(props);
314314
}
315315

316316
@Substitute
317317
public static String setProperty(String key, String value) {
318318
checkKey(key);
319-
return ImageSingletons.lookup(SystemPropertiesSupport.class).setProperty(key, value);
319+
return SystemPropertiesSupport.singleton().setProperty(key, value);
320320
}
321321

322322
@Substitute
323323
private static String getProperty(String key) {
324324
checkKey(key);
325-
return ImageSingletons.lookup(SystemPropertiesSupport.class).getProperty(key);
325+
return SystemPropertiesSupport.singleton().getProperty(key);
326326
}
327327

328328
@Substitute
329329
public static String clearProperty(String key) {
330330
checkKey(key);
331-
return ImageSingletons.lookup(SystemPropertiesSupport.class).clearProperty(key);
331+
return SystemPropertiesSupport.singleton().clearProperty(key);
332332
}
333333

334334
@Substitute

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
import java.util.Properties;
3131
import java.util.function.Supplier;
3232

33+
import org.graalvm.compiler.api.replacements.Fold;
3334
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
3435
import org.graalvm.nativeimage.ImageInfo;
3536
import org.graalvm.nativeimage.ImageSingletons;
3637
import org.graalvm.nativeimage.Platform;
3738
import org.graalvm.nativeimage.Platforms;
39+
import org.graalvm.nativeimage.impl.RuntimeSystemPropertiesSupport;
3840

3941
import com.oracle.svm.core.VM;
4042
import com.oracle.svm.core.config.ConfigurationValues;
@@ -49,7 +51,7 @@
4951
* the current working directory is quite expensive. We initialize such a property either when it is
5052
* explicitly accessed, or when all properties are accessed.
5153
*/
52-
public abstract class SystemPropertiesSupport {
54+
public abstract class SystemPropertiesSupport implements RuntimeSystemPropertiesSupport {
5355

5456
/** System properties that are taken from the VM hosting the image generator. */
5557
private static final String[] HOSTED_PROPERTIES = {
@@ -88,6 +90,11 @@ public abstract class SystemPropertiesSupport {
8890

8991
private volatile boolean fullyInitialized;
9092

93+
@Fold
94+
public static SystemPropertiesSupport singleton() {
95+
return ImageSingletons.lookup(SystemPropertiesSupport.class);
96+
}
97+
9198
@Platforms(Platform.HOSTED_ONLY.class)
9299
protected SystemPropertiesSupport() {
93100
properties = new Properties();
@@ -193,8 +200,12 @@ public void setProperties(Properties props) {
193200
* Initializes a property at startup from external input (e.g., command line arguments). This
194201
* must only be called while the runtime is single threaded.
195202
*/
203+
@Override
196204
public void initializeProperty(String key, String value) {
197-
savedProperties.put(key, value);
205+
String prevValue = savedProperties.put(key, value);
206+
if (prevValue != null && !prevValue.equals(value)) {
207+
VMError.shouldNotReachHere("System property " + key + " is initialized to " + value + " but was previously initialized to " + prevValue + ".");
208+
}
198209
properties.setProperty(key, value);
199210
}
200211

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,17 @@
2626

2727
import java.util.Map;
2828

29-
import org.graalvm.nativeimage.ImageSingletons;
30-
29+
import com.oracle.svm.core.NeverInline;
3130
import com.oracle.svm.core.SubstrateOptions;
31+
import com.oracle.svm.core.Uninterruptible;
3232
import com.oracle.svm.core.annotate.Alias;
3333
import com.oracle.svm.core.annotate.AnnotateOriginal;
3434
import com.oracle.svm.core.annotate.Delete;
3535
import com.oracle.svm.core.annotate.InjectAccessors;
36-
import com.oracle.svm.core.NeverInline;
3736
import com.oracle.svm.core.annotate.RecomputeFieldValue;
3837
import com.oracle.svm.core.annotate.RecomputeFieldValue.Kind;
3938
import com.oracle.svm.core.annotate.Substitute;
4039
import com.oracle.svm.core.annotate.TargetClass;
41-
import com.oracle.svm.core.Uninterruptible;
4240
import com.oracle.svm.core.snippets.KnownIntrinsics;
4341

4442
import jdk.internal.misc.Unsafe;
@@ -51,7 +49,7 @@ public final class Target_jdk_internal_misc_VM {
5149

5250
@Substitute
5351
public static String getSavedProperty(String name) {
54-
return ImageSingletons.lookup(SystemPropertiesSupport.class).getSavedProperties().get(name);
52+
return SystemPropertiesSupport.singleton().getSavedProperties().get(name);
5553
}
5654

5755
@AnnotateOriginal

0 commit comments

Comments
 (0)