Skip to content

Commit 4928fa8

Browse files
committed
Report warning for unsupported system properties
Many system properties are not supported by native image. Report warning for the usage of such properties at native image build and run time.
1 parent 3cda6f3 commit 4928fa8

File tree

5 files changed

+225
-3
lines changed

5 files changed

+225
-3
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/configure/ConfigurationParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ protected static boolean asBoolean(Object value, String propertyName) {
7171
}
7272
throw new JSONParserException("Invalid boolean value '" + value + "' for element '" + propertyName + "'");
7373
}
74-
}
74+
}

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package com.oracle.svm.core.jdk;
2626

27+
import java.util.Arrays;
2728
import java.util.Collections;
2829
import java.util.HashMap;
2930
import java.util.Map;
@@ -65,7 +66,36 @@ public abstract class SystemPropertiesSupport {
6566
"java.specification.version",
6667
"java.vm.specification.name",
6768
"java.vm.specification.vendor",
68-
"java.vm.specification.version"
69+
"java.vm.specification.version",
70+
"sun.arch.data.model"
71+
};
72+
73+
/**
74+
* System properties are unsupported by default.
75+
*/
76+
private static final String[] UNSUPPORTED_PROPERTIES = {
77+
"awt.toolkit",
78+
"file.encoding.pkg",
79+
"java.awt.graphicsenv",
80+
"java.awt.printerjob",
81+
"java.home",
82+
"java.library.path",
83+
"java.runtime.name",
84+
"java.runtime.version",
85+
"java.vendor.url.bug",
86+
"java.vm.info",
87+
"sun.boot.class.path",
88+
"sun.boot.library.path",
89+
"sun.cpu.endian",
90+
"sun.cpu.isalist",
91+
"sun.io.unicode.encoding",
92+
"sun.java.command",
93+
"sun.java.launcher",
94+
"sun.management.compiler",
95+
"sun.os.patch.level",
96+
"user.country",
97+
"user.language",
98+
"user.timezone"
6999
};
70100

71101
/** System properties that are lazily computed at run time on first access. */
@@ -141,7 +171,15 @@ public Properties getProperties() {
141171

142172
protected String getProperty(String key) {
143173
initializeLazyValue(key);
144-
return properties.getProperty(key);
174+
String ret = properties.getProperty(key);
175+
if (ret == null && isNotSupported(key)) {
176+
System.out.println("Java system property " + key + " is not available in native image. Please explicitly assign the expected value via -D" + key + "=, or avoid using this property.");
177+
}
178+
return ret;
179+
}
180+
181+
public static boolean isNotSupported(String key) {
182+
return Arrays.stream(UNSUPPORTED_PROPERTIES).anyMatch(s -> s.equals(key));
145183
}
146184

147185
public void setProperties(Properties props) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation. Oracle designates this
9+
* particular file as subject to the "Classpath" exception as provided
10+
* by Oracle in the LICENSE file that accompanied this code.
11+
*
12+
* This code is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
* version 2 for more details (a copy is included in the LICENSE file that
16+
* accompanied this code).
17+
*
18+
* You should have received a copy of the GNU General Public License version
19+
* 2 along with this work; if not, write to the Free Software Foundation,
20+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*
22+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23+
* or visit www.oracle.com if you need additional information or have any
24+
* questions.
25+
*/
26+
package com.oracle.svm.hosted;
27+
28+
import com.oracle.svm.core.annotate.AutomaticFeature;
29+
import com.oracle.svm.core.graal.GraalFeature;
30+
import com.oracle.svm.hosted.analysis.Inflation;
31+
import com.oracle.svm.hosted.snippets.SystemPropertyPlugin;
32+
import com.oracle.svm.hosted.substitute.AnnotationSubstitutionProcessor;
33+
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
34+
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
35+
import org.graalvm.compiler.phases.util.Providers;
36+
import org.graalvm.nativeimage.ImageSingletons;
37+
38+
@AutomaticFeature
39+
public class SystemPropertyFeature implements GraalFeature {
40+
private ImageClassLoader loader;
41+
private SVMHost hostVM;
42+
private AnnotationSubstitutionProcessor annotationSubstitutions;
43+
44+
public SystemPropertyFeature() {
45+
ImageSingletons.add(UnsupportedSystemPropertyErrors.class, new UnsupportedSystemPropertyErrors());
46+
}
47+
48+
@Override
49+
public void duringSetup(DuringSetupAccess a) {
50+
FeatureImpl.DuringSetupAccessImpl access = (FeatureImpl.DuringSetupAccessImpl) a;
51+
hostVM = access.getHostVM();
52+
loader = access.getImageClassLoader();
53+
annotationSubstitutions = ((Inflation) access.getBigBang()).getAnnotationSubstitutionProcessor();
54+
}
55+
56+
@Override
57+
public void afterImageWrite(AfterImageWriteAccess access) {
58+
if (!ImageSingletons.contains(UnsupportedSystemPropertyErrors.class)) {
59+
return;
60+
}
61+
UnsupportedSystemPropertyErrors unsupportedPropertyErrors = ImageSingletons.lookup(UnsupportedSystemPropertyErrors.class);
62+
unsupportedPropertyErrors.report();
63+
}
64+
65+
@Override
66+
public void registerInvocationPlugins(Providers providers, SnippetReflectionProvider snippetReflection, InvocationPlugins invocationPlugins, boolean analysis, boolean hosted) {
67+
SystemPropertyPlugin.registerInvocationPlugins(loader, snippetReflection, annotationSubstitutions, invocationPlugins, hostVM, analysis, hosted);
68+
}
69+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation. Oracle designates this
9+
* particular file as subject to the "Classpath" exception as provided
10+
* by Oracle in the LICENSE file that accompanied this code.
11+
*
12+
* This code is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
* version 2 for more details (a copy is included in the LICENSE file that
16+
* accompanied this code).
17+
*
18+
* You should have received a copy of the GNU General Public License version
19+
* 2 along with this work; if not, write to the Free Software Foundation,
20+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*
22+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23+
* or visit www.oracle.com if you need additional information or have any
24+
* questions.
25+
*/
26+
package com.oracle.svm.hosted;
27+
28+
import java.util.HashSet;
29+
import java.util.Set;
30+
31+
public class UnsupportedSystemPropertyErrors {
32+
private final Set<String> systemPropertyCalls = new HashSet<>();
33+
34+
public void report() {
35+
if (!systemPropertyCalls.isEmpty()) {
36+
StringBuilder sb = new StringBuilder("\n\nSystem properties unsupported by native image are used in the program:\n");
37+
for (String s : systemPropertyCalls) {
38+
sb.append(" ").append(s).append("\n");
39+
}
40+
sb.append("The returned value of calling System.getProperty with these keys will be null in native image.\n");
41+
sb.append("Please make sure the expected property values are specified with -D when running the native image program or just avoid using them.\n\n");
42+
System.out.println(sb.toString());
43+
}
44+
}
45+
46+
public void addUnsupportedInvoke(String invokeInfo) {
47+
systemPropertyCalls.add(invokeInfo);
48+
}
49+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation. Oracle designates this
9+
* particular file as subject to the "Classpath" exception as provided
10+
* by Oracle in the LICENSE file that accompanied this code.
11+
*
12+
* This code is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
* version 2 for more details (a copy is included in the LICENSE file that
16+
* accompanied this code).
17+
*
18+
* You should have received a copy of the GNU General Public License version
19+
* 2 along with this work; if not, write to the Free Software Foundation,
20+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*
22+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23+
* or visit www.oracle.com if you need additional information or have any
24+
* questions.
25+
*/
26+
package com.oracle.svm.hosted.snippets;
27+
28+
import com.oracle.svm.core.jdk.SystemPropertiesSupport;
29+
import com.oracle.svm.hosted.ImageClassLoader;
30+
import com.oracle.svm.hosted.SVMHost;
31+
import com.oracle.svm.hosted.UnsupportedSystemPropertyErrors;
32+
import com.oracle.svm.hosted.substitute.AnnotationSubstitutionProcessor;
33+
34+
import jdk.vm.ci.meta.ResolvedJavaMethod;
35+
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
36+
import org.graalvm.compiler.nodes.ValueNode;
37+
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
38+
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
39+
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
40+
import org.graalvm.nativeimage.ImageSingletons;
41+
42+
public class SystemPropertyPlugin {
43+
@SuppressWarnings("unused")
44+
public static void registerInvocationPlugins(ImageClassLoader imageClassLoader, SnippetReflectionProvider snippetReflection, AnnotationSubstitutionProcessor annotationSubstitutions,
45+
InvocationPlugins plugins, SVMHost hostVM, boolean analysis, boolean hosted) {
46+
InvocationPlugins.Registration r = new InvocationPlugins.Registration(plugins, System.class);
47+
r.register1("getProperty", String.class, new InvocationPlugin() {
48+
@Override
49+
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode name) {
50+
if (name.isConstant()) {
51+
String propertyKey = snippetReflection.asObject(String.class, name.asJavaConstant());
52+
if (SystemPropertiesSupport.isNotSupported(propertyKey) && ImageSingletons.contains(UnsupportedSystemPropertyErrors.class)) {
53+
UnsupportedSystemPropertyErrors unsupportedPropertyErrors = ImageSingletons.lookup(UnsupportedSystemPropertyErrors.class);
54+
unsupportedPropertyErrors.addUnsupportedInvoke("Unsupported property:" + propertyKey + " is used at " + b.getCode().asStackTraceElement(b.bci()));
55+
}
56+
}
57+
return false;
58+
}
59+
60+
@Override
61+
public boolean isDecorator() {
62+
return true;
63+
}
64+
});
65+
}
66+
}

0 commit comments

Comments
 (0)