Skip to content

Commit 525266c

Browse files
committed
wolfssl/wolfcrypt/mem_track.h and wolfcrypt/src/memory.c: add WOLFSSL_API extern memoryStats *wc_MemStats_Ptr, set by InitMemoryTracker() and cleared by CleanupMemoryTracker(), allowing public access to the memory statistics.
tests/unit.c: at end of unit_test(), when WOLFSSL_TRACK_MEMORY, explicitly wolfSSL_Cleanup() then check and error if wc_MemStats_Ptr->currentBytes > 0.
1 parent 1e38a10 commit 525266c

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

tests/unit.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,17 @@ int unit_test(int argc, char** argv)
324324
err_sys("Failed to free netRandom context");
325325
#endif /* HAVE_WNR */
326326

327+
#ifdef WOLFSSL_TRACK_MEMORY
328+
if (ret == 0) {
329+
(void)wolfSSL_Cleanup();
330+
if (wc_MemStats_Ptr->currentBytes > 0)
331+
{
332+
fprintf(stderr, "WOLFSSL_TRACK_MEMORY: currentBytes after cleanup is %ld\n", wc_MemStats_Ptr->currentBytes);
333+
ret = MEMORY_E;
334+
}
335+
}
336+
#endif
337+
327338
if (ret == 0) {
328339
puts("\nunit_test: Success for all configured tests.");
329340
fflush(stdout);

wolfcrypt/src/memory.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,11 @@ void* wolfSSL_Realloc(void *ptr, size_t size)
521521
}
522522
#endif /* WOLFSSL_STATIC_MEMORY */
523523

524+
#ifdef WOLFSSL_TRACK_MEMORY
525+
#include <wolfssl/wolfcrypt/mem_track.h>
526+
WOLFSSL_API memoryStats *wc_MemStats_Ptr;
527+
#endif
528+
524529
#ifdef WOLFSSL_STATIC_MEMORY
525530

526531
struct wc_Memory {

wolfssl/wolfcrypt/mem_track.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ typedef struct memoryList {
133133

134134

135135
static memoryStats ourMemStats;
136+
WOLFSSL_API extern memoryStats *wc_MemStats_Ptr;
136137

137138
#ifdef DO_MEM_LIST
138139
#include <pthread.h>
@@ -384,6 +385,8 @@ static WC_INLINE int InitMemoryTracker(void)
384385
#endif
385386
}
386387

388+
wc_MemStats_Ptr = &ourMemStats;
389+
387390
return ret;
388391
}
389392

@@ -427,6 +430,7 @@ static WC_INLINE void ShowMemoryTracker(void)
427430

428431
static WC_INLINE int CleanupMemoryTracker(void)
429432
{
433+
wc_MemStats_Ptr = NULL;
430434
/* restore default allocators */
431435
return wolfSSL_SetAllocators(mfDefault, ffDefault, rfDefault);
432436
}

0 commit comments

Comments
 (0)