Skip to content

Commit cadf9d0

Browse files
Main thread spread exception when thread-mgr is enabled (#1889)
And refactor clear_wasi_proc_exit_exception, refer to #1869
1 parent cb7ac8f commit cadf9d0

File tree

10 files changed

+255
-142
lines changed

10 files changed

+255
-142
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -899,24 +899,6 @@ create_exports(AOTModuleInstance *module_inst, AOTModule *module,
899899
return create_export_funcs(module_inst, module, error_buf, error_buf_size);
900900
}
901901

902-
static bool
903-
clear_wasi_proc_exit_exception(AOTModuleInstance *module_inst)
904-
{
905-
#if WASM_ENABLE_LIBC_WASI != 0
906-
const char *exception = aot_get_exception(module_inst);
907-
if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
908-
/* The "wasi proc exit" exception is thrown by native lib to
909-
let wasm app exit, which is a normal behavior, we clear
910-
the exception here. */
911-
aot_set_exception(module_inst, NULL);
912-
return true;
913-
}
914-
return false;
915-
#else
916-
return false;
917-
#endif
918-
}
919-
920902
static bool
921903
execute_post_inst_function(AOTModuleInstance *module_inst)
922904
{
@@ -956,7 +938,6 @@ execute_start_function(AOTModuleInstance *module_inst)
956938
u.f(exec_env);
957939

958940
wasm_exec_env_destroy(exec_env);
959-
(void)clear_wasi_proc_exit_exception(module_inst);
960941
return !aot_get_exception(module_inst);
961942
}
962943

@@ -1407,13 +1388,6 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
14071388
ret = invoke_native_internal(exec_env, function->u.func.func_ptr,
14081389
func_type, NULL, NULL, argv1, argc, argv);
14091390

1410-
if (!ret || aot_get_exception(module_inst)) {
1411-
if (clear_wasi_proc_exit_exception(module_inst))
1412-
ret = true;
1413-
else
1414-
ret = false;
1415-
}
1416-
14171391
#if WASM_ENABLE_DUMP_CALL_STACK != 0
14181392
if (!ret) {
14191393
if (aot_create_call_stack(exec_env)) {
@@ -1473,9 +1447,6 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
14731447
ret = invoke_native_internal(exec_env, function->u.func.func_ptr,
14741448
func_type, NULL, NULL, argv, argc, argv);
14751449

1476-
if (clear_wasi_proc_exit_exception(module_inst))
1477-
ret = true;
1478-
14791450
#if WASM_ENABLE_DUMP_CALL_STACK != 0
14801451
if (aot_get_exception(module_inst)) {
14811452
if (aot_create_call_stack(exec_env)) {
@@ -1516,7 +1487,7 @@ aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
15161487
}
15171488
}
15181489

1519-
ret = aot_call_function(exec_env, func, argc, argv);
1490+
ret = wasm_runtime_call_wasm(exec_env, func, argc, argv);
15201491

15211492
/* don't destroy the exec_env if it isn't created in this function */
15221493
if (!existing_exec_env)
@@ -2006,9 +1977,6 @@ aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
20061977
}
20071978

20081979
fail:
2009-
if (clear_wasi_proc_exit_exception(module_inst))
2010-
return true;
2011-
20121980
#ifdef OS_ENABLE_HW_BOUND_CHECK
20131981
wasm_runtime_access_exce_check_guard_page();
20141982
#endif

core/iwasm/common/wasm_exec_env.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ wasm_exec_env_destroy(WASMExecEnv *exec_env)
181181
the stopped thread will be overrided by other threads */
182182
wasm_cluster_thread_exited(exec_env);
183183
#endif
184+
/* We have terminated other threads, this is the only alive thread, so
185+
* we don't acquire cluster->lock because the cluster will be destroyed
186+
* inside this function */
184187
wasm_cluster_del_exec_env(cluster, exec_env);
185188
}
186189
#endif /* end of WASM_ENABLE_THREAD_MGR */

core/iwasm/common/wasm_runtime_common.c

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,30 @@ wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
17431743
}
17441744
#endif
17451745

1746+
static bool
1747+
clear_wasi_proc_exit_exception(WASMModuleInstanceCommon *module_inst_comm)
1748+
{
1749+
#if WASM_ENABLE_LIBC_WASI != 0
1750+
const char *exception;
1751+
WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
1752+
1753+
bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
1754+
|| module_inst_comm->module_type == Wasm_Module_AoT);
1755+
1756+
exception = wasm_get_exception(module_inst);
1757+
if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
1758+
/* The "wasi proc exit" exception is thrown by native lib to
1759+
let wasm app exit, which is a normal behavior, we clear
1760+
the exception here. */
1761+
wasm_set_exception(module_inst, NULL);
1762+
return true;
1763+
}
1764+
return false;
1765+
#else
1766+
return false;
1767+
#endif
1768+
}
1769+
17461770
bool
17471771
wasm_runtime_call_wasm(WASMExecEnv *exec_env,
17481772
WASMFunctionInstanceCommon *function, uint32 argc,
@@ -1783,10 +1807,15 @@ wasm_runtime_call_wasm(WASMExecEnv *exec_env,
17831807
param_argc, new_argv);
17841808
#endif
17851809
if (!ret) {
1786-
if (new_argv != argv) {
1787-
wasm_runtime_free(new_argv);
1810+
if (clear_wasi_proc_exit_exception(exec_env->module_inst)) {
1811+
ret = true;
1812+
}
1813+
else {
1814+
if (new_argv != argv) {
1815+
wasm_runtime_free(new_argv);
1816+
}
1817+
return false;
17881818
}
1789-
return false;
17901819
}
17911820

17921821
#if WASM_ENABLE_REF_TYPES != 0
@@ -2150,11 +2179,25 @@ wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst_comm)
21502179
void
21512180
wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
21522181
{
2153-
if (exception)
2182+
WASMExecEnv *exec_env = NULL;
2183+
2184+
if (exception) {
21542185
snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception),
21552186
"Exception: %s", exception);
2156-
else
2187+
}
2188+
else {
21572189
module_inst->cur_exception[0] = '\0';
2190+
}
2191+
2192+
#if WASM_ENABLE_THREAD_MGR != 0
2193+
exec_env =
2194+
wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
2195+
if (exec_env) {
2196+
wasm_cluster_spread_exception(exec_env, exception ? false : true);
2197+
}
2198+
#else
2199+
(void)exec_env;
2200+
#endif
21582201
}
21592202

21602203
/* clang-format off */
@@ -4179,6 +4222,8 @@ bool
41794222
wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices,
41804223
uint32 argc, uint32 argv[])
41814224
{
4225+
bool ret = false;
4226+
41824227
if (!wasm_runtime_exec_env_check(exec_env)) {
41834228
LOG_ERROR("Invalid exec env stack info.");
41844229
return false;
@@ -4190,13 +4235,18 @@ wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices,
41904235

41914236
#if WASM_ENABLE_INTERP != 0
41924237
if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
4193-
return wasm_call_indirect(exec_env, 0, element_indices, argc, argv);
4238+
ret = wasm_call_indirect(exec_env, 0, element_indices, argc, argv);
41944239
#endif
41954240
#if WASM_ENABLE_AOT != 0
41964241
if (exec_env->module_inst->module_type == Wasm_Module_AoT)
4197-
return aot_call_indirect(exec_env, 0, element_indices, argc, argv);
4242+
ret = aot_call_indirect(exec_env, 0, element_indices, argc, argv);
41984243
#endif
4199-
return false;
4244+
4245+
if (!ret && clear_wasi_proc_exit_exception(exec_env->module_inst)) {
4246+
ret = true;
4247+
}
4248+
4249+
return ret;
42004250
}
42014251

42024252
static void

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4015,24 +4015,6 @@ fast_jit_call_func_bytecode(WASMModuleInstance *module_inst,
40154015
#endif /* end of WASM_ENABLE_FAST_JIT != 0 */
40164016

40174017
#if WASM_ENABLE_JIT != 0
4018-
static bool
4019-
clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
4020-
{
4021-
#if WASM_ENABLE_LIBC_WASI != 0
4022-
const char *exception = wasm_get_exception(module_inst);
4023-
if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
4024-
/* The "wasi proc exit" exception is thrown by native lib to
4025-
let wasm app exit, which is a normal behavior, we clear
4026-
the exception here. */
4027-
wasm_set_exception(module_inst, NULL);
4028-
return true;
4029-
}
4030-
return false;
4031-
#else
4032-
return false;
4033-
#endif
4034-
}
4035-
40364018
static bool
40374019
llvm_jit_call_func_bytecode(WASMModuleInstance *module_inst,
40384020
WASMExecEnv *exec_env,
@@ -4092,14 +4074,6 @@ llvm_jit_call_func_bytecode(WASMModuleInstance *module_inst,
40924074
ret = wasm_runtime_invoke_native(
40934075
exec_env, module_inst->func_ptrs[func_idx], func_type, NULL, NULL,
40944076
argv1, argc, argv);
4095-
4096-
if (!ret || wasm_get_exception(module_inst)) {
4097-
if (clear_wasi_proc_exit_exception(module_inst))
4098-
ret = true;
4099-
else
4100-
ret = false;
4101-
}
4102-
41034077
if (!ret) {
41044078
if (argv1 != argv1_buf)
41054079
wasm_runtime_free(argv1);
@@ -4144,9 +4118,6 @@ llvm_jit_call_func_bytecode(WASMModuleInstance *module_inst,
41444118
exec_env, module_inst->func_ptrs[func_idx], func_type, NULL, NULL,
41454119
argv, argc, argv);
41464120

4147-
if (clear_wasi_proc_exit_exception(module_inst))
4148-
ret = true;
4149-
41504121
return ret && !wasm_get_exception(module_inst) ? true : false;
41514122
}
41524123
}

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,24 +2033,6 @@ wasm_lookup_table(const WASMModuleInstance *module_inst, const char *name)
20332033
}
20342034
#endif
20352035

2036-
static bool
2037-
clear_wasi_proc_exit_exception(WASMModuleInstance *module_inst)
2038-
{
2039-
#if WASM_ENABLE_LIBC_WASI != 0
2040-
const char *exception = wasm_get_exception(module_inst);
2041-
if (exception && !strcmp(exception, "Exception: wasi proc exit")) {
2042-
/* The "wasi proc exit" exception is thrown by native lib to
2043-
let wasm app exit, which is a normal behavior, we clear
2044-
the exception here. */
2045-
wasm_set_exception(module_inst, NULL);
2046-
return true;
2047-
}
2048-
return false;
2049-
#else
2050-
return false;
2051-
#endif
2052-
}
2053-
20542036
#ifdef OS_ENABLE_HW_BOUND_CHECK
20552037

20562038
static void
@@ -2160,7 +2142,6 @@ wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
21602142
wasm_exec_env_set_thread_info(exec_env);
21612143

21622144
interp_call_wasm(module_inst, exec_env, function, argc, argv);
2163-
(void)clear_wasi_proc_exit_exception(module_inst);
21642145
return !wasm_get_exception(module_inst) ? true : false;
21652146
}
21662147

@@ -2188,7 +2169,7 @@ wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
21882169
}
21892170
}
21902171

2191-
ret = wasm_call_function(exec_env, func, argc, argv);
2172+
ret = wasm_runtime_call_wasm(exec_env, func, argc, argv);
21922173

21932174
/* don't destroy the exec_env if it isn't created in this function */
21942175
if (!existing_exec_env)
@@ -2458,7 +2439,6 @@ call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 elem_idx,
24582439

24592440
interp_call_wasm(module_inst, exec_env, func_inst, argc, argv);
24602441

2461-
(void)clear_wasi_proc_exit_exception(module_inst);
24622442
return !wasm_get_exception(module_inst) ? true : false;
24632443

24642444
got_exception:

core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,7 @@ pthread_start_routine(void *arg)
520520

521521
if (!wasm_runtime_call_indirect(exec_env, routine_args->elem_index, 1,
522522
argv)) {
523-
if (wasm_runtime_get_exception(module_inst))
524-
wasm_cluster_spread_exception(exec_env);
523+
/* Exception has already been spread during throwing */
525524
}
526525

527526
/* destroy pthread key values */

0 commit comments

Comments
 (0)