From de3d1a488d13f4df1d844624653595b60529e60a Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Fri, 26 Apr 2024 08:25:16 -0600 Subject: [PATCH 1/4] add global heap hint setter function --- examples/client/client.c | 4 ++++ examples/server/server.c | 4 ++++ wolfcrypt/src/memory.c | 38 ++++++++++++++++++++++++++++++++------ wolfcrypt/test/test.c | 1 + wolfssl/wolfcrypt/memory.h | 1 + 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/examples/client/client.c b/examples/client/client.c index 7c2aa674d4..c577562235 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -3042,6 +3042,10 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) != 0) { err_sys("unable to load static memory"); } + /* for test case (does not handle all memory used on default build) + wolfSSL_SetGlobalHeapHint(heap); + */ + ctx = wolfSSL_CTX_new_ex(method(heap), heap); if (ctx == NULL) diff --git a/examples/server/server.c b/examples/server/server.c index 6f0faf3385..2572a185e4 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -2518,6 +2518,10 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args) != WOLFSSL_SUCCESS) err_sys_ex(catastrophic, "unable to load static memory and create ctx"); + /* for test case (does not handle all memory used on default build) + wolfSSL_SetGlobalHeapHint(wolfSSL_CTX_GetHeap(ctx, NULL)); + */ + /* load in a buffer for IO */ if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO), WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1) diff --git a/wolfcrypt/src/memory.c b/wolfcrypt/src/memory.c index e3a2bb5d07..884bfafc06 100644 --- a/wolfcrypt/src/memory.c +++ b/wolfcrypt/src/memory.c @@ -899,6 +899,17 @@ int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats) } +/* global heap hint to fall back on when no heap hint is passed to + * XMALLOC/XFREE + * NOT thread safe, should be set once before any expected XMALLOC XFREE calls + */ +static void* globalHeapHint = NULL; +void wolfSSL_SetGlobalHeapHint(void* heap) +{ + globalHeapHint = heap; +} + + #ifdef WOLFSSL_DEBUG_MEMORY void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line) #else @@ -917,7 +928,7 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type) #endif /* if no heap hint then use dynamic memory*/ - if (heap == NULL) { + if (heap == NULL && globalHeapHint == NULL) { #ifdef WOLFSSL_HEAP_TEST /* allow using malloc for creating ctx and method */ if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD || @@ -952,7 +963,12 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type) } else { WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap; - WOLFSSL_HEAP* mem = hint->memory; + WOLFSSL_HEAP* mem; + + if (hint == NULL) { + hint = (WOLFSSL_HEAP_HINT*)globalHeapHint; + } + mem = hint->memory; if (wc_LockMutex(&(mem->memory_mutex)) != 0) { WOLFSSL_MSG("Bad memory_mutex lock"); @@ -1073,7 +1089,7 @@ void wolfSSL_Free(void *ptr, void* heap, int type) } #endif - if (heap == NULL) { + if (heap == NULL && globalHeapHint == NULL) { #ifdef WOLFSSL_HEAP_TEST /* allow using malloc for creating ctx and method */ if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD || @@ -1098,9 +1114,14 @@ void wolfSSL_Free(void *ptr, void* heap, int type) } else { WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap; - WOLFSSL_HEAP* mem = hint->memory; + WOLFSSL_HEAP* mem; word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1); + if (hint == NULL) { + hint = (WOLFSSL_HEAP_HINT*)globalHeapHint; + } + mem = hint->memory; + /* get memory struct and add it to available list */ pt = (wc_Memory*)((byte*)ptr - sizeof(wc_Memory) - padSz); if (wc_LockMutex(&(mem->memory_mutex)) != 0) { @@ -1181,7 +1202,7 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type) } #endif - if (heap == NULL) { + if (heap == NULL && globalHeapHint == NULL) { #ifdef WOLFSSL_HEAP_TEST WOLFSSL_MSG("ERROR null heap hint passed in to XREALLOC"); #endif @@ -1193,9 +1214,14 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type) } else { WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap; - WOLFSSL_HEAP* mem = hint->memory; + WOLFSSL_HEAP* mem; word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1); + if (hint == NULL) { + hint = (WOLFSSL_HEAP_HINT*)globalHeapHint; + } + mem = hint->memory; + if (ptr == NULL) { #ifdef WOLFSSL_DEBUG_MEMORY return wolfSSL_Malloc(size, heap, type, func, line); diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 3eccd85a37..e1d6387f10 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -1059,6 +1059,7 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\ printf("unable to load static memory.\n"); return(EXIT_FAILURE); } + wolfSSL_SetGlobalHeapHint(HEAP_HINT); #endif #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) diff --git a/wolfssl/wolfcrypt/memory.h b/wolfssl/wolfcrypt/memory.h index e6b7dc5469..9af3c728f2 100644 --- a/wolfssl/wolfcrypt/memory.h +++ b/wolfssl/wolfcrypt/memory.h @@ -240,6 +240,7 @@ WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf, byte haFlag; /* flag used for checking handshake count */ } WOLFSSL_HEAP_HINT; + WOLFSSL_API void wolfSSL_SetGlobalHeapHint(void* heap); WOLFSSL_API int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint, unsigned int listSz, const unsigned int *sizeList, const unsigned int *distList, unsigned char* buf, unsigned int sz, From 98a19f9fdd379ad64fd1496b27775457834aeed8 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Tue, 30 Apr 2024 16:38:45 -0600 Subject: [PATCH 2/4] add debug log and adjust set global heap hint function --- examples/client/client.c | 4 ---- examples/server/server.c | 4 ---- wolfcrypt/src/memory.c | 25 ++++++++++++++++++++++++- wolfcrypt/test/test.c | 5 +++++ wolfssl/wolfcrypt/memory.h | 2 ++ 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/examples/client/client.c b/examples/client/client.c index c577562235..7c2aa674d4 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -3042,10 +3042,6 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) != 0) { err_sys("unable to load static memory"); } - /* for test case (does not handle all memory used on default build) - wolfSSL_SetGlobalHeapHint(heap); - */ - ctx = wolfSSL_CTX_new_ex(method(heap), heap); if (ctx == NULL) diff --git a/examples/server/server.c b/examples/server/server.c index 2572a185e4..6f0faf3385 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -2518,10 +2518,6 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args) != WOLFSSL_SUCCESS) err_sys_ex(catastrophic, "unable to load static memory and create ctx"); - /* for test case (does not handle all memory used on default build) - wolfSSL_SetGlobalHeapHint(wolfSSL_CTX_GetHeap(ctx, NULL)); - */ - /* load in a buffer for IO */ if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO), WOLFMEM_IO_POOL_FIXED | WOLFMEM_TRACK_STATS, 1) diff --git a/wolfcrypt/src/memory.c b/wolfcrypt/src/memory.c index 884bfafc06..2e30c4c818 100644 --- a/wolfcrypt/src/memory.c +++ b/wolfcrypt/src/memory.c @@ -904,9 +904,23 @@ int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats) * NOT thread safe, should be set once before any expected XMALLOC XFREE calls */ static void* globalHeapHint = NULL; -void wolfSSL_SetGlobalHeapHint(void* heap) + + +/* Used to set a new global heap hint. Returns a pointer to the current global + * heap hint before being set. */ +void* wolfSSL_SetGlobalHeapHint(void* heap) { + void *oldHint = globalHeapHint; + globalHeapHint = heap; + return oldHint; +} + + +/* returns a pointer to the current global heap hint */ +void* wolfSSL_GetGlobalHeapHint() +{ + return globalHeapHint; } @@ -967,6 +981,9 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type) if (hint == NULL) { hint = (WOLFSSL_HEAP_HINT*)globalHeapHint; + #ifdef WOLFSSL_DEBUG_MEMORY + fprintf(stderr, "(Using global heap hint %p) ", hint); + #endif } mem = hint->memory; @@ -1119,6 +1136,9 @@ void wolfSSL_Free(void *ptr, void* heap, int type) if (hint == NULL) { hint = (WOLFSSL_HEAP_HINT*)globalHeapHint; + #ifdef WOLFSSL_DEBUG_MEMORY + fprintf(stderr, "(Using global heap hint %p) ", hint); + #endif } mem = hint->memory; @@ -1219,6 +1239,9 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type) if (hint == NULL) { hint = (WOLFSSL_HEAP_HINT*)globalHeapHint; + #ifdef WOLFSSL_DEBUG_MEMORY + fprintf(stderr, "(Using global heap hint %p) ", hint); + #endif } mem = hint->memory; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index e1d6387f10..3a34454a77 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -1059,7 +1059,9 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\ printf("unable to load static memory.\n"); return(EXIT_FAILURE); } + #ifndef OPENSSL_EXTRA wolfSSL_SetGlobalHeapHint(HEAP_HINT); + #endif #endif #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) @@ -2014,6 +2016,9 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\ #endif #endif +#ifndef OPENSSL_EXTRA + wolfSSL_SetGlobalHeapHint(NULL); +#endif TEST_PASS("Test complete\n"); EXIT_TEST(ret); diff --git a/wolfssl/wolfcrypt/memory.h b/wolfssl/wolfcrypt/memory.h index 9af3c728f2..87f277c70c 100644 --- a/wolfssl/wolfcrypt/memory.h +++ b/wolfssl/wolfcrypt/memory.h @@ -241,6 +241,8 @@ WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf, } WOLFSSL_HEAP_HINT; WOLFSSL_API void wolfSSL_SetGlobalHeapHint(void* heap); + WOLFSSL_API void* wolfSSL_SetGlobalHeapHint(void* heap); + WOLFSSL_API void* wolfSSL_GetGlobalHeapHint(void); WOLFSSL_API int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint, unsigned int listSz, const unsigned int *sizeList, const unsigned int *distList, unsigned char* buf, unsigned int sz, From d68f3cf63ce402b05a318305915e7a67720be1c7 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Tue, 30 Apr 2024 16:43:01 -0600 Subject: [PATCH 3/4] add macro guard around test case --- wolfcrypt/test/test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 3a34454a77..d8f4e8c28e 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -2016,7 +2016,7 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\ #endif #endif -#ifndef OPENSSL_EXTRA +#if defined(WOLFSSL_STATIC_MEMORY) && !defined(OPENSSL_EXTRA) wolfSSL_SetGlobalHeapHint(NULL); #endif TEST_PASS("Test complete\n"); From 9aeef1d857f5cee29f60021ed10be921057ffc05 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Wed, 15 May 2024 10:28:39 -0600 Subject: [PATCH 4/4] add void and remove rebase issue --- wolfcrypt/src/memory.c | 2 +- wolfssl/wolfcrypt/memory.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/wolfcrypt/src/memory.c b/wolfcrypt/src/memory.c index 2e30c4c818..43f9861b9d 100644 --- a/wolfcrypt/src/memory.c +++ b/wolfcrypt/src/memory.c @@ -918,7 +918,7 @@ void* wolfSSL_SetGlobalHeapHint(void* heap) /* returns a pointer to the current global heap hint */ -void* wolfSSL_GetGlobalHeapHint() +void* wolfSSL_GetGlobalHeapHint(void) { return globalHeapHint; } diff --git a/wolfssl/wolfcrypt/memory.h b/wolfssl/wolfcrypt/memory.h index 87f277c70c..a901a64c3e 100644 --- a/wolfssl/wolfcrypt/memory.h +++ b/wolfssl/wolfcrypt/memory.h @@ -240,7 +240,6 @@ WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf, byte haFlag; /* flag used for checking handshake count */ } WOLFSSL_HEAP_HINT; - WOLFSSL_API void wolfSSL_SetGlobalHeapHint(void* heap); WOLFSSL_API void* wolfSSL_SetGlobalHeapHint(void* heap); WOLFSSL_API void* wolfSSL_GetGlobalHeapHint(void); WOLFSSL_API int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,