Skip to content

Commit 195bbcc

Browse files
committed
Generic Memory Pools Fix
1. Add some expository comments describing the purpose of: * WOLFMEM_MAX_BUCKETS * WOLFMEM_DEF_BUCKETS * WOLFMEM_BUCKETS * WOLFMEM_DIST 2. Switch the API test for LoadStaticMemory() to named constants. 3. Delete redundant test case. Add a new test case. 4. In the wolfCrypt test for the memory constants, check the sizes of the WOLFMEM_BUCKETS and WOLFMEM_DIST lists against WOLFMEM_DEF_BUCKETS which should be their length. Check that WOLFMEM_DEF_BUCKETS is not greater than WOLFMEM_MAX_BUCKETS. 5. Default for WOLFMEM_MAX_BUCKETS should be WOLFMEM_DEF_BUCKETS, set it to what is specified. Add a warning if MAX is less than DEF. 6. Separate the definition of the constant LARGEST_MEM_BUCKET so it is dependent on config and not if WOLFMEM_BUCKETS isn't set.
1 parent d22991b commit 195bbcc

4 files changed

Lines changed: 98 additions & 41 deletions

File tree

tests/api.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -672,15 +672,32 @@ static int test_wolfCrypt_Cleanup(void)
672672
return EXPECT_RESULT();
673673
}
674674

675+
676+
#ifdef WOLFSSL_STATIC_MEMORY
677+
#define TEST_LSM_STATIC_SIZE 440000
678+
/* Create new bucket list, using the default list, adding
679+
* one dang large buffer size. */
680+
#define TEST_LSM_DEF_BUCKETS (WOLFMEM_DEF_BUCKETS+1)
681+
#define TEST_LSM_BUCKETS WOLFMEM_BUCKETS,(LARGEST_MEM_BUCKET*2)
682+
#define TEST_LSM_DIST WOLFMEM_DIST,1
683+
#endif
684+
675685
static int test_wc_LoadStaticMemory_ex(void)
676686
{
677687
EXPECT_DECLS;
678688
#ifdef WOLFSSL_STATIC_MEMORY
679-
byte staticMemory[440000];
680-
word32 sizeList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_BUCKETS };
681-
word32 distList[WOLFMEM_DEF_BUCKETS] = { WOLFMEM_DIST };
689+
byte staticMemory[TEST_LSM_STATIC_SIZE];
690+
word32 sizeList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_BUCKETS };
691+
word32 distList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_DIST };
682692
WOLFSSL_HEAP_HINT* heap;
683693

694+
/* For this test, the size and dist lists will be the ones configured
695+
* for the build, or default. The value of WOLFMEM_DEF_BUCKETS is 9,
696+
* so these lists are 10 long. For most tests, the value of
697+
* WOLFMEM_DEF_BUCKETS is used. There's a test case where one is added
698+
* to that, to make sure the list size is larger than
699+
* WOLFMEM_MAX_BUCKETS. */
700+
684701
/* Pass in zero everything. */
685702
ExpectIntEQ(wc_LoadStaticMemory_ex(NULL, 0, NULL, NULL, NULL, 0, 0, 0),
686703
BAD_FUNC_ARG);
@@ -711,6 +728,7 @@ static int test_wc_LoadStaticMemory_ex(void)
711728
NULL, (word32)sizeof(staticMemory),
712729
0, 1),
713730
BAD_FUNC_ARG);
731+
714732
/* Set the size of the static buffer to 0. */
715733
heap = NULL;
716734
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
@@ -728,14 +746,6 @@ static int test_wc_LoadStaticMemory_ex(void)
728746
0, 1),
729747
BUFFER_E);
730748

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-
739749
/* Set the size of the static buffer to exactly the minimum size. */
740750
heap = NULL;
741751
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
@@ -746,6 +756,15 @@ static int test_wc_LoadStaticMemory_ex(void)
746756
0);
747757
wc_UnloadStaticMemory(heap);
748758

759+
/* Use more buckets than able. Success case. */
760+
heap = NULL;
761+
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
762+
WOLFMEM_DEF_BUCKETS*2, sizeList, distList,
763+
staticMemory, (word32)sizeof(staticMemory),
764+
0, 1),
765+
0);
766+
wc_UnloadStaticMemory(heap);
767+
749768
/* Success case. */
750769
heap = NULL;
751770
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,

wolfcrypt/src/memory.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,16 @@ int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
656656

657657
WOLFSSL_ENTER("wc_LoadStaticMemory_ex");
658658

659-
if (pHint == NULL || buf == NULL || listSz > WOLFMEM_MAX_BUCKETS
660-
|| sizeList == NULL || distList == NULL) {
659+
if (pHint == NULL || buf == NULL || sizeList == NULL || distList == NULL) {
661660
return BAD_FUNC_ARG;
662661
}
663662

663+
/* Cap the listSz to the actual number of items allocated in the list. */
664+
if (listSz > WOLFMEM_MAX_BUCKETS) {
665+
WOLFSSL_MSG("Truncating the list of memory buckets");
666+
listSz = WOLFMEM_MAX_BUCKETS;
667+
}
668+
664669
if ((sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) > sz - idx) {
665670
WOLFSSL_MSG("Not enough memory for partition tracking");
666671
return BUFFER_E; /* not enough memory for structures */
@@ -761,11 +766,16 @@ int wolfSSL_StaticBufferSz_ex(unsigned int listSz,
761766

762767
WOLFSSL_ENTER("wolfSSL_StaticBufferSz_ex");
763768

764-
if (buffer == NULL || listSz > WOLFMEM_MAX_BUCKETS
765-
|| sizeList == NULL || distList == NULL) {
769+
if (buffer == NULL || sizeList == NULL || distList == NULL) {
766770
return BAD_FUNC_ARG;
767771
}
768772

773+
/* Cap the listSz to the actual number of items allocated in the list. */
774+
if (listSz > WOLFMEM_MAX_BUCKETS) {
775+
WOLFSSL_MSG("Truncating the list of memory buckets");
776+
listSz = WOLFMEM_MAX_BUCKETS;
777+
}
778+
769779
/* align pt */
770780
while ((wc_ptr_t)pt % WOLFSSL_STATIC_ALIGN && pt < (buffer + sz)) {
771781
pt++;

wolfcrypt/test/test.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16089,22 +16089,26 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
1608916089

1609016090
#ifdef WOLFSSL_STATIC_MEMORY
1609116091
/* check macro settings */
16092-
if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
16092+
if (sizeof(size)/sizeof(word32) != WOLFMEM_DEF_BUCKETS) {
1609316093
return WC_TEST_RET_ENC_NC;
1609416094
}
1609516095

16096-
if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
16096+
if (sizeof(dist)/sizeof(word32) != WOLFMEM_DEF_BUCKETS) {
1609716097
return WC_TEST_RET_ENC_NC;
1609816098
}
1609916099

16100-
for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
16100+
if (WOLFMEM_DEF_BUCKETS > WOLFMEM_MAX_BUCKETS) {
16101+
return WC_TEST_RET_ENC_NC;
16102+
}
16103+
16104+
for (i = 0; i < WOLFMEM_DEF_BUCKETS; i++) {
1610116105
if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
1610216106
/* each element in array should be divisible by alignment size */
1610316107
return WC_TEST_RET_ENC_NC;
1610416108
}
1610516109
}
1610616110

16107-
for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
16111+
for (i = 1; i < WOLFMEM_DEF_BUCKETS; i++) {
1610816112
if (size[i - 1] >= size[i]) {
1610916113
return WC_TEST_RET_ENC_NC; /* sizes should be in increasing order */
1611016114
}

wolfssl/wolfcrypt/memory.h

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,48 +101,72 @@ WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf,
101101
#ifndef WOLFSSL_STATIC_ALIGN
102102
#define WOLFSSL_STATIC_ALIGN 16
103103
#endif
104+
/* WOLFMEM_BUCKETS - list of the sizes of buckets in the pool
105+
* WOLFMEM_DIST - list of quantities of buffers in the buckets
106+
* WOLFMEM_DEF_BUCKETS - number of values in WOLFMEM_BUCKETS and WOLFMEM_DIST
107+
* WOLFMEM_MAX_BUCKETS - size of the arrays used to store the buckets and
108+
* dists in the memory pool; defaults to WOLFMEM_DEF_BUCKETS
109+
*
110+
* The following defines provide a reasonable set of buckets in the memory
111+
* pool for running wolfSSL on a Linux box. The bucket and dist lists below
112+
* have nine items each, so WOLFMEM_DEF_BUCKETS is set to 9.
113+
*
114+
* If WOLFMEM_DEF_BUCKETS is less then WOLFMEM_MAX_BUCKETS, the unused values
115+
* are set to zero and ignored. If WOLFMEM_MAX_BUCKETS is less than
116+
* WOLFMEM_DEF_BUCKETS, not all the buckets will be created in the pool.
117+
*/
118+
#ifndef WOLFMEM_DEF_BUCKETS
119+
#define WOLFMEM_DEF_BUCKETS 9 /* number of default memory blocks */
120+
#endif
121+
104122
#ifndef WOLFMEM_MAX_BUCKETS
105-
#define WOLFMEM_MAX_BUCKETS 9
123+
#define WOLFMEM_MAX_BUCKETS WOLFMEM_DEF_BUCKETS
124+
#endif
125+
126+
#if WOLFMEM_MAX_BUCKETS < WOLFMEM_DEF_BUCKETS
127+
#warning "ignoring excess buckets, MAX_BUCKETS less than DEF_BUCKETS"
106128
#endif
107-
#define WOLFMEM_DEF_BUCKETS 9 /* number of default memory blocks */
129+
108130
#ifndef WOLFMEM_IO_SZ
109131
#define WOLFMEM_IO_SZ 16992 /* 16 byte aligned */
110132
#endif
133+
134+
#ifndef LARGEST_MEM_BUCKET
135+
#ifndef SESSION_CERTS
136+
#define LARGEST_MEM_BUCKET 16128
137+
#elif defined(OPENSSL_EXTRA)
138+
#ifdef WOLFSSL_TLS13
139+
#define LARGEST_MEM_BUCKET 30400
140+
#else
141+
#define LARGEST_MEM_BUCKET 25600
142+
#endif
143+
#elif defined(WOLFSSL_CERT_EXT)
144+
/* certificate extensions requires 24k for the SSL struct */
145+
#define LARGEST_MEM_BUCKET 24576
146+
#else
147+
/* increase 23k for object member of WOLFSSL_X509_NAME_ENTRY */
148+
#define LARGEST_MEM_BUCKET 23440
149+
#endif
150+
#endif
151+
111152
#ifndef WOLFMEM_BUCKETS
112153
#ifndef SESSION_CERTS
113154
/* default size of chunks of memory to separate into */
114-
#ifndef LARGEST_MEM_BUCKET
115-
#define LARGEST_MEM_BUCKET 16128
116-
#endif
117155
#define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,\
118156
LARGEST_MEM_BUCKET
119-
#elif defined (OPENSSL_EXTRA)
157+
#elif defined(OPENSSL_EXTRA)
120158
/* extra storage in structs for multiple attributes and order */
121-
#ifndef LARGEST_MEM_BUCKET
122-
#ifdef WOLFSSL_TLS13
123-
#define LARGEST_MEM_BUCKET 30400
124-
#else
125-
#define LARGEST_MEM_BUCKET 25600
126-
#endif
127-
#endif
128159
#define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3360,4480,\
129160
LARGEST_MEM_BUCKET
130-
#elif defined (WOLFSSL_CERT_EXT)
131-
/* certificate extensions requires 24k for the SSL struct */
132-
#ifndef LARGEST_MEM_BUCKET
133-
#define LARGEST_MEM_BUCKET 24576
134-
#endif
161+
#elif defined(WOLFSSL_CERT_EXT)
135162
#define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,\
136163
LARGEST_MEM_BUCKET
137164
#else
138-
/* increase 23k for object member of WOLFSSL_X509_NAME_ENTRY */
139-
#ifndef LARGEST_MEM_BUCKET
140-
#define LARGEST_MEM_BUCKET 23440
141-
#endif
142165
#define WOLFMEM_BUCKETS 64,128,256,512,1024,2432,3456,4544,\
143166
LARGEST_MEM_BUCKET
144167
#endif
145168
#endif
169+
146170
#ifndef WOLFMEM_DIST
147171
#ifndef WOLFSSL_STATIC_MEMORY_SMALL
148172
#define WOLFMEM_DIST 49,10,6,14,5,6,9,1,1

0 commit comments

Comments
 (0)