Merge pull request #405 from dgarske/memtrackdebug

Memory tracking cleanup and new debug memory option
This commit is contained in:
toddouska
2016-05-04 14:01:37 -07:00
9 changed files with 134 additions and 26 deletions

View File

@ -130,7 +130,7 @@
#include "wolfcrypt/benchmark/benchmark.h" #include "wolfcrypt/benchmark/benchmark.h"
#ifdef USE_WOLFSSL_MEMORY #ifdef USE_WOLFSSL_MEMORY
#include "wolfssl/mem_track.h" #include "wolfssl/wolfcrypt/mem_track.h"
#endif #endif
void bench_des(void); void bench_des(void);

View File

@ -43,6 +43,7 @@
#include <stdio.h> #include <stdio.h>
#endif #endif
/* Set these to default values initially. */ /* Set these to default values initially. */
static wolfSSL_Malloc_cb malloc_function = 0; static wolfSSL_Malloc_cb malloc_function = 0;
static wolfSSL_Free_cb free_function = 0; static wolfSSL_Free_cb free_function = 0;
@ -72,15 +73,24 @@ int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
return res; return res;
} }
#ifdef WOLFSSL_DEBUG_MEMORY
void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line)
#else
void* wolfSSL_Malloc(size_t size) void* wolfSSL_Malloc(size_t size)
#endif
{ {
void* res = 0; void* res = 0;
if (malloc_function) if (malloc_function) {
#ifdef WOLFSSL_DEBUG_MEMORY
res = malloc_function(size, func, line);
#else
res = malloc_function(size); res = malloc_function(size);
else #endif
}
else {
res = malloc(size); res = malloc(size);
}
#ifdef WOLFSSL_MALLOC_CHECK #ifdef WOLFSSL_MALLOC_CHECK
if (res == NULL) if (res == NULL)
@ -90,22 +100,42 @@ void* wolfSSL_Malloc(size_t size)
return res; return res;
} }
#ifdef WOLFSSL_DEBUG_MEMORY
void wolfSSL_Free(void *ptr, const char* func, unsigned int line)
#else
void wolfSSL_Free(void *ptr) void wolfSSL_Free(void *ptr)
#endif
{ {
if (free_function) if (free_function) {
#ifdef WOLFSSL_DEBUG_MEMORY
free_function(ptr, func, line);
#else
free_function(ptr); free_function(ptr);
else #endif
}
else {
free(ptr); free(ptr);
} }
}
#ifdef WOLFSSL_DEBUG_MEMORY
void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line)
#else
void* wolfSSL_Realloc(void *ptr, size_t size) void* wolfSSL_Realloc(void *ptr, size_t size)
#endif
{ {
void* res = 0; void* res = 0;
if (realloc_function) if (realloc_function) {
#ifdef WOLFSSL_DEBUG_MEMORY
res = realloc_function(ptr, size, func, line);
#else
res = realloc_function(ptr, size); res = realloc_function(ptr, size);
else #endif
}
else {
res = realloc(ptr, size); res = realloc(ptr, size);
}
return res; return res;
} }

View File

@ -143,7 +143,7 @@
#include "wolfcrypt/test/test.h" #include "wolfcrypt/test/test.h"
#ifdef USE_WOLFSSL_MEMORY #ifdef USE_WOLFSSL_MEMORY
#include "wolfssl/mem_track.h" #include "wolfssl/wolfcrypt/mem_track.h"
#endif #endif

View File

@ -18,8 +18,7 @@ nobase_include_HEADERS+= \
wolfssl/version.h \ wolfssl/version.h \
wolfssl/options.h \ wolfssl/options.h \
wolfssl/ocsp.h \ wolfssl/ocsp.h \
wolfssl/crl.h \ wolfssl/crl.h
wolfssl/mem_track.h
noinst_HEADERS+= \ noinst_HEADERS+= \
wolfssl/internal.h wolfssl/internal.h

View File

@ -10,7 +10,7 @@
#include <wolfssl/wolfcrypt/types.h> #include <wolfssl/wolfcrypt/types.h>
#include <wolfssl/wolfcrypt/error-crypt.h> #include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/random.h> #include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/mem_track.h> #include <wolfssl/wolfcrypt/mem_track.h>
#ifdef ATOMIC_USER #ifdef ATOMIC_USER
#include <wolfssl/wolfcrypt/aes.h> #include <wolfssl/wolfcrypt/aes.h>

View File

@ -54,7 +54,8 @@ nobase_include_HEADERS+= \
wolfssl/wolfcrypt/logging.h \ wolfssl/wolfcrypt/logging.h \
wolfssl/wolfcrypt/memory.h \ wolfssl/wolfcrypt/memory.h \
wolfssl/wolfcrypt/mpi_class.h \ wolfssl/wolfcrypt/mpi_class.h \
wolfssl/wolfcrypt/mpi_superclass.h wolfssl/wolfcrypt/mpi_superclass.h \
wolfssl/wolfcrypt/mem_track.h
noinst_HEADERS+= \ noinst_HEADERS+= \
wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h \ wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h \

View File

@ -20,6 +20,40 @@
*/ */
/* The memory tracker overrides the wolfSSL memory callback system and uses a
* static to track the total, peak and currently allocated bytes.
*
* If you are already using the memory callbacks then enabling this will
* override the memory callbacks and prevent your memory callbacks from
* working. This assumes malloc() and free() are available. Feel free to
* customize this for your needs.
* The enable this feature define the following:
* #define USE_WOLFSSL_MEMORY
* #define WOLFSSL_TRACK_MEMORY
*
* On startup call:
* InitMemoryTracker();
*
* When ready to dump the memory report call:
* ShowMemoryTracker();
*
* Report example:
* total Allocs = 228
* total Bytes = 93442
* peak Bytes = 8840
* current Bytes = 0
*
*
* You can also:
* #define WOLFSSL_DEBUG_MEMORY
*
* To print every alloc/free along with the function and line number.
* Example output:
* Alloc: 0x7fa14a500010 -> 120 at wc_InitRng:496
* Free: 0x7fa14a500010 -> 120 at wc_FreeRng:606
*/
#ifndef WOLFSSL_MEM_TRACK_H #ifndef WOLFSSL_MEM_TRACK_H
#define WOLFSSL_MEM_TRACK_H #define WOLFSSL_MEM_TRACK_H
@ -64,7 +98,11 @@
#define STATIC static #define STATIC static
#endif #endif
#ifdef WOLFSSL_DEBUG_MEMORY
STATIC INLINE void* TrackMalloc(size_t sz, const char* func, unsigned int line)
#else
STATIC INLINE void* TrackMalloc(size_t sz) STATIC INLINE void* TrackMalloc(size_t sz)
#endif
{ {
memoryTrack* mt; memoryTrack* mt;
@ -78,6 +116,10 @@
mt->u.hint.thisSize = sz; mt->u.hint.thisSize = sz;
mt->u.hint.thisMemory = (byte*)mt + sizeof(memoryTrack); mt->u.hint.thisMemory = (byte*)mt + sizeof(memoryTrack);
#ifdef WOLFSSL_DEBUG_MEMORY
printf("Alloc: %p -> %u at %s:%d\n", mt->u.hint.thisMemory, (word32)sz, func, line);
#endif
#ifdef DO_MEM_STATS #ifdef DO_MEM_STATS
ourMemStats.totalAllocs++; ourMemStats.totalAllocs++;
ourMemStats.totalBytes += sz; ourMemStats.totalBytes += sz;
@ -90,7 +132,11 @@
} }
#ifdef WOLFSSL_DEBUG_MEMORY
STATIC INLINE void TrackFree(void* ptr, const char* func, unsigned int line)
#else
STATIC INLINE void TrackFree(void* ptr) STATIC INLINE void TrackFree(void* ptr)
#endif
{ {
memoryTrack* mt; memoryTrack* mt;
@ -105,13 +151,25 @@
ourMemStats.currentBytes -= mt->u.hint.thisSize; ourMemStats.currentBytes -= mt->u.hint.thisSize;
#endif #endif
#ifdef WOLFSSL_DEBUG_MEMORY
printf("Free: %p -> %u at %s:%d\n", ptr, (word32)mt->u.hint.thisSize, func, line);
#endif
free(mt); free(mt);
} }
#ifdef WOLFSSL_DEBUG_MEMORY
STATIC INLINE void* TrackRealloc(void* ptr, size_t sz, const char* func, unsigned int line)
#else
STATIC INLINE void* TrackRealloc(void* ptr, size_t sz) STATIC INLINE void* TrackRealloc(void* ptr, size_t sz)
#endif
{ {
#ifdef WOLFSSL_DEBUG_MEMORY
void* ret = TrackMalloc(sz, func, line);
#else
void* ret = TrackMalloc(sz); void* ret = TrackMalloc(sz);
#endif
if (ptr) { if (ptr) {
/* if realloc is bigger, don't overread old ptr */ /* if realloc is bigger, don't overread old ptr */
@ -125,8 +183,13 @@
if (ret && ptr) if (ret && ptr)
memcpy(ret, ptr, sz); memcpy(ret, ptr, sz);
if (ret) if (ret) {
#ifdef WOLFSSL_DEBUG_MEMORY
TrackFree(ptr, func, line);
#else
TrackFree(ptr); TrackFree(ptr);
#endif
}
return ret; return ret;
} }

View File

@ -33,21 +33,30 @@
extern "C" { extern "C" {
#endif #endif
#ifdef WOLFSSL_DEBUG_MEMORY
typedef void *(*wolfSSL_Malloc_cb)(size_t size, const char* func, unsigned int line);
typedef void (*wolfSSL_Free_cb)(void *ptr, const char* func, unsigned int line);
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size, const char* func, unsigned int line);
/* Public in case user app wants to use XMALLOC/XFREE */
WOLFSSL_API void* wolfSSL_Malloc(size_t size, const char* func, unsigned int line);
WOLFSSL_API void wolfSSL_Free(void *ptr, const char* func, unsigned int line);
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, const char* func, unsigned int line);
#else
typedef void *(*wolfSSL_Malloc_cb)(size_t size); typedef void *(*wolfSSL_Malloc_cb)(size_t size);
typedef void (*wolfSSL_Free_cb)(void *ptr); typedef void (*wolfSSL_Free_cb)(void *ptr);
typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size); typedef void *(*wolfSSL_Realloc_cb)(void *ptr, size_t size);
/* Public set function */
WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb malloc_function,
wolfSSL_Free_cb free_function,
wolfSSL_Realloc_cb realloc_function);
/* Public in case user app wants to use XMALLOC/XFREE */ /* Public in case user app wants to use XMALLOC/XFREE */
WOLFSSL_API void* wolfSSL_Malloc(size_t size); WOLFSSL_API void* wolfSSL_Malloc(size_t size);
WOLFSSL_API void wolfSSL_Free(void *ptr); WOLFSSL_API void wolfSSL_Free(void *ptr);
WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size); WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size);
#endif
/* Public set function */
WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb malloc_function,
wolfSSL_Free_cb free_function,
wolfSSL_Realloc_cb realloc_function);
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */

View File

@ -189,9 +189,15 @@
&& !defined(WOLFSSL_uITRON4) && !defined(WOLFSSL_uTKERNEL2) && !defined(WOLFSSL_uITRON4) && !defined(WOLFSSL_uTKERNEL2)
/* default C runtime, can install different routines at runtime via cbs */ /* default C runtime, can install different routines at runtime via cbs */
#include <wolfssl/wolfcrypt/memory.h> #include <wolfssl/wolfcrypt/memory.h>
#ifdef WOLFSSL_DEBUG_MEMORY
#define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s), __func__, __LINE__))
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), __func__, __LINE__);}
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), __func__, __LINE__)
#else
#define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s))) #define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s)))
#define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp));} #define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp));}
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n)) #define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n))
#endif
#endif #endif
#ifndef STRING_USER #ifndef STRING_USER