Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | cmake_minimum_required(VERSION 3.9) |
| 2 | |
| 3 | set(CMAKE_VERBOSE_MAKEFILE ON) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 4 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
| 5 | set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 6 | |
| 7 | project(FlatBuffersFuzzerTests) |
| 8 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 9 | option(BUILD_DEBUGGER "Compile a debugger with main() and without libFuzzer" OFF) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 10 | |
| 11 | if(NOT DEFINED FLATBUFFERS_MAX_PARSING_DEPTH) |
| 12 | # Force checking of RecursionError in the test |
| 13 | set(FLATBUFFERS_MAX_PARSING_DEPTH 8) |
| 14 | endif() |
| 15 | message(STATUS "FLATBUFFERS_MAX_PARSING_DEPTH: ${FLATBUFFERS_MAX_PARSING_DEPTH}") |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 16 | |
| 17 | # Usage '-fsanitize=address' doesn't allowed with '-fsanitize=memory'. |
| 18 | # MemorySanitizer will not work out-of-the-box, and will instead report false |
| 19 | # positives coming from uninstrumented code. Need to re-build both C++ standard |
| 20 | # library: https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo |
| 21 | option(USE_ASAN "Use fuzzers with ASASN" OFF) |
| 22 | option(USE_MSAN "Use fuzzers with MSASN" OFF) |
| 23 | option(OSS_FUZZ "Set this option to use flags by oss-fuzz" OFF) |
| 24 | |
| 25 | # Use Clang linker. |
| 26 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld") |
| 27 | |
| 28 | # add_link_options(-stdlib=libc++) |
| 29 | |
| 30 | add_compile_options( |
| 31 | # -stdlib=libc++ # Use Clang libc++ instead of GNU. |
| 32 | -std=c++14 |
| 33 | -Wall |
| 34 | -pedantic |
| 35 | -Werror |
| 36 | -Wextra |
| 37 | -Wno-unused-parameter |
| 38 | -fsigned-char |
| 39 | -fno-omit-frame-pointer |
| 40 | -g # Generate source-level debug information |
| 41 | # -flto # enable link-time optimisation |
| 42 | ) |
| 43 | |
| 44 | # https://llvm.org/docs/Passes.html save IR to see call graph make one bitcode |
| 45 | # file:> llvm-link *.bc -o out.bc print call-graph:> opt out.bc -analyze -print- |
| 46 | # callgraph &> callgraph.txt set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -save-temps |
| 47 | # -flto") |
| 48 | |
| 49 | # A special target with fuzzer+sanitizer flags. |
| 50 | add_library(fuzzer_config INTERFACE) |
| 51 | |
| 52 | target_compile_options( |
| 53 | fuzzer_config |
| 54 | INTERFACE |
| 55 | #-fsanitize-coverage=edge,trace-cmp |
| 56 | $<$<BOOL:${USE_ASAN}>: |
| 57 | -fsanitize=fuzzer,undefined,address |
| 58 | > |
| 59 | $<$<BOOL:${USE_MSAN}>: |
| 60 | -fsanitize=fuzzer,undefined,memory |
| 61 | -fsanitize-memory-track-origins=2 |
| 62 | > |
| 63 | $<$<BOOL:${OSS_FUZZ}>: |
| 64 | ${CXX} |
| 65 | ${CXXFLAGS} |
| 66 | > |
| 67 | ) |
| 68 | |
| 69 | target_link_libraries( |
| 70 | fuzzer_config |
| 71 | INTERFACE |
| 72 | $<$<BOOL:${USE_ASAN}>: |
| 73 | -fsanitize=fuzzer,undefined,address |
| 74 | > |
| 75 | $<$<BOOL:${USE_MSAN}>: |
| 76 | -fsanitize=fuzzer,undefined,memory |
| 77 | > |
| 78 | $<$<BOOL:${OSS_FUZZ}>: |
| 79 | $ENV{LIB_FUZZING_ENGINE} |
| 80 | > |
| 81 | ) |
| 82 | |
| 83 | set(FLATBUFFERS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../") |
| 84 | |
| 85 | set(FlatBuffers_Library_SRCS |
| 86 | ${FLATBUFFERS_DIR}/include/flatbuffers/base.h |
| 87 | ${FLATBUFFERS_DIR}/include/flatbuffers/flatbuffers.h |
| 88 | ${FLATBUFFERS_DIR}/include/flatbuffers/hash.h |
| 89 | ${FLATBUFFERS_DIR}/include/flatbuffers/idl.h |
| 90 | ${FLATBUFFERS_DIR}/include/flatbuffers/util.h |
| 91 | ${FLATBUFFERS_DIR}/include/flatbuffers/reflection.h |
| 92 | ${FLATBUFFERS_DIR}/include/flatbuffers/reflection_generated.h |
| 93 | ${FLATBUFFERS_DIR}/include/flatbuffers/stl_emulation.h |
| 94 | ${FLATBUFFERS_DIR}/include/flatbuffers/flexbuffers.h |
| 95 | ${FLATBUFFERS_DIR}/include/flatbuffers/registry.h |
| 96 | ${FLATBUFFERS_DIR}/include/flatbuffers/minireflect.h |
| 97 | ${FLATBUFFERS_DIR}/src/idl_parser.cpp |
| 98 | ${FLATBUFFERS_DIR}/src/idl_gen_text.cpp |
| 99 | ${FLATBUFFERS_DIR}/src/reflection.cpp |
| 100 | ${FLATBUFFERS_DIR}/src/util.cpp |
| 101 | ${FLATBUFFERS_DIR}/tests/test_assert.cpp |
| 102 | ) |
| 103 | |
| 104 | include_directories(${FLATBUFFERS_DIR}/include) |
| 105 | include_directories(${FLATBUFFERS_DIR}/tests) |
| 106 | |
| 107 | add_library(flatbuffers_fuzzed STATIC ${FlatBuffers_Library_SRCS}) |
| 108 | # Use PUBLIC to force 'fuzzer_config' for all dependent targets |
| 109 | target_link_libraries(flatbuffers_fuzzed PUBLIC fuzzer_config) |
| 110 | |
| 111 | # FLATBUFFERS_ASSERT should assert in Release as well. Redefine |
| 112 | # FLATBUFFERS_ASSERT macro definition. Declare as PUBLIC to cover asserts in all |
| 113 | # included header files. |
| 114 | target_compile_definitions( |
| 115 | flatbuffers_fuzzed |
| 116 | PUBLIC |
| 117 | FLATBUFFERS_ASSERT=fuzzer_assert_impl |
| 118 | FLATBUFFERS_ASSERT_INCLUDE="${CMAKE_CURRENT_SOURCE_DIR}/fuzzer_assert.h" |
| 119 | PRIVATE |
| 120 | FLATBUFFERS_MAX_PARSING_DEPTH=${FLATBUFFERS_MAX_PARSING_DEPTH} |
| 121 | ) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 122 | |
| 123 | # Setup fuzzer tests. |
| 124 | |
| 125 | add_executable(scalar_fuzzer flatbuffers_scalar_fuzzer.cc) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 126 | target_link_libraries(scalar_fuzzer PRIVATE flatbuffers_fuzzed) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 127 | |
| 128 | add_executable(parser_fuzzer flatbuffers_parser_fuzzer.cc) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 129 | target_link_libraries(parser_fuzzer PRIVATE flatbuffers_fuzzed) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 130 | |
| 131 | add_executable(verifier_fuzzer flatbuffers_verifier_fuzzer.cc) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 132 | target_link_libraries(verifier_fuzzer PRIVATE flatbuffers_fuzzed) |
| 133 | |
| 134 | # Build debugger for weird cases found with fuzzer. |
| 135 | if(BUILD_DEBUGGER) |
| 136 | add_library(flatbuffers_nonfuzz STATIC ${FlatBuffers_Library_SRCS}) |
| 137 | target_compile_definitions( |
| 138 | flatbuffers_nonfuzz |
| 139 | PUBLIC |
| 140 | FLATBUFFERS_ASSERT=fuzzer_assert_impl |
| 141 | FLATBUFFERS_ASSERT_INCLUDE="${CMAKE_CURRENT_SOURCE_DIR}/fuzzer_assert.h" |
| 142 | PRIVATE |
| 143 | FLATBUFFERS_MAX_PARSING_DEPTH=${FLATBUFFERS_MAX_PARSING_DEPTH} |
| 144 | ) |
| 145 | add_executable(scalar_debug flatbuffers_scalar_fuzzer.cc scalar_debug.cpp) |
| 146 | target_link_libraries(scalar_debug PRIVATE flatbuffers_nonfuzz) |
| 147 | endif(BUILD_DEBUGGER) |