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.
This commit is contained in:
Daniel Pouzzner
2025-12-16 17:00:24 -06:00
parent 1e38a1011e
commit 525266c467
3 changed files with 20 additions and 0 deletions

View File

@@ -324,6 +324,17 @@ exit:
err_sys("Failed to free netRandom context");
#endif /* HAVE_WNR */
#ifdef WOLFSSL_TRACK_MEMORY
if (ret == 0) {
(void)wolfSSL_Cleanup();
if (wc_MemStats_Ptr->currentBytes > 0)
{
fprintf(stderr, "WOLFSSL_TRACK_MEMORY: currentBytes after cleanup is %ld\n", wc_MemStats_Ptr->currentBytes);
ret = MEMORY_E;
}
}
#endif
if (ret == 0) {
puts("\nunit_test: Success for all configured tests.");
fflush(stdout);

View File

@@ -521,6 +521,11 @@ void* wolfSSL_Realloc(void *ptr, size_t size)
}
#endif /* WOLFSSL_STATIC_MEMORY */
#ifdef WOLFSSL_TRACK_MEMORY
#include <wolfssl/wolfcrypt/mem_track.h>
WOLFSSL_API memoryStats *wc_MemStats_Ptr;
#endif
#ifdef WOLFSSL_STATIC_MEMORY
struct wc_Memory {

View File

@@ -133,6 +133,7 @@ typedef struct memoryList {
static memoryStats ourMemStats;
WOLFSSL_API extern memoryStats *wc_MemStats_Ptr;
#ifdef DO_MEM_LIST
#include <pthread.h>
@@ -384,6 +385,8 @@ static WC_INLINE int InitMemoryTracker(void)
#endif
}
wc_MemStats_Ptr = &ourMemStats;
return ret;
}
@@ -427,6 +430,7 @@ static WC_INLINE void ShowMemoryTracker(void)
static WC_INLINE int CleanupMemoryTracker(void)
{
wc_MemStats_Ptr = NULL;
/* restore default allocators */
return wolfSSL_SetAllocators(mfDefault, ffDefault, rfDefault);
}