Skip to content

Commit 288fe43

Browse files
tying in lean staticmemory build with --enable-staticmemory=small
1 parent 18d8086 commit 288fe43

9 files changed

Lines changed: 76 additions & 22 deletions

File tree

configure.ac

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8070,6 +8070,19 @@ AC_ARG_ENABLE([staticmemory],
80708070
[ ENABLED_STATICMEMORY=no ]
80718071
)
80728072
8073+
for v in `echo $ENABLED_STATICMEMORY | tr "," " "`
8074+
do
8075+
case $v in
8076+
small)
8077+
ENABLED_STATICMEMORY=yes
8078+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_LEAN_STATIC_MEMORY"
8079+
;;
8080+
*)
8081+
AC_MSG_ERROR([Invalid choice for staticmemory.])
8082+
break;;
8083+
esac
8084+
done
8085+
80738086
if test "x$ENABLED_STATICMEMORY" = "xyes"
80748087
then
80758088
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_STATIC_MEMORY"

examples/client/client.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,10 +2096,12 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
20962096
byte memory[80000];
20972097
#endif
20982098
byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
2099+
#if !defined(WOLFSSL_LEAN_STATIC_MEMORY)
20992100
WOLFSSL_MEM_CONN_STATS ssl_stats;
2100-
#ifdef DEBUG_WOLFSSL
2101+
#if defined(DEBUG_WOLFSSL)
21012102
WOLFSSL_MEM_STATS mem_stats;
21022103
#endif
2104+
#endif
21032105
WOLFSSL_HEAP_HINT *heap = NULL;
21042106
#endif
21052107

@@ -3026,7 +3028,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
30263028

30273029

30283030
#ifdef WOLFSSL_STATIC_MEMORY
3029-
#ifdef DEBUG_WOLFSSL
3031+
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_LEAN_STATIC_MEMORY)
30303032
/* print off helper buffer sizes for use with static memory
30313033
* printing to stderr in case of debug mode turned on */
30323034
LOG_ERROR("static memory management size = %d\n",
@@ -3584,7 +3586,8 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
35843586
}
35853587
#endif
35863588

3587-
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
3589+
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
3590+
!defined(WOLFSSL_LEAN_STATIC_MEMORY)
35883591
LOG_ERROR("Before creating SSL\n");
35893592
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
35903593
err_sys("ctx not using static memory");
@@ -3682,7 +3685,8 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
36823685
}
36833686
#endif
36843687

3685-
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
3688+
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
3689+
!defined(WOLFSSL_LEAN_STATIC_MEMORY)
36863690
LOG_ERROR("After creating SSL\n");
36873691
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
36883692
err_sys("ctx not using static memory");
@@ -4390,7 +4394,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
43904394
#endif
43914395

43924396
/* display collected statistics */
4393-
#ifdef WOLFSSL_STATIC_MEMORY
4397+
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_LEAN_STATIC_MEMORY)
43944398
if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
43954399
err_sys("static memory was not used with ssl");
43964400

@@ -4617,7 +4621,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
46174621
wolfSSL_shutdown(sslResume); /* bidirectional shutdown */
46184622

46194623
/* display collected statistics */
4620-
#ifdef WOLFSSL_STATIC_MEMORY
4624+
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_LEAN_STATIC_MEMORY)
46214625
if (wolfSSL_is_static_memory(sslResume, &ssl_stats) != 1)
46224626
err_sys("static memory was not used with ssl");
46234627

examples/server/server.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,10 +1601,12 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
16011601
byte memory[80000];
16021602
#endif
16031603
byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */
1604+
#if !defined(WOLFSSL_LEAN_STATIC_MEMORY)
16041605
WOLFSSL_MEM_CONN_STATS ssl_stats;
1605-
#ifdef DEBUG_WOLFSSL
1606+
#if defined(DEBUG_WOLFSSL)
16061607
WOLFSSL_MEM_STATS mem_stats;
16071608
#endif
1609+
#endif
16081610
#endif
16091611
#if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
16101612
int onlyKeyShare = 0;
@@ -2503,7 +2505,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
25032505
err_sys_ex(runWithErrors, "unable to get method");
25042506

25052507
#ifdef WOLFSSL_STATIC_MEMORY
2506-
#ifdef DEBUG_WOLFSSL
2508+
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_LEAN_STATIC_MEMORY)
25072509
/* print off helper buffer sizes for use with static memory
25082510
* printing to stderr in case of debug mode turned on */
25092511
LOG_ERROR("static memory management size = %d\n",
@@ -2964,7 +2966,8 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
29642966
err_sys_ex(runWithErrors, "tcp accept failed");
29652967
}
29662968
}
2967-
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
2969+
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
2970+
!defined(WOLFSSL_LEAN_STATIC_MEMORY)
29682971
LOG_ERROR("Before creating SSL\n");
29692972
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
29702973
err_sys_ex(runWithErrors, "ctx not using static memory");
@@ -3053,7 +3056,8 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
30533056
}
30543057
#endif
30553058

3056-
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL)
3059+
#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \
3060+
!defined(WOLFSSL_LEAN_STATIC_MEMORY)
30573061
LOG_ERROR("After creating SSL\n");
30583062
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1)
30593063
err_sys_ex(runWithErrors, "ctx not using static memory");
@@ -3799,7 +3803,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
37993803
}
38003804

38013805
/* display collected statistics */
3802-
#ifdef WOLFSSL_STATIC_MEMORY
3806+
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_LEAN_STATIC_MEMORY)
38033807
if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1)
38043808
err_sys_ex(runWithErrors, "static memory was not used with ssl");
38053809

src/internal.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7241,6 +7241,8 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
72417241
ssl_hint = ((WOLFSSL_HEAP_HINT*)(ssl->heap));
72427242
ctx_hint = ((WOLFSSL_HEAP_HINT*)(ctx->heap));
72437243

7244+
ssl_hint->memory = ctx_hint->memory;
7245+
#ifndef WOLFSSL_LEAN_STATIC_MEMORY
72447246
/* lock and check IO count / handshake count */
72457247
if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) {
72467248
WOLFSSL_MSG("Bad memory_mutex lock");
@@ -7268,7 +7270,6 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
72687270
}
72697271
ctx_hint->memory->curIO++;
72707272
ctx_hint->memory->curHa++;
7271-
ssl_hint->memory = ctx_hint->memory;
72727273
ssl_hint->haFlag = 1;
72737274
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
72747275

@@ -7304,6 +7305,7 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
73047305
}
73057306
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
73067307
}
7308+
#endif /* !WOLFSSL_LEAN_STATIC_MEMORY */
73077309
#ifdef WOLFSSL_HEAP_TEST
73087310
}
73097311
#endif
@@ -8382,30 +8384,38 @@ void SSL_ResourceFree(WOLFSSL* ssl)
83828384
/* avoid dereferencing a test value */
83838385
if (ssl->heap != (void*)WOLFSSL_HEAP_TEST) {
83848386
#endif
8387+
void* heap = ssl->ctx ? ssl->ctx->heap : ssl->heap;
8388+
#ifndef WOLFSSL_LEAN_STATIC_MEMORY
83858389
WOLFSSL_HEAP_HINT* ssl_hint = (WOLFSSL_HEAP_HINT*)ssl->heap;
83868390
WOLFSSL_HEAP* ctx_heap;
8387-
void* heap = ssl->ctx ? ssl->ctx->heap : ssl->heap;
83888391

83898392
ctx_heap = ssl_hint->memory;
8393+
#ifndef SINGLE_THREADED
83908394
if (wc_LockMutex(&(ctx_heap->memory_mutex)) != 0) {
83918395
WOLFSSL_MSG("Bad memory_mutex lock");
83928396
}
8397+
#endif
83938398
ctx_heap->curIO--;
83948399
if (FreeFixedIO(ctx_heap, &(ssl_hint->outBuf)) != 1) {
83958400
WOLFSSL_MSG("Error freeing fixed output buffer");
83968401
}
83978402
if (FreeFixedIO(ctx_heap, &(ssl_hint->inBuf)) != 1) {
83988403
WOLFSSL_MSG("Error freeing fixed output buffer");
83998404
}
8400-
if (ssl_hint->haFlag && ctx_heap->curHa > 0) { /* check if handshake count has been decreased*/
8405+
8406+
/* check if handshake count has been decreased*/
8407+
if (ssl_hint->haFlag && ctx_heap->curHa > 0) {
84018408
ctx_heap->curHa--;
84028409
}
8410+
#ifndef SINGLE_THREADED
84038411
wc_UnLockMutex(&(ctx_heap->memory_mutex));
8412+
#endif
84048413

84058414
/* check if tracking stats */
84068415
if (ctx_heap->flag & WOLFMEM_TRACK_STATS) {
84078416
XFREE(ssl_hint->stats, heap, DYNAMIC_TYPE_SSL);
84088417
}
8418+
#endif /* !WOLFSSL_LEAN_STATIC_MEMORY */
84098419
XFREE(ssl->heap, heap, DYNAMIC_TYPE_SSL);
84108420
#ifdef WOLFSSL_HEAP_TEST
84118421
}
@@ -8673,14 +8683,20 @@ void FreeHandshakeResources(WOLFSSL* ssl)
86738683
WOLFSSL_HEAP* ctx_heap;
86748684

86758685
ctx_heap = ssl_hint->memory;
8686+
#ifndef SINGLE_THREADED
86768687
if (wc_LockMutex(&(ctx_heap->memory_mutex)) != 0) {
86778688
WOLFSSL_MSG("Bad memory_mutex lock");
86788689
}
8690+
#endif
8691+
#ifndef WOLFSSL_LEAN_STATIC_MEMORY
86798692
if (ctx_heap->curHa > 0) {
86808693
ctx_heap->curHa--;
86818694
}
86828695
ssl_hint->haFlag = 0; /* set to zero since handshake has been dec */
8696+
#endif
8697+
#ifndef SINGLE_THREADED
86838698
wc_UnLockMutex(&(ctx_heap->memory_mutex));
8699+
#endif
86848700
#ifdef WOLFSSL_HEAP_TEST
86858701
}
86868702
#endif

src/ssl.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,6 +2573,7 @@ int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats)
25732573
}
25742574
WOLFSSL_ENTER("wolfSSL_is_static_memory");
25752575

2576+
#ifndef WOLFSSL_LEAN_STATIC_MEMORY
25762577
/* fill out statistics if wanted and WOLFMEM_TRACK_STATS flag */
25772578
if (mem_stats != NULL && ssl->heap != NULL) {
25782579
WOLFSSL_HEAP_HINT* hint = ((WOLFSSL_HEAP_HINT*)(ssl->heap));
@@ -2581,7 +2582,9 @@ int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats)
25812582
XMEMCPY(mem_stats, hint->stats, sizeof(WOLFSSL_MEM_CONN_STATS));
25822583
}
25832584
}
2585+
#endif
25842586

2587+
(void)mem_stats;
25852588
return (ssl->heap) ? 1 : 0;
25862589
}
25872590

@@ -2593,14 +2596,17 @@ int wolfSSL_CTX_is_static_memory(WOLFSSL_CTX* ctx, WOLFSSL_MEM_STATS* mem_stats)
25932596
}
25942597
WOLFSSL_ENTER("wolfSSL_CTX_is_static_memory");
25952598

2599+
#ifndef WOLFSSL_LEAN_STATIC_MEMORY
25962600
/* fill out statistics if wanted */
25972601
if (mem_stats != NULL && ctx->heap != NULL) {
25982602
WOLFSSL_HEAP* heap = ((WOLFSSL_HEAP_HINT*)(ctx->heap))->memory;
25992603
if (wolfSSL_GetMemStats(heap, mem_stats) != 1) {
26002604
return MEMORY_E;
26012605
}
26022606
}
2607+
#endif
26032608

2609+
(void)mem_stats;
26042610
return (ctx->heap) ? 1 : 0;
26052611
}
26062612

tests/api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66534,15 +66534,15 @@ static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
6653466534
ExpectNull((ssl3 = wolfSSL_new(ctx)));
6653566535

6653666536
if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
66537-
#ifdef DEBUG_WOLFSSL
66537+
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_LEAN_STATIC_MEMORY)
6653866538
wolfSSL_PrintStatsConn(&ssl_stats);
6653966539
#endif
6654066540
(void)ssl_stats;
6654166541
}
6654266542

6654366543
/* display collected statistics */
6654466544
if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
66545-
#ifdef DEBUG_WOLFSSL
66545+
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_LEAN_STATIC_MEMORY)
6654666546
wolfSSL_PrintStats(&mem_stats);
6654766547
#endif
6654866548
(void)mem_stats;

wolfcrypt/src/memory.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ static int wc_partition_static_memory(byte* buffer, word32 sz, int flag,
610610
#endif
611611

612612
/* divide into chunks of memory and add them to available list */
613-
while (ava >= (heap->sizeList[0] + padSz + memSz)) {
613+
while (ava >= (word32)(heap->sizeList[0] + padSz + memSz)) {
614+
#ifndef WOLFSSL_LEAN_STATIC_MEMORY
614615
/* creating only IO buffers from memory passed in, max TLS is 16k */
615616
if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
616617
if ((ret = wc_create_memory_buckets(pt, ava,
@@ -629,11 +630,13 @@ static int wc_partition_static_memory(byte* buffer, word32 sz, int flag,
629630
pt += ret;
630631
ava -= ret;
631632
}
632-
else {
633+
else
634+
#endif
635+
{
633636
int i;
634637
/* start at largest and move to smaller buckets */
635638
for (i = (WOLFMEM_MAX_BUCKETS - 1); i >= 0; i--) {
636-
if ((heap->sizeList[i] + padSz + memSz) <= ava) {
639+
if ((word32)(heap->sizeList[i] + padSz + memSz) <= ava) {
637640
if ((ret = wc_create_memory_buckets(pt, ava,
638641
heap->sizeList[i], heap->distList[i],
639642
&(heap->ava[i]))) < 0) {
@@ -650,6 +653,7 @@ static int wc_partition_static_memory(byte* buffer, word32 sz, int flag,
650653
}
651654
}
652655

656+
(void)flag;
653657
return 1;
654658
}
655659

@@ -751,6 +755,7 @@ int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
751755
#endif
752756
*pHint = hint;
753757

758+
(void)maxSz;
754759
return 0;
755760
}
756761

@@ -1054,7 +1059,7 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
10541059
else
10551060
#endif
10561061
{
1057-
#ifndef WOLFSSL_LEANPSK
1062+
#ifndef WOLFSSL_LEAN_STATIC_MEMORY
10581063
/* check if using IO pool flag */
10591064
if (mem->flag & WOLFMEM_IO_POOL &&
10601065
(type == DYNAMIC_TYPE_OUT_BUFFER ||
@@ -1353,6 +1358,7 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
13531358
}
13541359
#endif
13551360

1361+
#ifndef WOLFSSL_LEAN_STATIC_MEMORY
13561362
/* case of using fixed IO buffers or IO pool */
13571363
if (((mem->flag & WOLFMEM_IO_POOL)||(mem->flag & WOLFMEM_IO_POOL_FIXED))
13581364
&& (type == DYNAMIC_TYPE_OUT_BUFFER ||
@@ -1365,7 +1371,9 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type)
13651371
}
13661372
res = pt->buffer;
13671373
}
1368-
else {
1374+
else
1375+
#endif
1376+
{
13691377
/* general memory */
13701378
for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
13711379
if ((word32)size <= mem->sizeList[i]) {

wolfcrypt/test/test.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17406,6 +17406,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
1740617406
}
1740717407
}
1740817408

17409+
#ifndef WOLFSSL_LEAN_STATIC_MEMORY
1740917410
/* check that padding size returned is possible */
1741017411
if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
1741117412
return WC_TEST_RET_ENC_NC; /* no room for wc_Memory struct */
@@ -17470,7 +17471,9 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
1747017471
return WC_TEST_RET_ENC_NC; /* should round to 0
1747117472
since struct + bucket will not fit */
1747217473
}
17474+
#endif
1747317475

17476+
(void)pad;
1747417477
(void)dist; /* avoid static analysis warning of variable not used */
1747517478
#endif
1747617479

wolfssl/test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3102,7 +3102,7 @@ static WC_INLINE void FreeAtomicUser(WOLFSSL* ssl)
31023102

31033103
#endif /* ATOMIC_USER */
31043104

3105-
#ifdef WOLFSSL_STATIC_MEMORY
3105+
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_LEAN_STATIC_MEMORY)
31063106
static WC_INLINE int wolfSSL_PrintStats(WOLFSSL_MEM_STATS* stats)
31073107
{
31083108
word16 i;

0 commit comments

Comments
 (0)