From 6703a58c5133a98f9e7711a86d2a717a310fce37 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Wed, 10 Jul 2024 09:44:10 -0600 Subject: [PATCH] fix for staticmemory and singlethreaded build --- src/internal.c | 22 ++++++++++++++++++++++ wolfcrypt/src/memory.c | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/src/internal.c b/src/internal.c index 6bbd38fa8..eea01f1d4 100644 --- a/src/internal.c +++ b/src/internal.c @@ -2774,6 +2774,7 @@ void SSL_CtxResourceFree(WOLFSSL_CTX* ctx) #ifdef WOLFSSL_STATIC_MEMORY static void SSL_CtxResourceFreeStaticMem(void* heap) { +#ifndef SINGLE_THREADED if (heap != NULL #ifdef WOLFSSL_HEAP_TEST /* avoid dereferencing a test value */ @@ -2784,6 +2785,9 @@ static void SSL_CtxResourceFreeStaticMem(void* heap) WOLFSSL_HEAP* mem = hint->memory; wc_FreeMutex(&mem->memory_mutex); } +#else + (void)heap; +#endif } #endif /* WOLFSSL_STATIC_MEMORY */ @@ -7235,6 +7239,7 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) ssl_hint->memory = ctx_hint->memory; #ifndef WOLFSSL_STATIC_MEMORY_LEAN + #ifndef SINGLE_THREADED /* lock and check IO count / handshake count */ if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) { WOLFSSL_MSG("Bad memory_mutex lock"); @@ -7243,10 +7248,13 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) WOLFSSL_ERROR_VERBOSE(BAD_MUTEX_E); return BAD_MUTEX_E; } + #endif if (ctx_hint->memory->maxHa > 0 && ctx_hint->memory->maxHa <= ctx_hint->memory->curHa) { WOLFSSL_MSG("At max number of handshakes for static memory"); + #ifndef SINGLE_THREADED wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); + #endif XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL); ssl->heap = NULL; /* free and set to NULL for IO counter */ return MEMORY_E; @@ -7255,7 +7263,9 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) if (ctx_hint->memory->maxIO > 0 && ctx_hint->memory->maxIO <= ctx_hint->memory->curIO) { WOLFSSL_MSG("At max number of IO allowed for static memory"); + #ifndef SINGLE_THREADED wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); + #endif XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL); ssl->heap = NULL; /* free and set to NULL for IO counter */ return MEMORY_E; @@ -7263,7 +7273,9 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) ctx_hint->memory->curIO++; ctx_hint->memory->curHa++; ssl_hint->haFlag = 1; + #ifndef SINGLE_THREADED wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); + #endif /* check if tracking stats */ if (ctx_hint->memory->flag & WOLFMEM_TRACK_STATS) { @@ -7277,25 +7289,35 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup) /* check if using fixed IO buffers */ if (ctx_hint->memory->flag & WOLFMEM_IO_POOL_FIXED) { + #ifndef SINGLE_THREADED if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) { WOLFSSL_MSG("Bad memory_mutex lock"); WOLFSSL_ERROR_VERBOSE(BAD_MUTEX_E); return BAD_MUTEX_E; } + #endif if (SetFixedIO(ctx_hint->memory, &(ssl_hint->inBuf)) != 1) { + #ifndef SINGLE_THREADED wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); + #endif return MEMORY_E; } if (SetFixedIO(ctx_hint->memory, &(ssl_hint->outBuf)) != 1) { + #ifndef SINGLE_THREADED wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); + #endif return MEMORY_E; } if (ssl_hint->outBuf == NULL || ssl_hint->inBuf == NULL) { WOLFSSL_MSG("Not enough memory to create fixed IO buffers"); + #ifndef SINGLE_THREADED wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); + #endif return MEMORY_E; } + #ifndef SINGLE_THREADED wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); + #endif } #endif /* !WOLFSSL_STATIC_MEMORY_LEAN */ #ifdef WOLFSSL_HEAP_TEST diff --git a/wolfcrypt/src/memory.c b/wolfcrypt/src/memory.c index d9958a9e3..916e46234 100644 --- a/wolfcrypt/src/memory.c +++ b/wolfcrypt/src/memory.c @@ -793,9 +793,13 @@ int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint, void wc_UnloadStaticMemory(WOLFSSL_HEAP_HINT* heap) { WOLFSSL_ENTER("wc_UnloadStaticMemory"); +#ifndef SINGLE_THREADED if (heap != NULL && heap->memory != NULL) { wc_FreeMutex(&heap->memory->memory_mutex); } +#else + (void)heap; +#endif } #ifndef WOLFSSL_STATIC_MEMORY_LEAN