From d21e6b97275f3a8e6b49327224232a29977dfa4a Mon Sep 17 00:00:00 2001 From: Samantha Date: Wed, 12 Oct 2016 09:41:36 +0200 Subject: [PATCH 1/5] pseudo randomly failing malloc; logging successes and failures in mem_test.log --- Makefile | 8 ++++++++ include/pico_config.h | 18 +++++++++++++++++- stack/pico_stack.c | 8 +++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 60d3a9760..b249463c3 100644 --- a/Makefile +++ b/Makefile @@ -157,6 +157,14 @@ ifeq ($(ARCH),faulty) DUMMY_EXTRA+=test/pico_faulty.o endif +ifdef CHECK_MEM + CFLAGS+=-DCHECK_MEM +endif + +ifdef MEM_TEST_SEED + CFLAGS+=-DMEM_TEST_SEED=$(MEM_TEST_SEED) +endif + ifeq ($(ARCH),msp430) CFLAGS+=-DMSP430 endif diff --git a/include/pico_config.h b/include/pico_config.h index b37ba63c0..1dd1f579f 100644 --- a/include/pico_config.h +++ b/include/pico_config.h @@ -9,6 +9,7 @@ #ifndef __KERNEL__ #include #include +#include #include #else #include @@ -51,6 +52,8 @@ #define long_be(x) (x) #define long_long_be(x) (x) + + static inline uint16_t short_from(void *_p) { unsigned char *p = (unsigned char *)_p; @@ -163,7 +166,6 @@ static inline uint64_t long_long_be(uint64_t le) # endif /* BYTESWAP_GCC */ #endif - /* Mockables */ #if defined UNIT_TEST # define MOCKABLE __attribute__((weak)) @@ -229,9 +231,23 @@ static inline uint64_t long_long_be(uint64_t le) # include "arch/pico_posix.h" #endif +static inline void log(const char* filename, const char* message) +{ + FILE * file; + file = fopen(filename, "a"); + fprintf(file, message); + fclose(file); +} + #ifdef PICO_SUPPORT_MM #define PICO_ZALLOC(x) pico_mem_zalloc(x) #define PICO_FREE(x) pico_mem_free(x) +#elif defined CHECK_MEM +#define PICO_ZALLOC(x) \ +((((double)(rand())/(double)RAND_MAX) < 0.02) \ +? (log("mem_test.log", "Malloc FAILED\n\n"), NULL) \ +: (log("mem_test.log", "Malloc Succeeded\n\n"), pico_zalloc(x))) +#define PICO_FREE(x) pico_free(x) #else #define PICO_ZALLOC(x) pico_zalloc(x) #define PICO_FREE(x) pico_free(x) diff --git a/stack/pico_stack.c b/stack/pico_stack.c index 9d86d1379..11b154e5a 100644 --- a/stack/pico_stack.c +++ b/stack/pico_stack.c @@ -38,7 +38,6 @@ # define MOCKABLE #endif - volatile pico_time pico_tick; volatile pico_err_t pico_err; @@ -857,6 +856,11 @@ uint32_t pico_timer_add_hashed(pico_time expire, void (*timer)(pico_time, void * int MOCKABLE pico_stack_init(void) { +#if ((defined CHECK_MEM) && (defined MEM_TEST_SEED)) + srand(MEM_TEST_SEED); + fopen("mem_test.log", "w"); +#endif + #ifdef PICO_SUPPORT_ETH pico_protocol_init(&pico_proto_ethernet); #endif @@ -917,9 +921,11 @@ int MOCKABLE pico_stack_init(void) pico_aodv_init(); #endif + pico_stack_tick(); pico_stack_tick(); pico_stack_tick(); + return 0; } From 5eedf8ef4f3325ae16648cc4ea7ee24e585d1657 Mon Sep 17 00:00:00 2001 From: Samantha Date: Wed, 12 Oct 2016 11:36:48 +0200 Subject: [PATCH 2/5] prints stack traces in log file --- Makefile | 2 +- include/pico_config.h | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index b249463c3..f648349af 100644 --- a/Makefile +++ b/Makefile @@ -158,7 +158,7 @@ ifeq ($(ARCH),faulty) endif ifdef CHECK_MEM - CFLAGS+=-DCHECK_MEM + CFLAGS+=-DCHECK_MEM -rdynamic endif ifdef MEM_TEST_SEED diff --git a/include/pico_config.h b/include/pico_config.h index 1dd1f579f..0d92cd4e1 100644 --- a/include/pico_config.h +++ b/include/pico_config.h @@ -239,14 +239,33 @@ static inline void log(const char* filename, const char* message) fclose(file); } +static inline void append_backtrace(const char* filename) +{ + void *array[10]; + size_t size; + int fd; + FILE * file; + file = fopen(filename, "a"); + fd = fileno(file); + + fprintf(file, "Backtrace:\n"); + fseek(file, 0, SEEK_END); + + size = backtrace(array, 10); + backtrace_symbols_fd(array, size, fd); + fprintf(file, "\n"); + + fclose(file); +} + #ifdef PICO_SUPPORT_MM #define PICO_ZALLOC(x) pico_mem_zalloc(x) #define PICO_FREE(x) pico_mem_free(x) #elif defined CHECK_MEM #define PICO_ZALLOC(x) \ -((((double)(rand())/(double)RAND_MAX) < 0.02) \ -? (log("mem_test.log", "Malloc FAILED\n\n"), NULL) \ -: (log("mem_test.log", "Malloc Succeeded\n\n"), pico_zalloc(x))) +((((double)(rand())/(double)RAND_MAX) < 0.4) \ +? (log("mem_test.log", "Malloc FAILED\n"), append_backtrace("mem_test.log"), NULL) \ +: (log("mem_test.log", "Malloc Succeeded\n"), append_backtrace("mem_test.log"), pico_zalloc(x))) #define PICO_FREE(x) pico_free(x) #else #define PICO_ZALLOC(x) pico_zalloc(x) From e371afaa675ce190623705bfa53c238f96d9f2dd Mon Sep 17 00:00:00 2001 From: Samantha Date: Wed, 19 Oct 2016 10:47:03 +0200 Subject: [PATCH 3/5] changed random malloc seed to environment variable; rearranged added code --- include/pico_config.h | 20 +++++++++++++------- stack/pico_stack.c | 13 +++++++++++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/include/pico_config.h b/include/pico_config.h index 0d92cd4e1..42be5998c 100644 --- a/include/pico_config.h +++ b/include/pico_config.h @@ -14,6 +14,10 @@ #else #include #endif +#ifdef __linux__ +#include +#endif + #if defined __IAR_SYSTEMS_ICC__ || defined ATOP # define PACKED_STRUCT_DEF __packed struct @@ -231,7 +235,10 @@ static inline uint64_t long_long_be(uint64_t le) # include "arch/pico_posix.h" #endif -static inline void log(const char* filename, const char* message) + + +#ifdef CHECK_MEM +static inline void log_malloc(const char* filename, const char* message) { FILE * file; file = fopen(filename, "a"); @@ -258,15 +265,14 @@ static inline void append_backtrace(const char* filename) fclose(file); } -#ifdef PICO_SUPPORT_MM -#define PICO_ZALLOC(x) pico_mem_zalloc(x) -#define PICO_FREE(x) pico_mem_free(x) -#elif defined CHECK_MEM #define PICO_ZALLOC(x) \ ((((double)(rand())/(double)RAND_MAX) < 0.4) \ -? (log("mem_test.log", "Malloc FAILED\n"), append_backtrace("mem_test.log"), NULL) \ -: (log("mem_test.log", "Malloc Succeeded\n"), append_backtrace("mem_test.log"), pico_zalloc(x))) +? (log_malloc("mem_test.log", "Malloc FAILED\n"), append_backtrace("mem_test.log"), NULL) \ +: (log_malloc("mem_test.log", "Malloc Succeeded\n"), append_backtrace("mem_test.log"), pico_zalloc(x))) #define PICO_FREE(x) pico_free(x) +#elif defined PICO_SUPPORT_MM +#define PICO_ZALLOC(x) pico_mem_zalloc(x) +#define PICO_FREE(x) pico_mem_free(x) #else #define PICO_ZALLOC(x) pico_zalloc(x) #define PICO_FREE(x) pico_free(x) diff --git a/stack/pico_stack.c b/stack/pico_stack.c index 11b154e5a..bbca72f98 100644 --- a/stack/pico_stack.c +++ b/stack/pico_stack.c @@ -856,9 +856,18 @@ uint32_t pico_timer_add_hashed(pico_time expire, void (*timer)(pico_time, void * int MOCKABLE pico_stack_init(void) { -#if ((defined CHECK_MEM) && (defined MEM_TEST_SEED)) - srand(MEM_TEST_SEED); + +#ifdef CHECK_MEM + //New log for malloc information fopen("mem_test.log", "w"); + + //Set rand seed + char *buf = getenv("MEM_TEST_SEED"); + if(buf!=NULL){ + srand(atoi(buf)); + } + else + srand(1); #endif #ifdef PICO_SUPPORT_ETH From 58dd1cf500cd796eac94bd1bc8d48c59c17c8d07 Mon Sep 17 00:00:00 2001 From: Samantha Date: Fri, 21 Oct 2016 15:52:54 +0200 Subject: [PATCH 4/5] control where mallocs start failing via external variable --- include/pico_config.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/pico_config.h b/include/pico_config.h index 42be5998c..60f2af946 100644 --- a/include/pico_config.h +++ b/include/pico_config.h @@ -236,8 +236,9 @@ static inline uint64_t long_long_be(uint64_t le) #endif - #ifdef CHECK_MEM +extern int start_failing_mallocs; + static inline void log_malloc(const char* filename, const char* message) { FILE * file; @@ -266,7 +267,7 @@ static inline void append_backtrace(const char* filename) } #define PICO_ZALLOC(x) \ -((((double)(rand())/(double)RAND_MAX) < 0.4) \ +((start_failing_mallocs && (((double)(rand())/(double)RAND_MAX) < 0.4)) \ ? (log_malloc("mem_test.log", "Malloc FAILED\n"), append_backtrace("mem_test.log"), NULL) \ : (log_malloc("mem_test.log", "Malloc Succeeded\n"), append_backtrace("mem_test.log"), pico_zalloc(x))) #define PICO_FREE(x) pico_free(x) From 8910cda2e163820906af7ce167ef0451a621ae19 Mon Sep 17 00:00:00 2001 From: Samantha Date: Fri, 21 Oct 2016 15:58:30 +0200 Subject: [PATCH 5/5] took out MEM_TEST_SEED compiler flag, sent in as env variable --- Makefile | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Makefile b/Makefile index f648349af..d9540d963 100644 --- a/Makefile +++ b/Makefile @@ -161,10 +161,6 @@ ifdef CHECK_MEM CFLAGS+=-DCHECK_MEM -rdynamic endif -ifdef MEM_TEST_SEED - CFLAGS+=-DMEM_TEST_SEED=$(MEM_TEST_SEED) -endif - ifeq ($(ARCH),msp430) CFLAGS+=-DMSP430 endif