Skip to content

Commit e1ba1dd

Browse files
authored
enhance WAMR_BUILD_SANITIZER to support multiple sanitizers (#4489)
1 parent 390e780 commit e1ba1dd

File tree

2 files changed

+56
-41
lines changed

2 files changed

+56
-41
lines changed

build-scripts/config_common.cmake

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,58 @@ 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+
set(SANITIZER_FLAGS)
159+
set(NO_SANITIZER_FLAGS)
160+
foreach(sanitizer ${SANITIZER_LIST})
161+
string(STRIP "${sanitizer}" sanitizer)
162+
if(NOT sanitizer IN_LIST SUPPORTED_SANITIZERS)
163+
list(APPEND INVALID_SANITIZERS "${sanitizer}")
164+
elseif(sanitizer STREQUAL "ubsan")
165+
list(APPEND SANITIZER_FLAGS "undefined")
166+
list(APPEND NO_SANITIZER_FLAGS "alignment")
167+
elseif(sanitizer STREQUAL "asan")
168+
if (NOT WAMR_BUILD_JIT EQUAL 1)
169+
set(ENV{ASAN_OPTIONS} "verbosity=2 debug=true")
170+
list(APPEND SANITIZER_FLAGS "address")
171+
else()
172+
message(WARNING "AddressSanitizer is not supported in LLVM JIT mode, skip it")
173+
endif()
174+
elseif(sanitizer STREQUAL "tsan")
175+
list(APPEND SANITIZER_FLAGS "thread")
176+
elseif(sanitizer STREQUAL "posan")
177+
list(APPEND SANITIZER_FLAGS "pointer-overflow")
178+
endif()
179+
endforeach()
180+
181+
if(INVALID_SANITIZERS)
182+
message(FATAL_ERROR "Unsupported sanitizers: ${INVALID_SANITIZERS}")
183+
endif()
184+
# common flags for all sanitizers
185+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fno-sanitize-recover=all")
186+
if(SANITIZER_FLAGS)
187+
string(REPLACE ";" "," SANITIZER_FLAGS_STR "${SANITIZER_FLAGS}")
188+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${SANITIZER_FLAGS_STR}")
189+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${SANITIZER_FLAGS_STR}")
190+
endif()
191+
if(NO_SANITIZER_FLAGS)
192+
string(REPLACE ";" "," NO_SANITIZER_FLAGS_STR "${NO_SANITIZER_FLAGS}")
193+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=${NO_SANITIZER_FLAGS_STR}")
194+
endif()
195+
endif ()
166196

167197
if (WAMR_BUILD_LINUX_PERF EQUAL 1)
168198
if (NOT WAMR_BUILD_JIT AND NOT WAMR_BUILD_AOT)

tests/wamr-test-suites/test_wamr.sh

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ 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 the sanitizer(s) used during testing. It can be either a comma-separated list
43+
(e.g., ubsan, asan) or a single option
44+
(e.g., ubsan, tsan, asan, posan)."
4345
echo "-A use the specified wamrc command instead of building it"
4446
echo "-N enable extended const expression feature"
4547
echo "-r [requirement name] [N [N ...]] specify a requirement name followed by one or more"
@@ -1066,26 +1068,9 @@ function trigger()
10661068
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_TAIL_CALL=1"
10671069
fi
10681070

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"
1071+
if [[ -n "$WAMR_BUILD_SANITIZER" ]]; then
1072+
echo "Setting run with sanitizer(s): $WAMR_BUILD_SANITIZER"
1073+
EXTRA_COMPILE_FLAGS+=" -DWAMR_BUILD_SANITIZER=$WAMR_BUILD_SANITIZER"
10891074
fi
10901075

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

0 commit comments

Comments
 (0)