|
| 1 | +# Fuzzing configuration |
| 2 | +# Supports both local fuzzing and OSS-Fuzz integration |
| 3 | + |
| 4 | +# Detect if we're running in OSS-Fuzz environment |
| 5 | +if(DEFINED ENV{LIB_FUZZING_ENGINE}) |
| 6 | + set(OSS_FUZZ ON) |
| 7 | + message(STATUS "OSS-Fuzz environment detected") |
| 8 | +else() |
| 9 | + set(OSS_FUZZ OFF) |
| 10 | +endif() |
| 11 | + |
| 12 | +# Auto-detect AFL++ compiler if not in OSS-Fuzz mode |
| 13 | +if(NOT OSS_FUZZ AND (CMAKE_C_COMPILER MATCHES ".*afl-.*" OR CMAKE_CXX_COMPILER MATCHES ".*afl-.*")) |
| 14 | + set(USE_AFLPLUSPLUS ON CACHE BOOL "Use AFL++ instead of libFuzzer" FORCE) |
| 15 | + message(STATUS "AFL++ compiler detected - automatically enabling AFL++ mode") |
| 16 | +endif() |
| 17 | + |
| 18 | +# When building for fuzzing, we want static library by default |
| 19 | +set(BTCPP_SHARED_LIBS OFF CACHE BOOL "Build static library for fuzzing" FORCE) |
| 20 | + |
| 21 | +# Only apply static linking settings if explicitly requested |
| 22 | +if(FORCE_STATIC_LINKING) |
| 23 | + set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES}) |
| 24 | + set(BUILD_SHARED_LIBS OFF) |
| 25 | + |
| 26 | + # Force static linking for dependencies |
| 27 | + if(BTCPP_GROOT_INTERFACE) |
| 28 | + set(ZeroMQ_USE_STATIC_LIBS ON) |
| 29 | + set(ZEROMQ_STATIC_LIBRARY ON) |
| 30 | + endif() |
| 31 | + |
| 32 | + if(BTCPP_SQLITE_LOGGING) |
| 33 | + set(SQLite3_USE_STATIC_LIBS ON) |
| 34 | + endif() |
| 35 | +endif() |
| 36 | + |
| 37 | +# Set up flags for local fuzzing (not used for OSS-Fuzz) |
| 38 | +if(NOT OSS_FUZZ) |
| 39 | + list(APPEND BASE_FLAGS -O2) |
| 40 | + |
| 41 | + if(USE_AFLPLUSPLUS) |
| 42 | + set(SANITIZER_FLAGS |
| 43 | + -fsanitize=address,undefined |
| 44 | + ) |
| 45 | + else() |
| 46 | + # For libFuzzer, use fuzzer-no-link for the library |
| 47 | + set(SANITIZER_FLAGS |
| 48 | + -fsanitize=address,undefined,fuzzer-no-link |
| 49 | + ) |
| 50 | + endif() |
| 51 | + |
| 52 | + # Apply sanitizer flags to the base library |
| 53 | + list(APPEND BASE_FLAGS ${SANITIZER_FLAGS}) |
| 54 | + |
| 55 | + add_compile_options(${BASE_FLAGS}) |
| 56 | + add_link_options(${BASE_FLAGS}) |
| 57 | +endif() |
| 58 | + |
| 59 | +# Disable certain features during fuzzing |
| 60 | +set(BTCPP_EXAMPLES OFF CACHE BOOL "Disable examples during fuzzing" FORCE) |
| 61 | +set(BTCPP_BUILD_TOOLS OFF CACHE BOOL "Disable tools during fuzzing" FORCE) |
| 62 | +set(BTCPP_UNIT_TESTS OFF CACHE BOOL "Disable tests during fuzzing" FORCE) |
| 63 | +set(BTCPP_SHARED_LIBS OFF CACHE BOOL "Build static library for fuzzing" FORCE) |
| 64 | + |
| 65 | +# Function to apply fuzzing flags for local development builds |
| 66 | +function(apply_local_fuzzing_flags target) |
| 67 | + target_compile_options(${target} PRIVATE |
| 68 | + ${BASE_FLAGS} |
| 69 | + ${SANITIZER_FLAGS} |
| 70 | + ) |
| 71 | + |
| 72 | + if(FORCE_STATIC_LINKING) |
| 73 | + if(USE_AFLPLUSPLUS) |
| 74 | + target_link_options(${target} PRIVATE |
| 75 | + ${BASE_FLAGS} |
| 76 | + ${SANITIZER_FLAGS} |
| 77 | + -static-libstdc++ |
| 78 | + -static-libgcc |
| 79 | + -fsanitize=fuzzer |
| 80 | + ) |
| 81 | + else() |
| 82 | + target_link_options(${target} PRIVATE |
| 83 | + ${BASE_FLAGS} |
| 84 | + -fsanitize=fuzzer |
| 85 | + ${SANITIZER_FLAGS} |
| 86 | + -static-libstdc++ |
| 87 | + -static-libgcc |
| 88 | + ) |
| 89 | + endif() |
| 90 | + else() |
| 91 | + if(USE_AFLPLUSPLUS) |
| 92 | + target_link_options(${target} PRIVATE |
| 93 | + ${BASE_FLAGS} |
| 94 | + ${SANITIZER_FLAGS} |
| 95 | + -fsanitize=fuzzer |
| 96 | + ) |
| 97 | + else() |
| 98 | + target_link_options(${target} PRIVATE |
| 99 | + ${BASE_FLAGS} |
| 100 | + -fsanitize=fuzzer |
| 101 | + ${SANITIZER_FLAGS} |
| 102 | + ) |
| 103 | + endif() |
| 104 | + endif() |
| 105 | +endfunction() |
| 106 | + |
| 107 | +# Function to add fuzzing targets - compatible with both local and OSS-Fuzz builds |
| 108 | +function(add_fuzzing_targets) |
| 109 | + set(FUZZERS bt_fuzzer script_fuzzer bb_fuzzer) |
| 110 | + |
| 111 | + foreach(fuzzer ${FUZZERS}) |
| 112 | + add_executable(${fuzzer} fuzzing/${fuzzer}.cpp) |
| 113 | + |
| 114 | + if(OSS_FUZZ) |
| 115 | + # For OSS-Fuzz environment, we rely on environment variables |
| 116 | + # like $CC, $CXX, $CFLAGS, $CXXFLAGS, and $LIB_FUZZING_ENGINE |
| 117 | + target_link_libraries(${fuzzer} PRIVATE |
| 118 | + ${BTCPP_LIBRARY} |
| 119 | + ${BTCPP_EXTRA_LIBRARIES} |
| 120 | + $ENV{LIB_FUZZING_ENGINE} |
| 121 | + ) |
| 122 | + else() |
| 123 | + # For local development, use our own flags |
| 124 | + apply_local_fuzzing_flags(${fuzzer}) |
| 125 | + target_link_libraries(${fuzzer} PRIVATE |
| 126 | + ${BTCPP_LIBRARY} |
| 127 | + ${BTCPP_EXTRA_LIBRARIES} |
| 128 | + ) |
| 129 | + endif() |
| 130 | + |
| 131 | + # Setup corpus directories (useful for both environments) |
| 132 | + set(CORPUS_DIR ${CMAKE_BINARY_DIR}/corpus/${fuzzer}) |
| 133 | + file(MAKE_DIRECTORY ${CORPUS_DIR}) |
| 134 | + endforeach() |
| 135 | + |
| 136 | + # Copy corpus files if they exist (useful for local testing) |
| 137 | + # OSS-Fuzz provides its own corpus handling |
| 138 | + if(NOT OSS_FUZZ) |
| 139 | + file(GLOB BT_CORPUS_FILES "${CMAKE_SOURCE_DIR}/fuzzing/corpus/bt_corpus/*") |
| 140 | + file(GLOB SCRIPT_CORPUS_FILES "${CMAKE_SOURCE_DIR}/fuzzing/corpus/script_corpus/*") |
| 141 | + file(GLOB BB_CORPUS_FILES "${CMAKE_SOURCE_DIR}/fuzzing/corpus/bb_corpus/*") |
| 142 | + |
| 143 | + if(BT_CORPUS_FILES) |
| 144 | + file(COPY ${BT_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR}/corpus/bt_fuzzer) |
| 145 | + endif() |
| 146 | + if(SCRIPT_CORPUS_FILES) |
| 147 | + file(COPY ${SCRIPT_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR}/corpus/script_fuzzer) |
| 148 | + endif() |
| 149 | + if(BB_CORPUS_FILES) |
| 150 | + file(COPY ${BB_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR}/corpus/bb_fuzzer) |
| 151 | + endif() |
| 152 | + endif() |
| 153 | +endfunction() |
0 commit comments