mirror of
https://github.com/eclipse-mosquitto/mosquitto.git
synced 2026-05-21 12:24:29 +02:00
Experimental fuzz memory checking
This commit is contained in:
parent
b1a2be20c3
commit
1f5d33b1e7
|
|
@ -3,11 +3,17 @@
|
|||
#include "libcommon_fuzz_property.pb.h"
|
||||
#include "mosquitto.h"
|
||||
|
||||
extern int32_t fuzz_memory_check_limit;
|
||||
extern int32_t fuzz_memory_check_count;
|
||||
|
||||
DEFINE_PROTO_FUZZER(const fuzz_property::FuzzerInput& fuzzer_input)
|
||||
{
|
||||
mosquitto_property *prop_list = nullptr;
|
||||
mosquitto_property *prop_copy = nullptr;
|
||||
|
||||
fuzz_memory_check_count = 0;
|
||||
fuzz_memory_check_limit = fuzzer_input.memory_check_limit();
|
||||
|
||||
for(const fuzz_property::Property& property : fuzzer_input.properties()){
|
||||
int identifier = property.identifier();
|
||||
switch(property.data_case()){
|
||||
|
|
|
|||
|
|
@ -22,4 +22,5 @@ message Property {
|
|||
|
||||
message FuzzerInput {
|
||||
repeated Property properties = 1;
|
||||
required int32 memory_check_limit = 2;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,19 @@ Contributors:
|
|||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef WITH_FUZZING
|
||||
int32_t fuzz_memory_check_count = 0;
|
||||
int32_t fuzz_memory_check_limit = -1;
|
||||
bool fuzz_memory_check(void)
|
||||
{
|
||||
fuzz_memory_check_count++;
|
||||
return (fuzz_memory_check_limit > 0 && fuzz_memory_check_count >= fuzz_memory_check_limit);
|
||||
}
|
||||
# define FUZZ_MEMORY_CHECK() if(fuzz_memory_check()){ return NULL; }
|
||||
#else
|
||||
# define FUZZ_MEMORY_CHECK()
|
||||
#endif
|
||||
|
||||
static unsigned long memcount = 0;
|
||||
static unsigned long max_memcount = 0;
|
||||
|
||||
|
|
@ -182,6 +195,8 @@ BROKER_EXPORT void *mosquitto_realloc(void *ptr, size_t size)
|
|||
bool alloc_mismatch = free_size > 0 && !check_alloc_marker(ptr, free_size);
|
||||
#endif
|
||||
|
||||
FUZZ_MEMORY_CHECK();
|
||||
|
||||
/* Avoid counter underflow due to mismatched memory allocation function usage */
|
||||
if(free_size > memcount){
|
||||
free_size = memcount;
|
||||
|
|
|
|||
Loading…
Reference in a new issue