1cmake_minimum_required(VERSION 3.9)
2
3set(CMAKE_VERBOSE_MAKEFILE ON)
4
5set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
6set(CMAKE_POSITION_INDEPENDENT_CODE ON)
7
8project(FlatBuffersFuzzerTests)
9
10set(CMAKE_CXX_FLAGS
11  "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -pedantic -Werror -Wextra -Wno-unused-parameter -fsigned-char")
12
13set(CMAKE_CXX_FLAGS
14  "${CMAKE_CXX_FLAGS} -g -fsigned-char -fno-omit-frame-pointer")
15
16# Typical slowdown introduced by MemorySanitizer (memory) is 3x.
17# '-fsanitize=address' not allowed with '-fsanitize=memory'
18if(YES)
19  set(CMAKE_CXX_FLAGS
20    "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer,address,undefined")
21else()
22  set(CMAKE_CXX_FLAGS
23    "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer,memory,undefined -fsanitize-memory-track-origins=2")
24endif()
25
26set(CMAKE_CXX_FLAGS
27  "${CMAKE_CXX_FLAGS} -fsanitize-coverage=edge,trace-cmp")
28
29# enable link-time optimisation
30# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
31
32# https://llvm.org/docs/Passes.html
33# save IR to see call graph
34# make one bitcode file:> llvm-link *.bc -o out.bc
35# print call-graph:> opt out.bc -analyze -print-callgraph &> callgraph.txt
36# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -save-temps -flto")
37
38set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
39
40set(FLATBUFFERS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../")
41
42set(FlatBuffers_Library_SRCS
43  ${FLATBUFFERS_DIR}/include/flatbuffers/code_generators.h
44  ${FLATBUFFERS_DIR}/include/flatbuffers/base.h
45  ${FLATBUFFERS_DIR}/include/flatbuffers/flatbuffers.h
46  ${FLATBUFFERS_DIR}/include/flatbuffers/hash.h
47  ${FLATBUFFERS_DIR}/include/flatbuffers/idl.h
48  ${FLATBUFFERS_DIR}/include/flatbuffers/util.h
49  ${FLATBUFFERS_DIR}/include/flatbuffers/reflection.h
50  ${FLATBUFFERS_DIR}/include/flatbuffers/reflection_generated.h
51  ${FLATBUFFERS_DIR}/include/flatbuffers/stl_emulation.h
52  ${FLATBUFFERS_DIR}/include/flatbuffers/flexbuffers.h
53  ${FLATBUFFERS_DIR}/include/flatbuffers/registry.h
54  ${FLATBUFFERS_DIR}/include/flatbuffers/minireflect.h
55  ${FLATBUFFERS_DIR}/src/code_generators.cpp
56  ${FLATBUFFERS_DIR}/src/idl_parser.cpp
57  ${FLATBUFFERS_DIR}/src/idl_gen_text.cpp
58  ${FLATBUFFERS_DIR}/src/reflection.cpp
59  ${FLATBUFFERS_DIR}/src/util.cpp
60  ${FLATBUFFERS_DIR}/tests/test_assert.cpp
61)
62
63include_directories(${FLATBUFFERS_DIR}/include)
64include_directories(${FLATBUFFERS_DIR}/tests)
65add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS})
66
67# FLATBUFFERS_ASSERT should assert in Release as well.
68# Redefine FLATBUFFERS_ASSERT macro definition.
69# Declare as PUBLIC to cover asserts in all included header files.
70target_compile_definitions(flatbuffers PUBLIC
71   FLATBUFFERS_ASSERT=fuzzer_assert_impl)
72target_compile_definitions(flatbuffers PUBLIC
73   FLATBUFFERS_ASSERT_INCLUDE="${CMAKE_CURRENT_SOURCE_DIR}/fuzzer_assert.h")
74
75if(NOT DEFINED FLATBUFFERS_MAX_PARSING_DEPTH)
76  # Force checking of RecursionError in the test
77  set(FLATBUFFERS_MAX_PARSING_DEPTH 8)
78endif()
79message(STATUS "FLATBUFFERS_MAX_PARSING_DEPTH: ${FLATBUFFERS_MAX_PARSING_DEPTH}")
80target_compile_definitions(flatbuffers PRIVATE FLATBUFFERS_MAX_PARSING_DEPTH=8)
81
82# Setup fuzzer tests.
83
84add_executable(scalar_fuzzer flatbuffers_scalar_fuzzer.cc)
85target_link_libraries(scalar_fuzzer PRIVATE flatbuffers)
86
87add_executable(parser_fuzzer flatbuffers_parser_fuzzer.cc)
88target_link_libraries(parser_fuzzer PRIVATE flatbuffers)
89
90add_executable(verifier_fuzzer flatbuffers_verifier_fuzzer.cc)
91target_link_libraries(verifier_fuzzer PRIVATE flatbuffers)
92