From ce28fae6837f034e756251dfbca6812412d5d9ca Mon Sep 17 00:00:00 2001 From: Tom Shull Date: Wed, 16 Mar 2022 21:03:34 +0100 Subject: [PATCH] Implement JVM_GetTemporaryDirectory. --- .../darwin/DarwinSystemPropertiesSupport.java | 5 ++--- .../linux/LinuxSystemPropertiesSupport.java | 2 +- .../WindowsSystemPropertiesSupport.java | 2 +- .../jdk/PlatformNativeLibrarySupport.java | 1 + .../svm/core/jdk/SystemPropertiesSupport.java | 21 ++++++++++++------ ...rget_jdk_internal_util_StaticProperty.java | 2 +- .../src/JvmFuncs.c | 22 +++++++++++++++++++ .../src/JvmFuncs.c | 9 ++++++++ 8 files changed, 51 insertions(+), 13 deletions(-) diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSystemPropertiesSupport.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSystemPropertiesSupport.java index 1567f7380e12..8adf8d44c61a 100644 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSystemPropertiesSupport.java +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSystemPropertiesSupport.java @@ -47,7 +47,7 @@ public class DarwinSystemPropertiesSupport extends PosixSystemPropertiesSupport { @Override - protected String tmpdirValue() { + protected String javaIoTmpdirValue() { /* Darwin has a per-user temp dir */ int buflen = Limits.PATH_MAX(); CCharPointer tmpPath = StackValue.get(buflen); @@ -56,8 +56,7 @@ protected String tmpdirValue() { return CTypeConversion.toJavaString(tmpPath); } else { /* - * Default as defined in JDK source/jdk/src/solaris/native/java/lang/java_props_md.c - * line 135. + * Default as defined in JDK src/java.base/unix/native/libjava/java_props_md.c line 90. */ return "/var/tmp"; } diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/linux/LinuxSystemPropertiesSupport.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/linux/LinuxSystemPropertiesSupport.java index 4263f54b27a4..dc556c73017f 100644 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/linux/LinuxSystemPropertiesSupport.java +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/linux/LinuxSystemPropertiesSupport.java @@ -40,7 +40,7 @@ public class LinuxSystemPropertiesSupport extends PosixSystemPropertiesSupport { @Override - protected String tmpdirValue() { + protected String javaIoTmpdirValue() { /* * The initial value of `java.io.tmpdir` is hard coded in libjava when building the JDK. So * to be completely correct, we would have to use the value from libjava, but since it is diff --git a/substratevm/src/com.oracle.svm.core.windows/src/com/oracle/svm/core/windows/WindowsSystemPropertiesSupport.java b/substratevm/src/com.oracle.svm.core.windows/src/com/oracle/svm/core/windows/WindowsSystemPropertiesSupport.java index c8d7aec46e1e..b550005635a3 100644 --- a/substratevm/src/com.oracle.svm.core.windows/src/com/oracle/svm/core/windows/WindowsSystemPropertiesSupport.java +++ b/substratevm/src/com.oracle.svm.core.windows/src/com/oracle/svm/core/windows/WindowsSystemPropertiesSupport.java @@ -125,7 +125,7 @@ protected String userDirValue() { } @Override - protected String tmpdirValue() { + protected String javaIoTmpdirValue() { int maxLength = WinBase.MAX_PATH + 1; WCharPointer tmpdir = StackValue.get(maxLength, WCharPointer.class); int length = FileAPI.GetTempPathW(maxLength, tmpdir); diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/PlatformNativeLibrarySupport.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/PlatformNativeLibrarySupport.java index 294c5cad9cbf..61c0fb049681 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/PlatformNativeLibrarySupport.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/PlatformNativeLibrarySupport.java @@ -69,6 +69,7 @@ public abstract class PlatformNativeLibrarySupport { "jdk_internal_misc", "jdk_internal_util", "jdk_internal_jimage", + "jdk_internal_vm", "jdk_net", "sun_invoke", "sun_launcher", diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SystemPropertiesSupport.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SystemPropertiesSupport.java index b93af2b7a5a6..6dd43ed34cd7 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SystemPropertiesSupport.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SystemPropertiesSupport.java @@ -38,6 +38,7 @@ import com.oracle.svm.core.VM; import com.oracle.svm.core.config.ConfigurationValues; +import com.oracle.svm.core.util.VMError; /** * This class maintains the system properties at run time. @@ -123,7 +124,7 @@ protected SystemPropertiesSupport() { lazyRuntimeValues.put("user.name", this::userName); lazyRuntimeValues.put("user.home", this::userHome); lazyRuntimeValues.put("user.dir", this::userDir); - lazyRuntimeValues.put("java.io.tmpdir", this::tmpDir); + lazyRuntimeValues.put("java.io.tmpdir", this::javaIoTmpDir); lazyRuntimeValues.put("java.library.path", this::javaLibraryPath); lazyRuntimeValues.put("os.version", this::osVersionValue); @@ -249,13 +250,13 @@ String userDir() { return cachedUserDir; } - private String cachedtmpDir; + private String cachedJavaIoTmpdir; - String tmpDir() { - if (cachedtmpDir == null) { - cachedtmpDir = tmpdirValue(); + String javaIoTmpDir() { + if (cachedJavaIoTmpdir == null) { + cachedJavaIoTmpdir = javaIoTmpdirValue(); } - return cachedtmpDir; + return cachedJavaIoTmpdir; } private String cachedJavaLibraryPath; @@ -275,7 +276,13 @@ String javaLibraryPath() { protected abstract String userDirValue(); - protected abstract String tmpdirValue(); + protected String javaIoTmpdirValue() { + return tmpdirValue(); + } + + protected String tmpdirValue() { + throw VMError.unimplemented(); + } protected String javaLibraryPathValue() { /* Default implementation. */ diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_util_StaticProperty.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_util_StaticProperty.java index cabbe05e3982..f2192a018f0f 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_util_StaticProperty.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_util_StaticProperty.java @@ -65,7 +65,7 @@ private static String userName() { @Substitute @TargetElement(onlyWith = JDK17OrLater.class) private static String javaIoTmpDir() { - return ImageSingletons.lookup(SystemPropertiesSupport.class).tmpDir(); + return ImageSingletons.lookup(SystemPropertiesSupport.class).javaIoTmpDir(); } @Substitute diff --git a/substratevm/src/com.oracle.svm.native.jvm.posix/src/JvmFuncs.c b/substratevm/src/com.oracle.svm.native.jvm.posix/src/JvmFuncs.c index 33982e90dc09..2e6ced8dd59e 100644 --- a/substratevm/src/com.oracle.svm.native.jvm.posix/src/JvmFuncs.c +++ b/substratevm/src/com.oracle.svm.native.jvm.posix/src/JvmFuncs.c @@ -38,6 +38,7 @@ #include #include #include +#include #include @@ -319,6 +320,27 @@ JNIEXPORT jobject JNICALL JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject acti return NULL; } +#ifdef __APPLE__ +char temp_path_storage[PATH_MAX]; +JNIEXPORT jstring JNICALL JVM_GetTemporaryDirectory(JNIEnv *env) { + // see os_bsd.cpp line 910 + static char *temp_path = NULL; + if (temp_path == NULL) { + int pathSize = confstr(_CS_DARWIN_USER_TEMP_DIR, temp_path_storage, PATH_MAX); + if (pathSize == 0 || pathSize > PATH_MAX) { + strlcpy(temp_path_storage, "/tmp/", sizeof(temp_path_storage)); + } + temp_path = temp_path_storage; + } + return (*env)->NewStringUTF(env, temp_path); +} +#else +JNIEXPORT jstring JNICALL JVM_GetTemporaryDirectory(JNIEnv *env) { + // see os_linux.cpp line 1380 + return (*env)->NewStringUTF(env, "/tmp"); +} +#endif /* __APPLE__ */ + JNIEXPORT jobject JNICALL Java_sun_nio_ch_sctp_SctpChannelImpl_initIDs(JNIEnv *env) { (*env)->FatalError(env, "Currently SCTP not supported for native-images"); return NULL; diff --git a/substratevm/src/com.oracle.svm.native.jvm.windows/src/JvmFuncs.c b/substratevm/src/com.oracle.svm.native.jvm.windows/src/JvmFuncs.c index 2cff045f0867..47c71640fde3 100644 --- a/substratevm/src/com.oracle.svm.native.jvm.windows/src/JvmFuncs.c +++ b/substratevm/src/com.oracle.svm.native.jvm.windows/src/JvmFuncs.c @@ -242,6 +242,15 @@ JNIEXPORT jobject JNICALL JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject acti return NULL; } +JNIEXPORT jstring JNICALL JVM_GetTemporaryDirectory(JNIEnv *env) { + // see os_windows.cpp line 1367 + static char path_buf[MAX_PATH]; + if (GetTempPath(MAX_PATH, path_buf) <= 0) { + path_buf[0] = '\0'; + } + return (*env)->NewStringUTF(env, path_buf); +} + jboolean VerifyFixClassname(char *utf_name) { fprintf(stderr, "VerifyFixClassname(%s) called: Unimplemented\n", utf_name); abort();