Skip to content

Commit 015a8f7

Browse files
committed
[GR-59499] Fix System.getProperties() when called from virtual thread.
PullRequest: graal/19336
2 parents c006c5d + aade614 commit 015a8f7

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import java.io.FileDescriptor;
3131

3232
import org.graalvm.nativeimage.Platform;
33-
import org.graalvm.nativeimage.StackValue;
33+
import org.graalvm.nativeimage.c.struct.SizeOf;
3434
import org.graalvm.nativeimage.c.type.CCharPointer;
3535
import org.graalvm.nativeimage.c.type.CIntPointer;
3636
import org.graalvm.nativeimage.c.type.CTypeConversion;
@@ -335,15 +335,17 @@ private static String getUserNameOrDir(int uid, boolean name) {
335335
bufSize = 1024;
336336
}
337337

338-
/* Retrieve the username and copy it to a String object. */
339-
CCharPointer pwBuf = NullableNativeMemory.malloc(WordFactory.unsigned(bufSize), NmtCategory.Internal);
340-
if (pwBuf.isNull()) {
338+
/* Does not use StackValue because it is not safe to use in virtual threads. */
339+
UnsignedWord allocSize = WordFactory.unsigned(SizeOf.get(passwdPointer.class) + SizeOf.get(passwd.class) + bufSize);
340+
Pointer alloc = NullableNativeMemory.malloc(allocSize, NmtCategory.Internal);
341+
if (alloc.isNull()) {
341342
return null;
342343
}
343344

344345
try {
345-
passwd pwent = StackValue.get(passwd.class);
346-
passwdPointer p = StackValue.get(passwdPointer.class);
346+
passwdPointer p = (passwdPointer) alloc;
347+
passwd pwent = (passwd) ((Pointer) p).add(SizeOf.get(passwdPointer.class));
348+
CCharPointer pwBuf = (CCharPointer) ((Pointer) pwent).add(SizeOf.get(passwd.class));
347349
int code = Pwd.getpwuid_r(uid, pwent, pwBuf, WordFactory.unsigned(bufSize), p);
348350
if (code != 0) {
349351
return null;
@@ -361,7 +363,7 @@ private static String getUserNameOrDir(int uid, boolean name) {
361363

362364
return CTypeConversion.toJavaString(pwName);
363365
} finally {
364-
NullableNativeMemory.free(pwBuf);
366+
NullableNativeMemory.free(alloc);
365367
}
366368
}
367369
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.graalvm.nativeimage.c.struct.CPointerTo;
3131
import org.graalvm.nativeimage.c.struct.CStruct;
3232
import org.graalvm.nativeimage.c.type.CCharPointer;
33-
import org.graalvm.word.Pointer;
3433
import org.graalvm.word.PointerBase;
3534
import org.graalvm.word.UnsignedWord;
3635

@@ -55,7 +54,7 @@ public interface passwd extends PointerBase {
5554
}
5655

5756
@CPointerTo(passwd.class)
58-
public interface passwdPointer extends Pointer {
57+
public interface passwdPointer extends PointerBase {
5958
passwd read();
6059
}
6160

0 commit comments

Comments
 (0)