Skip to content

[GR-60649] [GR-61423] Various fixes for system properties and locales. #10613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/src/org.graalvm.nativeimage/snapshot.sigtest
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ meth public static java.lang.String getExecutableName()
meth public static java.lang.String getObjectFile(java.lang.String)
meth public static java.lang.String getObjectFile(org.graalvm.nativeimage.c.function.CEntryPointLiteral<?>)
meth public static java.lang.String setLocale(java.lang.String,java.lang.String)
anno 0 java.lang.Deprecated(boolean forRemoval=false, java.lang.String since="25.0")
meth public static long getProcessID()
meth public static long getProcessID(java.lang.Process)
meth public static void exec(java.nio.file.Path,java.lang.String[],java.util.Map<java.lang.String,java.lang.String>)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -145,7 +145,11 @@ public static String getObjectFile(CEntryPointLiteral<?> symbol) {
* Set the program locale.
*
* @since 19.0
* @deprecated in 25.0 without replacement. This method is inherently unsafe because
* {@code setlocale(...)} is not thread-safe on the OS level.
*/
@Deprecated(since = "25.0")
@SuppressWarnings("deprecation")
public static String setLocale(String category, String locale) {
return ImageSingletons.lookup(ProcessPropertiesSupport.class).setLocale(category, locale);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -64,6 +64,11 @@ default String getObjectFile(PointerBase symbolAddress) {
return null;
}

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

boolean destroy(long processID);
Expand Down
3 changes: 3 additions & 0 deletions substratevm/mx.substratevm/mx_substratevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,9 @@ def prevent_build_path_in_libgraal():
# Reduce image size by outlining all write barriers.
# Benchmarking showed no performance degradation.
'-H:+OutlineWriteBarriers',

# Libgraal must not change the process-wide locale settings.
'-H:-UseSystemLocale',
] + ([
# Force page size to support libgraal on AArch64 machines with a page size up to 64K.
'-H:PageSize=64K'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,22 @@
import java.util.Map.Entry;
import java.util.stream.Collectors;

import jdk.graal.compiler.word.Word;
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
import org.graalvm.word.PointerBase;

import com.oracle.svm.core.BaseProcessPropertiesSupport;
import com.oracle.svm.core.c.locale.LocaleSupport;
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
import com.oracle.svm.core.memory.UntrackedNullableNativeMemory;
import com.oracle.svm.core.posix.headers.Dlfcn;
import com.oracle.svm.core.posix.headers.Signal;
import com.oracle.svm.core.posix.headers.Stdlib;
import com.oracle.svm.core.posix.headers.Unistd;

import jdk.graal.compiler.word.Word;

public abstract class PosixProcessPropertiesSupport extends BaseProcessPropertiesSupport {

@Override
Expand Down Expand Up @@ -107,7 +109,9 @@ public String getObjectFile(PointerBase symbolAddress) {
}
}

/** This method is unsafe and should not be used, see {@link LocaleSupport}. */
@Override
@SuppressWarnings("deprecation")
public String setLocale(String category, String locale) {
return PosixUtils.setLocale(category, locale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.c.libc.GLibC;
import com.oracle.svm.core.c.libc.LibCBase;
import com.oracle.svm.core.c.locale.LocaleSupport;
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
import com.oracle.svm.core.headers.LibC;
import com.oracle.svm.core.hub.DynamicHub;
Expand All @@ -70,12 +71,13 @@
import jdk.graal.compiler.word.Word;

public class PosixUtils {
/** This method is unsafe and should not be used, see {@link LocaleSupport}. */
static String setLocale(String category, String locale) {
int intCategory = getCategory(category);

return setLocale(intCategory, locale);
}

/** This method is unsafe and should not be used, see {@link LocaleSupport}. */
private static String setLocale(int category, String locale) {
if (locale == null) {
CCharPointer cstrResult = Locale.setlocale(category, Word.nullPointer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
import java.util.List;
import java.util.Map;

import jdk.graal.compiler.word.Word;
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.nativeimage.impl.ProcessPropertiesSupport;
import org.graalvm.word.PointerBase;

import com.oracle.svm.core.BaseProcessPropertiesSupport;
import com.oracle.svm.core.c.locale.LocaleSupport;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
import com.oracle.svm.core.util.VMError;
Expand All @@ -48,6 +48,8 @@
import com.oracle.svm.core.windows.headers.WinBase.HANDLE;
import com.oracle.svm.core.windows.headers.WindowsLibC.WCharPointer;

import jdk.graal.compiler.word.Word;

@AutomaticallyRegisteredImageSingleton(ProcessPropertiesSupport.class)
public class WindowsProcessPropertiesSupport extends BaseProcessPropertiesSupport {

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

/** This method is unsafe and should not be used, see {@link LocaleSupport}. */
@Override
@SuppressWarnings("deprecation")
public String setLocale(String category, String locale) {
throw VMError.intentionallyUnimplemented(); // ExcludeFromJacocoGeneratedReport
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CCharPointerPointer;

import com.oracle.svm.core.util.BasedOnJDKFile;

@CLibrary(value = "libchelper", requireStatic = true, dependsOn = "java")
public class LibCHelper {
@CFunction(transition = Transition.NO_TRANSITION)
Expand All @@ -41,18 +39,4 @@ public class LibCHelper {
// Checkstyle: stop
public static native CCharPointer SVM_FindJavaTZmd(CCharPointer tzMappings, int length);
// Checkstyle: start

@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+13/src/java.base/unix/native/libjava/locale_str.h")
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+13/src/java.base/windows/native/libjava/locale_str.h")
public static class Locale {
@CFunction(transition = Transition.TO_NATIVE)
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+17/src/java.base/unix/native/libjava/java_props_md.c#L93-L357")
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+24/src/java.base/windows/native/libjava/java_props_md.c#L321-L713")
public static native CCharPointerPointer parseDisplayLocale();

@CFunction(transition = Transition.TO_NATIVE)
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+17/src/java.base/unix/native/libjava/java_props_md.c#L93-L357")
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+24/src/java.base/windows/native/libjava/java_props_md.c#L321-L713")
public static native CCharPointerPointer parseFormatLocale();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,9 @@ public Boolean getValue(OptionValues values) {
}
};

@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)//
public static final HostedOptionKey<Boolean> UseSystemLocale = new HostedOptionKey<>(true);

@Option(help = "Dump heap to file (see HeapDumpPath) the first time the image throws java.lang.OutOfMemoryError because it ran out of Java heap.")//
public static final RuntimeOptionKey<Boolean> HeapDumpOnOutOfMemoryError = new RuntimeOptionKey<>(false);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.c.locale;

import static org.graalvm.nativeimage.c.function.CFunction.Transition.NO_TRANSITION;

import org.graalvm.nativeimage.c.CContext;
import org.graalvm.nativeimage.c.constant.CConstant;
import org.graalvm.nativeimage.c.function.CFunction;
import org.graalvm.nativeimage.c.function.CLibrary;
import org.graalvm.nativeimage.c.struct.CField;
import org.graalvm.nativeimage.c.struct.CStruct;
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.word.PointerBase;

import com.oracle.svm.core.util.BasedOnJDKFile;

@CContext(LocaleDirectives.class)
@CLibrary(value = "libchelper", requireStatic = true, dependsOn = "java")
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+13/src/java.base/unix/native/libjava/locale_str.h")
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+13/src/java.base/windows/native/libjava/locale_str.h")
class LocaleCHelper {
// Checkstyle: stop
@CConstant
static native int SVM_LOCALE_INITIALIZATION_SUCCEEDED();

@CConstant
static native int SVM_LOCALE_INITIALIZATION_OUT_OF_MEMORY();
// Checkstyle: resume

/**
* This method changes the process-wide locale settings and should therefore only be called
* during early startup. Calling it at a later point in time is unsafe and may result in
* crashes.
*
* @return {@link #SVM_LOCALE_INITIALIZATION_SUCCEEDED} or
* {@link #SVM_LOCALE_INITIALIZATION_OUT_OF_MEMORY}.
*/
@CFunction(value = "svm_initialize_locale", transition = NO_TRANSITION)
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+17/src/java.base/unix/native/libjava/java_props_md.c#L71-L357")
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+17/src/java.base/unix/native/libjava/java_props_md.c#L436-L460")
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+24/src/java.base/windows/native/libjava/java_props_md.c#L254-L713")
static native int initializeLocale();

@CFunction(value = "svm_get_locale", transition = NO_TRANSITION)
static native LocaleProps getLocale();

@CStruct(value = "svm_locale_props_t")
interface LocaleProps extends PointerBase {
@CField(value = "display_language")
CCharPointer displayLanguage();

@CField(value = "display_script")
CCharPointer displayScript();

@CField(value = "display_country")
CCharPointer displayCountry();

@CField(value = "display_variant")
CCharPointer displayVariant();

@CField(value = "format_language")
CCharPointer formatLanguage();

@CField(value = "format_script")
CCharPointer formatScript();

@CField(value = "format_country")
CCharPointer formatCountry();

@CField(value = "format_variant")
CCharPointer formatVariant();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.c.locale;

public record LocaleData(String country, String displayCountry, String formatCountry,
String language, String displayLanguage, String formatLanguage,
String script, String displayScript, String formatScript,
String variant, String displayVariant, String formatVariant) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.c.locale;

import java.util.Collections;
import java.util.List;

import org.graalvm.nativeimage.c.CContext;

import com.oracle.svm.core.c.ProjectHeaderFile;

class LocaleDirectives implements CContext.Directives {
@Override
public List<String> getHeaderFiles() {
return Collections.singletonList(ProjectHeaderFile.resolve("com.oracle.svm.native.libchelper", "include/svm_locale.h"));
}
}
Loading