Skip to content

Commit e417151

Browse files
committed
[GR-71128] Expose argc and argv in graal_create_isolate_params_t.
PullRequest: graal/22596
2 parents 4c8887e + aeff2a7 commit e417151

13 files changed

Lines changed: 119 additions & 82 deletions

File tree

docs/reference-manual/native-image/C-API.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The C API is available when Native Image is built as a shared library and its de
1717
* passed to an entry point as the execution context.
1818
*/
1919
struct __graal_isolate_t;
20-
typedef struct _graal_isolate_t graal_isolate_t;
20+
typedef struct __graal_isolate_t graal_isolate_t;
2121

2222
/*
2323
* Structure representing a thread that is attached to an isolate. A pointer to
@@ -28,17 +28,38 @@ struct __graal_isolatethread_t;
2828
typedef struct __graal_isolatethread_t graal_isolatethread_t;
2929

3030
/* Parameters for the creation of a new isolate. */
31+
enum { __graal_create_isolate_params_version = 5 };
3132
struct __graal_create_isolate_params_t {
32-
/* for future use */
33+
/* Version of this struct. Set to __graal_create_isolate_params_version after zeroing this struct. */
34+
int version;
35+
36+
/* Fields introduced in version 1 */
37+
__graal_uword reserved_address_space_size; /* Size of virtual address space to reserve for the heap. */
38+
39+
/* Fields introduced in version 2. Internal usage, do not use. */
40+
const char *auxiliary_image_path; /* Path to an auxiliary image to load. */
41+
__graal_uword auxiliary_image_reserved_space_size; /* Reserved bytes for loading an auxiliary image. */
42+
43+
/* Fields introduced in version 3 */
44+
int argc; /* Number of char* argument strings in argv. */
45+
char **argv; /* Array of argument strings, parsed like command line arguments. */
46+
int pkey; /* Isolate protection key or domain. Internal usage, do not use. */
47+
48+
/* Fields introduced in version 4 */
49+
char ignore_unrecognized_args; /* Ignore unrecognized arguments in argv when 1. */
50+
char _reserved_4; /* Internal usage, do not use. */
51+
52+
/* Fields introduced in version 5 */
53+
char _reserved_5; /* Internal usage, do not use. */
3354
};
3455
typedef struct __graal_create_isolate_params_t graal_create_isolate_params_t;
3556

3657
/*
3758
* Create a new isolate, considering the passed parameters (which may be NULL).
3859
* Returns 0 on success, or a non-zero value on failure.
3960
* On success, the current thread is attached to the created isolate, and the
40-
* address of the isolate and the isolate thread structures is written to the
41-
* passed pointers if they are not NULL.
61+
* address of the isolate and the isolate thread are written to the passed pointers
62+
* if they are not NULL.
4263
*/
4364
int graal_create_isolate(graal_create_isolate_params_t* params, graal_isolate_t** isolate, graal_isolatethread_t** thread);
4465

@@ -59,9 +80,8 @@ int graal_attach_thread(graal_isolate_t* isolate, graal_isolatethread_t** thread
5980
graal_isolatethread_t* graal_get_current_thread(graal_isolate_t* isolate);
6081

6182
/*
62-
* Given an isolate thread structure, determines to which isolate it belongs and
63-
* returns the address of its isolate structure. If an error occurs, returns NULL
64-
* instead.
83+
* Given an isolate thread structure, determines to which isolate it belongs and returns
84+
* the address of its isolate structure. If an error occurs, returns NULL instead.
6585
*/
6686
graal_isolate_t* graal_get_isolate(graal_isolatethread_t* thread);
6787

@@ -74,10 +94,18 @@ graal_isolate_t* graal_get_isolate(graal_isolatethread_t* thread);
7494
int graal_detach_thread(graal_isolatethread_t* thread);
7595

7696
/*
77-
* Tears down the isolate of the passed (and still attached) isolate thread
97+
* Tears down the isolate of the passed (and still attached) isolate thread,
7898
* waiting for any attached threads to detach from it, then discards its objects,
7999
* threads, and any other state or context that is associated with it.
80100
* Returns 0 on success, or a non-zero value on failure.
101+
*
102+
* If this call blocks indefinitely, this means there are still Java threads running
103+
* which do not terminate after receiving the Thread.interrupt() event.
104+
* To prevent indefinite blocking, these threads should be cooperatively shut down
105+
* within Java before invoking this call.
106+
* To diagnose such issues, use the option '-R:TearDownWarningSeconds=<secs>' to detect
107+
* the threads that are still running.
108+
* This will print the stack traces of all threads that block tear-down.
81109
*/
82110
int graal_tear_down_isolate(graal_isolatethread_t* thread);
83111
```

docs/reference-manual/native-image/guides/troubleshoot-run-time-errors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ This can be avoided by eagerly checking for missing metadata.
3939

4040
For diagnosing shared libraries built with Native Image, you can either:
4141
* specify `-R:MissingRegistrationReportingMode=Exit` when building a native shared library;
42-
* or specify `-XX:MissingRegistrationReportingMode=Exit` when the isolate is created. `graal_create_isolate_params_t` has `argc (_reserved_1)` and `argv (_reserved_2)` fields that can be used to pass C-style command-line options at run time. However, note that both fields are currently not public APIs.
42+
* or specify `-XX:MissingRegistrationReportingMode=Exit` when the isolate is created. `graal_create_isolate_params_t` has `argc` and `argv` fields that can be used to pass C-style command-line options at run time.
4343

4444
### 2. Set java.home Explicitly
4545

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,25 @@ 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 = 4 };
57+
enum { __graal_create_isolate_params_version = 5 };
5858
struct __graal_create_isolate_params_t {
59-
int version; /* Version of this struct */
59+
/* Version of this struct. Set to __graal_create_isolate_params_version after zeroing this struct. */
60+
int version;
6061

6162
/* Fields introduced in version 1 */
62-
__graal_uword reserved_address_space_size; /* Size of address space to reserve */
63+
__graal_uword reserved_address_space_size; /* Size of virtual address space to reserve for the heap. */
6364

64-
/* Fields introduced in version 2 */
65+
/* Fields introduced in version 2. Internal usage, do not use. */
6566
const char *auxiliary_image_path; /* Path to an auxiliary image to load. */
6667
__graal_uword auxiliary_image_reserved_space_size; /* Reserved bytes for loading an auxiliary image. */
6768

6869
/* Fields introduced in version 3 */
69-
int _reserved_1; /* Internal usage, do not use. */
70-
char **_reserved_2; /* Internal usage, do not use. */
71-
int pkey; /* Isolate protection key or domain. */
70+
int argc; /* Number of char* argument strings in argv. */
71+
char **argv; /* Array of argument strings, parsed like command line arguments. */
72+
int pkey; /* Isolate protection key or domain. Internal usage, do not use. */
7273

7374
/* Fields introduced in version 4 */
74-
char _reserved_3; /* Internal usage, do not use. */
75+
char ignore_unrecognized_args; /* Ignore unrecognized arguments in argv when 1. */
7576
char _reserved_4; /* Internal usage, do not use. */
7677

7778
/* Fields introduced in version 5 */
@@ -136,7 +137,7 @@ typedef int (*graal_detach_thread_fn_t)(graal_isolatethread_t* thread);
136137
* the threads that are still running.
137138
* This will print the stack traces of all threads that block tear-down.
138139
*/
139-
typedef int (*graal_tear_down_isolate_fn_t)(graal_isolatethread_t* isolateThread);
140+
typedef int (*graal_tear_down_isolate_fn_t)(graal_isolatethread_t* thread);
140141

141142
/*
142143
* In the isolate of the passed isolate thread, detach all those threads that were
@@ -158,7 +159,7 @@ typedef int (*graal_tear_down_isolate_fn_t)(graal_isolatethread_t* isolateThread
158159
* the threads that are still running.
159160
* This will print the stack traces of all threads that block tear-down.
160161
*/
161-
typedef int (*graal_detach_all_threads_and_tear_down_isolate_fn_t)(graal_isolatethread_t* isolateThread);
162+
typedef int (*graal_detach_all_threads_and_tear_down_isolate_fn_t)(graal_isolatethread_t* thread);
162163

163164
#if defined(__cplusplus)
164165
}

espresso/src/com.oracle.truffle.espresso.mokapot/src/mokapot.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,12 +2228,10 @@ _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_CreateJavaVM(JavaVM **vm_ptr, void **pen
22282228
}
22292229

22302230
params.version = 4;
2231-
char ignore_unrecognized_arguments = initArgs->ignoreUnrecognized == JNI_TRUE ? 1 : 0;
2232-
char exit_on_arg_parse_fail = 0;
2233-
params._reserved_1 = isolate_argc;
2234-
params._reserved_2 = isolate_argv;
2235-
params._reserved_3 = ignore_unrecognized_arguments;
2236-
params._reserved_4 = exit_on_arg_parse_fail;
2231+
char ignore_unrecognized_args = initArgs->ignoreUnrecognized == JNI_TRUE ? 1 : 0;
2232+
params.argc = isolate_argc;
2233+
params.argv = isolate_argv;
2234+
params.ignore_unrecognized_args = ignore_unrecognized_args;
22372235

22382236
if (libjavavm->create_isolate(&params, &isolate, &thread) != 0) {
22392237
if (isolate_argv != NULL) free(isolate_argv);

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,25 @@ 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 = 4 };
32+
enum { __graal_create_isolate_params_version = 5 };
3333
struct __graal_create_isolate_params_t {
34-
int version; /* Version of this struct */
34+
/* Version of this struct. Set to __graal_create_isolate_params_version after zeroing this struct. */
35+
int version;
3536

3637
/* Fields introduced in version 1 */
37-
__graal_uword reserved_address_space_size; /* Size of address space to reserve */
38+
__graal_uword reserved_address_space_size; /* Size of virtual address space to reserve for the heap. */
3839

39-
/* Fields introduced in version 2 */
40+
/* Fields introduced in version 2. Internal usage, do not use. */
4041
const char *auxiliary_image_path; /* Path to an auxiliary image to load. */
4142
__graal_uword auxiliary_image_reserved_space_size; /* Reserved bytes for loading an auxiliary image. */
4243

4344
/* Fields introduced in version 3 */
44-
int _reserved_1; /* Internal usage, do not use. */
45-
char **_reserved_2; /* Internal usage, do not use. */
46-
int pkey; /* Isolate protection key or domain. */
45+
int argc; /* Number of char* argument strings in argv. */
46+
char **argv; /* Array of argument strings, parsed like command line arguments. */
47+
int pkey; /* Isolate protection key or domain. Internal usage, do not use. */
4748

4849
/* Fields introduced in version 4 */
49-
char _reserved_3; /* Internal usage, do not use. */
50+
char ignore_unrecognized_args; /* Ignore unrecognized arguments in argv when 1. */
5051
char _reserved_4; /* Internal usage, do not use. */
5152

5253
/* Fields introduced in version 5 */

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@
7777
import com.oracle.svm.core.thread.RecurringCallbackSupport;
7878
import com.oracle.svm.core.thread.VMThreads;
7979
import com.oracle.svm.core.thread.VMThreads.OSThreadHandle;
80-
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.AllAccess;
81-
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.NoLayeredCallbacks;
82-
import com.oracle.svm.shared.singletons.traits.SingletonLayeredInstallationKind.ApplicationLayerOnly;
83-
import com.oracle.svm.shared.singletons.traits.SingletonTraits;
8480
import com.oracle.svm.core.util.UserError;
8581
import com.oracle.svm.guest.staging.Uninterruptible;
8682
import com.oracle.svm.guest.staging.jdk.InternalVMMethod;
8783
import com.oracle.svm.sdk.staging.layeredimage.LayeredCompilationBehavior;
8884
import com.oracle.svm.sdk.staging.layeredimage.LayeredCompilationBehavior.Behavior;
85+
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.AllAccess;
86+
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.NoLayeredCallbacks;
87+
import com.oracle.svm.shared.singletons.traits.SingletonLayeredInstallationKind.ApplicationLayerOnly;
88+
import com.oracle.svm.shared.singletons.traits.SingletonTraits;
8989
import com.oracle.svm.shared.util.VMError;
9090
import com.oracle.svm.util.ClassUtil;
9191
import com.oracle.svm.util.ModuleSupport;
@@ -496,8 +496,8 @@ public static void enter(int paramArgc, CCharPointerPointer paramArgv) {
496496
args.setVersion(4);
497497
args.setArgc(paramArgc);
498498
args.setArgv(paramArgv);
499-
args.setIgnoreUnrecognizedArguments(false);
500-
args.setExitWhenArgumentParsingFails(true);
499+
args.setIgnoreUnrecognizedArgs(false);
500+
args.setForJavaMainCall(true);
501501

502502
int code = CEntryPointActions.enterCreateIsolate(args);
503503
if (code != CEntryPointErrors.NO_ERROR) {

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
package com.oracle.svm.core.c.function;
2626

27-
import org.graalvm.nativeimage.Isolates.CreateIsolateParameters;
2827
import org.graalvm.nativeimage.c.struct.CField;
2928
import org.graalvm.nativeimage.c.struct.CStruct;
3029
import org.graalvm.nativeimage.c.type.CCharPointer;
@@ -73,36 +72,30 @@ public interface CEntryPointCreateIsolateParameters extends PointerBase {
7372
@CField("pkey")
7473
int protectionKey();
7574

76-
/**
77-
* C arguments passed from the C main function into the isolate creation. These fields are not
78-
* public API, therefore they are named "reserved" in the C header files, and they are not
79-
* listed in the {@link CreateIsolateParameters} Java API to create isolates.
80-
*/
81-
82-
@CField("_reserved_1")
75+
@CField("argc")
8376
int getArgc();
8477

85-
@CField("_reserved_1")
78+
@CField("argc")
8679
void setArgc(int value);
8780

88-
@CField("_reserved_2")
81+
@CField("argv")
8982
CCharPointerPointer getArgv();
9083

91-
@CField("_reserved_2")
84+
@CField("argv")
9285
void setArgv(CCharPointerPointer value);
9386

9487
/* fields below: version 4 */
95-
@CField("_reserved_3")
96-
boolean getIgnoreUnrecognizedArguments();
88+
@CField("ignore_unrecognized_args")
89+
boolean getIgnoreUnrecognizedArgs();
9790

98-
@CField("_reserved_3")
99-
void setIgnoreUnrecognizedArguments(boolean value);
91+
@CField("ignore_unrecognized_args")
92+
void setIgnoreUnrecognizedArgs(boolean value);
10093

10194
@CField("_reserved_4")
102-
boolean getExitWhenArgumentParsingFails();
95+
boolean getForJavaMainCall();
10396

10497
@CField("_reserved_4")
105-
void setExitWhenArgumentParsingFails(boolean value);
98+
void setForJavaMainCall(boolean value);
10699

107100
/* fields below: version 5 */
108101
@CField("_reserved_5")

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
import org.graalvm.nativeimage.c.function.CEntryPoint;
3434
import org.graalvm.nativeimage.c.struct.CPointerTo;
3535
import org.graalvm.word.PointerBase;
36+
import org.graalvm.word.impl.Word;
3637

3738
import com.oracle.svm.core.SubstrateOptions;
38-
import com.oracle.svm.guest.staging.Uninterruptible;
3939
import com.oracle.svm.core.c.function.CEntryPointOptions.NoEpilogue;
4040
import com.oracle.svm.core.c.function.CEntryPointOptions.NoPrologue;
4141
import com.oracle.svm.core.thread.VMThreads;
42-
import org.graalvm.word.impl.Word;
42+
import com.oracle.svm.guest.staging.Uninterruptible;
4343

4444
@CHeader(value = GraalIsolateHeader.class)
4545
public final class CEntryPointNativeFunctions {
@@ -178,8 +178,8 @@ Tears down the isolate of the passed (and still attached) isolate thread,
178178
THREAD_TERMINATION_NOTE,
179179
})
180180
@CEntryPointOptions(prologue = NoPrologue.class, epilogue = NoEpilogue.class, nameTransformation = NameTransformation.class)
181-
public static int tearDownIsolate(IsolateThread isolateThread) {
182-
int result = CEntryPointActions.enter(isolateThread);
181+
public static int tearDownIsolate(IsolateThread thread) {
182+
int result = CEntryPointActions.enter(thread);
183183
if (result != 0) {
184184
return result;
185185
}
@@ -202,8 +202,8 @@ or this will cause entirely undefined (and likely fatal) behavior.
202202
THREAD_TERMINATION_NOTE,
203203
})
204204
@CEntryPointOptions(prologue = NoPrologue.class, epilogue = NoEpilogue.class, nameTransformation = NameTransformation.class)
205-
public static int detachAllThreadsAndTearDownIsolate(IsolateThread isolateThread) {
206-
int result = CEntryPointActions.enter(isolateThread);
205+
public static int detachAllThreadsAndTearDownIsolate(IsolateThread thread) {
206+
int result = CEntryPointActions.enter(thread);
207207
if (result != 0) {
208208
return result;
209209
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.graalvm.nativeimage.c.type.CCharPointerPointer;
3636
import org.graalvm.nativeimage.c.type.CTypeConversion;
3737
import org.graalvm.nativeimage.impl.IsolateSupport;
38+
import org.graalvm.word.impl.Word;
3839

3940
import com.oracle.svm.core.SubstrateOptions;
4041
import com.oracle.svm.core.c.function.CEntryPointNativeFunctions.IsolateThreadPointer;
@@ -49,7 +50,6 @@
4950
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.SingleLayer;
5051
import com.oracle.svm.shared.singletons.traits.SingletonLayeredInstallationKind.InitialLayerOnly;
5152
import com.oracle.svm.shared.singletons.traits.SingletonTraits;
52-
import org.graalvm.word.impl.Word;
5353

5454
@AutomaticallyRegisteredImageSingleton(IsolateSupport.class)
5555
@SingletonTraits(access = RuntimeAccessOnly.class, layeredCallbacks = SingleLayer.class, layeredInstallationKind = InitialLayerOnly.class)
@@ -111,8 +111,8 @@ public static IsolateThread createIsolate(CreateIsolateParameters parameters, bo
111111
params.setAuxiliaryImagePath(auxImagePath.get());
112112
params.setAuxiliaryImageReservedSpaceSize(parameters.getAuxiliaryImageReservedSpaceSize());
113113
params.setVersion(5);
114-
params.setIgnoreUnrecognizedArguments(false);
115-
params.setExitWhenArgumentParsingFails(false);
114+
params.setIgnoreUnrecognizedArgs(false);
115+
params.setForJavaMainCall(false);
116116
params.setArgc(argc);
117117
params.setArgv(argv);
118118
params.setIsCompilationIsolate(compilationIsolate);

0 commit comments

Comments
 (0)