Skip to content

Commit b87cc94

Browse files
committed
handle a return frow wasi _start function correctly
this fixes a few test cases in wasi-threads testsuite like wasi_threads_return_main_block. also, move the special handling for wasi proc exit to a more appropriate place.
1 parent 9d07250 commit b87cc94

File tree

2 files changed

+19
-40
lines changed

2 files changed

+19
-40
lines changed

core/iwasm/common/wasm_application.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,22 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[])
107107
the actual main function. Directly calling main function
108108
may cause exception thrown. */
109109
if ((func = wasm_runtime_lookup_wasi_start_function(module_inst))) {
110-
return wasm_runtime_call_wasm(exec_env, func, 0, NULL);
110+
const char *wasi_proc_exit_exception = "wasi proc exit";
111+
112+
ret = wasm_runtime_call_wasm(exec_env, func, 0, NULL);
113+
/* a successful return from _start is same as proc_exit(0). */
114+
if (ret) {
115+
wasm_runtime_set_exception(module_inst, wasi_proc_exit_exception);
116+
/* exit_code is zero-initialized */
117+
ret = false;
118+
}
119+
/* report wasm proc exit as a success */
120+
WASMModuleInstance *inst = (WASMModuleInstance *)module_inst;
121+
if (!ret && strstr(inst->cur_exception, wasi_proc_exit_exception)) {
122+
inst->cur_exception[0] = 0;
123+
ret = true;
124+
}
125+
return ret;
111126
}
112127
#endif /* end of WASM_ENABLE_LIBC_WASI */
113128

core/iwasm/common/wasm_runtime_common.c

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,33 +1955,6 @@ wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
19551955
}
19561956
#endif
19571957

1958-
static bool
1959-
clear_wasi_proc_exit_exception(WASMModuleInstanceCommon *module_inst_comm)
1960-
{
1961-
#if WASM_ENABLE_LIBC_WASI != 0
1962-
bool has_exception;
1963-
char exception[EXCEPTION_BUF_LEN];
1964-
WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
1965-
1966-
bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
1967-
|| module_inst_comm->module_type == Wasm_Module_AoT);
1968-
1969-
has_exception = wasm_copy_exception(module_inst, exception);
1970-
if (has_exception && !strcmp(exception, "Exception: wasi proc exit")) {
1971-
/* The "wasi proc exit" exception is thrown by native lib to
1972-
let wasm app exit, which is a normal behavior, we clear
1973-
the exception here. And just clear the exception of current
1974-
thread, don't call `wasm_set_exception(module_inst, NULL)`
1975-
which will clear the exception of all threads. */
1976-
module_inst->cur_exception[0] = '\0';
1977-
return true;
1978-
}
1979-
return false;
1980-
#else
1981-
return false;
1982-
#endif
1983-
}
1984-
19851958
bool
19861959
wasm_runtime_call_wasm(WASMExecEnv *exec_env,
19871960
WASMFunctionInstanceCommon *function, uint32 argc,
@@ -2022,15 +1995,10 @@ wasm_runtime_call_wasm(WASMExecEnv *exec_env,
20221995
param_argc, new_argv);
20231996
#endif
20241997
if (!ret) {
2025-
if (clear_wasi_proc_exit_exception(exec_env->module_inst)) {
2026-
ret = true;
2027-
}
2028-
else {
2029-
if (new_argv != argv) {
2030-
wasm_runtime_free(new_argv);
2031-
}
2032-
return false;
1998+
if (new_argv != argv) {
1999+
wasm_runtime_free(new_argv);
20332000
}
2001+
return false;
20342002
}
20352003

20362004
#if WASM_ENABLE_REF_TYPES != 0
@@ -4611,10 +4579,6 @@ wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
46114579
ret = aot_call_indirect(exec_env, 0, element_index, argc, argv);
46124580
#endif
46134581

4614-
if (!ret && clear_wasi_proc_exit_exception(exec_env->module_inst)) {
4615-
ret = true;
4616-
}
4617-
46184582
return ret;
46194583
}
46204584

0 commit comments

Comments
 (0)