diff --git a/cmake/fuzzing_build.cmake b/cmake/fuzzing_build.cmake index 354287121..7e2e3932c 100644 --- a/cmake/fuzzing_build.cmake +++ b/cmake/fuzzing_build.cmake @@ -106,7 +106,7 @@ endfunction() # Function to add fuzzing targets - compatible with both local and OSS-Fuzz builds function(add_fuzzing_targets) - set(FUZZERS bt_fuzzer script_fuzzer bb_fuzzer bt_tree_exec_fuzzer) + set(FUZZERS bt_fuzzer script_fuzzer bb_fuzzer bt_tree_exec_fuzzer bt_xml_roundtrip_fuzzer) foreach(fuzzer ${FUZZERS}) add_executable(${fuzzer} fuzzing/${fuzzer}.cpp) diff --git a/fuzzing/bt_xml_roundtrip_fuzzer.cpp b/fuzzing/bt_xml_roundtrip_fuzzer.cpp new file mode 100644 index 000000000..853f1a8f7 --- /dev/null +++ b/fuzzing/bt_xml_roundtrip_fuzzer.cpp @@ -0,0 +1,252 @@ +// Fuzz target: bt_xml_roundtrip_fuzzer +// +// Threat model: BehaviorTree.CPP both *reads* XML from untrusted sources and +// *writes* XML that other tools re-read (Groot2 project files, exported +// trees). A divergence between WriteTreeToXML and re-import is a real bug +// (e.g. a dropped SubTree name). Raw parser coverage belongs to bt_fuzzer. +// +// Strategy: generate a valid tree (optionally with a SubTree), create it, +// exported1 = WriteTreeToXML(tree, false, false), re-import in a fresh +// factory (re-import failures abort), exported2 = WriteTreeToXML(tree2, +// false, false), abort if exported1 != exported2. +// +// Generation is bounded (depth, child counts). No tree is ticked here. +// Exceptions from the *generated* input are expected and ignored; exceptions +// from re-import of our own export are not. + +#include "behaviortree_cpp/blackboard.h" +#include "behaviortree_cpp/bt_factory.h" +#include "behaviortree_cpp/xml_parsing.h" + +#include +#include +#include + +#include + +namespace +{ + +constexpr int kMaxDepth = 3; + +const char* kConditionScripts[] = { "var > 0", "var < 5", "flag", + "!flag", "str == 'abc'", "var == MODE_A" }; +const char* kLiterals[] = { "1", "2", "true", "false", "1.5", "hello", "{var}" }; +const char* kStatuses[] = { "SUCCESS", "FAILURE", "SKIPPED" }; + +std::string pickCondition(FuzzedDataProvider& fdp) +{ + return fdp.PickValueInArray(kConditionScripts); +} + +std::string genConditionLeaf(FuzzedDataProvider& fdp) +{ + switch(fdp.ConsumeIntegralInRange(0, 2)) + { + case 0: + return ""; + case 1: + return ""; + default: + return ""; + } +} + +std::string genLeaf(FuzzedDataProvider& fdp) +{ + switch(fdp.ConsumeIntegralInRange(0, 8)) + { + case 0: + return ""; + case 1: + return ""; + case 2: + return "