From deacb7a8d8ca96b9dff797907b7e3b2ced4887df Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 8 Oct 2024 10:08:29 +0800 Subject: [PATCH] Fix exec_env_tls assertion in module instantiation (#3844) The execute_post_instantiate_functions may be triggered by wasm_cluster_spawn_exec_env, in which the exec_env_tls can be NULL and cause the assertion invalid. p.s. https://github.com/bytecodealliance/wasm-micro-runtime/issues/3839. --- core/iwasm/aot/aot_runtime.c | 8 ++++++-- core/iwasm/interpreter/wasm_runtime.c | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 63a3c83c..eb88fa6c 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1560,8 +1560,12 @@ execute_post_instantiate_functions(AOTModuleInstance *module_inst, if (is_sub_inst) { bh_assert(exec_env_main); #ifdef OS_ENABLE_HW_BOUND_CHECK - bh_assert(exec_env_tls == exec_env_main); - (void)exec_env_tls; + /* May come from pthread_create_wrapper, thread_spawn_wrapper and + wasm_cluster_spawn_exec_env. If it comes from the former two, + the exec_env_tls must be not NULL and equal to exec_env_main, + else if it comes from the last one, it may be NULL. */ + if (exec_env_tls) + bh_assert(exec_env_tls == exec_env_main); #endif exec_env = exec_env_main; diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index e4142ab8..90299df9 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -1594,8 +1594,12 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst, if (is_sub_inst) { bh_assert(exec_env_main); #ifdef OS_ENABLE_HW_BOUND_CHECK - bh_assert(exec_env_tls == exec_env_main); - (void)exec_env_tls; + /* May come from pthread_create_wrapper, thread_spawn_wrapper and + wasm_cluster_spawn_exec_env. If it comes from the former two, + the exec_env_tls must be not NULL and equal to exec_env_main, + else if it comes from the last one, it may be NULL. */ + if (exec_env_tls) + bh_assert(exec_env_tls == exec_env_main); #endif exec_env = exec_env_main;