Skip to content

Commit c4be5cb

Browse files
committed
refactor: split root-level CMakeLists.txt to isolate fuzzing setup
1 parent 62bdd1f commit c4be5cb

File tree

2 files changed

+156
-124
lines changed

2 files changed

+156
-124
lines changed

CMakeLists.txt

Lines changed: 3 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -30,96 +30,9 @@ if(ENABLE_DEBUG)
3030
)
3131
endif()
3232

33-
# Fuzzing configuration
33+
# Include fuzzing configuration if enabled
3434
if(ENABLE_FUZZING)
35-
if(CMAKE_C_COMPILER MATCHES ".*afl-.*" OR CMAKE_CXX_COMPILER MATCHES ".*afl-.*")
36-
set(USE_AFLPLUSPLUS ON CACHE BOOL "Use AFL++ instead of libFuzzer" FORCE)
37-
message(STATUS "AFL++ compiler detected - automatically enabling AFL++ mode")
38-
endif()
39-
40-
# When building for fuzzing, we still want static library by default
41-
set(BTCPP_SHARED_LIBS OFF CACHE BOOL "Build static library for fuzzing" FORCE)
42-
43-
# Only apply static linking settings if explicitly requested
44-
if(FORCE_STATIC_LINKING)
45-
set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
46-
set(BUILD_SHARED_LIBS OFF)
47-
48-
# Force static linking for dependencies
49-
if(BTCPP_GROOT_INTERFACE)
50-
set(ZeroMQ_USE_STATIC_LIBS ON)
51-
set(ZEROMQ_STATIC_LIBRARY ON)
52-
endif()
53-
54-
if(BTCPP_SQLITE_LOGGING)
55-
set(SQLite3_USE_STATIC_LIBS ON)
56-
endif()
57-
endif()
58-
59-
list(APPEND BASE_FLAGS -O2)
60-
61-
if(USE_AFLPLUSPLUS)
62-
set(SANITIZER_FLAGS
63-
-fsanitize=address,undefined
64-
)
65-
else()
66-
# For libFuzzer, use fuzzer-no-link for the library
67-
set(SANITIZER_FLAGS
68-
-fsanitize=address,undefined,fuzzer-no-link
69-
)
70-
endif()
71-
72-
# Apply sanitizer flags to the base library
73-
list(APPEND BASE_FLAGS ${SANITIZER_FLAGS})
74-
75-
add_compile_options(${BASE_FLAGS})
76-
add_link_options(${BASE_FLAGS})
77-
78-
function(apply_fuzzing_flags target)
79-
target_compile_options(${target} PRIVATE
80-
${BASE_FLAGS}
81-
${SANITIZER_FLAGS}
82-
)
83-
84-
if(FORCE_STATIC_LINKING)
85-
if(USE_AFLPLUSPLUS)
86-
target_link_options(${target} PRIVATE
87-
${BASE_FLAGS}
88-
${SANITIZER_FLAGS}
89-
-static-libstdc++
90-
-static-libgcc
91-
-fsanitize=fuzzer
92-
)
93-
else()
94-
target_link_options(${target} PRIVATE
95-
${BASE_FLAGS}
96-
-fsanitize=fuzzer
97-
${SANITIZER_FLAGS}
98-
-static-libstdc++
99-
-static-libgcc
100-
)
101-
endif()
102-
else()
103-
if(USE_AFLPLUSPLUS)
104-
target_link_options(${target} PRIVATE
105-
${BASE_FLAGS}
106-
${SANITIZER_FLAGS}
107-
-fsanitize=fuzzer
108-
)
109-
else()
110-
target_link_options(${target} PRIVATE
111-
${BASE_FLAGS}
112-
-fsanitize=fuzzer
113-
${SANITIZER_FLAGS}
114-
)
115-
endif()
116-
endif()
117-
endfunction()
118-
119-
set(BTCPP_EXAMPLES OFF CACHE BOOL "Disable examples during fuzzing" FORCE)
120-
set(BTCPP_BUILD_TOOLS OFF CACHE BOOL "Disable tools during fuzzing" FORCE)
121-
set(BTCPP_UNIT_TESTS OFF CACHE BOOL "Disable tests during fuzzing" FORCE)
122-
set(BTCPP_SHARED_LIBS OFF CACHE BOOL "Build static library for fuzzing" FORCE)
35+
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/fuzzing_build.cmake)
12336
else()
12437
# Apply base flags for non-fuzzing builds
12538
add_compile_options(${BASE_FLAGS})
@@ -308,43 +221,9 @@ endif()
308221

309222
add_library(BT::${BTCPP_LIBRARY} ALIAS ${BTCPP_LIBRARY})
310223

311-
312224
# Add fuzzing targets
313225
if(ENABLE_FUZZING)
314-
foreach(fuzzer bt_fuzzer script_fuzzer bb_fuzzer)
315-
add_executable(${fuzzer} fuzzing/${fuzzer}.cpp)
316-
apply_fuzzing_flags(${fuzzer})
317-
318-
if(FORCE_STATIC_LINKING)
319-
target_link_libraries(${fuzzer} PRIVATE
320-
-static-libstdc++
321-
-static-libgcc
322-
${BTCPP_LIBRARY}
323-
${BTCPP_EXTRA_LIBRARIES}
324-
)
325-
else()
326-
target_link_libraries(${fuzzer} PRIVATE
327-
${BTCPP_LIBRARY}
328-
${BTCPP_EXTRA_LIBRARIES}
329-
)
330-
endif()
331-
332-
set(CORPUS_DIR ${CMAKE_BINARY_DIR}/corpus/${fuzzer})
333-
file(MAKE_DIRECTORY ${CORPUS_DIR})
334-
endforeach()
335-
336-
file(GLOB BT_CORPUS_FILES "${CMAKE_SOURCE_DIR}/fuzzing/corpus/bt_corpus/*")
337-
file(GLOB SCRIPT_CORPUS_FILES "${CMAKE_SOURCE_DIR}/fuzzing/corpus/script_corpus/*")
338-
file(GLOB BB_CORPUS_FILES "${CMAKE_SOURCE_DIR}/fuzzing/corpus/bb_corpus/*")
339-
if(BT_CORPUS_FILES)
340-
file(COPY ${BT_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR}/corpus/bt_fuzzer)
341-
endif()
342-
if(SCRIPT_CORPUS_FILES)
343-
file(COPY ${SCRIPT_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR}/corpus/script_fuzzer)
344-
endif()
345-
if(BB_CORPUS_FILES)
346-
file(COPY ${BB_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR}/corpus/bb_fuzzer)
347-
endif()
226+
add_fuzzing_targets()
348227
endif()
349228

350229
#############################################################

cmake/fuzzing_build.cmake

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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

Comments
 (0)