Skip to content

Commit 4594151

Browse files
authored
Merge pull request #7418 from ejohnstown/generic-pool
Generic Memory Pools
2 parents 72d4996 + 6be5526 commit 4594151

4 files changed

Lines changed: 236 additions & 144 deletions

File tree

src/ssl.c

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,9 +2572,7 @@ int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx,
25722572
wolfSSL_method_func method, unsigned char* buf, unsigned int sz, int flag,
25732573
int maxSz)
25742574
{
2575-
WOLFSSL_HEAP* heap;
2576-
WOLFSSL_HEAP_HINT* hint;
2577-
word32 idx = 0;
2575+
WOLFSSL_HEAP_HINT* hint = NULL;
25782576

25792577
if (ctx == NULL || buf == NULL) {
25802578
return BAD_FUNC_ARG;
@@ -2584,62 +2582,30 @@ int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx,
25842582
return BAD_FUNC_ARG;
25852583
}
25862584

2587-
if (*ctx == NULL || (*ctx)->heap == NULL) {
2588-
if (sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT) > sz - idx) {
2589-
return BUFFER_E; /* not enough memory for structures */
2590-
}
2591-
heap = (WOLFSSL_HEAP*)buf;
2592-
idx += sizeof(WOLFSSL_HEAP);
2593-
if (wolfSSL_init_memory_heap(heap) != 0) {
2594-
return WOLFSSL_FAILURE;
2595-
}
2596-
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
2597-
idx += sizeof(WOLFSSL_HEAP_HINT);
2598-
XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
2599-
hint->memory = heap;
2600-
2601-
if (*ctx && (*ctx)->heap == NULL) {
2602-
(*ctx)->heap = (void*)hint;
2603-
}
2604-
}
2605-
else {
2606-
#ifdef WOLFSSL_HEAP_TEST
2607-
/* do not load in memory if test has been set */
2608-
if ((*ctx)->heap == (void*)WOLFSSL_HEAP_TEST) {
2609-
return WOLFSSL_SUCCESS;
2610-
}
2611-
#endif
2612-
hint = (WOLFSSL_HEAP_HINT*)((*ctx)->heap);
2613-
heap = hint->memory;
2585+
/* If there is a heap already, capture it in hint. */
2586+
if (*ctx && (*ctx)->heap != NULL) {
2587+
hint = (*ctx)->heap;
26142588
}
26152589

2616-
if (wolfSSL_load_static_memory(buf + idx, sz - idx, flag, heap) != 1) {
2617-
WOLFSSL_MSG("Error partitioning memory");
2590+
if (wc_LoadStaticMemory(&hint, buf, sz, flag, maxSz)) {
2591+
WOLFSSL_MSG("Error loading static memory");
26182592
return WOLFSSL_FAILURE;
26192593
}
26202594

2621-
/* create ctx if needed */
2622-
if (*ctx == NULL) {
2595+
if (*ctx) {
2596+
if ((*ctx)->heap == NULL) {
2597+
(*ctx)->heap = (void*)hint;
2598+
}
2599+
}
2600+
else {
2601+
/* create ctx if needed */
26232602
*ctx = wolfSSL_CTX_new_ex(method(hint), hint);
26242603
if (*ctx == NULL) {
26252604
WOLFSSL_MSG("Error creating ctx");
26262605
return WOLFSSL_FAILURE;
26272606
}
26282607
}
26292608

2630-
/* determine what max applies too */
2631-
if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
2632-
heap->maxIO = maxSz;
2633-
}
2634-
else { /* general memory used in handshakes */
2635-
heap->maxHa = maxSz;
2636-
}
2637-
2638-
heap->flag |= flag;
2639-
2640-
(void)maxSz;
2641-
(void)method;
2642-
26432609
return WOLFSSL_SUCCESS;
26442610
}
26452611

tests/api.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,93 @@ static int test_wolfCrypt_Cleanup(void)
672672
return EXPECT_RESULT();
673673
}
674674

675+
static int test_wc_LoadStaticMemory_ex(void)
676+
{
677+
EXPECT_DECLS;
678+
#ifdef WOLFSSL_STATIC_MEMORY
679+
byte staticMemory[440000];
680+
word32 sizeList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
681+
word32 distList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
682+
WOLFSSL_HEAP_HINT* heap;
683+
684+
/* Pass in zero everything. */
685+
ExpectIntEQ(wc_LoadStaticMemory_ex(NULL, 0, NULL, NULL, NULL, 0, 0, 0),
686+
BAD_FUNC_ARG);
687+
688+
/* Set the heap pointer to NULL. */
689+
ExpectIntEQ(wc_LoadStaticMemory_ex(NULL,
690+
WOLFMEM_DEF_BUCKETS, sizeList, distList,
691+
staticMemory, (word32)sizeof(staticMemory),
692+
0, 1),
693+
BAD_FUNC_ARG);
694+
695+
/* Set other pointer values to NULL one at a time. */
696+
heap = NULL;
697+
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
698+
WOLFMEM_DEF_BUCKETS, NULL, distList,
699+
staticMemory, (word32)sizeof(staticMemory),
700+
0, 1),
701+
BAD_FUNC_ARG);
702+
heap = NULL;
703+
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
704+
WOLFMEM_DEF_BUCKETS, sizeList, NULL,
705+
staticMemory, (word32)sizeof(staticMemory),
706+
0, 1),
707+
BAD_FUNC_ARG);
708+
heap = NULL;
709+
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
710+
WOLFMEM_DEF_BUCKETS, sizeList, distList,
711+
NULL, (word32)sizeof(staticMemory),
712+
0, 1),
713+
BAD_FUNC_ARG);
714+
/* Set the size of the static buffer to 0. */
715+
heap = NULL;
716+
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
717+
WOLFMEM_DEF_BUCKETS, sizeList, distList,
718+
staticMemory, 0,
719+
0, 1),
720+
BUFFER_E);
721+
722+
/* Set the size of the static buffer to one less than minimum allowed. */
723+
heap = NULL;
724+
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
725+
WOLFMEM_DEF_BUCKETS, sizeList, distList,
726+
staticMemory,
727+
(word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) - 1,
728+
0, 1),
729+
BUFFER_E);
730+
731+
/* Set the number of buckets to 1 too many allowed. */
732+
heap = NULL;
733+
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
734+
WOLFMEM_MAX_BUCKETS+1, sizeList, distList,
735+
staticMemory, (word32)sizeof(staticMemory),
736+
0, 1),
737+
BAD_FUNC_ARG);
738+
739+
/* Set the size of the static buffer to exactly the minimum size. */
740+
heap = NULL;
741+
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
742+
WOLFMEM_DEF_BUCKETS, sizeList, distList,
743+
staticMemory,
744+
(word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)),
745+
0, 1),
746+
0);
747+
wc_UnloadStaticMemory(heap);
748+
749+
/* Success case. */
750+
heap = NULL;
751+
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
752+
WOLFMEM_DEF_BUCKETS, sizeList, distList,
753+
staticMemory, (word32)sizeof(staticMemory),
754+
0, 1),
755+
0);
756+
wc_UnloadStaticMemory(heap);
757+
#endif /* WOLFSSL_STATIC_MEMORY */
758+
return EXPECT_RESULT();
759+
}
760+
761+
675762
/*----------------------------------------------------------------------------*
676763
| Platform dependent function test
677764
*----------------------------------------------------------------------------*/
@@ -71571,6 +71658,8 @@ TEST_CASE testCases[] = {
7157171658

7157271659
TEST_DECL(test_wolfCrypt_Init),
7157371660

71661+
TEST_DECL(test_wc_LoadStaticMemory_ex),
71662+
7157471663
/* Locking with Compat Mutex */
7157571664
TEST_DECL(test_wc_SetMutexCb),
7157671665
TEST_DECL(test_wc_LockMutex_ex),

0 commit comments

Comments
 (0)