Skip to content

Commit 3f69a2d

Browse files
[GR-64566] Parse system properties before options.
PullRequest: graal/20662
2 parents fba5c40 + 8150042 commit 3f69a2d

File tree

4 files changed

+70
-37
lines changed

4 files changed

+70
-37
lines changed

substratevm/src/com.oracle.svm.common/src/com/oracle/svm/common/option/CommonOptionParser.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,15 +357,15 @@ private static Class<?> getMultiOptionValueElementType(OptionKey<?> optionKey) {
357357
}
358358

359359
public static long parseLong(String v) {
360-
String valueString = v.trim().toLowerCase();
360+
String valueString = v.trim();
361361
long scale = 1;
362-
if (valueString.endsWith("k")) {
362+
if (valueString.endsWith("k") || valueString.endsWith("K")) {
363363
scale = 1024L;
364-
} else if (valueString.endsWith("m")) {
364+
} else if (valueString.endsWith("m") || valueString.endsWith("M")) {
365365
scale = 1024L * 1024L;
366-
} else if (valueString.endsWith("g")) {
366+
} else if (valueString.endsWith("g") || valueString.endsWith("G")) {
367367
scale = 1024L * 1024L * 1024L;
368-
} else if (valueString.endsWith("t")) {
368+
} else if (valueString.endsWith("t") || valueString.endsWith("T")) {
369369
scale = 1024L * 1024L * 1024L * 1024L;
370370
}
371371

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

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
*/
2525
package com.oracle.svm.core.jdk;
2626

27+
import java.util.Objects;
28+
2729
import com.oracle.svm.core.SubstrateUtil;
2830
import com.oracle.svm.core.annotate.Alias;
2931
import com.oracle.svm.core.annotate.Delete;
30-
import com.oracle.svm.core.annotate.KeepOriginal;
3132
import com.oracle.svm.core.annotate.RecomputeFieldValue;
3233
import com.oracle.svm.core.annotate.Substitute;
3334
import com.oracle.svm.core.annotate.TargetClass;
@@ -42,14 +43,14 @@
4243
* corresponding system properties.
4344
* <p>
4445
* We {@link Substitute substitute} the whole class so that it is possible to use a custom static
45-
* constructor at run-time.
46+
* constructor at run-time. If this class is used before the system properties are fully parsed and
47+
* initialized, it can happen that we return or cache invalid values (see GR-64572).
4648
* <p>
4749
* Note for updating: use {@link Delete} for static fields that should be unreachable (e.g, because
4850
* we substituted an accessor and the field is therefore unused). Use {@link Alias} for static
4951
* fields that can be initialized in our custom static constructor. Use {@link Substitute} for
5052
* methods that access expensive lazily initialized system properties (see
51-
* {@link SystemPropertiesSupport} for a list of all lazily initialized properties). Use
52-
* {@link KeepOriginal} for methods that we don't want to substitute.
53+
* {@link SystemPropertiesSupport} for a list of all lazily initialized properties).
5354
*/
5455
@Substitute
5556
@TargetClass(jdk.internal.util.StaticProperty.class)
@@ -284,39 +285,66 @@ private static String javaIoTmpDir() {
284285
return SystemPropertiesSupport.singleton().getInitialProperty("java.io.tmpdir");
285286
}
286287

287-
@KeepOriginal
288-
public static native String sunBootLibraryPath();
288+
@Substitute
289+
public static String sunBootLibraryPath() {
290+
assert Objects.equals(SUN_BOOT_LIBRARY_PATH, SystemPropertiesSupport.singleton().getInitialProperty("sun.boot.library.path", ""));
291+
return SUN_BOOT_LIBRARY_PATH;
292+
}
289293

290-
@KeepOriginal
291-
public static native String jdkSerialFilter();
294+
@Substitute
295+
public static String jdkSerialFilter() {
296+
assert Objects.equals(JDK_SERIAL_FILTER, SystemPropertiesSupport.singleton().getInitialProperty("jdk.serialFilter"));
297+
return JDK_SERIAL_FILTER;
298+
}
292299

293-
@KeepOriginal
294-
public static native String jdkSerialFilterFactory();
300+
@Substitute
301+
public static String jdkSerialFilterFactory() {
302+
assert Objects.equals(JDK_SERIAL_FILTER_FACTORY, SystemPropertiesSupport.singleton().getInitialProperty("jdk.serialFilterFactory"));
303+
return JDK_SERIAL_FILTER_FACTORY;
304+
}
295305

296-
@KeepOriginal
297-
public static native String nativeEncoding();
306+
@Substitute
307+
public static String nativeEncoding() {
308+
assert Objects.equals(NATIVE_ENCODING, SystemPropertiesSupport.singleton().getInitialProperty("native.encoding"));
309+
return NATIVE_ENCODING;
310+
}
298311

299-
@KeepOriginal
300-
public static native String fileEncoding();
312+
@Substitute
313+
public static String fileEncoding() {
314+
assert Objects.equals(FILE_ENCODING, SystemPropertiesSupport.singleton().getInitialProperty("file.encoding"));
315+
return FILE_ENCODING;
316+
}
301317

302-
@KeepOriginal
303-
public static native String javaPropertiesDate();
318+
@Substitute
319+
public static String javaPropertiesDate() {
320+
assert Objects.equals(JAVA_PROPERTIES_DATE, SystemPropertiesSupport.singleton().getInitialProperty("java.properties.date"));
321+
return JAVA_PROPERTIES_DATE;
322+
}
304323

305-
@KeepOriginal
306-
public static native String jnuEncoding();
324+
@Substitute
325+
public static String jnuEncoding() {
326+
assert Objects.equals(SUN_JNU_ENCODING, SystemPropertiesSupport.singleton().getInitialProperty("sun.jnu.encoding"));
327+
return SUN_JNU_ENCODING;
328+
}
307329

308-
@KeepOriginal
309-
public static native String javaLocaleUseOldISOCodes();
330+
@Substitute
331+
public static String javaLocaleUseOldISOCodes() {
332+
assert Objects.equals(JAVA_LOCALE_USE_OLD_ISO_CODES, SystemPropertiesSupport.singleton().getInitialProperty("java.locale.useOldISOCodes", ""));
333+
return JAVA_LOCALE_USE_OLD_ISO_CODES;
334+
}
310335

311336
@Substitute
312337
@TargetElement(onlyWith = JDKLatest.class)//
313338
public static String osName() {
314339
return SystemPropertiesSupport.singleton().getInitialProperty("os.name");
315340
}
316341

317-
@KeepOriginal
342+
@Substitute
318343
@TargetElement(onlyWith = JDKLatest.class)//
319-
public static native String osArch();
344+
public static String osArch() {
345+
assert Objects.equals(OS_ARCH, SystemPropertiesSupport.singleton().getInitialProperty("os.arch"));
346+
return OS_ARCH;
347+
}
320348

321349
@Substitute
322350
@TargetElement(onlyWith = JDKLatest.class)//

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/option/RuntimeOptionParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import com.oracle.svm.core.SubstrateOptions;
4141
import com.oracle.svm.core.graal.RuntimeCompilation;
4242
import com.oracle.svm.core.log.Log;
43-
import com.oracle.svm.core.properties.RuntimePropertyParser;
43+
import com.oracle.svm.core.properties.RuntimeSystemPropertyParser;
4444
import com.oracle.svm.core.util.ImageHeapMap;
4545

4646
import jdk.graal.compiler.api.replacements.Fold;
@@ -92,8 +92,9 @@ public final class RuntimeOptionParser {
9292
public static String[] parseAndConsumeAllOptions(String[] initialArgs, boolean ignoreUnrecognized) {
9393
String[] args = initialArgs;
9494
if (SubstrateOptions.ParseRuntimeOptions.getValue()) {
95+
/* JDK code may access and cache system properties, so parse them early. */
96+
args = RuntimeSystemPropertyParser.parse(args, GRAAL_OPTION_PREFIX, LEGACY_GRAAL_OPTION_PREFIX);
9597
args = RuntimeOptionParser.singleton().parse(args, NORMAL_OPTION_PREFIX, GRAAL_OPTION_PREFIX, LEGACY_GRAAL_OPTION_PREFIX, X_OPTION_PREFIX, ignoreUnrecognized);
96-
args = RuntimePropertyParser.parse(args);
9798
} else if (RuntimeCompilation.isEnabled() && SubstrateOptions.supportCompileInIsolates() && IsolateArgumentParser.isCompilationIsolate()) {
9899
/*
99100
* Compilation isolates always need to parse the Native Image options that the main

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/properties/RuntimePropertyParser.java renamed to substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/properties/RuntimeSystemPropertyParser.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import com.oracle.svm.core.jdk.SystemPropertiesSupport;
3333

34-
public final class RuntimePropertyParser {
34+
public final class RuntimeSystemPropertyParser {
3535

3636
private static final String PROPERTY_PREFIX = "-D";
3737

@@ -41,14 +41,12 @@ public final class RuntimePropertyParser {
4141
* property is set. The returned array of arguments are those that should be passed through to
4242
* the application.
4343
*/
44-
public static String[] parse(String[] args) {
44+
public static String[] parse(String[] args, String graalOptionPrefix, String legacyGraalOptionPrefix) {
4545
int newIdx = 0;
4646
EconomicMap<String, String> properties = EconomicMap.create();
4747
for (int oldIdx = 0; oldIdx < args.length; oldIdx++) {
4848
String arg = args[oldIdx];
49-
if (arg.startsWith(PROPERTY_PREFIX) && parseProperty(arg.substring(PROPERTY_PREFIX.length()), properties)) {
50-
// Option consumed
51-
} else {
49+
if (!parseProperty(arg, properties, graalOptionPrefix, legacyGraalOptionPrefix)) {
5250
assert newIdx <= oldIdx;
5351
args[newIdx] = arg;
5452
newIdx++;
@@ -66,17 +64,23 @@ public static String[] parse(String[] args) {
6664
}
6765
}
6866

69-
private static boolean parseProperty(String property, EconomicMap<String, String> parsedProperties) {
67+
private static boolean parseProperty(String arg, EconomicMap<String, String> properties, String graalOptionPrefix, String legacyGraalOptionPrefix) {
68+
if (!arg.startsWith(PROPERTY_PREFIX) || arg.startsWith(graalOptionPrefix) || arg.startsWith(legacyGraalOptionPrefix)) {
69+
return false;
70+
}
71+
return parseProperty0(arg, properties);
72+
}
73+
74+
private static boolean parseProperty0(String arg, EconomicMap<String, String> parsedProperties) {
75+
String property = arg.substring(PROPERTY_PREFIX.length());
7076
int splitIndex = property.indexOf('=');
7177
if (splitIndex == -1) {
7278
return false;
7379
}
7480

7581
String key = property.substring(0, splitIndex);
7682
String value = property.substring(splitIndex + 1);
77-
7883
parsedProperties.put(key, value);
79-
8084
return true;
8185
}
8286
}

0 commit comments

Comments
 (0)