Skip to content

Commit ac7aea9

Browse files
authored
Merge pull request #7478 from JacobBarthelmeh/staticmemory
add global heap hint setter function
2 parents c73e433 + 9aeef1d commit ac7aea9

3 files changed

Lines changed: 63 additions & 6 deletions

File tree

wolfcrypt/src/memory.c

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,31 @@ int wolfSSL_GetMemStats(WOLFSSL_HEAP* heap, WOLFSSL_MEM_STATS* stats)
899899
}
900900

901901

902+
/* global heap hint to fall back on when no heap hint is passed to
903+
* XMALLOC/XFREE
904+
* NOT thread safe, should be set once before any expected XMALLOC XFREE calls
905+
*/
906+
static void* globalHeapHint = NULL;
907+
908+
909+
/* Used to set a new global heap hint. Returns a pointer to the current global
910+
* heap hint before being set. */
911+
void* wolfSSL_SetGlobalHeapHint(void* heap)
912+
{
913+
void *oldHint = globalHeapHint;
914+
915+
globalHeapHint = heap;
916+
return oldHint;
917+
}
918+
919+
920+
/* returns a pointer to the current global heap hint */
921+
void* wolfSSL_GetGlobalHeapHint(void)
922+
{
923+
return globalHeapHint;
924+
}
925+
926+
902927
#ifdef WOLFSSL_DEBUG_MEMORY
903928
void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line)
904929
#else
@@ -917,7 +942,7 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
917942
#endif
918943

919944
/* if no heap hint then use dynamic memory*/
920-
if (heap == NULL) {
945+
if (heap == NULL && globalHeapHint == NULL) {
921946
#ifdef WOLFSSL_HEAP_TEST
922947
/* allow using malloc for creating ctx and method */
923948
if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
@@ -952,7 +977,15 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
952977
}
953978
else {
954979
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
955-
WOLFSSL_HEAP* mem = hint->memory;
980+
WOLFSSL_HEAP* mem;
981+
982+
if (hint == NULL) {
983+
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
984+
#ifdef WOLFSSL_DEBUG_MEMORY
985+
fprintf(stderr, "(Using global heap hint %p) ", hint);
986+
#endif
987+
}
988+
mem = hint->memory;
956989

957990
if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
958991
WOLFSSL_MSG("Bad memory_mutex lock");
@@ -1073,7 +1106,7 @@ void wolfSSL_Free(void *ptr, void* heap, int type)
10731106
}
10741107
#endif
10751108

1076-
if (heap == NULL) {
1109+
if (heap == NULL && globalHeapHint == NULL) {
10771110
#ifdef WOLFSSL_HEAP_TEST
10781111
/* allow using malloc for creating ctx and method */
10791112
if (type == DYNAMIC_TYPE_CTX || type == DYNAMIC_TYPE_METHOD ||
@@ -1098,9 +1131,17 @@ void wolfSSL_Free(void *ptr, void* heap, int type)
10981131
}
10991132
else {
11001133
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
1101-
WOLFSSL_HEAP* mem = hint->memory;
1134+
WOLFSSL_HEAP* mem;
11021135
word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
11031136

1137+
if (hint == NULL) {
1138+
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
1139+
#ifdef WOLFSSL_DEBUG_MEMORY
1140+
fprintf(stderr, "(Using global heap hint %p) ", hint);
1141+
#endif
1142+
}
1143+
mem = hint->memory;
1144+
11041145
/* get memory struct and add it to available list */
11051146
pt = (wc_Memory*)((byte*)ptr - sizeof(wc_Memory) - padSz);
11061147
if (wc_LockMutex(&(mem->memory_mutex)) != 0) {
@@ -1181,7 +1222,7 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
11811222
}
11821223
#endif
11831224

1184-
if (heap == NULL) {
1225+
if (heap == NULL && globalHeapHint == NULL) {
11851226
#ifdef WOLFSSL_HEAP_TEST
11861227
WOLFSSL_MSG("ERROR null heap hint passed in to XREALLOC");
11871228
#endif
@@ -1193,9 +1234,17 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
11931234
}
11941235
else {
11951236
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
1196-
WOLFSSL_HEAP* mem = hint->memory;
1237+
WOLFSSL_HEAP* mem;
11971238
word32 padSz = -(int)sizeof(wc_Memory) & (WOLFSSL_STATIC_ALIGN - 1);
11981239

1240+
if (hint == NULL) {
1241+
hint = (WOLFSSL_HEAP_HINT*)globalHeapHint;
1242+
#ifdef WOLFSSL_DEBUG_MEMORY
1243+
fprintf(stderr, "(Using global heap hint %p) ", hint);
1244+
#endif
1245+
}
1246+
mem = hint->memory;
1247+
11991248
if (ptr == NULL) {
12001249
#ifdef WOLFSSL_DEBUG_MEMORY
12011250
return wolfSSL_Malloc(size, heap, type, func, line);

wolfcrypt/test/test.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,9 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
10591059
printf("unable to load static memory.\n");
10601060
return(EXIT_FAILURE);
10611061
}
1062+
#ifndef OPENSSL_EXTRA
1063+
wolfSSL_SetGlobalHeapHint(HEAP_HINT);
1064+
#endif
10621065
#endif
10631066

10641067
#if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
@@ -2013,6 +2016,9 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
20132016
#endif
20142017
#endif
20152018

2019+
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(OPENSSL_EXTRA)
2020+
wolfSSL_SetGlobalHeapHint(NULL);
2021+
#endif
20162022
TEST_PASS("Test complete\n");
20172023

20182024
EXIT_TEST(ret);

wolfssl/wolfcrypt/memory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf,
240240
byte haFlag; /* flag used for checking handshake count */
241241
} WOLFSSL_HEAP_HINT;
242242

243+
WOLFSSL_API void* wolfSSL_SetGlobalHeapHint(void* heap);
244+
WOLFSSL_API void* wolfSSL_GetGlobalHeapHint(void);
243245
WOLFSSL_API int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
244246
unsigned int listSz, const unsigned int *sizeList,
245247
const unsigned int *distList, unsigned char* buf, unsigned int sz,

0 commit comments

Comments
 (0)