aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Nardi <12729895+IvanNardi@users.noreply.github.com>2024-09-30 16:54:42 +0200
committerGitHub <noreply@github.com>2024-09-30 16:54:42 +0200
commita081a5578b05e2fe40b20838b80047f7540a40ad (patch)
tree5674bbbb04a79a1a7a2c07b695425fc10ef887b6
parent726bb6704ccdf247fc3ac4fc9802c17a15bed753 (diff)
fuzz: try to be a little bit faster (#2578)
See: 9d07cf281
-rw-r--r--fuzz/Makefile.am4
-rw-r--r--fuzz/fuzz_common_code.c6
-rw-r--r--fuzz/fuzz_ds_domain_classify.cpp24
3 files changed, 27 insertions, 7 deletions
diff --git a/fuzz/Makefile.am b/fuzz/Makefile.am
index 2c7b227b5..198091c88 100644
--- a/fuzz/Makefile.am
+++ b/fuzz/Makefile.am
@@ -385,8 +385,8 @@ fuzz_ds_bitmap64_fuse_LINK=$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
$(fuzz_ds_bitmap64_fuse_LDFLAGS) @NDPI_LDFLAGS@ $(LDFLAGS) -o $@
fuzz_ds_domain_classify_SOURCES = fuzz_ds_domain_classify.cpp fuzz_common_code.c
-fuzz_ds_domain_classify_CXXFLAGS = @NDPI_CFLAGS@ $(CXXFLAGS)
-fuzz_ds_domain_classify_CFLAGS = @NDPI_CFLAGS@ $(CXXFLAGS)
+fuzz_ds_domain_classify_CXXFLAGS = @NDPI_CFLAGS@ $(CXXFLAGS) -DNDPI_LIB_COMPILATION
+fuzz_ds_domain_classify_CFLAGS = @NDPI_CFLAGS@ $(CXXFLAGS) -DNDPI_LIB_COMPILATION
fuzz_ds_domain_classify_LDADD = ../src/lib/libndpi.a $(ADDITIONAL_LIBS)
fuzz_ds_domain_classify_LDFLAGS = $(LIBS)
if HAS_FUZZLDFLAGS
diff --git a/fuzz/fuzz_common_code.c b/fuzz/fuzz_common_code.c
index 08147d635..647a28413 100644
--- a/fuzz/fuzz_common_code.c
+++ b/fuzz/fuzz_common_code.c
@@ -45,6 +45,9 @@ void fuzz_init_detection_module(struct ndpi_detection_module_struct **ndpi_info_
ndpi_set_config_u64(*ndpi_info_mod, NULL, "log.level", 3);
ndpi_set_config(*ndpi_info_mod, "all", "log", "enable");
+ NDPI_BITMASK_SET_ALL(all);
+ ndpi_set_protocol_detection_bitmask2(*ndpi_info_mod, &all);
+
ndpi_load_domain_suffixes(*ndpi_info_mod, "public_suffix_list.dat");
ndpi_load_categories_dir(*ndpi_info_mod, "./lists/");
ndpi_load_protocols_file(*ndpi_info_mod, "protos.txt");
@@ -53,9 +56,6 @@ void fuzz_init_detection_module(struct ndpi_detection_module_struct **ndpi_info_
ndpi_load_malicious_ja3_file(*ndpi_info_mod, "ja3_fingerprints.csv");
ndpi_load_malicious_sha1_file(*ndpi_info_mod, "sha1_fingerprints.csv");
- NDPI_BITMASK_SET_ALL(all);
- ndpi_set_protocol_detection_bitmask2(*ndpi_info_mod, &all);
-
ndpi_set_config(*ndpi_info_mod, NULL, "filename.config", "config.txt");
ndpi_finalize_initialization(*ndpi_info_mod);
diff --git a/fuzz/fuzz_ds_domain_classify.cpp b/fuzz/fuzz_ds_domain_classify.cpp
index 5e068a9fe..ccb106a6f 100644
--- a/fuzz/fuzz_ds_domain_classify.cpp
+++ b/fuzz/fuzz_ds_domain_classify.cpp
@@ -1,4 +1,5 @@
#include "ndpi_api.h"
+#include "ndpi_private.h"
#include "fuzz_common_code.h"
#include <stdint.h>
@@ -8,7 +9,14 @@
static struct ndpi_detection_module_struct *ndpi_struct = NULL;
-extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+extern "C" {
+
+#ifdef NDPI_ENABLE_DEBUG_MESSAGES
+void ndpi_debug_printf(unsigned int proto, struct ndpi_detection_module_struct *ndpi_str, ndpi_log_level_t log_level,
+ const char *file_name, const char *func_name, unsigned int line_number, const char *format, ...);
+#endif
+
+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;
@@ -16,8 +24,18 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
u_int16_t class_id;
std::string value, value_added;
+ /* We don't need a complete (and costly to set up) context!
+ Setting up manually only what is really needed is complex (and error prone!)
+ but allow us to be significant faster and to have better coverage */
if (ndpi_struct == NULL) {
- fuzz_init_detection_module(&ndpi_struct, NULL);
+ ndpi_struct = (struct ndpi_detection_module_struct *)ndpi_calloc(1, sizeof(struct ndpi_detection_module_struct));
+#ifdef NDPI_ENABLE_DEBUG_MESSAGES
+ set_ndpi_debug_function(ndpi_struct, (ndpi_debug_function_ptr)ndpi_debug_printf);
+#endif
+ if (ndpi_struct) {
+ ndpi_struct->cfg.log_level = NDPI_LOG_DEBUG_EXTRA;
+ ndpi_load_domain_suffixes(ndpi_struct, (char *)"public_suffix_list.dat");
+ }
}
/* To allow memory allocation failures */
@@ -61,3 +79,5 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
return 0;
}
+
+}