Skip to content

Commit 153f2c3

Browse files
cktiicktii
cktii
authored and
cktii
committed
feat: add fuzzing harnesses
1 parent b4bbef6 commit 153f2c3

File tree

19 files changed

+692
-10
lines changed

19 files changed

+692
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ CMakeSettings.json
1616
.pixi
1717

1818
CMakeUserPresets.json
19+
20+
tags

CMakeLists.txt

Lines changed: 126 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,95 @@ cmake_minimum_required(VERSION 3.16.3) # version on Ubuntu Focal
22

33
project(behaviortree_cpp VERSION 4.6.2 LANGUAGES C CXX)
44

5-
set(CMAKE_CONFIG_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake")
5+
# Build configuration options
6+
option(ENABLE_FUZZING "Enable fuzzing builds" OFF)
7+
option(USE_AFLPLUSPLUS "Use AFL++ instead of libFuzzer" OFF)
8+
option(ENABLE_DEBUG "Enable debug build with full symbols" OFF)
9+
10+
set(BASE_FLAGS "")
11+
12+
# Debug build configuration
13+
if(ENABLE_DEBUG)
14+
list(APPEND BASE_FLAGS
15+
-g3
16+
-ggdb3
17+
-O0
18+
-fno-omit-frame-pointer
19+
)
20+
endif()
21+
22+
# Fuzzing configuration
23+
if(ENABLE_FUZZING)
24+
if(USE_AFLPLUSPLUS)
25+
list(APPEND BASE_FLAGS -O3)
26+
else()
27+
list(APPEND BASE_FLAGS -O2)
28+
endif()
29+
30+
if(USE_AFLPLUSPLUS)
31+
set(SANITIZER_FLAGS
32+
-fsanitize=address,undefined
33+
)
34+
else()
35+
# For libFuzzer, use fuzzer-no-link for the library
36+
set(SANITIZER_FLAGS
37+
-fsanitize=address,undefined,fuzzer-no-link
38+
)
39+
endif()
40+
41+
# Apply sanitizer flags to the base library
42+
list(APPEND BASE_FLAGS ${SANITIZER_FLAGS})
43+
44+
# Apply base flags globally
45+
add_compile_options(${BASE_FLAGS})
46+
add_link_options(${BASE_FLAGS})
47+
48+
function(apply_fuzzing_flags target)
49+
if(USE_AFLPLUSPLUS)
50+
# AFL++ specific flags
51+
target_compile_options(${target} PRIVATE
52+
${BASE_FLAGS}
53+
${SANITIZER_FLAGS}
54+
)
55+
target_link_options(${target} PRIVATE
56+
${BASE_FLAGS}
57+
-fsanitize=fuzzer,address,undefined
58+
)
59+
else()
60+
# libFuzzer specific flags
61+
target_compile_options(${target} PRIVATE
62+
${BASE_FLAGS}
63+
-fsanitize=fuzzer
64+
${SANITIZER_FLAGS}
65+
)
66+
target_link_options(${target} PRIVATE
67+
${BASE_FLAGS}
68+
-fsanitize=fuzzer
69+
${SANITIZER_FLAGS}
70+
)
71+
endif()
72+
endfunction()
73+
74+
set(BTCPP_EXAMPLES OFF CACHE BOOL "Disable examples during fuzzing" FORCE)
75+
set(BTCPP_BUILD_TOOLS OFF CACHE BOOL "Disable tools during fuzzing" FORCE)
76+
set(BTCPP_UNIT_TESTS OFF CACHE BOOL "Disable tests during fuzzing" FORCE)
77+
set(BTCPP_SHARED_LIBS OFF CACHE BOOL "Build static library for fuzzing" FORCE)
78+
else()
79+
# Apply base flags for non-fuzzing builds
80+
add_compile_options(${BASE_FLAGS})
81+
add_link_options(${BASE_FLAGS})
82+
endif()
83+
84+
set(CMAKE_CONFIG_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake")
685
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CONFIG_PATH}")
786

887
set(BTCPP_LIBRARY ${PROJECT_NAME})
988

1089
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
11-
message(STATUS "Setting build type to 'Release' as none was specified.")
12-
set(CMAKE_BUILD_TYPE "Release" CACHE
13-
STRING "Choose the type of build." FORCE)
14-
# Set the possible values of build type for cmake-gui
15-
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
16-
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
90+
message(STATUS "Setting build type to 'Release' as none was specified.")
91+
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
92+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
93+
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
1794
endif()
1895

1996
if(MSVC)
@@ -186,20 +263,59 @@ target_compile_definitions(${BTCPP_LIBRARY} PUBLIC BTCPP_LIBRARY_VERSION="${CMAK
186263
target_compile_features(${BTCPP_LIBRARY} PUBLIC cxx_std_17)
187264

188265
if(MSVC)
189-
target_compile_options(${BTCPP_LIBRARY} PRIVATE "/source-charset:utf-8")
266+
target_compile_options(${BTCPP_LIBRARY} PRIVATE "/source-charset:utf-8")
190267
else()
191-
target_compile_options(${BTCPP_LIBRARY} PRIVATE -Wall -Wextra)
268+
if(ENABLE_DEBUG)
269+
target_compile_options(${BTCPP_LIBRARY} PRIVATE -Wall -Wextra -g3 -ggdb3 -O0 -fno-omit-frame-pointer)
270+
else()
271+
target_compile_options(${BTCPP_LIBRARY} PRIVATE -Wall -Wextra)
272+
endif()
192273
endif()
193274

194275
add_library(BT::${BTCPP_LIBRARY} ALIAS ${BTCPP_LIBRARY})
195276

277+
278+
# Add fuzzing targets
279+
if(ENABLE_FUZZING)
280+
add_executable(bt_fuzzer fuzzing/bt_fuzzer.cpp)
281+
apply_fuzzing_flags(bt_fuzzer)
282+
target_link_libraries(bt_fuzzer PRIVATE ${BTCPP_LIBRARY} ${BTCPP_EXTRA_LIBRARIES})
283+
284+
add_executable(script_fuzzer fuzzing/script_fuzzer.cpp)
285+
apply_fuzzing_flags(script_fuzzer)
286+
target_link_libraries(script_fuzzer PRIVATE ${BTCPP_LIBRARY} ${BTCPP_EXTRA_LIBRARIES})
287+
288+
add_executable(bb_fuzzer fuzzing/bb_fuzzer.cpp)
289+
apply_fuzzing_flags(bb_fuzzer)
290+
target_link_libraries(bb_fuzzer PRIVATE ${BTCPP_LIBRARY} ${BTCPP_EXTRA_LIBRARIES})
291+
292+
foreach(fuzzer bt_fuzzer script_fuzzer bb_fuzzer)
293+
set(CORPUS_DIR ${CMAKE_BINARY_DIR}/corpus/${fuzzer})
294+
file(MAKE_DIRECTORY ${CORPUS_DIR})
295+
endforeach()
296+
297+
file(GLOB BT_CORPUS_FILES "fuzzing/corpus/bt_fuzzer/*")
298+
file(GLOB SCRIPT_CORPUS_FILES "fuzzing/corpus/script_fuzzer/*")
299+
file(GLOB BB_CORPUS_FILES "fuzzing/corpus/bb_fuzzer/*")
300+
301+
if(BT_CORPUS_FILES)
302+
file(COPY ${BT_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR}/corpus/bt_fuzzer)
303+
endif()
304+
if(SCRIPT_CORPUS_FILES)
305+
file(COPY ${SCRIPT_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR}/corpus/script_fuzzer)
306+
endif()
307+
if(BB_CORPUS_FILES)
308+
file(COPY ${BB_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR}/corpus/bb_fuzzer)
309+
endif()
310+
endif()
311+
196312
#############################################################
197313
message( STATUS "BTCPP_LIB_DESTINATION: ${BTCPP_LIB_DESTINATION} " )
198314
message( STATUS "BTCPP_INCLUDE_DESTINATION: ${BTCPP_INCLUDE_DESTINATION} " )
199315
message( STATUS "BTCPP_UNIT_TESTS: ${BTCPP_UNIT_TESTS} " )
200316

201317
if (BTCPP_UNIT_TESTS OR BTCPP_EXAMPLES)
202-
add_subdirectory(sample_nodes)
318+
add_subdirectory(sample_nodes)
203319
endif()
204320

205321
######################################################

fuzzing/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Fuzzing BehaviorTree.CPP
2+
3+
You can build the existing harnesses either for libfuzzer or AFL++:
4+
5+
## libfuzzer
6+
7+
```bash
8+
mkdir build_libfuzzer && cd build_libfuzzer
9+
cmake -DENABLE_FUZZING ..
10+
```
11+
12+
## AFL++
13+
14+
```bash
15+
export CC=afl-clang-fast
16+
export CXX=afl-clang-fast++
17+
mkdir build_afl && cd build_afl
18+
cmake -DENABLE_FUZZING -DUSE_AFLPLUSPLUS ..
19+
```

0 commit comments

Comments
 (0)