From ef6085370f75ae7c6c53bfbd0e36c588483a5047 Mon Sep 17 00:00:00 2001 From: Ivan Nardi <12729895+IvanNardi@users.noreply.github.com> Date: Sun, 10 Sep 2023 18:44:50 +0200 Subject: fuzz: add fuzzers to test bitmap64 and domain_classify data structures (#2082) --- fuzz/fuzz_ds_domain_classify.cpp | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 fuzz/fuzz_ds_domain_classify.cpp (limited to 'fuzz/fuzz_ds_domain_classify.cpp') diff --git a/fuzz/fuzz_ds_domain_classify.cpp b/fuzz/fuzz_ds_domain_classify.cpp new file mode 100644 index 000000000..afd43a796 --- /dev/null +++ b/fuzz/fuzz_ds_domain_classify.cpp @@ -0,0 +1,53 @@ +#include "ndpi_api.h" +#include "fuzz_common_code.h" + +#include +#include +#include +#include "fuzzer/FuzzedDataProvider.h" + +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; + bool rc; + ndpi_domain_classify *d; + u_int8_t class_id; + std::string value, value_added; + + /* To allow memory allocation failures */ + fuzz_set_alloc_callbacks_and_seed(size); + + d = ndpi_domain_classify_alloc(); + + num_iteration = fuzzed_data.ConsumeIntegral(); + for (i = 0; i < num_iteration; i++) { + + value = fuzzed_data.ConsumeBytesAsString(fuzzed_data.ConsumeIntegral()); + class_id = fuzzed_data.ConsumeIntegral(); + + rc = ndpi_domain_classify_add(d, class_id, value.c_str()); + /* Keep one random entry really added */ + if (rc == true && is_added == 0 && fuzzed_data.ConsumeBool()) { + value_added = value; + is_added = 1; + } + } + + /* "Random" search */ + num_iteration = fuzzed_data.ConsumeIntegral(); + for (i = 0; i < num_iteration; i++) { + value = fuzzed_data.ConsumeBytesAsString(fuzzed_data.ConsumeIntegral()); + + ndpi_domain_classify_contains(d, &class_id, value.c_str()); + } + /* Search of an added entry */ + if (is_added) { + ndpi_domain_classify_contains(d, &class_id, value_added.c_str()); + } + + ndpi_domain_classify_size(d); + + ndpi_domain_classify_free(d); + + return 0; +} -- cgit v1.2.3