Coverage Report

Created: 2026-12-01 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/behaviortreecpp/fuzzing/bt_fuzzer.cpp
Line
Count
Source
1
#include "behaviortree_cpp/bt_factory.h"
2
#include "behaviortree_cpp/xml_parsing.h"
3
4
#include <string>
5
#include <tuple>
6
7
#include <fuzzer/FuzzedDataProvider.h>
8
9
// List of valid node types we can use to construct valid-ish XML
10
constexpr const char* NODE_TYPES[] = {
11
  "Sequence",         "Fallback",         "ParallelAll",
12
  "ReactiveSequence", "ReactiveFallback", "IfThenElse",
13
  "WhileDoElse",      "Inverter",         "RetryUntilSuccessful",
14
  "Repeat",           "Timeout",          "Delay",
15
  "ForceSuccess",     "ForceFailure",     "AlwaysSuccess",
16
  "AlwaysFailure",    "SetBlackboard",    "SubTree"
17
};
18
19
// Attributes that can be added to nodes
20
constexpr const char* NODE_ATTRIBUTES[] = { "name",      "ID",         "port_1",
21
                                            "port_2",    "timeout_ms", "delay_ms",
22
                                            "threshold", "max_repeats" };
23
24
std::string generateFuzzedNodeXML(FuzzedDataProvider& fdp, int depth = 0)
25
30.0k
{
26
  // Prevent stack overflow with max depth
27
30.0k
  if(depth > 6)
28
0
  {  // Reasonable limit for XML tree depth
29
0
    return "<AlwaysSuccess/>";
30
0
  }
31
32
30.0k
  std::string xml;
33
30.0k
  const std::string node_type = fdp.PickValueInArray(NODE_TYPES);
34
35
30.0k
  xml += "<" + node_type;
36
37
30.0k
  size_t num_attributes = fdp.ConsumeIntegralInRange<size_t>(0, 3);
38
52.1k
  for(size_t i = 0; i < num_attributes; i++)
39
22.0k
  {
40
22.0k
    const std::string attr = fdp.PickValueInArray(NODE_ATTRIBUTES);
41
22.0k
    std::string value = fdp.ConsumeRandomLengthString(10);
42
22.0k
    xml += " " + attr + "=\"" + value + "\"";
43
22.0k
  }
44
45
30.0k
  if(depth > 3 || fdp.ConsumeBool())
46
11.2k
  {
47
11.2k
    xml += "/>";
48
11.2k
  }
49
18.7k
  else
50
18.7k
  {
51
18.7k
    xml += ">";
52
    // Add some child nodes recursively with depth limit
53
18.7k
    size_t num_children = fdp.ConsumeIntegralInRange<size_t>(0, 2);
54
40.5k
    for(size_t i = 0; i < num_children; i++)
55
21.8k
    {
56
21.8k
      xml += generateFuzzedNodeXML(fdp, depth + 1);
57
21.8k
    }
58
18.7k
    xml += "</" + node_type + ">";
59
18.7k
  }
60
61
30.0k
  return xml;
62
30.0k
}
63
64
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65
6.97k
{
66
6.97k
  if(size < 4)
67
2
  {
68
2
    return 0;
69
2
  }
70
71
6.97k
  FuzzedDataProvider fdp(data, size);
72
6.97k
  BT::BehaviorTreeFactory factory;
73
74
6.97k
  try
75
6.97k
  {
76
    // Strategy 1: Test with completely random data
77
6.97k
    if(fdp.ConsumeBool())
78
2.28k
    {
79
2.28k
      std::string random_xml = fdp.ConsumeRandomLengthString(size - 1);
80
2.28k
      try
81
2.28k
      {
82
2.28k
        std::ignore = factory.createTreeFromText(random_xml);
83
2.28k
      }
84
2.28k
      catch(const std::exception&)
85
2.28k
      {}
86
2.28k
    }
87
    // Strategy 2: Generate semi-valid XML
88
4.68k
    else
89
4.68k
    {
90
4.68k
      std::string xml = R"(
91
4.68k
                <root BTCPP_format="4">
92
4.68k
                    <BehaviorTree ID="MainTree">)";
93
94
4.68k
      size_t num_nodes = fdp.ConsumeIntegralInRange<size_t>(1, 5);
95
12.9k
      for(size_t i = 0; i < num_nodes; i++)
96
8.24k
      {
97
8.24k
        xml += generateFuzzedNodeXML(fdp);
98
8.24k
      }
99
100
4.68k
      xml += R"(
101
4.68k
                    </BehaviorTree>
102
4.68k
                </root>)";
103
104
4.68k
      auto blackboard = BT::Blackboard::create();
105
106
4.68k
      switch(fdp.ConsumeIntegralInRange<int>(0, 2))
107
4.68k
      {
108
4.50k
        case 0:
109
4.50k
          std::ignore = factory.createTreeFromText(xml, blackboard);
110
4.50k
          break;
111
79
        case 1:
112
79
          BT::VerifyXML(xml, {});
113
79
          break;
114
104
        case 2:
115
104
          factory.registerBehaviorTreeFromText(xml);
116
104
          if(!factory.registeredBehaviorTrees().empty())
117
38
          {
118
38
            std::ignore =
119
38
                factory.createTree(factory.registeredBehaviorTrees().front(), blackboard);
120
38
          }
121
104
          break;
122
4.68k
      }
123
4.68k
    }
124
6.97k
  }
125
6.97k
  catch(const std::exception&)
126
6.97k
  {}
127
128
6.97k
  return 0;
129
6.97k
}