Skip to content

Commit b94a92f

Browse files
committed
fix: disable unsigned integer overflow sanitization in build configurations
FYI: from https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html `-fsanitize=unsigned-integer-overflow`: Unsigned integer overflow, where the result of an unsigned integer computation cannot be represented in its type. Unlike signed integer overflow, this is not undefined behavior, but it is often unintentional. This sanitizer does not check for lossy implicit conversions performed before such a computation. It brings a more common question: which is better, pre-additional-check or post-additional-check to fix a potential unsigned integer overflow? A pre-additional-check involves using a check to prevent integer overflow from the very beginning. A post-additional-check involves using a check after addition to see if there is an overflow. In this project, post-additional-checking is widely used. let's follow the routine. for performance sensitive logic, use __builtin_add_overflow etc. provide something like https://github.com/yamt/toywasm/blob/9a5622791e99395e26e6e96cef830af3d91a1685/lib/platform.h#L176-L191 and encourage the use of them. ref. #4549 (comment)
1 parent d4034f1 commit b94a92f

4 files changed

Lines changed: 32 additions & 24 deletions

File tree

build-scripts/config_common.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ if (NOT WAMR_BUILD_SANITIZER STREQUAL "")
196196
message(FATAL_ERROR "Unsupported sanitizers: ${INVALID_SANITIZERS}")
197197
endif()
198198
# common flags for all sanitizers
199-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fno-sanitize-recover=all")
199+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fno-sanitize-recover=all -fno-sanitize=alignment")
200+
if(CMAKE_C_COMPILER_ID MATCHES ".*Clang")
201+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=unsigned-integer-overflow")
202+
endif()
200203
if(SANITIZER_FLAGS)
201204
string(REPLACE ";" "," SANITIZER_FLAGS_STR "${SANITIZER_FLAGS}")
202205
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${SANITIZER_FLAGS_STR}")

tests/fuzz/wasm-mutator-fuzz/aot-compiler/CMakeLists.txt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,7 @@ target_link_directories(aotclib PUBLIC ${LLVM_LIBRARY_DIR})
6767

6868
target_link_libraries(aotclib PUBLIC ${REQUIRED_LLVM_LIBS})
6969

70-
if(NOT IN_OSS_FUZZ)
71-
message(STATUS "Enable ASan and UBSan in non-oss-fuzz environment for aotclib")
72-
target_compile_options(aotclib PUBLIC
73-
-fprofile-instr-generate -fcoverage-mapping
74-
-fno-sanitize-recover=all
75-
-fsanitize=address,undefined
76-
-fsanitize=float-divide-by-zero,unsigned-integer-overflow,local-bounds,nullability
77-
-fno-sanitize=alignment
78-
)
79-
target_link_options(aotclib PUBLIC -fsanitize=address,undefined -fprofile-instr-generate)
80-
endif()
81-
8270
add_executable(aot_compiler_fuzz aot_compiler_fuzz.cc)
8371
target_link_libraries(aot_compiler_fuzz PRIVATE stdc++ aotclib)
72+
73+
include(${CMAKE_CURRENT_LIST_DIR}/../sanitizer_flags.cmake)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
if(NOT IN_OSS_FUZZ)
2+
message(STATUS "Enable ASan and UBSan in non-oss-fuzz environment for vmlib")
3+
4+
add_compile_options(-fprofile-instr-generate -fcoverage-mapping)
5+
6+
#
7+
# Sync up with the content of infra/base-images/base-builder/Dockerfile in oss-fuzz
8+
#
9+
10+
# SANITIZER_FLAGS_address
11+
add_compile_options(-fsanitize=address -fsanitize-address-use-after-scope)
12+
13+
# SANITIZER_FLAGS_undefined
14+
add_compile_options(
15+
-fsanitize=array-bounds,bool,builtin,enum,function,integer-divide-by-zero,null,object-size,return,returns-nonnull-attribute,shift,signed-integer-overflow,unsigned-integer-overflow,unreachable,vla-bound,vptr
16+
-fno-sanitize-recover=array-bounds,bool,builtin,enum,function,integer-divide-by-zero,null,object-size,return,returns-nonnull-attribute,shift,signed-integer-overflow,unreachable,vla-bound,vptr
17+
)
18+
19+
add_link_options(-fsanitize=address,undefined -fprofile-instr-generate)
20+
endif()
21+
22+
# Always disable unsigned-integer-overflow
23+
if(CMAKE_C_COMPILER_ID MATCHES ".*Clang")
24+
add_compile_options(-fno-sanitize=unsigned-integer-overflow)
25+
endif()

tests/fuzz/wasm-mutator-fuzz/wasm-mutator/CMakeLists.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,4 @@ target_link_libraries(vmlib PUBLIC ${REQUIRED_LLVM_LIBS})
5757
add_executable(wasm_mutator_fuzz wasm_mutator_fuzz.cc)
5858
target_link_libraries(wasm_mutator_fuzz PRIVATE vmlib m)
5959

60-
if(NOT IN_OSS_FUZZ)
61-
message(STATUS "Enable ASan and UBSan in non-oss-fuzz environment for vmlib")
62-
target_compile_options(vmlib PUBLIC
63-
-fprofile-instr-generate -fcoverage-mapping
64-
-fno-sanitize-recover=all
65-
-fsanitize=address,undefined
66-
-fsanitize=float-divide-by-zero,unsigned-integer-overflow,local-bounds,nullability
67-
-fno-sanitize=alignment
68-
)
69-
target_link_options(vmlib PUBLIC -fsanitize=address,undefined -fprofile-instr-generate)
70-
endif()
60+
include(${CMAKE_CURRENT_LIST_DIR}/../sanitizer_flags.cmake)

0 commit comments

Comments
 (0)