Skip to content

Commit edcef61

Browse files
committed
[GR-2617] Replace/remove calls to C functions that are not thread-safe or considered unsafe.
PullRequest: graal/23261
2 parents c104f50 + 04ac9f7 commit edcef61

5 files changed

Lines changed: 22 additions & 24 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public void exec(Path executable, String[] args) {
116116
try (CTypeConversion.CCharPointerHolder pathHolder = CTypeConversion.toCString(executable.toString());
117117
CTypeConversion.CCharPointerPointerHolder argvHolder = CTypeConversion.toCStrings(args)) {
118118
if (Unistd.execv(pathHolder.get(), argvHolder.get()) != 0) {
119-
String msg = PosixUtils.lastErrorString("Executing " + executable + " with arguments " + String.join(" ", args) + " failed");
120-
throw new RuntimeException(msg);
119+
String errnoString = PosixUtils.strerrorErrno();
120+
throw new RuntimeException(String.format("Executing %s with arguments %s failed: %s", executable, String.join(" ", args), errnoString));
121121
}
122122
}
123123
}
@@ -138,9 +138,9 @@ public void exec(Path executable, String[] args, Map<String, String> env) {
138138
CTypeConversion.CCharPointerPointerHolder argvHolder = CTypeConversion.toCStrings(args);
139139
CTypeConversion.CCharPointerPointerHolder envpHolder = CTypeConversion.toCStrings(envArray)) {
140140
if (Unistd.execve(pathHolder.get(), argvHolder.get(), envpHolder.get()) != 0) {
141+
String errnoString = PosixUtils.strerrorErrno();
141142
String envString = env.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(" "));
142-
String msg = PosixUtils.lastErrorString("Executing " + executable + " with arguments " + String.join(" ", args) + " and environment " + envString + " failed");
143-
throw new RuntimeException(msg);
143+
throw new RuntimeException(String.format("Executing %s with arguments %s and environment %s failed: %s", executable, String.join(" ", args), envString, errnoString));
144144
}
145145
}
146146
}

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

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

27-
import static com.oracle.svm.guest.staging.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;
2827
import static com.oracle.svm.core.jdk.Target_jdk_internal_misc_Signal.Constants.DEFAULT_HANDLER;
2928
import static com.oracle.svm.core.jdk.Target_jdk_internal_misc_Signal.Constants.DISPATCH_HANDLER;
3029
import static com.oracle.svm.core.jdk.Target_jdk_internal_misc_Signal.Constants.ERROR_HANDLER;
3130
import static com.oracle.svm.core.jdk.Target_jdk_internal_misc_Signal.Constants.IGNORE_HANDLER;
31+
import static com.oracle.svm.guest.staging.Uninterruptible.CALLED_FROM_UNINTERRUPTIBLE_CODE;
3232

3333
import java.util.HashMap;
3434
import java.util.List;
@@ -50,7 +50,6 @@
5050

5151
import com.oracle.svm.core.SubstrateOptions;
5252
import com.oracle.svm.core.SubstrateOptions.ConcealedOptions;
53-
import com.oracle.svm.guest.staging.Uninterruptible;
5453
import com.oracle.svm.core.c.CGlobalData;
5554
import com.oracle.svm.core.c.CGlobalDataFactory;
5655
import com.oracle.svm.core.c.function.CEntryPointOptions;
@@ -69,7 +68,6 @@
6968
import com.oracle.svm.core.log.Log;
7069
import com.oracle.svm.core.monitor.MonitorSupport;
7170
import com.oracle.svm.core.posix.headers.CSunMiscSignal;
72-
import com.oracle.svm.core.posix.headers.Errno;
7371
import com.oracle.svm.core.posix.headers.Signal;
7472
import com.oracle.svm.core.posix.headers.Signal.SignalDispatcher;
7573
import com.oracle.svm.core.posix.headers.Signal.SignalEnum;
@@ -82,6 +80,7 @@
8280
import com.oracle.svm.shared.singletons.traits.SingletonLayeredInstallationKind.InitialLayerOnly;
8381
import com.oracle.svm.shared.singletons.traits.SingletonTraits;
8482
import com.oracle.svm.core.util.VMError;
83+
import com.oracle.svm.guest.staging.Uninterruptible;
8584

8685
import jdk.graal.compiler.api.replacements.Fold;
8786

@@ -207,7 +206,7 @@ private void ensureInitialized() throws IllegalArgumentException {
207206
throw new IllegalArgumentException("Java signal handler mechanism is already used by another isolate.");
208207
} else {
209208
int errno = LibC.errno();
210-
Log.log().string("CSunMiscSignal.open() failed.").string(" errno: ").signed(errno).string(" ").string(Errno.strerror(errno)).newline();
209+
Log.log().string("CSunMiscSignal.open() failed.").string(" errno: ").signed(errno).string(" ").string(PosixUtils.strerror(errno)).newline();
211210
throw VMError.shouldNotReachHere("CSunMiscSignal.open() failed.");
212211
}
213212
}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,23 @@ public static void setFD(FileDescriptor descriptor, int fd) {
8080
SubstrateUtil.cast(descriptor, Target_java_io_FileDescriptor.class).fd = fd;
8181
}
8282

83-
/** Return the error string for the last error, or a default message. */
84-
public static String lastErrorString(String defaultMsg) {
83+
public static String strerrorErrno() {
8584
int errno = LibC.errno();
86-
return errorString(errno, defaultMsg);
85+
return strerror(errno);
8786
}
8887

89-
/** Return the error string for the given error number, or a default message. */
90-
public static String errorString(int errno, String defaultMsg) {
91-
String result = "";
92-
if (errno != 0) {
93-
result = CTypeConversion.toJavaString(Errno.strerror(errno));
88+
public static String strerror(int errno) {
89+
UnsignedWord size = Word.unsigned(1024);
90+
CCharPointer buf = NullableNativeMemory.malloc(size, NmtCategory.Internal);
91+
if (buf.isNull()) {
92+
return "[could not allocate memory for error string]";
93+
}
94+
try {
95+
CCharPointer cstr = Errno.strerror_r(errno, buf, size);
96+
return CTypeConversion.toJavaString(cstr);
97+
} finally {
98+
NullableNativeMemory.free(buf);
9499
}
95-
return result.length() != 0 ? result : defaultMsg;
96100
}
97101

98102
public static int getpid() {

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Errno.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.graalvm.nativeimage.c.constant.CConstant;
2929
import org.graalvm.nativeimage.c.function.CFunction;
3030
import org.graalvm.nativeimage.c.type.CCharPointer;
31+
import org.graalvm.word.UnsignedWord;
3132

3233
// Checkstyle: stop
3334

@@ -65,5 +66,5 @@ public class Errno {
6566
public static native int EINVAL();
6667

6768
@CFunction
68-
public static native CCharPointer strerror(int errnum);
69+
public static native CCharPointer strerror_r(int errnum, CCharPointer buf, UnsignedWord size);
6970
}

substratevm/src/com.oracle.svm.core.windows/src/com/oracle/svm/core/windows/headers/WindowsLibC.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ public class WindowsLibC {
7777
@CFunction(transition = CFunction.Transition.NO_TRANSITION)
7878
public static native int strcmp(PointerBase s1, PointerBase s2);
7979

80-
@CFunction(transition = CFunction.Transition.NO_TRANSITION)
81-
public static native CCharPointer strcpy(CCharPointer dst, CCharPointer src);
82-
83-
@CFunction(transition = CFunction.Transition.NO_TRANSITION)
84-
public static native CCharPointer strncpy(CCharPointer dst, CCharPointer src, UnsignedWord len);
85-
8680
@CFunction(value = "_strdup", transition = CFunction.Transition.NO_TRANSITION)
8781
public static native CCharPointer strdup(CCharPointer src);
8882

0 commit comments

Comments
 (0)