Skip to content

Commit

Permalink
add global heap hint setter function
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed May 10, 2024
1 parent cb68910 commit de3d1a4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
4 changes: 4 additions & 0 deletions examples/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions examples/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 32 additions & 6 deletions wolfcrypt/src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ||
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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 ||
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions wolfssl/wolfcrypt/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit de3d1a4

Please sign in to comment.