Skip to content

Commit

Permalink
fuzz: simplify fuzzers dependencies in CIFuzz
Browse files Browse the repository at this point in the history
Move all the low-level allocation callbacks to a dedicated file.

TODO

Remove the dedicated allocation functions for `ndpi_flow` structures; we can
use the generic `ndpi_malloc()`/`ndpi_free()`
  • Loading branch information
IvanNardi committed Mar 2, 2023
1 parent 89cae9d commit a23e4e5
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 119 deletions.
4 changes: 2 additions & 2 deletions example/ndpiSimpleIntegration.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ static void ndpi_flow_info_freer(void * const node)
{
struct nDPI_flow_info * const flow = (struct nDPI_flow_info *)node;

ndpi_flow_free(flow->ndpi_flow);
ndpi_free_flow(flow->ndpi_flow);
ndpi_free(flow);
}

Expand Down Expand Up @@ -814,7 +814,7 @@ static void ndpi_process_packet(uint8_t * const args,
memcpy(flow_to_process, &flow, sizeof(*flow_to_process));
flow_to_process->flow_id = __sync_fetch_and_add(&flow_id, 1);

flow_to_process->ndpi_flow = (struct ndpi_flow_struct *)ndpi_flow_malloc(SIZEOF_FLOW_STRUCT);
flow_to_process->ndpi_flow = (struct ndpi_flow_struct *)ndpi_malloc(SIZEOF_FLOW_STRUCT);
if (flow_to_process->ndpi_flow == NULL) {
fprintf(stderr, "[%8llu, %d, %4u] Not enough memory for flow struct\n",
workflow->packets_captured, reader_thread->array_index, flow_to_process->flow_id);
Expand Down
5 changes: 2 additions & 3 deletions example/reader_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ void ndpi_report_payload_stats() {
/* ***************************************************** */

void ndpi_free_flow_info_half(struct ndpi_flow_info *flow) {
if(flow->ndpi_flow) { ndpi_flow_free(flow->ndpi_flow); flow->ndpi_flow = NULL; }
if(flow->ndpi_flow) { ndpi_free_flow(flow->ndpi_flow); flow->ndpi_flow = NULL; }
}

/* ***************************************************** */
Expand Down Expand Up @@ -436,7 +436,6 @@ struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * pref
static int _debug_protocols_ok = 0;

set_ndpi_malloc(ndpi_malloc_wrapper), set_ndpi_free(free_wrapper);
set_ndpi_flow_malloc(NULL), set_ndpi_flow_free(NULL);

/* TODO: just needed here to init ndpi ndpi_malloc wrapper */
module = ndpi_init_detection_module(init_prefs);
Expand Down Expand Up @@ -919,7 +918,7 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow
ndpi_patchIPv6Address(newflow->src_name), ndpi_patchIPv6Address(newflow->dst_name);
}

if((newflow->ndpi_flow = ndpi_flow_malloc(SIZEOF_FLOW_STRUCT)) == NULL) {
if((newflow->ndpi_flow = ndpi_malloc(SIZEOF_FLOW_STRUCT)) == NULL) {
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
/* Avoid too much logging while fuzzing */
LOG(NDPI_LOG_ERROR, "[NDPI] %s(2): not enough memory\n", __FUNCTION__);
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_process_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
ndpi_init_serializer(&csv_serializer, ndpi_serialization_format_csv);
}

struct ndpi_flow_struct *ndpi_flow = ndpi_flow_malloc(SIZEOF_FLOW_STRUCT);
struct ndpi_flow_struct *ndpi_flow = ndpi_malloc(SIZEOF_FLOW_STRUCT);
memset(ndpi_flow, 0, SIZEOF_FLOW_STRUCT);
ndpi_protocol detected_protocol =
ndpi_detection_process_packet(ndpi_info_mod, ndpi_flow, Data, Size, 0, NULL);
Expand Down
113 changes: 0 additions & 113 deletions src/lib/ndpi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,6 @@ u_int ndpi_verbose_dga_detection = 0;

/* ****************************************** */

static void *(*_ndpi_flow_malloc)(size_t size);
static void (*_ndpi_flow_free)(void *ptr);

static void *(*_ndpi_malloc)(size_t size);
static void (*_ndpi_free)(void *ptr);

/* ****************************************** */

static ndpi_risk_info ndpi_known_risks[] = {
{ NDPI_NO_RISK, NDPI_RISK_LOW, CLIENT_FAIR_RISK_PERCENTAGE, NDPI_NO_ACCOUNTABILITY },
{ NDPI_URL_POSSIBLE_XSS, NDPI_RISK_SEVERE, CLIENT_HIGH_RISK_PERCENTAGE, NDPI_CLIENT_ACCOUNTABLE },
Expand Down Expand Up @@ -226,97 +218,6 @@ static inline uint8_t flow_is_proto(struct ndpi_flow_struct *flow, u_int16_t p)
return((flow->detected_protocol_stack[0] == p) || (flow->detected_protocol_stack[1] == p));
}

/* ****************************************** */

static volatile long int ndpi_tot_allocated_memory;

/* ****************************************** */

u_int32_t ndpi_get_tot_allocated_memory() {
return(__sync_fetch_and_add(&ndpi_tot_allocated_memory, 0));
}

/* ****************************************** */

void *ndpi_malloc(size_t size) {
__sync_fetch_and_add(&ndpi_tot_allocated_memory, size);
return(_ndpi_malloc ? _ndpi_malloc(size) : malloc(size));
}

/* ****************************************** */

void *ndpi_flow_malloc(size_t size) {
return(_ndpi_flow_malloc ? _ndpi_flow_malloc(size) : ndpi_malloc(size));
}

/* ****************************************** */

void *ndpi_calloc(unsigned long count, size_t size) {
size_t len = count * size;
void *p = ndpi_malloc(len);

if(p) {
memset(p, 0, len);
__sync_fetch_and_add(&ndpi_tot_allocated_memory, size);
}

return(p);
}

/* ****************************************** */

void ndpi_free(void *ptr) {
if(_ndpi_free) {
if(ptr)
_ndpi_free(ptr);
} else {
if(ptr)
free(ptr);
}
}

/* ****************************************** */

void ndpi_flow_free(void *ptr) {
if(_ndpi_flow_free)
_ndpi_flow_free(ptr);
else
ndpi_free_flow((struct ndpi_flow_struct *) ptr);
}

/* ****************************************** */

void *ndpi_realloc(void *ptr, size_t old_size, size_t new_size) {
void *ret = ndpi_malloc(new_size);

if(!ret)
return(ret);
else {
if(ptr != NULL) {
memcpy(ret, ptr, (old_size < new_size ? old_size : new_size));
ndpi_free(ptr);
}
return(ret);
}
}
/* ****************************************** */

char *ndpi_strdup(const char *s) {
if(s == NULL ){
return NULL;
}

int len = strlen(s);
char *m = ndpi_malloc(len + 1);

if(m) {
memcpy(m, s, len);
m[len] = '\0';
}

return(m);
}

/* *********************************************************************************** */

/* Opaque structure defined here */
Expand Down Expand Up @@ -2515,20 +2416,6 @@ static int ndpi_add_host_ip_subprotocol(struct ndpi_detection_module_struct *ndp
return(0);
}

void set_ndpi_malloc(void *(*__ndpi_malloc)(size_t size)) {
_ndpi_malloc = __ndpi_malloc;
}
void set_ndpi_flow_malloc(void *(*__ndpi_flow_malloc)(size_t size)) {
_ndpi_flow_malloc = __ndpi_flow_malloc;
}

void set_ndpi_free(void (*__ndpi_free)(void *ptr)) {
_ndpi_free = __ndpi_free;
}
void set_ndpi_flow_free(void (*__ndpi_flow_free)(void *ptr)) {
_ndpi_flow_free = __ndpi_flow_free;
}

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, int line_number, const char *format, ...) {
#ifdef NDPI_ENABLE_DEBUG_MESSAGES
Expand Down
94 changes: 94 additions & 0 deletions src/lib/ndpi_memory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>


/* ****************************************** */

static void *(*_ndpi_malloc)(size_t size);
static void (*_ndpi_free)(void *ptr);

static volatile long int ndpi_tot_allocated_memory;

/* ****************************************** */

void set_ndpi_malloc(void *(*__ndpi_malloc)(size_t size)) {
_ndpi_malloc = __ndpi_malloc;
}

void set_ndpi_free(void (*__ndpi_free)(void *ptr)) {
_ndpi_free = __ndpi_free;
}

/* ****************************************** */

u_int32_t ndpi_get_tot_allocated_memory() {
return(__sync_fetch_and_add(&ndpi_tot_allocated_memory, 0));
}

/* ****************************************** */

void *ndpi_malloc(size_t size) {
__sync_fetch_and_add(&ndpi_tot_allocated_memory, size);
return(_ndpi_malloc ? _ndpi_malloc(size) : malloc(size));
}

/* ****************************************** */

void *ndpi_calloc(unsigned long count, size_t size) {
size_t len = count * size;
void *p = ndpi_malloc(len);

if(p) {
memset(p, 0, len);
__sync_fetch_and_add(&ndpi_tot_allocated_memory, size);
}

return(p);
}

/* ****************************************** */

void ndpi_free(void *ptr) {
if(_ndpi_free) {
if(ptr)
_ndpi_free(ptr);
} else {
if(ptr)
free(ptr);
}
}

/* ****************************************** */

void *ndpi_realloc(void *ptr, size_t old_size, size_t new_size) {
void *ret = ndpi_malloc(new_size);

if(!ret)
return(ret);
else {
if(ptr != NULL) {
memcpy(ret, ptr, (old_size < new_size ? old_size : new_size));
ndpi_free(ptr);
}
return(ret);
}
}

/* ****************************************** */

char *ndpi_strdup(const char *s) {
if(s == NULL ){
return NULL;
}

int len = strlen(s);
char *m = ndpi_malloc(len + 1);

if(m) {
memcpy(m, s, len);
m[len] = '\0';
}

return(m);
}
1 change: 1 addition & 0 deletions windows/nDPI.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
<ClCompile Include="..\src\lib\ndpi_community_id.c" />
<ClCompile Include="..\src\lib\ndpi_geoip.c" />
<ClCompile Include="..\src\lib\ndpi_main.c" />
<ClCompile Include="..\src\lib\ndpi_memory.c" />
<ClCompile Include="..\src\lib\ndpi_serializer.c" />
<ClCompile Include="..\src\lib\ndpi_utils.c" />
<ClCompile Include="..\src\lib\protocols\activision.c" />
Expand Down
1 change: 1 addition & 0 deletions windows/nDPI.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<ClCompile Include="..\src\lib\ndpi_community_id.c" />
<ClCompile Include="..\src\lib\ndpi_geoip.c" />
<ClCompile Include="..\src\lib\ndpi_main.c" />
<ClCompile Include="..\src\lib\ndpi_memory.c" />
<ClCompile Include="..\src\lib\ndpi_serializer.c" />
<ClCompile Include="..\src\lib\ndpi_utils.c" />
<ClCompile Include="..\src\lib\protocols\ajp.c" />
Expand Down

0 comments on commit a23e4e5

Please sign in to comment.