Skip to content

Commit

Permalink
add debug log and adjust set global heap hint function
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed May 10, 2024
1 parent de3d1a4 commit 98a19f9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
4 changes: 0 additions & 4 deletions examples/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions examples/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 24 additions & 1 deletion wolfcrypt/src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions wolfssl/wolfcrypt/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 98a19f9

Please sign in to comment.