Skip to content

Commit 60a3c1f

Browse files
committed
enhance WAMR_BUILD_SANITIZER to support multiple sanitizers
1 parent 2a952b3 commit 60a3c1f

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

build-scripts/config_common.cmake

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,45 @@ 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+
foreach(sanitizer ${SANITIZER_LIST})
158+
string(STRIP "${sanitizer}" sanitizer)
159+
if(NOT sanitizer IN_LIST SUPPORTED_SANITIZERS)
160+
list(APPEND INVALID_SANITIZERS "${sanitizer}")
161+
elseif(sanitizer STREQUAL "ubsan")
162+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=alignment")
163+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
164+
elseif(sanitizer STREQUAL "asan")
165+
if (NOT WAMR_BUILD_JIT EQUAL 1)
166+
set(ASAN_OPTIONS "verbosity=2 debug=true")
167+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=address -fno-sanitize-recover=all")
168+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
169+
endif()
170+
elseif(sanitizer STREQUAL "tsan")
171+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=thread -fno-sanitize-recover=all")
172+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
173+
elseif(sanitizer STREQUAL "posan")
174+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=pointer-overflow -fno-sanitize-recover=all")
175+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=pointer-overflow")
176+
endif()
177+
endforeach()
178+
179+
if(INVALID_SANITIZERS)
180+
message(FATAL_ERROR "Unsupported sanitizers: ${INVALID_SANITIZERS}")
181+
endif()
182+
endif ()
166183

167184
if (WAMR_BUILD_LINUX_PERF EQUAL 1)
168185
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)