-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzz_ds_tree.cpp
96 lines (79 loc) · 2.4 KB
/
fuzz_ds_tree.cpp
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
#include "ndpi_api.h"
#include "fuzz_common_code.h"
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include "fuzzer/FuzzedDataProvider.h"
static int __compare(const void *a, const void *b)
{
u_int32_t *entry_a, *entry_b;
entry_a = (u_int32_t *)a;
entry_b = (u_int32_t *)b;
return *entry_a == *entry_b ? 0 : (*entry_a < *entry_b ? -1 : +1);
}
static void __free(void * const node)
{
u_int32_t *entry = (u_int32_t *)node;
ndpi_free(entry);
}
static void __walk(const void *a, ndpi_VISIT which, int depth, void *user_data)
{
assert(user_data == NULL && a);
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
FuzzedDataProvider fuzzed_data(data, size);
u_int16_t i, num_iteration, is_added = 0;
void *root = NULL;
u_int32_t *entry, value_added, e, *e2;
/* Just to have some data */
if (fuzzed_data.remaining_bytes() < 1024)
return -1;
/* To allow memory allocation failures */
fuzz_set_alloc_callbacks_and_seed(size);
num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
for (i = 0; i < num_iteration; i++) {
entry = (u_int32_t *)ndpi_malloc(sizeof(u_int32_t));
if (!entry)
continue;
*entry = fuzzed_data.ConsumeIntegral<u_int32_t>();
if(ndpi_tfind(entry, &root, __compare) == NULL) {
if(ndpi_tsearch(entry, &root, __compare) == NULL) {
ndpi_free(entry);
} else {
/* Keep one random entry really added */
if (is_added == 0 && fuzzed_data.ConsumeBool()) {
value_added = *entry;
is_added = 1;
}
}
} else {
ndpi_free(entry);
}
}
/* "Random" search */
num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
for (i = 0; i < num_iteration; i++) {
e = fuzzed_data.ConsumeIntegral<u_int32_t>();
ndpi_tfind(&e, &root, __compare);
}
/* Search of an added node */
if (is_added) {
ndpi_tfind(&value_added, &root, __compare);
}
ndpi_twalk(root, __walk, NULL);
/* "Random" delete */
num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
for (i = 0; i < num_iteration; i++) {
e = fuzzed_data.ConsumeIntegral<u_int32_t>();
e2 = (u_int32_t *)ndpi_tdelete(&e, &root, __compare);
ndpi_free(e2);
}
/* Delete of an added node */
if (is_added) {
e2 = (u_int32_t *)ndpi_tdelete(&value_added, &root, __compare);
ndpi_free(e2);
}
ndpi_twalk(root, __walk, NULL);
ndpi_tdestroy(root, __free);
return 0;
}