From 288fe430f55150539ea0d2aeb76ed1b3778c7001 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Wed, 29 May 2024 15:08:35 -0600 Subject: [PATCH] tying in lean staticmemory build with --enable-staticmemory=small --- configure.ac | 13 +++++++++++++ examples/client/client.c | 16 ++++++++++------ examples/server/server.c | 14 +++++++++----- src/internal.c | 22 +++++++++++++++++++--- src/ssl.c | 6 ++++++ tests/api.c | 4 ++-- wolfcrypt/src/memory.c | 18 +++++++++++++----- wolfcrypt/test/test.c | 3 +++ wolfssl/test.h | 2 +- 9 files changed, 76 insertions(+), 22 deletions(-) diff --git a/configure.ac b/configure.ac index 31dac5db60..053a763e94 100644 --- a/configure.ac +++ b/configure.ac @@ -8070,6 +8070,19 @@ AC_ARG_ENABLE([staticmemory], [ ENABLED_STATICMEMORY=no ] ) +for v in `echo $ENABLED_STATICMEMORY | tr "," " "` +do + case $v in + small) + ENABLED_STATICMEMORY=yes + AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_LEAN_STATIC_MEMORY" + ;; + *) + AC_MSG_ERROR([Invalid choice for staticmemory.]) + break;; + esac +done + if test "x$ENABLED_STATICMEMORY" = "xyes" then AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_STATIC_MEMORY" diff --git a/examples/client/client.c b/examples/client/client.c index f1ab9ea136..4a2f773d9e 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -2096,10 +2096,12 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) byte memory[80000]; #endif byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */ + #if !defined(WOLFSSL_LEAN_STATIC_MEMORY) WOLFSSL_MEM_CONN_STATS ssl_stats; - #ifdef DEBUG_WOLFSSL + #if defined(DEBUG_WOLFSSL) WOLFSSL_MEM_STATS mem_stats; #endif + #endif WOLFSSL_HEAP_HINT *heap = NULL; #endif @@ -3026,7 +3028,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) #ifdef WOLFSSL_STATIC_MEMORY - #ifdef DEBUG_WOLFSSL + #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_LEAN_STATIC_MEMORY) /* print off helper buffer sizes for use with static memory * printing to stderr in case of debug mode turned on */ LOG_ERROR("static memory management size = %d\n", @@ -3584,7 +3586,8 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) } #endif -#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) +#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \ + !defined(WOLFSSL_LEAN_STATIC_MEMORY) LOG_ERROR("Before creating SSL\n"); if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1) err_sys("ctx not using static memory"); @@ -3682,7 +3685,8 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) } #endif -#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) +#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \ + !defined(WOLFSSL_LEAN_STATIC_MEMORY) LOG_ERROR("After creating SSL\n"); if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1) err_sys("ctx not using static memory"); @@ -4390,7 +4394,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) #endif /* display collected statistics */ -#ifdef WOLFSSL_STATIC_MEMORY +#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_LEAN_STATIC_MEMORY) if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1) err_sys("static memory was not used with ssl"); @@ -4617,7 +4621,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) wolfSSL_shutdown(sslResume); /* bidirectional shutdown */ /* display collected statistics */ - #ifdef WOLFSSL_STATIC_MEMORY + #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_LEAN_STATIC_MEMORY) if (wolfSSL_is_static_memory(sslResume, &ssl_stats) != 1) err_sys("static memory was not used with ssl"); diff --git a/examples/server/server.c b/examples/server/server.c index 7d24b3f2c1..aef6e189e8 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -1601,10 +1601,12 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args) byte memory[80000]; #endif byte memoryIO[34500]; /* max for IO buffer (TLS packet can be 16k) */ + #if !defined(WOLFSSL_LEAN_STATIC_MEMORY) WOLFSSL_MEM_CONN_STATS ssl_stats; - #ifdef DEBUG_WOLFSSL + #if defined(DEBUG_WOLFSSL) WOLFSSL_MEM_STATS mem_stats; #endif + #endif #endif #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES) int onlyKeyShare = 0; @@ -2503,7 +2505,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args) err_sys_ex(runWithErrors, "unable to get method"); #ifdef WOLFSSL_STATIC_MEMORY - #ifdef DEBUG_WOLFSSL + #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_LEAN_STATIC_MEMORY) /* print off helper buffer sizes for use with static memory * printing to stderr in case of debug mode turned on */ LOG_ERROR("static memory management size = %d\n", @@ -2964,7 +2966,8 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args) err_sys_ex(runWithErrors, "tcp accept failed"); } } -#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) +#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \ + !defined(WOLFSSL_LEAN_STATIC_MEMORY) LOG_ERROR("Before creating SSL\n"); if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1) err_sys_ex(runWithErrors, "ctx not using static memory"); @@ -3053,7 +3056,8 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args) } #endif -#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) +#if defined(WOLFSSL_STATIC_MEMORY) && defined(DEBUG_WOLFSSL) && \ + !defined(WOLFSSL_LEAN_STATIC_MEMORY) LOG_ERROR("After creating SSL\n"); if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) != 1) err_sys_ex(runWithErrors, "ctx not using static memory"); @@ -3799,7 +3803,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args) } /* display collected statistics */ -#ifdef WOLFSSL_STATIC_MEMORY +#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_LEAN_STATIC_MEMORY) if (wolfSSL_is_static_memory(ssl, &ssl_stats) != 1) err_sys_ex(runWithErrors, "static memory was not used with ssl"); diff --git a/src/internal.c b/src/internal.c index b4f822119b..ce57c241ac 100644 --- a/src/internal.c +++ b/src/internal.c @@ -7241,6 +7241,8 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) ssl_hint = ((WOLFSSL_HEAP_HINT*)(ssl->heap)); ctx_hint = ((WOLFSSL_HEAP_HINT*)(ctx->heap)); + ssl_hint->memory = ctx_hint->memory; + #ifndef WOLFSSL_LEAN_STATIC_MEMORY /* lock and check IO count / handshake count */ if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) { WOLFSSL_MSG("Bad memory_mutex lock"); @@ -7268,7 +7270,6 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) } ctx_hint->memory->curIO++; ctx_hint->memory->curHa++; - ssl_hint->memory = ctx_hint->memory; ssl_hint->haFlag = 1; wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); @@ -7304,6 +7305,7 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) } wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); } + #endif /* !WOLFSSL_LEAN_STATIC_MEMORY */ #ifdef WOLFSSL_HEAP_TEST } #endif @@ -8382,14 +8384,17 @@ void SSL_ResourceFree(WOLFSSL* ssl) /* avoid dereferencing a test value */ if (ssl->heap != (void*)WOLFSSL_HEAP_TEST) { #endif + void* heap = ssl->ctx ? ssl->ctx->heap : ssl->heap; + #ifndef WOLFSSL_LEAN_STATIC_MEMORY WOLFSSL_HEAP_HINT* ssl_hint = (WOLFSSL_HEAP_HINT*)ssl->heap; WOLFSSL_HEAP* ctx_heap; - void* heap = ssl->ctx ? ssl->ctx->heap : ssl->heap; ctx_heap = ssl_hint->memory; + #ifndef SINGLE_THREADED if (wc_LockMutex(&(ctx_heap->memory_mutex)) != 0) { WOLFSSL_MSG("Bad memory_mutex lock"); } + #endif ctx_heap->curIO--; if (FreeFixedIO(ctx_heap, &(ssl_hint->outBuf)) != 1) { WOLFSSL_MSG("Error freeing fixed output buffer"); @@ -8397,15 +8402,20 @@ void SSL_ResourceFree(WOLFSSL* ssl) if (FreeFixedIO(ctx_heap, &(ssl_hint->inBuf)) != 1) { WOLFSSL_MSG("Error freeing fixed output buffer"); } - if (ssl_hint->haFlag && ctx_heap->curHa > 0) { /* check if handshake count has been decreased*/ + + /* check if handshake count has been decreased*/ + if (ssl_hint->haFlag && ctx_heap->curHa > 0) { ctx_heap->curHa--; } + #ifndef SINGLE_THREADED wc_UnLockMutex(&(ctx_heap->memory_mutex)); + #endif /* check if tracking stats */ if (ctx_heap->flag & WOLFMEM_TRACK_STATS) { XFREE(ssl_hint->stats, heap, DYNAMIC_TYPE_SSL); } + #endif /* !WOLFSSL_LEAN_STATIC_MEMORY */ XFREE(ssl->heap, heap, DYNAMIC_TYPE_SSL); #ifdef WOLFSSL_HEAP_TEST } @@ -8673,14 +8683,20 @@ void FreeHandshakeResources(WOLFSSL* ssl) WOLFSSL_HEAP* ctx_heap; ctx_heap = ssl_hint->memory; + #ifndef SINGLE_THREADED if (wc_LockMutex(&(ctx_heap->memory_mutex)) != 0) { WOLFSSL_MSG("Bad memory_mutex lock"); } + #endif + #ifndef WOLFSSL_LEAN_STATIC_MEMORY if (ctx_heap->curHa > 0) { ctx_heap->curHa--; } ssl_hint->haFlag = 0; /* set to zero since handshake has been dec */ + #endif + #ifndef SINGLE_THREADED wc_UnLockMutex(&(ctx_heap->memory_mutex)); + #endif #ifdef WOLFSSL_HEAP_TEST } #endif diff --git a/src/ssl.c b/src/ssl.c index d027ef01b0..090e3cb798 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -2573,6 +2573,7 @@ int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats) } WOLFSSL_ENTER("wolfSSL_is_static_memory"); +#ifndef WOLFSSL_LEAN_STATIC_MEMORY /* fill out statistics if wanted and WOLFMEM_TRACK_STATS flag */ if (mem_stats != NULL && ssl->heap != NULL) { 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) XMEMCPY(mem_stats, hint->stats, sizeof(WOLFSSL_MEM_CONN_STATS)); } } +#endif + (void)mem_stats; return (ssl->heap) ? 1 : 0; } @@ -2593,6 +2596,7 @@ int wolfSSL_CTX_is_static_memory(WOLFSSL_CTX* ctx, WOLFSSL_MEM_STATS* mem_stats) } WOLFSSL_ENTER("wolfSSL_CTX_is_static_memory"); +#ifndef WOLFSSL_LEAN_STATIC_MEMORY /* fill out statistics if wanted */ if (mem_stats != NULL && ctx->heap != NULL) { WOLFSSL_HEAP* heap = ((WOLFSSL_HEAP_HINT*)(ctx->heap))->memory; @@ -2600,7 +2604,9 @@ int wolfSSL_CTX_is_static_memory(WOLFSSL_CTX* ctx, WOLFSSL_MEM_STATS* mem_stats) return MEMORY_E; } } +#endif + (void)mem_stats; return (ctx->heap) ? 1 : 0; } diff --git a/tests/api.c b/tests/api.c index 785924a16a..06be9c1e18 100644 --- a/tests/api.c +++ b/tests/api.c @@ -66534,7 +66534,7 @@ static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx) ExpectNull((ssl3 = wolfSSL_new(ctx))); if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) { - #ifdef DEBUG_WOLFSSL + #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_LEAN_STATIC_MEMORY) wolfSSL_PrintStatsConn(&ssl_stats); #endif (void)ssl_stats; @@ -66542,7 +66542,7 @@ static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx) /* display collected statistics */ if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) { - #ifdef DEBUG_WOLFSSL + #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_LEAN_STATIC_MEMORY) wolfSSL_PrintStats(&mem_stats); #endif (void)mem_stats; diff --git a/wolfcrypt/src/memory.c b/wolfcrypt/src/memory.c index 6e834d9586..b478376327 100644 --- a/wolfcrypt/src/memory.c +++ b/wolfcrypt/src/memory.c @@ -610,7 +610,8 @@ static int wc_partition_static_memory(byte* buffer, word32 sz, int flag, #endif /* divide into chunks of memory and add them to available list */ - while (ava >= (heap->sizeList[0] + padSz + memSz)) { + while (ava >= (word32)(heap->sizeList[0] + padSz + memSz)) { + #ifndef WOLFSSL_LEAN_STATIC_MEMORY /* creating only IO buffers from memory passed in, max TLS is 16k */ if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) { if ((ret = wc_create_memory_buckets(pt, ava, @@ -629,11 +630,13 @@ static int wc_partition_static_memory(byte* buffer, word32 sz, int flag, pt += ret; ava -= ret; } - else { + else + #endif + { int i; /* start at largest and move to smaller buckets */ for (i = (WOLFMEM_MAX_BUCKETS - 1); i >= 0; i--) { - if ((heap->sizeList[i] + padSz + memSz) <= ava) { + if ((word32)(heap->sizeList[i] + padSz + memSz) <= ava) { if ((ret = wc_create_memory_buckets(pt, ava, heap->sizeList[i], heap->distList[i], &(heap->ava[i]))) < 0) { @@ -650,6 +653,7 @@ static int wc_partition_static_memory(byte* buffer, word32 sz, int flag, } } + (void)flag; return 1; } @@ -751,6 +755,7 @@ int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint, #endif *pHint = hint; + (void)maxSz; return 0; } @@ -1054,7 +1059,7 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type) else #endif { - #ifndef WOLFSSL_LEANPSK + #ifndef WOLFSSL_LEAN_STATIC_MEMORY /* check if using IO pool flag */ if (mem->flag & WOLFMEM_IO_POOL && (type == DYNAMIC_TYPE_OUT_BUFFER || @@ -1353,6 +1358,7 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type) } #endif + #ifndef WOLFSSL_LEAN_STATIC_MEMORY /* case of using fixed IO buffers or IO pool */ if (((mem->flag & WOLFMEM_IO_POOL)||(mem->flag & WOLFMEM_IO_POOL_FIXED)) && (type == DYNAMIC_TYPE_OUT_BUFFER || @@ -1365,7 +1371,9 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type) } res = pt->buffer; } - else { + else + #endif + { /* general memory */ for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) { if ((word32)size <= mem->sizeList[i]) { diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 604e2c627c..e6c276b8b1 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -17406,6 +17406,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void) } } +#ifndef WOLFSSL_LEAN_STATIC_MEMORY /* check that padding size returned is possible */ if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) { 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) return WC_TEST_RET_ENC_NC; /* should round to 0 since struct + bucket will not fit */ } +#endif + (void)pad; (void)dist; /* avoid static analysis warning of variable not used */ #endif diff --git a/wolfssl/test.h b/wolfssl/test.h index bad365fc97..4bf16a7106 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -3102,7 +3102,7 @@ static WC_INLINE void FreeAtomicUser(WOLFSSL* ssl) #endif /* ATOMIC_USER */ -#ifdef WOLFSSL_STATIC_MEMORY +#if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFSSL_LEAN_STATIC_MEMORY) static WC_INLINE int wolfSSL_PrintStats(WOLFSSL_MEM_STATS* stats) { word16 i;