Skip to content

Commit

Permalink
fuzz: try to be a little bit faster (#2578)
Browse files Browse the repository at this point in the history
See: 9d07cf2
  • Loading branch information
IvanNardi authored Sep 30, 2024
1 parent 726bb67 commit a081a55
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions fuzz/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_common_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down
24 changes: 22 additions & 2 deletions fuzz/fuzz_ds_domain_classify.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ndpi_api.h"
#include "ndpi_private.h"
#include "fuzz_common_code.h"

#include <stdint.h>
Expand All @@ -8,16 +9,33 @@

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;
ndpi_domain_classify *d;
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 */
Expand Down Expand Up @@ -61,3 +79,5 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {

return 0;
}

}

0 comments on commit a081a55

Please sign in to comment.