Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzz: simplify fuzzers dependencies in CIFuzz #1896

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 0 additions & 85 deletions src/lib/ndpi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,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[] = {
Expand Down Expand Up @@ -228,55 +225,12 @@ static inline uint8_t flow_is_proto(struct ndpi_flow_struct *flow, u_int16_t 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);
Expand All @@ -286,39 +240,6 @@ void ndpi_flow_free(void *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 */
struct ndpi_ptree
{
Expand Down Expand Up @@ -2515,16 +2436,10 @@ 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;
}
Expand Down
96 changes: 96 additions & 0 deletions src/lib/ndpi_memory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include "ndpi_win32.h" /* For __sync_fetch_and_add */
#endif

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

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