Skip to content

Commit 628d411

Browse files
committed
fix: clear exec_env_tls when destroying exec_env
When an exec_env is destroyed, check if it matches the current thread's exec_env_tls and clear it to avoid dangling pointer issues. Without this fix, in daemon-style execution where the same thread runs multiple WASM modules sequentially (like Cloudflare Workers), the exec_env_tls can point to freed memory after an exec_env is destroyed, causing crashes on subsequent executions when the signal handler tries to access it. This is critical for AOT mode with hardware bounds checking enabled, where signal handlers rely on exec_env_tls to handle SIGSEGV properly.
1 parent 2a2dd19 commit 628d411

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

core/iwasm/common/wasm_exec_env.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,20 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
199199
void
200200
wasm_exec_env_destroy(WASMExecEnv *exec_env)
201201
{
202+
#ifdef OS_ENABLE_HW_BOUND_CHECK
203+
/*
204+
* Clear exec_env_tls if it points to this exec_env to avoid dangling
205+
* pointer after destruction. This is critical for daemon-style execution
206+
* where the same thread runs multiple WASM modules sequentially.
207+
* Without this, the signal handler may access freed memory on subsequent
208+
* executions, causing crashes.
209+
*/
210+
WASMExecEnv *current_tls = wasm_runtime_get_exec_env_tls();
211+
if (current_tls == exec_env) {
212+
wasm_runtime_set_exec_env_tls(NULL);
213+
}
214+
#endif
215+
202216
#if WASM_ENABLE_THREAD_MGR != 0
203217
/* Wait for all sub-threads */
204218
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);

0 commit comments

Comments
 (0)