Skip to content

Commit

Permalink
tying in lean staticmemory build with --enable-staticmemory=small
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed May 29, 2024
1 parent 18d8086 commit 288fe43
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 22 deletions.
13 changes: 13 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 10 additions & 6 deletions examples/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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");

Expand Down
14 changes: 9 additions & 5 deletions examples/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");

Expand Down
22 changes: 19 additions & 3 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -8382,30 +8384,38 @@ 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");
}
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
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}

Expand All @@ -2593,14 +2596,17 @@ 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;
if (wolfSSL_GetMemStats(heap, mem_stats) != 1) {
return MEMORY_E;
}
}
#endif

(void)mem_stats;
return (ctx->heap) ? 1 : 0;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -66534,15 +66534,15 @@ 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;
}

/* 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;
Expand Down
18 changes: 13 additions & 5 deletions wolfcrypt/src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -650,6 +653,7 @@ static int wc_partition_static_memory(byte* buffer, word32 sz, int flag,
}
}

(void)flag;
return 1;
}

Expand Down Expand Up @@ -751,6 +755,7 @@ int wc_LoadStaticMemory_ex(WOLFSSL_HEAP_HINT** pHint,
#endif
*pHint = hint;

(void)maxSz;
return 0;
}

Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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 ||
Expand All @@ -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]) {
Expand Down
3 changes: 3 additions & 0 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion wolfssl/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 288fe43

Please sign in to comment.