Skip to content

Commit abd8fa1

Browse files
[GR-37635] Fix handling of isolate and JNI arguments.
PullRequest: graal/11439
2 parents bb8e6c5 + de45e89 commit abd8fa1

File tree

17 files changed

+229
-121
lines changed

17 files changed

+229
-121
lines changed

espresso/src/com.oracle.truffle.espresso.mokapot/include/graal_isolate_dynamic.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ typedef unsigned long __graal_uword;
5454
#define NEW_PROTECTION_DOMAIN -1
5555

5656
/* Parameters for the creation of a new isolate. */
57-
enum { __graal_create_isolate_params_version = 1 };
57+
enum { __graal_create_isolate_params_version = 4 };
5858
struct __graal_create_isolate_params_t {
5959
int version; /* Version of this struct */
6060

@@ -69,6 +69,10 @@ struct __graal_create_isolate_params_t {
6969
int _reserved_1; /* Internal usage, do not use. */
7070
char **_reserved_2; /* Internal usage, do not use. */
7171
int pkey; /* Isolate protection key or domain. */
72+
73+
/* Fields introduced in version 4 */
74+
char _reserved_3; /* Internal usage, do not use. */
75+
char _reserved_4; /* Internal usage, do not use. */
7276
};
7377
typedef struct __graal_create_isolate_params_t graal_create_isolate_params_t;
7478

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ public UnsignedWord strlen(CCharPointer str) {
8989
return PosixLibC.strlen(str);
9090
}
9191

92+
@Override
93+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
94+
public int strcmp(CCharPointer s1, CCharPointer s2) {
95+
return PosixLibC.strcmp(s1, s2);
96+
}
97+
9298
@Override
9399
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
94100
public int isdigit(int c) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public UnsignedWord strlen(CCharPointer str) {
101101
return WindowsLibC.strlen(str);
102102
}
103103

104+
@Override
105+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
106+
public int strcmp(CCharPointer s1, CCharPointer s2) {
107+
return WindowsLibC.strcmp(s1, s2);
108+
}
109+
104110
@Override
105111
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
106112
public int isdigit(int c) {

substratevm/src/com.oracle.svm.core/headers/graal_isolate.preamble

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef unsigned long __graal_uword;
2929
#define NEW_PROTECTION_DOMAIN -1
3030

3131
/* Parameters for the creation of a new isolate. */
32-
enum { __graal_create_isolate_params_version = 1 };
32+
enum { __graal_create_isolate_params_version = 4 };
3333
struct __graal_create_isolate_params_t {
3434
int version; /* Version of this struct */
3535

@@ -44,5 +44,9 @@ struct __graal_create_isolate_params_t {
4444
int _reserved_1; /* Internal usage, do not use. */
4545
char **_reserved_2; /* Internal usage, do not use. */
4646
int pkey; /* Isolate protection key or domain. */
47+
48+
/* Fields introduced in version 4 */
49+
char _reserved_3; /* Internal usage, do not use. */
50+
char _reserved_4; /* Internal usage, do not use. */
4751
};
4852
typedef struct __graal_create_isolate_params_t graal_create_isolate_params_t;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ public static void parse(CEntryPointCreateIsolateParameters parameters, CLongPoi
155155
}
156156

157157
CLongPointer numericValue = StackValue.get(Long.BYTES);
158-
for (int i = 0; i < argc; i++) {
158+
// Ignore the first argument as it represents the executable file name.
159+
for (int i = 1; i < argc; i++) {
159160
CCharPointer arg = argv.read(i);
160161
if (arg.isNonNull()) {
161162
CCharPointer tail = matchPrefix(arg);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public String getJavaCommand() {
107107
public List<String> getInputArguments() {
108108
CEntryPointCreateIsolateParameters args = MAIN_ISOLATE_PARAMETERS.get();
109109
if (args.getArgv().isNonNull() && args.getArgc() > 0) {
110-
String[] unmodifiedArgs = SubstrateUtil.getArgs(args.getArgc(), args.getArgv());
110+
String[] unmodifiedArgs = SubstrateUtil.convertCToJavaArgs(args.getArgc(), args.getArgv());
111111
List<String> inputArgs = new ArrayList<>(Arrays.asList(unmodifiedArgs));
112112

113113
if (mainArgs != null) {
@@ -271,9 +271,11 @@ private static class EnterCreateIsolateWithCArgumentsPrologue implements CEntryP
271271
@Uninterruptible(reason = "prologue")
272272
public static void enter(int paramArgc, CCharPointerPointer paramArgv) {
273273
CEntryPointCreateIsolateParameters args = MAIN_ISOLATE_PARAMETERS.get();
274-
args.setVersion(3);
274+
args.setVersion(4);
275275
args.setArgc(paramArgc);
276276
args.setArgv(paramArgv);
277+
args.setIgnoreUnrecognizedArguments(false);
278+
args.setExitWhenArgumentParsingFails(true);
277279

278280
int code = CEntryPointActions.enterCreateIsolate(args);
279281
if (code != CEntryPointErrors.NO_ERROR) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public static FileDescriptor getFileDescriptor(FileOutputStream out) {
161161
*
162162
* @return the command line argument strings in a Java string array.
163163
*/
164-
public static String[] getArgs(int argc, CCharPointerPointer argv) {
164+
public static String[] convertCToJavaArgs(int argc, CCharPointerPointer argv) {
165165
String[] args = new String[argc - 1];
166166
for (int i = 1; i < argc; ++i) {
167167
args[i - 1] = CTypeConversion.toJavaString(argv.read(i));

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/c/function/CEntryPointCreateIsolateParameters.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,17 @@ public interface CEntryPointCreateIsolateParameters extends PointerBase {
9090

9191
@CField("_reserved_2")
9292
void setArgv(CCharPointerPointer value);
93+
94+
/* fields below: version 4 */
95+
@CField("_reserved_3")
96+
boolean getIgnoreUnrecognizedArguments();
97+
98+
@CField("_reserved_3")
99+
void setIgnoreUnrecognizedArguments(boolean value);
100+
101+
@CField("_reserved_4")
102+
boolean getExitWhenArgumentParsingFails();
103+
104+
@CField("_reserved_4")
105+
void setExitWhenArgumentParsingFails(boolean value);
93106
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/c/function/CEntryPointErrors.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ private CEntryPointErrors() {
119119
@Description("The auxiliary image was built from a different primary image.") //
120120
public static final int AUX_IMAGE_PRIMARY_IMAGE_MISMATCH = 21;
121121

122+
@Description("The isolate arguments could not be parsed.") //
123+
public static final int ARGUMENT_PARSING_FAILED = 22;
124+
122125
public static String getDescription(int code) {
123126
String result = null;
124127
if (code >= 0 && code < DESCRIPTIONS.length) {

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/c/function/IsolateSupportImpl.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ public IsolateThread createIsolate(CreateIsolateParameters parameters) throws Is
7171
params.setReservedSpaceSize(parameters.getReservedAddressSpaceSize());
7272
params.setAuxiliaryImagePath(auxImagePath.get());
7373
params.setAuxiliaryImageReservedSpaceSize(parameters.getAuxiliaryImageReservedSpaceSize());
74-
params.setVersion(3);
74+
params.setVersion(4);
75+
params.setIgnoreUnrecognizedArguments(false);
76+
params.setExitWhenArgumentParsingFails(false);
7577

7678
if (MemoryProtectionProvider.isAvailable()) {
77-
7879
try {
7980
int pkey = MemoryProtectionProvider.singleton().asProtectionKey(parameters.getProtectionDomain());
8081
params.setProtectionKey(pkey);
@@ -86,21 +87,28 @@ public IsolateThread createIsolate(CreateIsolateParameters parameters) throws Is
8687
}
8788

8889
// Prepare argc and argv.
89-
List<String> args = parameters.getArguments();
90-
int argc = args.size();
91-
params.setArgc(argc);
92-
params.setArgv(WordFactory.nullPointer());
90+
int argc = 0;
91+
CCharPointerPointer argv = WordFactory.nullPointer();
9392

93+
List<String> args = parameters.getArguments();
9494
CTypeConversion.CCharPointerHolder[] pointerHolders = null;
95-
if (argc > 0) {
96-
CCharPointerPointer argv = UnmanagedMemory.malloc(SizeOf.unsigned(CCharPointerPointer.class).multiply(argc));
97-
pointerHolders = new CTypeConversion.CCharPointerHolder[argc];
98-
for (int i = 0; i < argc; i++) {
95+
if (!args.isEmpty()) {
96+
int isolateArgCount = args.size();
97+
98+
// Internally, we use C-style arguments, i.e., the first argument is reserved for
99+
// the name of the binary. We use null when isolates are created manually.
100+
argc = isolateArgCount + 1;
101+
argv = UnmanagedMemory.malloc(SizeOf.unsigned(CCharPointerPointer.class).multiply(argc));
102+
argv.write(0, WordFactory.nullPointer());
103+
104+
pointerHolders = new CTypeConversion.CCharPointerHolder[isolateArgCount];
105+
for (int i = 0; i < isolateArgCount; i++) {
99106
CTypeConversion.CCharPointerHolder ph = pointerHolders[i] = CTypeConversion.toCString(args.get(i));
100-
argv.write(i, ph.get());
107+
argv.write(i + 1, ph.get());
101108
}
102-
params.setArgv(argv);
103109
}
110+
params.setArgc(argc);
111+
params.setArgv(argv);
104112

105113
// Try to create the isolate.
106114
IsolateThreadPointer isolateThreadPtr = StackValue.get(IsolateThreadPointer.class);

0 commit comments

Comments
 (0)