aboutsummaryrefslogtreecommitdiff
path: root/fuzz/fuzz_common_code.c
diff options
context:
space:
mode:
authorIvan Nardi <12729895+IvanNardi@users.noreply.github.com>2023-01-20 14:27:33 +0100
committerGitHub <noreply@github.com>2023-01-20 14:27:33 +0100
commit5e8c1ebbb7f67033916ed4878cd6c2a662073898 (patch)
treef538977aa08015bffcf99ec9f6bb505c9143232c /fuzz/fuzz_common_code.c
parent496b284c9888c090696cc8e570d0b20c08dc3d63 (diff)
fuzz: fix memory allocation failure logic (#1867)
We *do* want to have some allocation errors. Fix some related bugs Fix: 29be01ef
Diffstat (limited to 'fuzz/fuzz_common_code.c')
-rw-r--r--fuzz/fuzz_common_code.c29
1 files changed, 15 insertions, 14 deletions
diff --git a/fuzz/fuzz_common_code.c b/fuzz/fuzz_common_code.c
index bd5ef20a4..e4ab68ad8 100644
--- a/fuzz/fuzz_common_code.c
+++ b/fuzz/fuzz_common_code.c
@@ -2,8 +2,6 @@
#include "fuzz_common_code.h"
-#ifdef ENABLE_MEM_ALLOC_FAILURES
-
static int mem_alloc_state = 0;
__attribute__((no_sanitize("integer")))
@@ -14,18 +12,27 @@ static int fastrand ()
return (mem_alloc_state >> 16) & 0x7FFF;
}
-void *malloc_wrapper(size_t size) {
+static void *malloc_wrapper(size_t size) {
return (fastrand () % 16) ? malloc (size) : NULL;
}
-void free_wrapper(void *freeable) {
+static void free_wrapper(void *freeable) {
free(freeable);
}
-void set_mem_alloc_state(int value) {
- mem_alloc_state = value;
+void fuzz_set_alloc_callbacks(void)
+{
+ set_ndpi_malloc(malloc_wrapper);
+ set_ndpi_free(free_wrapper);
+}
+void fuzz_set_alloc_seed(int seed)
+{
+ mem_alloc_state = seed;
+}
+void fuzz_set_alloc_callbacks_and_seed(int seed)
+{
+ fuzz_set_alloc_callbacks();
+ fuzz_set_alloc_seed(seed);
}
-
-#endif
void fuzz_init_detection_module(struct ndpi_detection_module_struct **ndpi_info_mod,
int enable_log)
@@ -34,10 +41,6 @@ void fuzz_init_detection_module(struct ndpi_detection_module_struct **ndpi_info_
NDPI_PROTOCOL_BITMASK all, debug_bitmask;
if(*ndpi_info_mod == NULL) {
-#ifdef ENABLE_MEM_ALLOC_FAILURES
- set_ndpi_malloc(malloc_wrapper);
- set_ndpi_free(free_wrapper);
-#endif
*ndpi_info_mod = ndpi_init_detection_module(prefs);
NDPI_BITMASK_SET_ALL(all);
ndpi_set_protocol_detection_bitmask2(*ndpi_info_mod, &all);
@@ -57,5 +60,3 @@ void fuzz_init_detection_module(struct ndpi_detection_module_struct **ndpi_info_
ndpi_finalize_initialization(*ndpi_info_mod);
}
}
-
-