Skip to content

Commit e6ed2f0

Browse files
[GR-60649] [GR-61423] Various fixes for system properties and locales.
PullRequest: graal/19787
2 parents 0fbef95 + a344a3e commit e6ed2f0

File tree

26 files changed

+969
-473
lines changed

26 files changed

+969
-473
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ meth public static java.lang.String getExecutableName()
453453
meth public static java.lang.String getObjectFile(java.lang.String)
454454
meth public static java.lang.String getObjectFile(org.graalvm.nativeimage.c.function.CEntryPointLiteral<?>)
455455
meth public static java.lang.String setLocale(java.lang.String,java.lang.String)
456+
anno 0 java.lang.Deprecated(boolean forRemoval=false, java.lang.String since="25.0")
456457
meth public static long getProcessID()
457458
meth public static long getProcessID(java.lang.Process)
458459
meth public static void exec(java.nio.file.Path,java.lang.String[],java.util.Map<java.lang.String,java.lang.String>)

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ProcessProperties.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, 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
@@ -145,7 +145,11 @@ public static String getObjectFile(CEntryPointLiteral<?> symbol) {
145145
* Set the program locale.
146146
*
147147
* @since 19.0
148+
* @deprecated in 25.0 without replacement. This method is inherently unsafe because
149+
* {@code setlocale(...)} is not thread-safe on the OS level.
148150
*/
151+
@Deprecated(since = "25.0")
152+
@SuppressWarnings("deprecation")
149153
public static String setLocale(String category, String locale) {
150154
return ImageSingletons.lookup(ProcessPropertiesSupport.class).setLocale(category, locale);
151155
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, 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
@@ -64,6 +64,11 @@ default String getObjectFile(PointerBase symbolAddress) {
6464
return null;
6565
}
6666

67+
/**
68+
* @deprecated in 25.0 without replacement. This method is inherently unsafe because
69+
* {@code setlocale(...)} is not thread-safe on the OS level.
70+
*/
71+
@Deprecated
6772
String setLocale(String category, String locale);
6873

6974
boolean destroy(long processID);

substratevm/mx.substratevm/mx_substratevm.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,9 @@ def prevent_build_path_in_libgraal():
15801580
# Reduce image size by outlining all write barriers.
15811581
# Benchmarking showed no performance degradation.
15821582
'-H:+OutlineWriteBarriers',
1583+
1584+
# Libgraal must not change the process-wide locale settings.
1585+
'-H:-UseSystemLocale',
15831586
] + ([
15841587
# Force page size to support libgraal on AArch64 machines with a page size up to 64K.
15851588
'-H:PageSize=64K'

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixProcessPropertiesSupport.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@
3333
import java.util.Map.Entry;
3434
import java.util.stream.Collectors;
3535

36-
import jdk.graal.compiler.word.Word;
3736
import org.graalvm.nativeimage.c.type.CCharPointer;
3837
import org.graalvm.nativeimage.c.type.CTypeConversion;
3938
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
4039
import org.graalvm.word.PointerBase;
4140

4241
import com.oracle.svm.core.BaseProcessPropertiesSupport;
42+
import com.oracle.svm.core.c.locale.LocaleSupport;
4343
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
4444
import com.oracle.svm.core.memory.UntrackedNullableNativeMemory;
4545
import com.oracle.svm.core.posix.headers.Dlfcn;
4646
import com.oracle.svm.core.posix.headers.Signal;
4747
import com.oracle.svm.core.posix.headers.Stdlib;
4848
import com.oracle.svm.core.posix.headers.Unistd;
4949

50+
import jdk.graal.compiler.word.Word;
51+
5052
public abstract class PosixProcessPropertiesSupport extends BaseProcessPropertiesSupport {
5153

5254
@Override
@@ -107,7 +109,9 @@ public String getObjectFile(PointerBase symbolAddress) {
107109
}
108110
}
109111

112+
/** This method is unsafe and should not be used, see {@link LocaleSupport}. */
110113
@Override
114+
@SuppressWarnings("deprecation")
111115
public String setLocale(String category, String locale) {
112116
return PosixUtils.setLocale(category, locale);
113117
}

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.oracle.svm.core.annotate.TargetClass;
4747
import com.oracle.svm.core.c.libc.GLibC;
4848
import com.oracle.svm.core.c.libc.LibCBase;
49+
import com.oracle.svm.core.c.locale.LocaleSupport;
4950
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
5051
import com.oracle.svm.core.headers.LibC;
5152
import com.oracle.svm.core.hub.DynamicHub;
@@ -70,12 +71,13 @@
7071
import jdk.graal.compiler.word.Word;
7172

7273
public class PosixUtils {
74+
/** This method is unsafe and should not be used, see {@link LocaleSupport}. */
7375
static String setLocale(String category, String locale) {
7476
int intCategory = getCategory(category);
75-
7677
return setLocale(intCategory, locale);
7778
}
7879

80+
/** This method is unsafe and should not be used, see {@link LocaleSupport}. */
7981
private static String setLocale(int category, String locale) {
8082
if (locale == null) {
8183
CCharPointer cstrResult = Locale.setlocale(category, Word.nullPointer());

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
import java.util.List;
3333
import java.util.Map;
3434

35-
import jdk.graal.compiler.word.Word;
3635
import org.graalvm.nativeimage.c.type.CCharPointer;
3736
import org.graalvm.nativeimage.c.type.CTypeConversion;
3837
import org.graalvm.nativeimage.impl.ProcessPropertiesSupport;
3938
import org.graalvm.word.PointerBase;
4039

4140
import com.oracle.svm.core.BaseProcessPropertiesSupport;
41+
import com.oracle.svm.core.c.locale.LocaleSupport;
4242
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
4343
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
4444
import com.oracle.svm.core.util.VMError;
@@ -48,6 +48,8 @@
4848
import com.oracle.svm.core.windows.headers.WinBase.HANDLE;
4949
import com.oracle.svm.core.windows.headers.WindowsLibC.WCharPointer;
5050

51+
import jdk.graal.compiler.word.Word;
52+
5153
@AutomaticallyRegisteredImageSingleton(ProcessPropertiesSupport.class)
5254
public class WindowsProcessPropertiesSupport extends BaseProcessPropertiesSupport {
5355

@@ -132,7 +134,9 @@ private static String getModulePath(WinBase.HMODULE module) {
132134
return WindowsSystemPropertiesSupport.toJavaString(path, length);
133135
}
134136

137+
/** This method is unsafe and should not be used, see {@link LocaleSupport}. */
135138
@Override
139+
@SuppressWarnings("deprecation")
136140
public String setLocale(String category, String locale) {
137141
throw VMError.intentionallyUnimplemented(); // ExcludeFromJacocoGeneratedReport
138142
}

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import org.graalvm.nativeimage.c.type.CCharPointer;
3131
import org.graalvm.nativeimage.c.type.CCharPointerPointer;
3232

33-
import com.oracle.svm.core.util.BasedOnJDKFile;
34-
3533
@CLibrary(value = "libchelper", requireStatic = true, dependsOn = "java")
3634
public class LibCHelper {
3735
@CFunction(transition = Transition.NO_TRANSITION)
@@ -41,18 +39,4 @@ public class LibCHelper {
4139
// Checkstyle: stop
4240
public static native CCharPointer SVM_FindJavaTZmd(CCharPointer tzMappings, int length);
4341
// Checkstyle: start
44-
45-
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+13/src/java.base/unix/native/libjava/locale_str.h")
46-
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+13/src/java.base/windows/native/libjava/locale_str.h")
47-
public static class Locale {
48-
@CFunction(transition = Transition.TO_NATIVE)
49-
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+17/src/java.base/unix/native/libjava/java_props_md.c#L93-L357")
50-
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+24/src/java.base/windows/native/libjava/java_props_md.c#L321-L713")
51-
public static native CCharPointerPointer parseDisplayLocale();
52-
53-
@CFunction(transition = Transition.TO_NATIVE)
54-
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+17/src/java.base/unix/native/libjava/java_props_md.c#L93-L357")
55-
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+24/src/java.base/windows/native/libjava/java_props_md.c#L321-L713")
56-
public static native CCharPointerPointer parseFormatLocale();
57-
}
5842
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,9 @@ public Boolean getValue(OptionValues values) {
11551155
}
11561156
};
11571157

1158+
@Option(help = "Determines if the system locale should be used at run-time. If this is disabled, the locale 'en-US' will be used instead.", stability = OptionStability.EXPERIMENTAL, type = Expert)//
1159+
public static final HostedOptionKey<Boolean> UseSystemLocale = new HostedOptionKey<>(true);
1160+
11581161
@Option(help = "Dump heap to file (see HeapDumpPath) the first time the image throws java.lang.OutOfMemoryError because it ran out of Java heap.")//
11591162
public static final RuntimeOptionKey<Boolean> HeapDumpOnOutOfMemoryError = new RuntimeOptionKey<>(false);
11601163

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2024, 2025, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.c.locale;
26+
27+
import static org.graalvm.nativeimage.c.function.CFunction.Transition.NO_TRANSITION;
28+
29+
import org.graalvm.nativeimage.c.CContext;
30+
import org.graalvm.nativeimage.c.constant.CConstant;
31+
import org.graalvm.nativeimage.c.function.CFunction;
32+
import org.graalvm.nativeimage.c.function.CLibrary;
33+
import org.graalvm.nativeimage.c.struct.CField;
34+
import org.graalvm.nativeimage.c.struct.CStruct;
35+
import org.graalvm.nativeimage.c.type.CCharPointer;
36+
import org.graalvm.word.PointerBase;
37+
38+
import com.oracle.svm.core.util.BasedOnJDKFile;
39+
40+
@CContext(LocaleDirectives.class)
41+
@CLibrary(value = "libchelper", requireStatic = true, dependsOn = "java")
42+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+13/src/java.base/unix/native/libjava/locale_str.h")
43+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+13/src/java.base/windows/native/libjava/locale_str.h")
44+
class LocaleCHelper {
45+
// Checkstyle: stop
46+
@CConstant
47+
static native int SVM_LOCALE_INITIALIZATION_SUCCEEDED();
48+
49+
@CConstant
50+
static native int SVM_LOCALE_INITIALIZATION_OUT_OF_MEMORY();
51+
// Checkstyle: resume
52+
53+
/**
54+
* This method changes the process-wide locale settings and should therefore only be called
55+
* during early startup. Calling it at a later point in time is unsafe and may result in
56+
* crashes.
57+
*
58+
* @return {@link #SVM_LOCALE_INITIALIZATION_SUCCEEDED} or
59+
* {@link #SVM_LOCALE_INITIALIZATION_OUT_OF_MEMORY}.
60+
*/
61+
@CFunction(value = "svm_initialize_locale", transition = NO_TRANSITION)
62+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+17/src/java.base/unix/native/libjava/java_props_md.c#L71-L357")
63+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+17/src/java.base/unix/native/libjava/java_props_md.c#L436-L460")
64+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+24/src/java.base/windows/native/libjava/java_props_md.c#L254-L713")
65+
static native int initializeLocale();
66+
67+
@CFunction(value = "svm_get_locale", transition = NO_TRANSITION)
68+
static native LocaleProps getLocale();
69+
70+
@CStruct(value = "svm_locale_props_t")
71+
interface LocaleProps extends PointerBase {
72+
@CField(value = "display_language")
73+
CCharPointer displayLanguage();
74+
75+
@CField(value = "display_script")
76+
CCharPointer displayScript();
77+
78+
@CField(value = "display_country")
79+
CCharPointer displayCountry();
80+
81+
@CField(value = "display_variant")
82+
CCharPointer displayVariant();
83+
84+
@CField(value = "format_language")
85+
CCharPointer formatLanguage();
86+
87+
@CField(value = "format_script")
88+
CCharPointer formatScript();
89+
90+
@CField(value = "format_country")
91+
CCharPointer formatCountry();
92+
93+
@CField(value = "format_variant")
94+
CCharPointer formatVariant();
95+
}
96+
}

0 commit comments

Comments
 (0)