Skip to content

Commit 0f18a9c

Browse files
committed
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.
1 parent 628d411 commit 0f18a9c

File tree

2 files changed

+462
-0
lines changed

2 files changed

+462
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright (C) 2024 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
cmake_minimum_required(VERSION 3.14)
5+
6+
project(test_exec_env_tls)
7+
8+
################ runtime settings ##############
9+
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
10+
if (APPLE)
11+
add_definitions(-DBH_PLATFORM_DARWIN)
12+
endif ()
13+
14+
# Reset default linker flags
15+
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
16+
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
17+
18+
# Set WAMR_BUILD_TARGET
19+
if (NOT DEFINED WAMR_BUILD_TARGET)
20+
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
21+
set (WAMR_BUILD_TARGET "AARCH64")
22+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
23+
set (WAMR_BUILD_TARGET "RISCV64")
24+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
25+
set (WAMR_BUILD_TARGET "X86_64")
26+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
27+
set (WAMR_BUILD_TARGET "X86_32")
28+
else ()
29+
message(SEND_ERROR "Unsupported build target platform!")
30+
endif ()
31+
endif ()
32+
33+
if (NOT CMAKE_BUILD_TYPE)
34+
set (CMAKE_BUILD_TYPE Debug)
35+
endif ()
36+
37+
# WAMR features - enable HW bound check for this test
38+
set (WAMR_BUILD_INTERP 1)
39+
set (WAMR_BUILD_AOT 1)
40+
set (WAMR_BUILD_LIBC_BUILTIN 1)
41+
42+
# compiling and linking flags
43+
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
44+
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
45+
endif ()
46+
47+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
48+
49+
# build out libiwasm
50+
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..)
51+
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
52+
53+
add_library(libiwasm STATIC ${WAMR_RUNTIME_LIB_SOURCE})
54+
set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm)
55+
56+
################ test executable ################
57+
add_executable (test_exec_env_tls test_exec_env_tls.c)
58+
59+
target_include_directories(test_exec_env_tls PRIVATE
60+
${WAMR_ROOT_DIR}/core/iwasm/include
61+
${WAMR_ROOT_DIR}/core/iwasm/common
62+
${SHARED_DIR}/include
63+
${PLATFORM_SHARED_DIR}
64+
)
65+
66+
target_link_libraries(test_exec_env_tls libiwasm -lpthread -lm)
67+
68+
if (NOT APPLE)
69+
target_link_libraries(test_exec_env_tls -ldl)
70+
endif ()

0 commit comments

Comments
 (0)