fuzz: add a new fuzzer testing memory allocation failures (#1818)
Try to fuzz error paths triggered by allocation errors. Fix some errors already found by this new fuzzer. Basic idea taken from: https://github.com/harfbuzz/harfbuzz/pull/2566/files `FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION` is a standard define used to (not)compile specific code in fuzzing builds. See: https://llvm.org/docs/LibFuzzer.html
This commit is contained in:
@@ -23,6 +23,26 @@ int malloc_size_stats = 0;
|
||||
int max_malloc_bins = 0;
|
||||
struct ndpi_bin malloc_bins; /* unused */
|
||||
|
||||
#ifdef ENABLE_MEM_ALLOC_FAILURES
|
||||
|
||||
static int mem_alloc_state = 0;
|
||||
|
||||
static int fastrand ()
|
||||
{
|
||||
if(!mem_alloc_state) return 1; /* No failures */
|
||||
mem_alloc_state = (214013 * mem_alloc_state + 2531011);
|
||||
return (mem_alloc_state >> 16) & 0x7FFF;
|
||||
}
|
||||
|
||||
static void *malloc_wrapper(size_t size) {
|
||||
return (fastrand () % 16) ? malloc (size) : NULL;
|
||||
}
|
||||
static void free_wrapper(void *freeable) {
|
||||
free(freeable);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
FILE *bufferToFile(const uint8_t *Data, size_t Size) {
|
||||
FILE *fd;
|
||||
fd = tmpfile();
|
||||
@@ -71,8 +91,17 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
|
||||
memset(workflow->stats.protocol_flows, 0,
|
||||
sizeof(workflow->stats.protocol_flows));
|
||||
ndpi_finalize_initialization(workflow->ndpi_struct);
|
||||
#ifdef ENABLE_MEM_ALLOC_FAILURES
|
||||
set_ndpi_malloc(malloc_wrapper);
|
||||
set_ndpi_free(free_wrapper);
|
||||
/* Don't fail memory allocations until init phase is done */
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef ENABLE_MEM_ALLOC_FAILURES
|
||||
mem_alloc_state = Size;
|
||||
#endif
|
||||
|
||||
fd = bufferToFile(Data, Size);
|
||||
if (fd == NULL)
|
||||
return 0;
|
||||
@@ -92,6 +121,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
|
||||
workflow->pcap_handle = pkts;
|
||||
/* Init flow tree */
|
||||
workflow->ndpi_flows_root = ndpi_calloc(workflow->prefs.num_roots, sizeof(void *));
|
||||
if(!workflow->ndpi_flows_root) {
|
||||
pcap_close(pkts);
|
||||
return 0;
|
||||
}
|
||||
|
||||
header = NULL;
|
||||
r = pcap_next_ex(pkts, &header, &pkt);
|
||||
|
||||
Reference in New Issue
Block a user