Skip to content

Commit 16eb5e1

Browse files
committed
enhance WAMR_BUILD_SANITIZER to support multiple sanitizers
1 parent 2a952b3 commit 16eb5e1

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

build-scripts/config_common.cmake

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,51 @@ include (${WAMR_ROOT_DIR}/build-scripts/package.cmake)
141141

142142
if (NOT DEFINED WAMR_BUILD_SANITIZER)
143143
set(WAMR_BUILD_SANITIZER $ENV{WAMR_BUILD_SANITIZER})
144-
endif ()
144+
endif()
145145

146-
if (NOT DEFINED WAMR_BUILD_SANITIZER)
147-
set(WAMR_BUILD_SANITIZER "")
148-
elseif (WAMR_BUILD_SANITIZER STREQUAL "ubsan")
149-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=alignment" )
150-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
151-
elseif (WAMR_BUILD_SANITIZER STREQUAL "asan")
152-
if (NOT WAMR_BUILD_JIT EQUAL 1)
153-
set (ASAN_OPTIONS "verbosity=2 debug=true ")
154-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=address -fno-sanitize-recover=all" )
155-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
146+
if (NOT WAMR_BUILD_SANITIZER STREQUAL "")
147+
set(SUPPORTED_SANITIZERS "ubsan;asan;tsan;posan")
148+
string(REPLACE "," ";" SANITIZER_LIST "${WAMR_BUILD_SANITIZER}")
149+
150+
# Check uncompabile sanitizers
151+
if("tsan" IN_LIST SANITIZER_LIST AND "asan" IN_LIST SANITIZER_LIST)
152+
message(FATAL_ERROR "ThreadSanitizer (tsan) and AddressSanitizer (asan) cannot be used together!")
156153
endif()
157-
elseif (WAMR_BUILD_SANITIZER STREQUAL "tsan")
158-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=thread -fno-sanitize-recover=all" )
159-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
160-
elseif (WAMR_BUILD_SANITIZER STREQUAL "posan")
161-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=pointer-overflow -fno-sanitize-recover=all" )
162-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=pointer-overflow")
163-
elseif (NOT (WAMR_BUILD_SANITIZER STREQUAL "") )
164-
message(SEND_ERROR "Unsupported sanitizer: ${WAMR_BUILD_SANITIZER}")
165-
endif()
154+
155+
# Check every sanitizer in the list
156+
set(INVALID_SANITIZERS "")
157+
list(REMOVE_DUPLICATES SANITIZER_LIST)
158+
message(STATUS "SANITIZER_LIST: ${SANITIZER_LIST}")
159+
# common flags for all sanitizers
160+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fno-sanitize-recover=all")
161+
foreach(sanitizer ${SANITIZER_LIST})
162+
string(STRIP "${sanitizer}" sanitizer)
163+
if(NOT sanitizer IN_LIST SUPPORTED_SANITIZERS)
164+
list(APPEND INVALID_SANITIZERS "${sanitizer}")
165+
elseif(sanitizer STREQUAL "ubsan")
166+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined -fno-sanitize=alignment")
167+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
168+
elseif(sanitizer STREQUAL "asan")
169+
if (NOT WAMR_BUILD_JIT EQUAL 1)
170+
set(ENV{ASAN_OPTIONS} "verbosity=2 debug=true")
171+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
172+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
173+
else()
174+
message(WARNING "AddressSanitizer is not supported in LLVM JIT mode, skip it")
175+
endif()
176+
elseif(sanitizer STREQUAL "tsan")
177+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
178+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
179+
elseif(sanitizer STREQUAL "posan")
180+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=pointer-overflow")
181+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=pointer-overflow")
182+
endif()
183+
endforeach()
184+
185+
if(INVALID_SANITIZERS)
186+
message(FATAL_ERROR "Unsupported sanitizers: ${INVALID_SANITIZERS}")
187+
endif()
188+
endif ()
166189

167190
if (WAMR_BUILD_LINUX_PERF EQUAL 1)
168191
if (NOT WAMR_BUILD_JIT AND NOT WAMR_BUILD_AOT)

tests/wamr-test-suites/test_wamr.sh

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function help()
3939
echo "-F set the firmware path used by qemu"
4040
echo "-C enable code coverage collect"
4141
echo "-j set the platform to test"
42-
echo "-T set sanitizer to use in tests(ubsan|tsan|asan|posan)"
42+
echo "-T set sanitizer(s) to use in tests (support comma-separated list, e.g. ubsan,asan) (ubsan|tsan|asan|posan)"
4343
echo "-A use the specified wamrc command instead of building it"
4444
echo "-N enable extended const expression feature"
4545
echo "-r [requirement name] [N [N ...]] specify a requirement name followed by one or more"
@@ -1066,26 +1066,9 @@ function trigger()
10661066
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_TAIL_CALL=1"
10671067
fi
10681068

1069-
echo "SANITIZER IS" $WAMR_BUILD_SANITIZER
1070-
1071-
if [[ "$WAMR_BUILD_SANITIZER" == "ubsan" ]]; then
1072-
echo "Setting run with ubsan"
1073-
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=ubsan"
1074-
fi
1075-
1076-
if [[ "$WAMR_BUILD_SANITIZER" == "asan" ]]; then
1077-
echo "Setting run with asan"
1078-
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=asan"
1079-
fi
1080-
1081-
if [[ "$WAMR_BUILD_SANITIZER" == "tsan" ]]; then
1082-
echo "Setting run with tsan"
1083-
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=tsan"
1084-
fi
1085-
1086-
if [[ "$WAMR_BUILD_SANITIZER" == "posan" ]]; then
1087-
echo "Setting run with posan"
1088-
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=posan"
1069+
if [[ -n "$WAMR_BUILD_SANITIZER" ]]; then
1070+
echo "Setting run with sanitizer(s): $WAMR_BUILD_SANITIZER"
1071+
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=$WAMR_BUILD_SANITIZER"
10891072
fi
10901073

10911074
# Make sure we're using the builtin WASI libc implementation

0 commit comments

Comments
 (0)