Skip to content

Enhance WAMR_BUILD_SANITIZER to support multiple sanitizers #4489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 50 additions & 20 deletions build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,58 @@ include (${WAMR_ROOT_DIR}/build-scripts/package.cmake)

if (NOT DEFINED WAMR_BUILD_SANITIZER)
set(WAMR_BUILD_SANITIZER $ENV{WAMR_BUILD_SANITIZER})
endif ()
endif()

if (NOT DEFINED WAMR_BUILD_SANITIZER)
set(WAMR_BUILD_SANITIZER "")
elseif (WAMR_BUILD_SANITIZER STREQUAL "ubsan")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=alignment" )
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
elseif (WAMR_BUILD_SANITIZER STREQUAL "asan")
if (NOT WAMR_BUILD_JIT EQUAL 1)
set (ASAN_OPTIONS "verbosity=2 debug=true ")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=address -fno-sanitize-recover=all" )
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
if (NOT WAMR_BUILD_SANITIZER STREQUAL "")
set(SUPPORTED_SANITIZERS "ubsan;asan;tsan;posan")
string(REPLACE "," ";" SANITIZER_LIST "${WAMR_BUILD_SANITIZER}")

# Check uncompabile sanitizers
if("tsan" IN_LIST SANITIZER_LIST AND "asan" IN_LIST SANITIZER_LIST)
message(FATAL_ERROR "ThreadSanitizer (tsan) and AddressSanitizer (asan) cannot be used together!")
endif()
elseif (WAMR_BUILD_SANITIZER STREQUAL "tsan")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=thread -fno-sanitize-recover=all" )
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
elseif (WAMR_BUILD_SANITIZER STREQUAL "posan")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=pointer-overflow -fno-sanitize-recover=all" )
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=pointer-overflow")
elseif (NOT (WAMR_BUILD_SANITIZER STREQUAL "") )
message(SEND_ERROR "Unsupported sanitizer: ${WAMR_BUILD_SANITIZER}")
endif()

# Check every sanitizer in the list
set(INVALID_SANITIZERS "")
list(REMOVE_DUPLICATES SANITIZER_LIST)
set(SANITIZER_FLAGS)
set(NO_SANITIZER_FLAGS)
foreach(sanitizer ${SANITIZER_LIST})
string(STRIP "${sanitizer}" sanitizer)
if(NOT sanitizer IN_LIST SUPPORTED_SANITIZERS)
list(APPEND INVALID_SANITIZERS "${sanitizer}")
elseif(sanitizer STREQUAL "ubsan")
list(APPEND SANITIZER_FLAGS "undefined")
list(APPEND NO_SANITIZER_FLAGS "alignment")
elseif(sanitizer STREQUAL "asan")
if (NOT WAMR_BUILD_JIT EQUAL 1)
set(ENV{ASAN_OPTIONS} "verbosity=2 debug=true")
list(APPEND SANITIZER_FLAGS "address")
else()
message(WARNING "AddressSanitizer is not supported in LLVM JIT mode, skip it")
endif()
elseif(sanitizer STREQUAL "tsan")
list(APPEND SANITIZER_FLAGS "thread")
elseif(sanitizer STREQUAL "posan")
list(APPEND SANITIZER_FLAGS "pointer-overflow")
endif()
endforeach()

if(INVALID_SANITIZERS)
message(FATAL_ERROR "Unsupported sanitizers: ${INVALID_SANITIZERS}")
endif()
# common flags for all sanitizers
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fno-sanitize-recover=all")
if(SANITIZER_FLAGS)
string(REPLACE ";" "," SANITIZER_FLAGS_STR "${SANITIZER_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${SANITIZER_FLAGS_STR}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${SANITIZER_FLAGS_STR}")
endif()
if(NO_SANITIZER_FLAGS)
string(REPLACE ";" "," NO_SANITIZER_FLAGS_STR "${NO_SANITIZER_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=${NO_SANITIZER_FLAGS_STR}")
endif()
endif ()

if (WAMR_BUILD_LINUX_PERF EQUAL 1)
if (NOT WAMR_BUILD_JIT AND NOT WAMR_BUILD_AOT)
Expand Down
27 changes: 6 additions & 21 deletions tests/wamr-test-suites/test_wamr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ function help()
echo "-F set the firmware path used by qemu"
echo "-C enable code coverage collect"
echo "-j set the platform to test"
echo "-T set sanitizer to use in tests(ubsan|tsan|asan|posan)"
echo "-T set the sanitizer(s) used during testing. It can be either a comma-separated list
(e.g., ubsan, asan) or a single option
(e.g., ubsan, tsan, asan, posan)."
echo "-A use the specified wamrc command instead of building it"
echo "-N enable extended const expression feature"
echo "-r [requirement name] [N [N ...]] specify a requirement name followed by one or more"
Expand Down Expand Up @@ -1066,26 +1068,9 @@ function trigger()
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_TAIL_CALL=1"
fi

echo "SANITIZER IS" $WAMR_BUILD_SANITIZER

if [[ "$WAMR_BUILD_SANITIZER" == "ubsan" ]]; then
echo "Setting run with ubsan"
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=ubsan"
fi

if [[ "$WAMR_BUILD_SANITIZER" == "asan" ]]; then
echo "Setting run with asan"
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=asan"
fi

if [[ "$WAMR_BUILD_SANITIZER" == "tsan" ]]; then
echo "Setting run with tsan"
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=tsan"
fi

if [[ "$WAMR_BUILD_SANITIZER" == "posan" ]]; then
echo "Setting run with posan"
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=posan"
if [[ -n "$WAMR_BUILD_SANITIZER" ]]; then
echo "Setting run with sanitizer(s): $WAMR_BUILD_SANITIZER"
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=$WAMR_BUILD_SANITIZER"
fi

# Make sure we're using the builtin WASI libc implementation
Expand Down
Loading