Skip to content

Commit edae5b3

Browse files
committed
feat(fuzz): add AOT compiler fuzzing tests and CMake configuration
1 parent 5f519fd commit edae5b3

File tree

5 files changed

+275
-148
lines changed

5 files changed

+275
-148
lines changed
Lines changed: 49 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,79 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
cmake_minimum_required (VERSION 2.8)
4+
cmake_minimum_required(VERSION 3.14)
55

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(wasm_fuzzing LANGUAGES C CXX)
127

13-
project(wasm_mutator)
8+
# Ensure Clang is used as the compiler
9+
if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang" OR NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
10+
message(FATAL_ERROR "Please use Clang as the C compiler for libFuzzer compatibility.")
11+
endif()
1412

15-
set (CMAKE_BUILD_TYPE Debug)
13+
set(CMAKE_BUILD_TYPE Debug)
14+
set(CMAKE_C_STANDARD 11)
15+
set(CMAKE_CXX_STANDARD 17)
1616

17-
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
17+
string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
1818

1919
# 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 ()
20+
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
21+
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
22+
23+
# Determine WAMR_BUILD_TARGET based on system properties
24+
if(NOT DEFINED WAMR_BUILD_TARGET)
25+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
26+
set(WAMR_BUILD_TARGET "AARCH64")
27+
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
28+
set(WAMR_BUILD_TARGET "RISCV64")
29+
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
30+
set(WAMR_BUILD_TARGET "X86_64")
31+
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
32+
set(WAMR_BUILD_TARGET "X86_32")
33+
else()
4134
message(SEND_ERROR "Unsupported build target platform!")
42-
endif ()
43-
endif ()
35+
endif()
36+
endif()
4437

45-
if (APPLE)
38+
if(APPLE)
4639
add_definitions(-DBH_PLATFORM_DARWIN)
47-
endif ()
48-
49-
if(CUSTOM_MUTATOR EQUAL 1)
50-
add_compile_definitions(CUSTOM_MUTATOR)
5140
endif()
5241

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)
42+
# Disable hardware bound check and enable AOT validator
43+
set(WAMR_DISABLE_HW_BOUND_CHECK 1)
44+
set(WAMR_BUILD_AOT_VALIDATOR 1)
45+
46+
set(REPO_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../..)
47+
message(STATUS "REPO_ROOT_DIR: ${REPO_ROOT_DIR}")
48+
49+
set(LLVM_SRC_ROOT ${REPO_ROOT_DIR}/core/deps/llvm)
50+
set(LLVM_BUILD_ROOT ${LLVM_SRC_ROOT}/build)
51+
set(LLVM_DIR ${LLVM_BUILD_ROOT}/lib/cmake/llvm)
52+
53+
find_package(LLVM REQUIRED CONFIG)
54+
55+
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
56+
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
13257

13358
# Enable fuzzer
59+
add_definitions(-DWASM_ENABLE_FUZZ_TEST=1)
13460
add_compile_options(-fsanitize=fuzzer)
13561
add_link_options(-fsanitize=fuzzer)
13662

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
63+
# Enable sanitizers if not in oss-fuzz environment
13964
set(CFLAGS_ENV $ENV{CFLAGS})
14065
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")
66+
if(IN_OSS_FUZZ EQUAL -1)
67+
message(STATUS "Enable ASan and UBSan in non-oss-fuzz environment")
14368
add_compile_options(
14469
-fprofile-instr-generate -fcoverage-mapping
14570
-fno-sanitize-recover=all
14671
-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-
#
15672
-fsanitize=float-divide-by-zero,unsigned-integer-overflow,local-bounds,nullability
15773
-fno-sanitize=alignment
15874
)
15975
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 ${WAMR_RUNTIME_LIB_SOURCE})
166-
167-
add_executable(wasm_mutator_fuzz wasm_mutator_fuzz.cc)
168-
target_link_libraries(wasm_mutator_fuzz vmlib -lm)
169-
170-
find_package(LLVM REQUIRED CONFIG)
171-
172-
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
173-
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
76+
endif()
17477

175-
include(${REPO_ROOT_DIR}/core/iwasm/compilation/iwasm_compl.cmake)
176-
add_library(aotclib ${IWASM_COMPL_SOURCE})
177-
target_compile_definitions(aotclib PUBLIC -DWASM_ENABLE_WAMR_COMPILER=1 ${LLVM_DEFINITIONS})
178-
target_include_directories(aotclib PUBLIC ${LLVM_INCLUDE_DIRS})
78+
add_subdirectory(wasm_mutator)
79+
add_subdirectory(aot_compiler)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
project(aot_compiler_fuzzing LANGUAGES ASM C CXX)
5+
6+
set(SHARED_DIR ${REPO_ROOT_DIR}/core/shared)
7+
set(IWASM_DIR ${REPO_ROOT_DIR}/core/iwasm)
8+
9+
# Set default build options with the ability to override from the command line
10+
if(NOT WAMR_BUILD_INTERP)
11+
set(WAMR_BUILD_INTERP 1)
12+
endif()
13+
14+
set(WAMR_BUILD_WAMR_COMPILER 1)
15+
set(WAMR_BUILD_INTERP 1)
16+
set(WAMR_BUILD_AOT 1)
17+
18+
include(${SHARED_DIR}/platform/${WAMR_BUILD_PLATFORM}/shared_platform.cmake)
19+
include(${SHARED_DIR}/mem-alloc/mem_alloc.cmake)
20+
include(${SHARED_DIR}/utils/shared_utils.cmake)
21+
include(${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
22+
include(${IWASM_DIR}/compilation/iwasm_compl.cmake)
23+
include(${IWASM_DIR}/libraries/thread-mgr/thread_mgr.cmake)
24+
include(${IWASM_DIR}/common/iwasm_common.cmake)
25+
include(${IWASM_DIR}/common/gc/iwasm_gc.cmake)
26+
include(${IWASM_DIR}/interpreter/iwasm_interp.cmake)
27+
include(${IWASM_DIR}/aot/iwasm_aot.cmake)
28+
include(${IWASM_DIR}/compilation/iwasm_compl.cmake)
29+
include(${REPO_ROOT_DIR}/build-scripts/version.cmake)
30+
31+
add_library(aotclib
32+
${PLATFORM_SHARED_SOURCE}
33+
${MEM_ALLOC_SHARED_SOURCE}
34+
${UTILS_SHARED_SOURCE}
35+
${UNCOMMON_SHARED_SOURCE}
36+
${THREAD_MGR_SOURCE}
37+
${IWASM_COMMON_SOURCE}
38+
${IWASM_INTERP_SOURCE}
39+
${IWASM_AOT_SOURCE}
40+
${IWASM_GC_SOURCE}
41+
${IWASM_COMPL_SOURCE}
42+
)
43+
44+
target_compile_definitions(aotclib
45+
PUBLIC
46+
-DWASM_ENABLE_WAMR_COMPILER=1
47+
-DWASM_ENABLE_FAST_INTERP=0
48+
-DWASM_ENABLE_INTERP=1
49+
-DWASM_ENABLE_BULK_MEMORY=1
50+
-DWASM_ENABLE_SHARED_MEMORY=1
51+
-DWASM_ENABLE_TAIL_CALL=1
52+
-DWASM_ENABLE_SIMD=1
53+
-DWASM_ENABLE_REF_TYPES=1
54+
-DWASM_ENABLE_MEMORY64=1
55+
-DWASM_ENABLE_GC=1
56+
-DWASM_ENABLE_CUSTOM_NAME_SECTION=1
57+
-DWASM_ENABLE_AOT_STACK_FRAME=1
58+
-DWASM_ENABLE_DUMP_CALL_STACK=1
59+
-DWASM_ENABLE_PERF_PROFILING=1
60+
-DWASM_ENABLE_LOAD_CUSTOM_SECTION=1
61+
-DWASM_ENABLE_THREAD_MGR=1
62+
${LLVM_DEFINITIONS}
63+
)
64+
65+
target_include_directories(aotclib PUBLIC
66+
${LLVM_INCLUDE_DIRS}
67+
${IWASM_DIR}/include
68+
${SHARED_DIR}/include
69+
)
70+
target_link_libraries(aotclib PUBLIC ${LLVM_AVAILABLE_LIBS})
71+
72+
add_executable(aot_compiler_fuzz aot_compiler_fuzz.cc)
73+
target_link_libraries(aot_compiler_fuzz PRIVATE aotclib m)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
#include <errno.h>
7+
#include <string.h>
8+
#include <iostream>
9+
#include <vector>
10+
11+
#include "aot_export.h"
12+
#include "wasm_export.h"
13+
#include "bh_read_file.h"
14+
15+
static void
16+
handle_aot_recent_error(const char *tag)
17+
{
18+
const char *error = aot_get_last_error();
19+
if (strlen(error) == 0) {
20+
error = "UNKNOWN ERROR";
21+
}
22+
23+
std::cout << tag << " " << error << std::endl;
24+
}
25+
26+
extern "C" int
27+
LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
28+
{
29+
wasm_module_t module = NULL;
30+
char error_buf[128] = { 0 };
31+
AOTCompOption option = { 0 };
32+
aot_comp_data_t comp_data = NULL;
33+
aot_comp_context_t comp_ctx = NULL;
34+
35+
/* libfuzzer don't allow to modify the given Data, so make a copy here */
36+
std::vector<uint8_t> myData(Data, Data + Size);
37+
38+
wasm_runtime_init();
39+
40+
module = wasm_runtime_load((uint8_t *)myData.data(), Size, error_buf, 120);
41+
if (!module) {
42+
std::cout << "[LOADING] " << error_buf << std::endl;
43+
goto DESTROY_RUNTIME;
44+
}
45+
46+
// TODO: target_arch and other fields
47+
option.target_arch = "x86_64";
48+
option.target_abi = "gnu";
49+
option.enable_bulk_memory = true;
50+
option.enable_thread_mgr = true;
51+
option.enable_tail_call = true;
52+
option.enable_simd = true;
53+
option.enable_ref_types = true;
54+
option.enable_gc = true;
55+
56+
comp_data =
57+
aot_create_comp_data(module, option.target_arch, option.enable_gc);
58+
if (!comp_data) {
59+
handle_aot_recent_error("[CREATING comp_data]");
60+
goto UNLOAD_MODULE;
61+
}
62+
63+
comp_ctx = aot_create_comp_context(comp_data, &option);
64+
if (!comp_ctx) {
65+
handle_aot_recent_error("[CREATING comp_context]");
66+
goto DESTROY_COMP_DATA;
67+
}
68+
69+
if (!aot_compile_wasm(comp_ctx)) {
70+
handle_aot_recent_error("[COMPILING]");
71+
goto DESTROY_COMP_CTX;
72+
}
73+
74+
DESTROY_COMP_CTX:
75+
aot_destroy_comp_context(comp_ctx);
76+
DESTROY_COMP_DATA:
77+
aot_destroy_comp_data(comp_data);
78+
UNLOAD_MODULE:
79+
wasm_runtime_unload(module);
80+
DESTROY_RUNTIME:
81+
wasm_runtime_destroy();
82+
83+
/* Values other than 0 and -1 are reserved for future use. */
84+
return 0;
85+
}

0 commit comments

Comments
 (0)