Skip to content

Commit 71d9c5b

Browse files
committed
Add wasm_runtime_get_wasi_exit_code
Preserve wasi exit code so that an embedder can query it.
1 parent c4defb8 commit 71d9c5b

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed

core/iwasm/common/wasm_runtime_common.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2675,17 +2675,18 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
26752675
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
26762676
int stderrfd, char *error_buf, uint32 error_buf_size)
26772677
{
2678-
uvwasi_t *uvwasi = NULL;
2678+
WASIContext *ctx;
2679+
uvwasi_t *uvwasi;
26792680
uvwasi_options_t init_options;
26802681
const char **envp = NULL;
26812682
uint64 total_size;
26822683
uint32 i;
26832684
bool ret = false;
26842685

2685-
uvwasi = runtime_malloc(sizeof(uvwasi_t), module_inst, error_buf,
2686-
error_buf_size);
2687-
if (!uvwasi)
2686+
ctx = runtime_malloc(sizeof(*ctx), module_inst, error_buf, error_buf_size);
2687+
if (!ctx)
26882688
return false;
2689+
uvwasi = &ctx->uvwasi;
26892690

26902691
/* Setup the initialization options */
26912692
uvwasi_options_init(&init_options);
@@ -2733,7 +2734,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
27332734
goto fail;
27342735
}
27352736

2736-
wasm_runtime_set_wasi_ctx(module_inst, uvwasi);
2737+
wasm_runtime_set_wasi_ctx(module_inst, ctx);
27372738

27382739
ret = true;
27392740

@@ -2863,12 +2864,19 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
28632864
WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
28642865

28652866
if (wasi_ctx) {
2866-
uvwasi_destroy(wasi_ctx);
2867+
uvwasi_destroy(&wasi_ctx->uvwasi);
28672868
wasm_runtime_free(wasi_ctx);
28682869
}
28692870
}
28702871
#endif
28712872

2873+
uint32_t
2874+
wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst)
2875+
{
2876+
WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
2877+
return wasi_ctx->exit_code;
2878+
}
2879+
28722880
WASIContext *
28732881
wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst_comm)
28742882
{

core/iwasm/common/wasm_runtime_common.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,13 @@ typedef struct WASIContext {
369369
char **argv_list;
370370
char *env_buf;
371371
char **env_list;
372+
uint32_t exit_code;
372373
} WASIContext;
373374
#else
374-
typedef uvwasi_t WASIContext;
375+
typedef struct WASIContext {
376+
uvwasi_t uvwasi;
377+
uint32_t exit_code;
378+
} WASIContext;
375379
#endif
376380
#endif
377381

@@ -768,6 +772,10 @@ wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst);
768772
WASM_RUNTIME_API_EXTERN WASMFunctionInstanceCommon *
769773
wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
770774

775+
/* See wasm_export.h for description */
776+
WASM_RUNTIME_API_EXTERN uint32_t
777+
wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst);
778+
771779
bool
772780
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
773781
const char *dir_list[], uint32 dir_count,

core/iwasm/include/wasm_export.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,18 @@ wasm_runtime_is_wasi_mode(wasm_module_inst_t module_inst);
450450
WASM_RUNTIME_API_EXTERN wasm_function_inst_t
451451
wasm_runtime_lookup_wasi_start_function(wasm_module_inst_t module_inst);
452452

453+
/**
454+
* Get WASI exit code.
455+
*
456+
* After a WASI command completed its execution, an embedder can
457+
* call this function to get its exit code. (that is, the value given
458+
* to proc_exit.)
459+
*
460+
* @param module_inst the module instance
461+
*/
462+
WASM_RUNTIME_API_EXTERN uint32_t
463+
wasm_runtime_get_wasi_exit_code(wasm_module_inst_t module_inst);
464+
453465
/**
454466
* Lookup an exported function in the WASM module instance.
455467
*

core/iwasm/libraries/libc-uvwasi/libc_uvwasi_wrapper.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#define get_module_inst(exec_env) \
1212
wasm_runtime_get_module_inst(exec_env)
1313

14-
#define get_wasi_ctx(module_inst) \
15-
wasm_runtime_get_wasi_ctx(module_inst)
16-
1714
#define validate_app_addr(offset, size) \
1815
wasm_runtime_validate_app_addr(module_inst, offset, size)
1916

@@ -72,9 +69,24 @@ typedef struct iovec_app {
7269
uint32 buf_len;
7370
} iovec_app_t;
7471

72+
typedef struct WASIContext {
73+
uvwasi_t uvwasi;
74+
uint32_t exit_code;
75+
} WASIContext;
76+
7577
void *
7678
wasm_runtime_get_wasi_ctx(wasm_module_inst_t module_inst);
7779

80+
static uvwasi_t *
81+
get_wasi_ctx(wasm_module_inst_t module_inst)
82+
{
83+
WASIContext *ctx = wasm_runtime_get_wasi_ctx(module_inst);
84+
if (ctx == NULL) {
85+
return NULL;
86+
}
87+
return &ctx->uvwasi;
88+
}
89+
7890
static wasi_errno_t
7991
wasi_args_get(wasm_exec_env_t exec_env, uint32 *argv_offsets, char *argv_buf)
8092
{
@@ -924,10 +936,12 @@ static void
924936
wasi_proc_exit(wasm_exec_env_t exec_env, wasi_exitcode_t rval)
925937
{
926938
wasm_module_inst_t module_inst = get_module_inst(exec_env);
939+
WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
927940
/* Here throwing exception is just to let wasm app exit,
928941
the upper layer should clear the exception and return
929942
as normal */
930943
wasm_runtime_set_exception(module_inst, "wasi proc exit");
944+
wasi_ctx->exit_code = rval;
931945
}
932946

933947
static wasi_errno_t

core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ typedef struct WASIContext {
5757
char **argv_list;
5858
char *env_buf;
5959
char **env_list;
60+
uint32_t exit_code;
6061
} * wasi_ctx_t;
6162

6263
wasi_ctx_t
@@ -980,10 +981,12 @@ static void
980981
wasi_proc_exit(wasm_exec_env_t exec_env, wasi_exitcode_t rval)
981982
{
982983
wasm_module_inst_t module_inst = get_module_inst(exec_env);
984+
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
983985
/* Here throwing exception is just to let wasm app exit,
984986
the upper layer should clear the exception and return
985987
as normal */
986988
wasm_runtime_set_exception(module_inst, "wasi proc exit");
989+
wasi_ctx->exit_code = rval;
987990
}
988991

989992
static wasi_errno_t

0 commit comments

Comments
 (0)