Repository navigation
Expand file tree
/
Copy pathbt_convert_fuzzer.cpp
More file actions
379 lines (360 loc) · 10.3 KB
/
Copy pathbt_convert_fuzzer.cpp
File metadata and controls
379 lines (360 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Fuzz target: bt_convert_fuzzer
//
// Threat model: every XML attribute, script literal and blackboard value that
// enters a BehaviorTree.CPP application passes through BT::convertFromString<T>
// (port parsing), SafeAny/Any type-erased casts, or typed blackboard access.
// These string->type and type->type conversions are the numeric- and
// string-parsing funnel for untrusted data (attacker-supplied XML documents,
// Groot files, scripting expressions). polymorphic_cast_registry powers
// shared_ptr port compatibility (Issue #943) and is dark in production
// coverage (220 lines, 0.9%).
//
// Strategy (component-API harness, typed inputs):
// 1. convertFromString<T> over the full builtin type matrix with a fuzzed
// string (numerics incl. int8..64/uint8..64/float/double overflow and
// underflow, bool, enums NodeStatus/NodeType/PortDirection, split
// containers vector<T>, SharedQueue<T>),
// 2. Any value storage + tryCast/cast cross-type matrix,
// 3. PolymorphicCastRegistry upcasts/downcasts/transitive chains over a
// harness-defined class hierarchy (Animal/Cat/Dog/Sphynx), including via
// a blackboard with the registry attached,
// 4. blackboard typed set/get mismatch matrix.
//
// All thrown BT exceptions (RuntimeError/LogicError on invalid input) are
// expected and caught; the point is exercising the conversion code paths, not
// crashing on them.
//
// Expected bug classes: integer/float overflow and truncation handling,
// out-of-bounds reads in split-string parsing, enum parsing mismatches,
// type-confusion in Any casts, registry lookup errors on shared_ptr ports.
#include "behaviortree_cpp/basic_types.h"
#include "behaviortree_cpp/blackboard.h"
#include "behaviortree_cpp/decorators/loop_node.h"
#include "behaviortree_cpp/utils/polymorphic_cast_registry.hpp"
#include "behaviortree_cpp/utils/safe_any.hpp"
#include <cstdint>
#include <memory>
#include <string>
#include <fuzzer/FuzzedDataProvider.h>
namespace
{
// Harness-defined hierarchy for the polymorphic cast registry. No upstream
// product code is involved: the registry API is public and standalone.
struct Animal
{
virtual ~Animal() = default;
virtual const char* species() const
{
return "Animal";
}
};
struct Cat : Animal
{
const char* species() const override
{
return "Cat";
}
};
struct Dog : Animal
{
const char* species() const override
{
return "Dog";
}
};
struct Sphynx : Cat
{
const char* species() const override
{
return "Sphynx";
}
};
using AnimalPtr = std::shared_ptr<Animal>;
using CatPtr = std::shared_ptr<Cat>;
using DogPtr = std::shared_ptr<Dog>;
using SphynxPtr = std::shared_ptr<Sphynx>;
} // namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if(size < 8)
{
return 0;
}
FuzzedDataProvider fdp(data, size);
const std::string text = fdp.ConsumeRandomLengthString();
const BT::StringView sv(text);
// ---- Stage 1: convertFromString<T> over the builtin type matrix
try
{
switch(fdp.ConsumeIntegralInRange<uint8_t>(0, 19))
{
case 0:
(void)BT::convertFromString<std::string>(sv);
break;
case 1:
(void)BT::convertFromString<int8_t>(sv);
break;
case 2:
(void)BT::convertFromString<int16_t>(sv);
break;
case 3:
(void)BT::convertFromString<int32_t>(sv);
break;
case 4:
(void)BT::convertFromString<int64_t>(sv);
break;
case 5:
(void)BT::convertFromString<uint8_t>(sv);
break;
case 6:
(void)BT::convertFromString<uint16_t>(sv);
break;
case 7:
(void)BT::convertFromString<uint32_t>(sv);
break;
case 8:
(void)BT::convertFromString<uint64_t>(sv);
break;
case 9:
(void)BT::convertFromString<float>(sv);
break;
case 10:
(void)BT::convertFromString<double>(sv);
break;
case 11:
(void)BT::convertFromString<bool>(sv);
break;
case 12:
(void)BT::convertFromString<BT::NodeStatus>(sv);
break;
case 13:
(void)BT::convertFromString<BT::NodeType>(sv);
break;
case 14:
(void)BT::convertFromString<BT::PortDirection>(sv);
break;
case 15:
(void)BT::convertFromString<std::vector<int>>(sv);
break;
case 16:
(void)BT::convertFromString<std::vector<double>>(sv);
break;
case 17:
(void)BT::convertFromString<std::vector<bool>>(sv);
break;
case 18:
(void)BT::convertFromString<std::vector<std::string>>(sv);
break;
default:
(void)BT::convertFromString<BT::SharedQueue<int>>(sv);
break;
}
}
catch(const std::exception&)
{
// invalid numeric/enum input raises RuntimeError: expected
}
// Stages 2-4 are wrapped so that BT exceptions raised by the conversion
// APIs are contained per-iteration and never escape LLVMFuzzerTestOneInput.
try
{
// ---- Stage 2: Any storage + cross-type cast matrix
{
BT::Any any;
switch(fdp.ConsumeIntegralInRange<uint8_t>(0, 2))
{
case 0:
any = BT::Any(text);
break;
case 1:
any = BT::Any(static_cast<int64_t>(text.size()) * 1000000);
break;
default:
any = BT::Any(static_cast<double>(text.size()) * 1.5);
break;
}
(void)any.isNumber();
(void)any.isIntegral();
(void)any.isString();
switch(fdp.ConsumeIntegralInRange<uint8_t>(0, 5))
{
case 0: {
auto res = any.tryCast<int64_t>();
(void)res.has_value();
break;
}
case 1: {
auto res = any.tryCast<uint64_t>();
(void)res.has_value();
break;
}
case 2: {
auto res = any.tryCast<double>();
(void)res.has_value();
break;
}
case 3: {
auto res = any.tryCast<std::string>();
(void)res.has_value();
break;
}
case 4: {
auto res = any.tryCast<int>();
(void)res.has_value();
break;
}
default: {
// cast<T>() throws RuntimeError on a lossy/invalid cast
try
{
(void)any.cast<uint64_t>();
}
catch(const std::exception&)
{
// expected on invalid casts
}
break;
}
}
}
// ---- Stage 3: polymorphic cast registry
{
BT::PolymorphicCastRegistry registry;
registry.registerCast<Cat, Animal>();
registry.registerCast<Dog, Animal>();
registry.registerCast<Sphynx, Cat>();
BT::Any animal_any;
switch(fdp.ConsumeIntegralInRange<uint8_t>(0, 3))
{
case 0:
animal_any = BT::Any(AnimalPtr(std::make_shared<Animal>()));
break;
case 1:
animal_any = BT::Any(CatPtr(std::make_shared<Cat>()));
break;
case 2:
animal_any = BT::Any(DogPtr(std::make_shared<Dog>()));
break;
default:
animal_any = BT::Any(SphynxPtr(std::make_shared<Sphynx>()));
break;
}
switch(fdp.ConsumeIntegralInRange<uint8_t>(0, 4))
{
case 0: {
auto res = animal_any.tryCastWithRegistry<AnimalPtr>(registry);
(void)res.has_value();
break;
}
case 1: {
auto res = animal_any.tryCastWithRegistry<CatPtr>(registry);
(void)res.has_value();
break;
}
case 2: {
auto res = animal_any.tryCastWithRegistry<DogPtr>(registry);
(void)res.has_value();
break;
}
case 3: {
auto res = animal_any.tryCastWithRegistry<SphynxPtr>(registry);
(void)res.has_value();
break;
}
default: {
try
{
auto p = animal_any.cast<CatPtr>();
(void)p;
}
catch(const std::exception&)
{
// expected when the stored type is not Cat-compatible
}
break;
}
}
// Blackboard with an attached registry: typed set/get through the
// polymorphic path (Issue #943 usage pattern).
auto reg2 = std::make_shared<BT::PolymorphicCastRegistry>();
reg2->registerCast<Cat, Animal>();
reg2->registerCast<Sphynx, Cat>();
auto bb_poly = BT::Blackboard::create();
bb_poly->setPolymorphicCastRegistry(reg2);
bb_poly->set("obj", CatPtr(std::make_shared<Sphynx>()));
try
{
auto up = bb_poly->get<AnimalPtr>("obj");
(void)up;
}
catch(const std::exception&)
{
// expected when the registry cannot satisfy the cast
}
try
{
auto down = bb_poly->get<SphynxPtr>("obj");
(void)down;
}
catch(const std::exception&)
{
// expected on unsupported downcasts
}
}
// ---- Stage 4: blackboard typed set/get mismatch matrix
{
auto bb = BT::Blackboard::create();
switch(fdp.ConsumeIntegralInRange<uint8_t>(0, 3))
{
case 0:
bb->set("k", static_cast<int64_t>(text.size()));
break;
case 1:
bb->set("k", static_cast<double>(text.size()));
break;
case 2:
bb->set("k", text);
break;
default:
bb->set("k", !text.empty());
break;
}
const auto fetch = [&bb](auto type_tag) {
using T = decltype(type_tag);
try
{
// typed get: mismatched entry type raises RuntimeError (expected)
(void)bb->get<T>("k");
}
catch(const std::exception&)
{
// expected: type mismatch or missing key
}
};
switch(fdp.ConsumeIntegralInRange<uint8_t>(0, 4))
{
case 0:
fetch(int64_t{});
break;
case 1:
fetch(double{});
break;
case 2:
fetch(std::string{});
break;
case 3:
fetch(bool{});
break;
default:
fetch(int64_t{}); // on a missing key: RuntimeError path
break;
}
}
}
catch(const std::exception&)
{
// expected: Any/registry/blackboard APIs raise RuntimeError on
// lossy or mismatched conversions
}
return 0;
}