Skip to content

Commit 520410d

Browse files
authored
fix: disable unsigned integer overflow sanitization (#4785)
* 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) * fix: ensure proper definition checks for build options in CMakeLists of wasm-mutator * optimize how to involve sanitizer flags * fix: update LLVM branch and refine sanitizer flags in CMake configurations * fix: add requests package to development requirements
1 parent 9a23968 commit 520410d

File tree

8 files changed

+55
-45
lines changed

8 files changed

+55
-45
lines changed

.devcontainer/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ black
22
nose
33
pycparser
44
pylint
5+
requests

build-scripts/build_llvm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def main():
304304
"default": {
305305
"repo": "https://github.com/llvm/llvm-project.git",
306306
"repo_ssh": "git@github.com:llvm/llvm-project.git",
307-
"branch": "release/18.x",
307+
"branch": "llvmorg-18.1.8",
308308
},
309309
}
310310

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}")

build-scripts/unsupported_combination.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ endfunction()
6161
# Below are the unsupported combinations checks
6262
# Please keep this list in sync with tests/unit/unsupported-features/CMakeLists.txt
6363
# and tests/wamr-test-suites/test_wamr.sh
64-
cmake_print_variables(WAMR_BUILD_INTERP WAMR_BUILD_FAST_INTERP WAMR_BUILD_JIT WAMR_BUILD_EXCE_HANDLING)
6564

6665
if(WAMR_BUILD_EXCE_HANDLING EQUAL 1)
6766
check_aot_mode_error("Unsupported build configuration: EXCE_HANDLING + AOT")

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,19 @@ set(IWASM_DIR ${REPO_ROOT_DIR}/core/iwasm)
172172
# Global setting
173173
add_compile_options(-Wno-unused-command-line-argument)
174174

175-
# Enable fuzzer
176-
add_definitions(-DWASM_ENABLE_FUZZ_TEST=1)
177-
# '-fsanitize=vptr' not allowed with '-fno-rtti
178-
# But, LLVM by default, disables the use of `rtti` in the compiler
179-
add_compile_options(-fsanitize=fuzzer -fno-sanitize=vptr)
180-
add_link_options(-fsanitize=fuzzer -fno-sanitize=vptr)
181-
182175
# Enable sanitizers if not in oss-fuzz environment
183176
set(CFLAGS_ENV $ENV{CFLAGS})
184-
string(FIND "${CFLAGS_ENV}" "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" FUZZ_POS)
177+
string(FIND "${CFLAGS_ENV}" "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" FUZZ_POS)
185178
if (FUZZ_POS GREATER -1)
186179
set(IN_OSS_FUZZ 1)
187180
else()
188181
set(IN_OSS_FUZZ 0)
189182
endif()
190183

184+
# Enable fuzzer
185+
add_definitions(-DWASM_ENABLE_FUZZ_TEST=1)
186+
187+
include(${CMAKE_CURRENT_LIST_DIR}/sanitizer_flags.cmake)
188+
191189
add_subdirectory(aot-compiler)
192190
add_subdirectory(wasm-mutator)

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,5 @@ 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)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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()
26+
27+
# '-fsanitize=vptr' not allowed with '-fno-rtti
28+
# But, LLVM by default, disables the use of `rtti` in the compiler
29+
add_compile_options(-fsanitize=fuzzer -fno-sanitize=vptr)
30+
add_link_options(-fsanitize=fuzzer -fno-sanitize=vptr)

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,46 @@ if(CUSTOM_MUTATOR EQUAL 1)
66
endif()
77

88
# Set default build options with the ability to override from the command line
9-
if(NOT WAMR_BUILD_INTERP)
9+
if(NOT DEFINED WAMR_BUILD_INTERP)
1010
set(WAMR_BUILD_INTERP 1)
1111
endif()
1212

13-
if(NOT WAMR_BUILD_AOT)
13+
if(NOT DEFINED WAMR_BUILD_AOT)
1414
set(WAMR_BUILD_AOT 1)
1515
endif()
1616

17-
if(NOT WAMR_BUILD_JIT)
17+
if(NOT DEFINED WAMR_BUILD_JIT)
1818
set(WAMR_BUILD_JIT 0)
1919
endif()
2020

21-
if(NOT WAMR_BUILD_LIBC_BUILTIN)
21+
if(NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
2222
set(WAMR_BUILD_LIBC_BUILTIN 0)
2323
endif()
2424

25-
if(NOT WAMR_BUILD_LIBC_WASI)
25+
if(NOT DEFINED WAMR_BUILD_LIBC_WASI)
2626
set(WAMR_BUILD_LIBC_WASI 1)
2727
endif()
2828

29-
if(NOT WAMR_BUILD_FAST_INTERP)
29+
if(NOT DEFINED WAMR_BUILD_FAST_INTERP)
3030
set(WAMR_BUILD_FAST_INTERP 1)
3131
endif()
3232

33-
if(NOT WAMR_BUILD_MULTI_MODULE)
33+
if(NOT DEFINED WAMR_BUILD_MULTI_MODULE)
3434
set(WAMR_BUILD_MULTI_MODULE 0)
3535
endif()
3636

37-
if(NOT WAMR_BUILD_LIB_PTHREAD)
37+
if(NOT DEFINED WAMR_BUILD_LIB_PTHREAD)
3838
set(WAMR_BUILD_LIB_PTHREAD 0)
3939
endif()
4040

41-
if(NOT WAMR_BUILD_MINI_LOADER)
41+
if(NOT DEFINED WAMR_BUILD_MINI_LOADER)
4242
set(WAMR_BUILD_MINI_LOADER 0)
4343
endif()
4444

45-
set(WAMR_BUILD_SIMD 1)
45+
if(NOT DEFINED WAMR_BUILD_SIMD)
46+
set(WAMR_BUILD_SIMD 1)
47+
endif()
48+
4649
set(WAMR_BUILD_REF_TYPES 1)
4750
set(WAMR_BUILD_GC 1)
4851

@@ -56,15 +59,3 @@ target_link_libraries(vmlib PUBLIC ${REQUIRED_LLVM_LIBS})
5659

5760
add_executable(wasm_mutator_fuzz wasm_mutator_fuzz.cc)
5861
target_link_libraries(wasm_mutator_fuzz PRIVATE vmlib m)
59-
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()

0 commit comments

Comments
 (0)