Move malloc_wrappers.c to tests/common
This commit is contained in:
@@ -5,8 +5,8 @@ Import("env")
|
||||
# We need our own pb_decode.o for the malloc support
|
||||
env = env.Clone()
|
||||
env.Append(CPPDEFINES = {'PB_ENABLE_MALLOC': 1,
|
||||
'PB_SYSTEM_HEADER': '\\"fuzz_syshdr.h\\"'})
|
||||
env.Append(CPPPATH = ".")
|
||||
'PB_SYSTEM_HEADER': '\\"malloc_wrappers_syshdr.h\\"'})
|
||||
env.Append(CPPPATH = [".", "$COMMON"])
|
||||
|
||||
if 'SYSHDR' in env:
|
||||
env.Append(CPPDEFINES = {'PB_OLD_SYSHDR': env['SYSHDR']})
|
||||
@@ -42,8 +42,7 @@ fuzz = env.Program(["fuzztest.c",
|
||||
"pb_encode_with_malloc.o",
|
||||
"pb_decode_with_malloc.o",
|
||||
"pb_common_with_malloc.o",
|
||||
"malloc_wrappers.c"])
|
||||
Depends([p1, p2, fuzz], ["fuzz_syshdr.h", "malloc_wrappers.h"])
|
||||
"$COMMON/malloc_wrappers.o"])
|
||||
|
||||
env.RunTest(fuzz)
|
||||
|
||||
@@ -53,6 +52,6 @@ fuzzstub = env.Program(["fuzzstub.c",
|
||||
"pb_encode_with_malloc.o",
|
||||
"pb_decode_with_malloc.o",
|
||||
"pb_common_with_malloc.o",
|
||||
"malloc_wrappers.c"])
|
||||
"$COMMON/malloc_wrappers.o"])
|
||||
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
/* This is just a wrapper in order to get our own malloc wrappers into nanopb core. */
|
||||
|
||||
#define pb_realloc(ptr,size) counting_realloc(ptr,size)
|
||||
#define pb_free(ptr) counting_free(ptr)
|
||||
|
||||
#ifdef PB_OLD_SYSHDR
|
||||
#include PB_OLD_SYSHDR
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#include <malloc_wrappers.h>
|
||||
@@ -10,7 +10,7 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include "malloc_wrappers.h"
|
||||
#include <malloc_wrappers.h>
|
||||
#include "alltypes_static.pb.h"
|
||||
#include "alltypes_pointer.pb.h"
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include "malloc_wrappers.h"
|
||||
#include <malloc_wrappers.h>
|
||||
#include "alltypes_static.pb.h"
|
||||
#include "alltypes_pointer.pb.h"
|
||||
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
#include "malloc_wrappers.h"
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
static size_t alloc_count = 0;
|
||||
|
||||
/* Allocate memory and place check values before and after. */
|
||||
void* malloc_with_check(size_t size)
|
||||
{
|
||||
size_t size32 = (size + 3) / 4 + 3;
|
||||
uint32_t *buf = malloc(size32 * sizeof(uint32_t));
|
||||
buf[0] = size32;
|
||||
buf[1] = 0xDEADBEEF;
|
||||
buf[size32 - 1] = 0xBADBAD;
|
||||
return buf + 2;
|
||||
}
|
||||
|
||||
/* Free memory allocated with malloc_with_check() and do the checks. */
|
||||
void free_with_check(void *mem)
|
||||
{
|
||||
uint32_t *buf = (uint32_t*)mem - 2;
|
||||
assert(buf[1] == 0xDEADBEEF);
|
||||
assert(buf[buf[0] - 1] == 0xBADBAD);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
/* Track memory usage */
|
||||
void* counting_realloc(void *ptr, size_t size)
|
||||
{
|
||||
/* Don't allocate crazy amounts of RAM when fuzzing */
|
||||
if (size > 1000000)
|
||||
return NULL;
|
||||
|
||||
if (!ptr && size)
|
||||
alloc_count++;
|
||||
|
||||
return realloc(ptr, size);
|
||||
}
|
||||
|
||||
void counting_free(void *ptr)
|
||||
{
|
||||
if (ptr)
|
||||
{
|
||||
assert(alloc_count > 0);
|
||||
alloc_count--;
|
||||
free(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
size_t get_alloc_count()
|
||||
{
|
||||
return alloc_count;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
void* malloc_with_check(size_t size);
|
||||
void free_with_check(void *mem);
|
||||
void* counting_realloc(void *ptr, size_t size);
|
||||
void counting_free(void *ptr);
|
||||
size_t get_alloc_count();
|
||||
Reference in New Issue
Block a user