|
1 | 1 | # Copyright (C) 2019 Intel Corporation. All rights reserved.
|
2 | 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
3 | 3 |
|
4 |
| -cmake_minimum_required (VERSION 3.14) |
| 4 | +cmake_minimum_required(VERSION 3.14) |
5 | 5 |
|
6 |
| -if (NOT DEFINED CMAKE_C_COMPILER) |
7 |
| -set (CMAKE_C_COMPILER "clang") |
8 |
| -endif () |
9 |
| -if (NOT DEFINED CMAKE_CXX_COMPILER) |
10 |
| -set (CMAKE_CXX_COMPILER "clang++") |
11 |
| -endif () |
| 6 | +project(wamr_fuzzing LANGUAGES ASM C CXX) |
12 | 7 |
|
13 |
| -project(wasm_mutator) |
| 8 | +include(CMakePrintHelpers) |
14 | 9 |
|
15 |
| -set (CMAKE_BUILD_TYPE Debug) |
| 10 | +# Ensure Clang is used as the compiler |
| 11 | +if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" |
| 12 | + OR NOT CMAKE_ASM_COMPILER_ID STREQUAL "Clang") |
| 13 | + message(FATAL_ERROR "Please use Clang as the C compiler for libFuzzer compatibility.") |
| 14 | +endif() |
| 15 | + |
| 16 | +# |
| 17 | +# Global settings |
| 18 | +# |
| 19 | +set(CMAKE_BUILD_TYPE Debug) |
| 20 | +set(CMAKE_C_STANDARD 11) |
| 21 | +set(CMAKE_CXX_STANDARD 17) |
16 | 22 |
|
17 |
| -string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM) |
| 23 | +string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM) |
18 | 24 |
|
19 | 25 | # Reset default linker flags
|
20 |
| -set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") |
21 |
| -set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") |
22 |
| - |
23 |
| -set (CMAKE_C_STANDARD 11) |
24 |
| -set (CMAKE_CXX_STANDARD 17) |
25 |
| - |
26 |
| -# Set WAMR_BUILD_TARGET, currently values supported: |
27 |
| -# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]", |
28 |
| -# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]" |
29 |
| -if (NOT DEFINED WAMR_BUILD_TARGET) |
30 |
| - if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)") |
31 |
| - set (WAMR_BUILD_TARGET "AARCH64") |
32 |
| - elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64") |
33 |
| - set (WAMR_BUILD_TARGET "RISCV64") |
34 |
| - elseif (CMAKE_SIZEOF_VOID_P EQUAL 8) |
35 |
| - # Build as X86_64 by default in 64-bit platform |
36 |
| - set (WAMR_BUILD_TARGET "X86_64") |
37 |
| - elseif (CMAKE_SIZEOF_VOID_P EQUAL 4) |
38 |
| - # Build as X86_32 by default in 32-bit platform |
39 |
| - set (WAMR_BUILD_TARGET "X86_32") |
40 |
| - else () |
| 26 | +set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") |
| 27 | +set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") |
| 28 | + |
| 29 | +# Check if the compiler supports the sanitizer flags |
| 30 | +include(CheckCXXCompilerFlag) |
| 31 | +check_cxx_compiler_flag("-fsanitize=address" HAS_ADDRESS_SANITIZER) |
| 32 | +check_cxx_compiler_flag("-fsanitize=memory" HAS_MEMORY_SANITIZER) |
| 33 | +check_cxx_compiler_flag("-fsanitize=undefined" HAS_UNDEFINED_SANITIZER) |
| 34 | + |
| 35 | +# Determine WAMR_BUILD_TARGET based on system properties |
| 36 | +if(NOT DEFINED WAMR_BUILD_TARGET) |
| 37 | + if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)") |
| 38 | + set(WAMR_BUILD_TARGET "AARCH64") |
| 39 | + elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64") |
| 40 | + set(WAMR_BUILD_TARGET "RISCV64") |
| 41 | + elseif(CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 42 | + set(WAMR_BUILD_TARGET "X86_64") |
| 43 | + elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) |
| 44 | + set(WAMR_BUILD_TARGET "X86_32") |
| 45 | + else() |
41 | 46 | message(SEND_ERROR "Unsupported build target platform!")
|
42 |
| - endif () |
43 |
| -endif () |
| 47 | + endif() |
| 48 | +endif() |
44 | 49 |
|
45 |
| -if (APPLE) |
| 50 | +if(APPLE) |
46 | 51 | add_definitions(-DBH_PLATFORM_DARWIN)
|
47 |
| -endif () |
| 52 | +endif() |
| 53 | + |
| 54 | +# Disable hardware bound check and enable AOT validator |
| 55 | +set(WAMR_DISABLE_HW_BOUND_CHECK 1) |
| 56 | +set(WAMR_BUILD_AOT_VALIDATOR 1) |
| 57 | + |
| 58 | +set(REPO_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..) |
| 59 | +message(STATUS "REPO_ROOT_DIR: ${REPO_ROOT_DIR}") |
| 60 | + |
| 61 | +# Use LLVM_DIR from command line if defined |
| 62 | +# LLVM_DIR should be something like /path/to/llvm/build/lib/cmake/llvm |
| 63 | +if(DEFINED LLVM_DIR) |
| 64 | + set(LLVM_DIR $ENV{LLVM_DIR}) |
| 65 | +else() |
| 66 | + set(LLVM_SRC_ROOT ${REPO_ROOT_DIR}/core/deps/llvm) |
| 67 | + set(LLVM_BUILD_ROOT ${LLVM_SRC_ROOT}/build) |
| 68 | + set(LLVM_DIR ${LLVM_BUILD_ROOT}/lib/cmake/llvm) |
| 69 | +endif() |
48 | 70 |
|
49 |
| -if(CUSTOM_MUTATOR EQUAL 1) |
50 |
| - add_compile_definitions(CUSTOM_MUTATOR) |
| 71 | +# if LLVM_DIR is an existing directory, use it |
| 72 | +if(NOT EXISTS ${LLVM_DIR}) |
| 73 | + message(FATAL_ERROR "LLVM_DIR not found: ${LLVM_DIR}") |
51 | 74 | endif()
|
52 | 75 |
|
53 |
| -if (NOT DEFINED WAMR_BUILD_INTERP) |
54 |
| - # Enable Interpreter by default |
55 |
| - set (WAMR_BUILD_INTERP 1) |
56 |
| -endif () |
57 |
| - |
58 |
| -if (NOT DEFINED WAMR_BUILD_AOT) |
59 |
| - # Enable AOT by default. |
60 |
| - set (WAMR_BUILD_AOT 1) |
61 |
| -endif () |
62 |
| - |
63 |
| -if (NOT DEFINED WAMR_BUILD_JIT) |
64 |
| - # Disable JIT by default. |
65 |
| - set (WAMR_BUILD_JIT 0) |
66 |
| -endif () |
67 |
| - |
68 |
| -if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN) |
69 |
| - # Disable libc builtin support by default |
70 |
| - set (WAMR_BUILD_LIBC_BUILTIN 0) |
71 |
| -endif () |
72 |
| - |
73 |
| -if (NOT DEFINED WAMR_BUILD_LIBC_WASI) |
74 |
| - # Enable libc wasi support by default |
75 |
| - set (WAMR_BUILD_LIBC_WASI 0) |
76 |
| -endif () |
77 |
| - |
78 |
| -if (NOT DEFINED WAMR_BUILD_FAST_INTERP) |
79 |
| - # Enable fast interpreter |
80 |
| - set (WAMR_BUILD_FAST_INTERP 1) |
81 |
| -endif () |
82 |
| - |
83 |
| -if (NOT DEFINED WAMR_BUILD_MULTI_MODULE) |
84 |
| - # Disable multiple modules |
85 |
| - set (WAMR_BUILD_MULTI_MODULE 0) |
86 |
| -endif () |
87 |
| - |
88 |
| -if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD) |
89 |
| - # Disable pthread library by default |
90 |
| - set (WAMR_BUILD_LIB_PTHREAD 0) |
91 |
| -endif () |
92 |
| - |
93 |
| -if (NOT DEFINED WAMR_BUILD_MINI_LOADER) |
94 |
| - # Disable wasm mini loader by default |
95 |
| - set (WAMR_BUILD_MINI_LOADER 0) |
96 |
| -endif () |
97 |
| - |
98 |
| -if (NOT DEFINED WAMR_BUILD_SIMD) |
99 |
| - # Enable SIMD by default |
100 |
| - set (WAMR_BUILD_SIMD 1) |
101 |
| -endif () |
102 |
| - |
103 |
| -if (NOT DEFINED WAMR_BUILD_REF_TYPES) |
104 |
| - # Enable reference type by default |
105 |
| - set (WAMR_BUILD_REF_TYPES 1) |
106 |
| -endif () |
107 |
| - |
108 |
| -if (NOT DEFINED WAMR_BUILD_DEBUG_INTERP) |
109 |
| - # Disable Debug feature by default |
110 |
| - set (WAMR_BUILD_DEBUG_INTERP 0) |
111 |
| -endif () |
112 |
| - |
113 |
| -if (WAMR_BUILD_DEBUG_INTERP EQUAL 1) |
114 |
| - set (WAMR_BUILD_FAST_INTERP 0) |
115 |
| - set (WAMR_BUILD_MINI_LOADER 0) |
116 |
| - set (WAMR_BUILD_SIMD 0) |
117 |
| -endif () |
118 |
| - |
119 |
| -# sanitizer may use kHandleSignalExclusive to handle SIGSEGV |
120 |
| -# like `UBSAN_OPTIONS=handle_segv=2:...` |
121 |
| -set (WAMR_DISABLE_HW_BOUND_CHECK 1) |
122 |
| -# Enable aot validator |
123 |
| -set (WAMR_BUILD_AOT_VALIDATOR 1) |
124 |
| - |
125 |
| -set (REPO_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..) |
126 |
| -message([ceith]:REPO_ROOT_DIR, ${REPO_ROOT_DIR}) |
127 |
| - |
128 |
| -set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") |
129 |
| -set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
130 |
| - |
131 |
| -add_definitions(-DWAMR_USE_MEM_POOL=0 -DWASM_ENABLE_FUZZ_TEST=1) |
| 76 | +find_package(LLVM REQUIRED CONFIG) |
| 77 | + |
| 78 | +message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") |
| 79 | +message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") |
| 80 | + |
| 81 | +include_directories(${LLVM_INCLUDE_DIRS}) |
| 82 | +separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS}) |
| 83 | +add_definitions(${LLVM_DEFINITIONS_LIST}) |
| 84 | + |
| 85 | +set(SHARED_DIR ${REPO_ROOT_DIR}/core/shared) |
| 86 | +set(IWASM_DIR ${REPO_ROOT_DIR}/core/iwasm) |
| 87 | + |
| 88 | +# Global setting |
| 89 | +add_compile_options(-Wno-unused-command-line-argument) |
132 | 90 |
|
133 | 91 | # Enable fuzzer
|
| 92 | +add_definitions(-DWASM_ENABLE_FUZZ_TEST=1) |
134 | 93 | add_compile_options(-fsanitize=fuzzer)
|
135 | 94 | add_link_options(-fsanitize=fuzzer)
|
136 | 95 |
|
137 |
| -# if not calling from oss-fuzz helper, enable all support sanitizers |
138 |
| -# oss-fuzz will define FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION in CFLAGS and CXXFLAGS |
| 96 | +# Enable sanitizers if not in oss-fuzz environment |
139 | 97 | set(CFLAGS_ENV $ENV{CFLAGS})
|
140 | 98 | string(FIND "${CFLAGS_ENV}" "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" IN_OSS_FUZZ)
|
141 |
| -if (IN_OSS_FUZZ EQUAL -1) |
142 |
| - message("[ceith]:Enable ASan and UBSan in non-oss-fuzz environment") |
143 |
| - add_compile_options( |
144 |
| - -fprofile-instr-generate -fcoverage-mapping |
145 |
| - -fno-sanitize-recover=all |
146 |
| - -fsanitize=address,undefined |
147 |
| - # reference: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html |
148 |
| - # -fsanitize=undefined: All of the checks listed above other than float-divide-by-zero, |
149 |
| - # unsigned-integer-overflow, implicit-conversion, local-bounds and |
150 |
| - # the nullability-* group of checks. |
151 |
| - # |
152 |
| - # for now, we disable below from UBSan |
153 |
| - # -alignment |
154 |
| - # -implicit-conversion |
155 |
| - # |
156 |
| - -fsanitize=float-divide-by-zero,unsigned-integer-overflow,local-bounds,nullability |
157 |
| - -fno-sanitize=alignment |
158 |
| - ) |
159 |
| - add_link_options(-fsanitize=address -fprofile-instr-generate) |
160 |
| -endif () |
161 |
| - |
162 |
| -include(${REPO_ROOT_DIR}/core/shared/utils/uncommon/shared_uncommon.cmake) |
163 |
| -include(${REPO_ROOT_DIR}/build-scripts/runtime_lib.cmake) |
164 |
| - |
165 |
| -add_library(vmlib |
166 |
| - ${WAMR_RUNTIME_LIB_SOURCE} |
167 |
| -) |
168 |
| - |
169 |
| -add_executable(wasm_mutator_fuzz wasm_mutator_fuzz.cc) |
170 |
| -target_link_libraries(wasm_mutator_fuzz vmlib -lm) |
| 99 | + |
| 100 | +add_subdirectory(aot-compiler) |
| 101 | +add_subdirectory(wasm-mutator) |
0 commit comments