1cmake_minimum_required(VERSION 3.9) 2 3set(CMAKE_VERBOSE_MAKEFILE ON) 4set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 5set(CMAKE_POSITION_INDEPENDENT_CODE ON) 6 7project(FlatBuffersFuzzerTests) 8 9option(BUILD_DEBUGGER "Compile a debugger with main() and without libFuzzer" OFF) 10 11if(NOT DEFINED FLATBUFFERS_MAX_PARSING_DEPTH) 12 # Force checking of RecursionError in the test 13 set(FLATBUFFERS_MAX_PARSING_DEPTH 24) 14endif() 15message(STATUS "FLATBUFFERS_MAX_PARSING_DEPTH: ${FLATBUFFERS_MAX_PARSING_DEPTH}") 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 21option(USE_ASAN "Use fuzzers with ASASN" OFF) 22option(USE_MSAN "Use fuzzers with MSASN" OFF) 23option(OSS_FUZZ "Set this option to use flags by oss-fuzz" OFF) 24 25# Use Clang linker. 26set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld") 27 28# add_link_options(-stdlib=libc++) 29 30add_compile_options( 31 # -stdlib=libc++ # Use Clang libc++ instead of GNU. 32 -std=c++17 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. 50add_library(fuzzer_config INTERFACE) 51 52target_compile_options( 53 fuzzer_config 54 INTERFACE 55 $<$<NOT:$<BOOL:${OSS_FUZZ}>>: 56 -fsanitize-coverage=trace-cmp 57 > 58 $<$<BOOL:${USE_ASAN}>: 59 -fsanitize=fuzzer,undefined,address 60 > 61 $<$<BOOL:${USE_MSAN}>: 62 -fsanitize=fuzzer,undefined,memory 63 -fsanitize-memory-track-origins=2 64 > 65 $<$<BOOL:${OSS_FUZZ}>: 66 ${CXX} 67 ${CXXFLAGS} 68 > 69) 70 71target_link_libraries( 72 fuzzer_config 73 INTERFACE 74 $<$<BOOL:${USE_ASAN}>: 75 -fsanitize=fuzzer,undefined,address 76 > 77 $<$<BOOL:${USE_MSAN}>: 78 -fsanitize=fuzzer,undefined,memory 79 > 80 $<$<BOOL:${OSS_FUZZ}>: 81 $ENV{LIB_FUZZING_ENGINE} 82 > 83) 84 85set(FLATBUFFERS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../") 86 87set(FlatBuffers_Library_SRCS 88 ${FLATBUFFERS_DIR}/include/flatbuffers/allocator.h 89 ${FLATBUFFERS_DIR}/include/flatbuffers/array.h 90 ${FLATBUFFERS_DIR}/include/flatbuffers/base.h 91 ${FLATBUFFERS_DIR}/include/flatbuffers/buffer.h 92 ${FLATBUFFERS_DIR}/include/flatbuffers/buffer_ref.h 93 ${FLATBUFFERS_DIR}/include/flatbuffers/default_allocator.h 94 ${FLATBUFFERS_DIR}/include/flatbuffers/detached_buffer.h 95 ${FLATBUFFERS_DIR}/include/flatbuffers/flatbuffer_builder.h 96 ${FLATBUFFERS_DIR}/include/flatbuffers/flatbuffers.h 97 ${FLATBUFFERS_DIR}/include/flatbuffers/flexbuffers.h 98 ${FLATBUFFERS_DIR}/include/flatbuffers/flex_flat_util.h 99 ${FLATBUFFERS_DIR}/include/flatbuffers/hash.h 100 ${FLATBUFFERS_DIR}/include/flatbuffers/idl.h 101 ${FLATBUFFERS_DIR}/include/flatbuffers/minireflect.h 102 ${FLATBUFFERS_DIR}/include/flatbuffers/reflection.h 103 ${FLATBUFFERS_DIR}/include/flatbuffers/reflection_generated.h 104 ${FLATBUFFERS_DIR}/include/flatbuffers/registry.h 105 ${FLATBUFFERS_DIR}/include/flatbuffers/stl_emulation.h 106 ${FLATBUFFERS_DIR}/include/flatbuffers/string.h 107 ${FLATBUFFERS_DIR}/include/flatbuffers/struct.h 108 ${FLATBUFFERS_DIR}/include/flatbuffers/table.h 109 ${FLATBUFFERS_DIR}/include/flatbuffers/util.h 110 ${FLATBUFFERS_DIR}/include/flatbuffers/vector.h 111 ${FLATBUFFERS_DIR}/include/flatbuffers/vector_downward.h 112 ${FLATBUFFERS_DIR}/include/flatbuffers/verifier.h 113 ${FLATBUFFERS_DIR}/src/idl_parser.cpp 114 ${FLATBUFFERS_DIR}/src/idl_gen_text.cpp 115 ${FLATBUFFERS_DIR}/src/reflection.cpp 116 ${FLATBUFFERS_DIR}/src/binary_annotator.h 117 ${FLATBUFFERS_DIR}/src/binary_annotator.cpp 118 ${FLATBUFFERS_DIR}/src/util.cpp 119 ${FLATBUFFERS_DIR}/tests/test_assert.cpp 120) 121 122include_directories(${FLATBUFFERS_DIR}/include) 123include_directories(${FLATBUFFERS_DIR}/tests) 124include_directories(${FLATBUFFERS_DIR}/src) 125 126add_library(flatbuffers_fuzzed STATIC ${FlatBuffers_Library_SRCS}) 127# Use PUBLIC to force 'fuzzer_config' for all dependent targets 128target_link_libraries(flatbuffers_fuzzed PUBLIC fuzzer_config) 129 130# FLATBUFFERS_ASSERT should assert in Release as well. Redefine 131# FLATBUFFERS_ASSERT macro definition. Declare as PUBLIC to cover asserts in all 132# included header files. 133target_compile_definitions( 134 flatbuffers_fuzzed 135 PUBLIC 136 FLATBUFFERS_ASSERT=fuzzer_assert_impl 137 FLATBUFFERS_ASSERT_INCLUDE="${CMAKE_CURRENT_SOURCE_DIR}/fuzzer_assert.h" 138 PRIVATE 139 FLATBUFFERS_MAX_PARSING_DEPTH=${FLATBUFFERS_MAX_PARSING_DEPTH} 140) 141 142# Setup fuzzer tests. 143 144add_executable(scalar_fuzzer flatbuffers_scalar_fuzzer.cc) 145target_link_libraries(scalar_fuzzer PRIVATE flatbuffers_fuzzed) 146 147add_executable(parser_fuzzer flatbuffers_parser_fuzzer.cc) 148target_link_libraries(parser_fuzzer PRIVATE flatbuffers_fuzzed) 149 150add_executable(verifier_fuzzer flatbuffers_verifier_fuzzer.cc) 151target_link_libraries(verifier_fuzzer PRIVATE flatbuffers_fuzzed) 152 153add_executable(flexverifier_fuzzer flexbuffers_verifier_fuzzer.cc) 154target_link_libraries(flexverifier_fuzzer PRIVATE flatbuffers_fuzzed) 155 156add_executable(monster_fuzzer flatbuffers_monster_fuzzer.cc) 157target_link_libraries(monster_fuzzer PRIVATE flatbuffers_fuzzed) 158add_custom_command( 159 TARGET monster_fuzzer PRE_BUILD 160 COMMAND ${CMAKE_COMMAND} -E copy 161 ${CMAKE_SOURCE_DIR}/../monster_test.bfbs 162 ${CMAKE_CURRENT_BINARY_DIR}/monster_test.bfbs) 163 164add_executable(annotator_fuzzer flatbuffers_annotator_fuzzer.cc) 165target_link_libraries(annotator_fuzzer PRIVATE flatbuffers_fuzzed) 166add_custom_command( 167 TARGET annotator_fuzzer PRE_BUILD 168 169 COMMAND ${CMAKE_COMMAND} -E copy 170 ${CMAKE_SOURCE_DIR}/../annotated_binary/annotated_binary.bfbs 171 ${CMAKE_CURRENT_BINARY_DIR}/annotated_binary.bfbs 172 173 COMMAND ${CMAKE_COMMAND} -E copy 174 ${CMAKE_SOURCE_DIR}/../annotated_binary/annotated_binary.bin 175 ${CMAKE_CURRENT_BINARY_DIR}/seed_annotator/annotated_binary.bin 176) 177 178# Build debugger for weird cases found with fuzzer. 179if(BUILD_DEBUGGER) 180 add_library(flatbuffers_nonfuzz STATIC ${FlatBuffers_Library_SRCS}) 181 target_compile_options( 182 flatbuffers_nonfuzz 183 PUBLIC 184 $<$<BOOL:${USE_ASAN}>: 185 -fsanitize=undefined,address 186 > 187 -fno-limit-debug-info 188 ) 189 190 target_link_libraries( 191 flatbuffers_nonfuzz 192 PUBLIC 193 $<$<BOOL:${USE_ASAN}>: 194 -fsanitize=undefined,address 195 > 196 ) 197 198 target_compile_definitions( 199 flatbuffers_nonfuzz 200 PUBLIC 201 FLATBUFFERS_ASSERT=fuzzer_assert_impl 202 FLATBUFFERS_ASSERT_INCLUDE="${CMAKE_CURRENT_SOURCE_DIR}/fuzzer_assert.h" 203 PRIVATE 204 FLATBUFFERS_MAX_PARSING_DEPTH=${FLATBUFFERS_MAX_PARSING_DEPTH} 205 ) 206 add_executable(scalar_debug 207 flatbuffers_scalar_fuzzer.cc 208 scalar_debug.cpp 209 ) 210 target_link_libraries(scalar_debug PRIVATE flatbuffers_nonfuzz) 211 212 add_executable(monster_debug 213 flatbuffers_monster_fuzzer.cc 214 monster_debug.cpp 215 ) 216 target_link_libraries(monster_debug PRIVATE flatbuffers_nonfuzz) 217 add_custom_command( 218 TARGET monster_debug PRE_BUILD 219 COMMAND ${CMAKE_COMMAND} -E copy 220 ${CMAKE_SOURCE_DIR}/../monster_test.bfbs 221 ${CMAKE_CURRENT_BINARY_DIR}/monster_test.bfbs) 222 223endif(BUILD_DEBUGGER) 224