Skip to content

Commit 2a6e636

Browse files
committed
feat(fuzz): enable sanitizers for non-oss-fuzz environment and update README instructions
1 parent 05cb89b commit 2a6e636

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
2323
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
2424
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
2525

26+
# Check if the compiler supports the sanitizer flags
27+
include(CheckCXXCompilerFlag)
28+
check_cxx_compiler_flag("-fsanitize=address" HAS_ADDRESS_SANITIZER)
29+
check_cxx_compiler_flag("-fsanitize=memory" HAS_MEMORY_SANITIZER)
30+
check_cxx_compiler_flag("-fsanitize=undefined" HAS_UNDEFINED_SANITIZER)
31+
2632
# Determine WAMR_BUILD_TARGET based on system properties
2733
if(NOT DEFINED WAMR_BUILD_TARGET)
2834
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
@@ -76,6 +82,9 @@ add_definitions(${LLVM_DEFINITIONS_LIST})
7682
set(SHARED_DIR ${REPO_ROOT_DIR}/core/shared)
7783
set(IWASM_DIR ${REPO_ROOT_DIR}/core/iwasm)
7884

85+
# Global setting
86+
add_compile_options(-Wno-unused-command-line-argument)
87+
7988
# Enable fuzzer
8089
add_definitions(-DWASM_ENABLE_FUZZ_TEST=1)
8190
add_compile_options(-fsanitize=fuzzer)
@@ -84,17 +93,6 @@ add_link_options(-fsanitize=fuzzer)
8493
# Enable sanitizers if not in oss-fuzz environment
8594
set(CFLAGS_ENV $ENV{CFLAGS})
8695
string(FIND "${CFLAGS_ENV}" "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" IN_OSS_FUZZ)
87-
if(IN_OSS_FUZZ EQUAL -1)
88-
message(STATUS "Enable ASan and UBSan in non-oss-fuzz environment")
89-
add_compile_options(
90-
-fprofile-instr-generate -fcoverage-mapping
91-
-fno-sanitize-recover=all
92-
-fsanitize=address,undefined
93-
-fsanitize=float-divide-by-zero,unsigned-integer-overflow,local-bounds,nullability
94-
-fno-sanitize=alignment
95-
)
96-
add_link_options(-fsanitize=address -fprofile-instr-generate)
97-
endif()
9896

9997
add_subdirectory(aot-compiler)
10098
add_subdirectory(wasm-mutator)

tests/fuzz/wasm-mutator-fuzz/README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,22 @@
22

33
## Install wasm-tools
44

5+
Download the release suitable for your specific platform from https://github.com/bytecodealliance/wasm-tools/releases/latest, unpack it, and add the executable wasm-tools to the `PATH`. Then, you should be able to verify that the installation was successful by using the following command:
6+
57
```bash
6-
1.git clone https://github.com/bytecodealliance/wasm-tools
7-
$ cd wasm-tools
8-
2.This project can be installed and compiled from source with this Cargo command:
9-
$ cargo install wasm-tools
10-
3.Installation can be confirmed with:
118
$ wasm-tools --version
12-
4.Subcommands can be explored with:
9+
# Or learn subcommands with
1310
$ wasm-tools help
1411
```
1512

1613
## Install clang Toolchain
1714

18-
Refer to: https://apt.llvm.org/ and Make sure you have clang installed.
15+
Refer to: https://apt.llvm.org/ and ensure that you have clang installed.
1916

2017
```bash
21-
$ which clang
22-
23-
$ which clang++
18+
$ clang --version
2419

20+
$ clang++ --version
2521
```
2622

2723
## Build

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,17 @@ target_link_libraries(aotclib
148148
LLVMWindowsManifest
149149
)
150150

151+
if(NOT IN_OSS_FUZZ)
152+
message(STATUS "Enable ASan and UBSan in non-oss-fuzz environment")
153+
target_compile_options(aotclib PUBLIC
154+
-fprofile-instr-generate -fcoverage-mapping
155+
-fno-sanitize-recover=all
156+
-fsanitize=address,undefined
157+
-fsanitize=float-divide-by-zero,unsigned-integer-overflow,local-bounds,nullability
158+
-fno-sanitize=alignment
159+
)
160+
target_link_options(aotclib PUBLIC -fsanitize=address,undefined -fprofile-instr-generate)
161+
endif()
162+
151163
add_executable(aot_compiler_fuzz aot_compiler_fuzz.cc)
152164
target_link_libraries(aot_compiler_fuzz PRIVATE stdc++ aotclib)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,15 @@ target_link_directories(vmlib PUBLIC ${RUNTIME_LIB_LINK_LIST})
5555

5656
add_executable(wasm_mutator_fuzz wasm_mutator_fuzz.cc)
5757
target_link_libraries(wasm_mutator_fuzz PRIVATE vmlib m)
58+
59+
if(NOT IN_OSS_FUZZ)
60+
message(STATUS "Enable ASan and UBSan in non-oss-fuzz environment")
61+
target_compile_options(vmlib PUBLIC
62+
-fprofile-instr-generate -fcoverage-mapping
63+
-fno-sanitize-recover=all
64+
-fsanitize=address,undefined
65+
-fsanitize=float-divide-by-zero,unsigned-integer-overflow,local-bounds,nullability
66+
-fno-sanitize=alignment
67+
)
68+
target_link_options(vmlib PUBLIC -fsanitize=address,undefined -fprofile-instr-generate)
69+
endif()

0 commit comments

Comments
 (0)