Skip to content

Commit c0d3e1e

Browse files
committed
fix(runtime): clear exec_env_tls on early return from stack overflow check
Move the fix to clear exec_env_tls at the source - in the early return path of invoke_native_with_hw_bound_check when native stack overflow check fails. Changes: - aot_runtime.c: Clear exec_env_tls before early return on stack overflow - wasm_runtime.c: Clear exec_env_tls before early return on stack overflow - Remove defensive fix from wasm_exec_env_destroy (no longer needed) - Move test from standalone to unit tests (runtime-common) The bug: When wasm_runtime_call_wasm sets exec_env_tls but returns early due to native stack overflow check failure, TLS was not cleared. This caused subsequent calls with a different exec_env to fail with "invalid exec env" error.
1 parent 9f73f59 commit c0d3e1e

6 files changed

Lines changed: 92 additions & 491 deletions

File tree

core/iwasm/aot/aot_runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,6 +2485,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
24852485
native stack to run the following codes before actually calling
24862486
the aot function in invokeNative function. */
24872487
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
2488+
wasm_runtime_set_exec_env_tls(NULL);
24882489
return false;
24892490
}
24902491

core/iwasm/common/wasm_exec_env.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,6 @@ 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-
216202
#if WASM_ENABLE_THREAD_MGR != 0
217203
/* Wait for all sub-threads */
218204
WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3618,6 +3618,7 @@ call_wasm_with_hw_bound_check(WASMModuleInstance *module_inst,
36183618
native stack to run the following codes before actually calling
36193619
the aot function in invokeNative function. */
36203620
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
3621+
wasm_runtime_set_exec_env_tls(NULL);
36213622
return;
36223623
}
36233624

tests/standalone/test-exec-env-tls/CMakeLists.txt

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)