From 8a28b70351baf09f2131fee2fc186a96d069cc2e Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Fri, 26 Dec 2014 17:08:17 +0200 Subject: Move malloc_wrappers.c to tests/common --- tests/common/SConscript | 4 +++ tests/common/malloc_wrappers.c | 54 +++++++++++++++++++++++++++++++++++ tests/common/malloc_wrappers.h | 7 +++++ tests/common/malloc_wrappers_syshdr.h | 15 ++++++++++ tests/fuzztest/SConscript | 9 +++--- tests/fuzztest/fuzz_syshdr.h | 15 ---------- tests/fuzztest/fuzzstub.c | 2 +- tests/fuzztest/fuzztest.c | 2 +- tests/fuzztest/malloc_wrappers.c | 54 ----------------------------------- tests/fuzztest/malloc_wrappers.h | 7 ----- 10 files changed, 86 insertions(+), 83 deletions(-) create mode 100644 tests/common/malloc_wrappers.c create mode 100644 tests/common/malloc_wrappers.h create mode 100644 tests/common/malloc_wrappers_syshdr.h delete mode 100644 tests/fuzztest/fuzz_syshdr.h delete mode 100644 tests/fuzztest/malloc_wrappers.c delete mode 100644 tests/fuzztest/malloc_wrappers.h (limited to 'tests') diff --git a/tests/common/SConscript b/tests/common/SConscript index f1dee0e2..4581bea1 100644 --- a/tests/common/SConscript +++ b/tests/common/SConscript @@ -15,3 +15,7 @@ strict.Append(CFLAGS = strict['CORECFLAGS']) strict.Object("pb_decode.o", "$NANOPB/pb_decode.c") strict.Object("pb_encode.o", "$NANOPB/pb_encode.c") strict.Object("pb_common.o", "$NANOPB/pb_common.c") + +mw = env.Object("malloc_wrappers.o", "malloc_wrappers.c") +Depends(mw, ["malloc_wrappers_syshdr.h"]) + diff --git a/tests/common/malloc_wrappers.c b/tests/common/malloc_wrappers.c new file mode 100644 index 00000000..ad69f1ce --- /dev/null +++ b/tests/common/malloc_wrappers.c @@ -0,0 +1,54 @@ +#include "malloc_wrappers.h" +#include +#include +#include + +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; +} diff --git a/tests/common/malloc_wrappers.h b/tests/common/malloc_wrappers.h new file mode 100644 index 00000000..7eec7952 --- /dev/null +++ b/tests/common/malloc_wrappers.h @@ -0,0 +1,7 @@ +#include + +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(); diff --git a/tests/common/malloc_wrappers_syshdr.h b/tests/common/malloc_wrappers_syshdr.h new file mode 100644 index 00000000..d295d9ed --- /dev/null +++ b/tests/common/malloc_wrappers_syshdr.h @@ -0,0 +1,15 @@ +/* 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 +#include +#include +#include +#endif + +#include diff --git a/tests/fuzztest/SConscript b/tests/fuzztest/SConscript index 6499714f..346ccab8 100644 --- a/tests/fuzztest/SConscript +++ b/tests/fuzztest/SConscript @@ -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"]) diff --git a/tests/fuzztest/fuzz_syshdr.h b/tests/fuzztest/fuzz_syshdr.h deleted file mode 100644 index d295d9ed..00000000 --- a/tests/fuzztest/fuzz_syshdr.h +++ /dev/null @@ -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 -#include -#include -#include -#endif - -#include diff --git a/tests/fuzztest/fuzzstub.c b/tests/fuzztest/fuzzstub.c index 50998416..ce14b9ba 100644 --- a/tests/fuzztest/fuzzstub.c +++ b/tests/fuzztest/fuzzstub.c @@ -10,7 +10,7 @@ #include #include #include -#include "malloc_wrappers.h" +#include #include "alltypes_static.pb.h" #include "alltypes_pointer.pb.h" diff --git a/tests/fuzztest/fuzztest.c b/tests/fuzztest/fuzztest.c index 996ed450..d3701724 100644 --- a/tests/fuzztest/fuzztest.c +++ b/tests/fuzztest/fuzztest.c @@ -9,7 +9,7 @@ #include #include #include -#include "malloc_wrappers.h" +#include #include "alltypes_static.pb.h" #include "alltypes_pointer.pb.h" diff --git a/tests/fuzztest/malloc_wrappers.c b/tests/fuzztest/malloc_wrappers.c deleted file mode 100644 index ad69f1ce..00000000 --- a/tests/fuzztest/malloc_wrappers.c +++ /dev/null @@ -1,54 +0,0 @@ -#include "malloc_wrappers.h" -#include -#include -#include - -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; -} diff --git a/tests/fuzztest/malloc_wrappers.h b/tests/fuzztest/malloc_wrappers.h deleted file mode 100644 index 7eec7952..00000000 --- a/tests/fuzztest/malloc_wrappers.h +++ /dev/null @@ -1,7 +0,0 @@ -#include - -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(); -- cgit 1.2.3-korg