wasm-micro-runtime/tests/unit/runtime-common
Steven 5664589daa
fix: clear exec_env_tls when destroying exec_env (#4774)
* 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.

* test(exec_env): add reproducer for exec_env_tls dangling pointer bug

Add test case that reproduces the bug where exec_env_tls is not cleared
on early return paths in invoke_native_with_hw_bound_check.

The test triggers native stack overflow check failure, which causes
wasm_runtime_call_wasm to return early after setting exec_env_tls but
without clearing it. This leaves exec_env_tls pointing to a destroyed
exec_env, causing subsequent calls to fail with "invalid exec env".

Test confirms the fix in wasm_exec_env_destroy correctly clears
exec_env_tls when destroying the exec_env it points to.

* 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.
2026-01-20 07:09:26 +08:00
..
wasm-apps Fix some compile warnings and typos (#3854) 2024-10-15 16:04:58 +08:00
build_aot.sh Add unit test suites (#3490) 2024-06-04 11:24:27 +08:00
CMakeLists.txt Refactor the CMake and related scripts for unit tests (#4605) 2025-11-04 11:44:46 +08:00
wasm_exec_env_test.cc fix: clear exec_env_tls when destroying exec_env (#4774) 2026-01-20 07:09:26 +08:00
wasm_runtime_common_test.cc Refactor the CMake and related scripts for unit tests (#4605) 2025-11-04 11:44:46 +08:00
wasm_runtime_init_test.cc fix: correct typos and improve comments across multiple files by codespell (#4116) 2025-03-07 08:21:54 +08:00