Merge pull request #7737 from JacobBarthelmeh/staticmemory-singlethreaded

fix for staticmemory and singlethreaded build
pull/7740/head
Sean Parkinson 2024-07-11 09:57:08 +10:00 committed by GitHub
commit 3cc7bbea67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 0 deletions

View File

@ -2774,6 +2774,7 @@ void SSL_CtxResourceFree(WOLFSSL_CTX* ctx)
#ifdef WOLFSSL_STATIC_MEMORY #ifdef WOLFSSL_STATIC_MEMORY
static void SSL_CtxResourceFreeStaticMem(void* heap) static void SSL_CtxResourceFreeStaticMem(void* heap)
{ {
#ifndef SINGLE_THREADED
if (heap != NULL if (heap != NULL
#ifdef WOLFSSL_HEAP_TEST #ifdef WOLFSSL_HEAP_TEST
/* avoid dereferencing a test value */ /* avoid dereferencing a test value */
@ -2784,6 +2785,9 @@ static void SSL_CtxResourceFreeStaticMem(void* heap)
WOLFSSL_HEAP* mem = hint->memory; WOLFSSL_HEAP* mem = hint->memory;
wc_FreeMutex(&mem->memory_mutex); wc_FreeMutex(&mem->memory_mutex);
} }
#else
(void)heap;
#endif
} }
#endif /* WOLFSSL_STATIC_MEMORY */ #endif /* WOLFSSL_STATIC_MEMORY */
@ -7235,6 +7239,7 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
ssl_hint->memory = ctx_hint->memory; ssl_hint->memory = ctx_hint->memory;
#ifndef WOLFSSL_STATIC_MEMORY_LEAN #ifndef WOLFSSL_STATIC_MEMORY_LEAN
#ifndef SINGLE_THREADED
/* lock and check IO count / handshake count */ /* lock and check IO count / handshake count */
if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) { if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) {
WOLFSSL_MSG("Bad memory_mutex lock"); 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); WOLFSSL_ERROR_VERBOSE(BAD_MUTEX_E);
return BAD_MUTEX_E; return BAD_MUTEX_E;
} }
#endif
if (ctx_hint->memory->maxHa > 0 && if (ctx_hint->memory->maxHa > 0 &&
ctx_hint->memory->maxHa <= ctx_hint->memory->curHa) { ctx_hint->memory->maxHa <= ctx_hint->memory->curHa) {
WOLFSSL_MSG("At max number of handshakes for static memory"); WOLFSSL_MSG("At max number of handshakes for static memory");
#ifndef SINGLE_THREADED
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
#endif
XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL); XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
ssl->heap = NULL; /* free and set to NULL for IO counter */ ssl->heap = NULL; /* free and set to NULL for IO counter */
return MEMORY_E; return MEMORY_E;
@ -7255,7 +7263,9 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
if (ctx_hint->memory->maxIO > 0 && if (ctx_hint->memory->maxIO > 0 &&
ctx_hint->memory->maxIO <= ctx_hint->memory->curIO) { ctx_hint->memory->maxIO <= ctx_hint->memory->curIO) {
WOLFSSL_MSG("At max number of IO allowed for static memory"); WOLFSSL_MSG("At max number of IO allowed for static memory");
#ifndef SINGLE_THREADED
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
#endif
XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL); XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
ssl->heap = NULL; /* free and set to NULL for IO counter */ ssl->heap = NULL; /* free and set to NULL for IO counter */
return MEMORY_E; return MEMORY_E;
@ -7263,7 +7273,9 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
ctx_hint->memory->curIO++; ctx_hint->memory->curIO++;
ctx_hint->memory->curHa++; ctx_hint->memory->curHa++;
ssl_hint->haFlag = 1; ssl_hint->haFlag = 1;
#ifndef SINGLE_THREADED
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
#endif
/* check if tracking stats */ /* check if tracking stats */
if (ctx_hint->memory->flag & WOLFMEM_TRACK_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 */ /* check if using fixed IO buffers */
if (ctx_hint->memory->flag & WOLFMEM_IO_POOL_FIXED) { if (ctx_hint->memory->flag & WOLFMEM_IO_POOL_FIXED) {
#ifndef SINGLE_THREADED
if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) { if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) {
WOLFSSL_MSG("Bad memory_mutex lock"); WOLFSSL_MSG("Bad memory_mutex lock");
WOLFSSL_ERROR_VERBOSE(BAD_MUTEX_E); WOLFSSL_ERROR_VERBOSE(BAD_MUTEX_E);
return BAD_MUTEX_E; return BAD_MUTEX_E;
} }
#endif
if (SetFixedIO(ctx_hint->memory, &(ssl_hint->inBuf)) != 1) { if (SetFixedIO(ctx_hint->memory, &(ssl_hint->inBuf)) != 1) {
#ifndef SINGLE_THREADED
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
#endif
return MEMORY_E; return MEMORY_E;
} }
if (SetFixedIO(ctx_hint->memory, &(ssl_hint->outBuf)) != 1) { if (SetFixedIO(ctx_hint->memory, &(ssl_hint->outBuf)) != 1) {
#ifndef SINGLE_THREADED
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
#endif
return MEMORY_E; return MEMORY_E;
} }
if (ssl_hint->outBuf == NULL || ssl_hint->inBuf == NULL) { if (ssl_hint->outBuf == NULL || ssl_hint->inBuf == NULL) {
WOLFSSL_MSG("Not enough memory to create fixed IO buffers"); WOLFSSL_MSG("Not enough memory to create fixed IO buffers");
#ifndef SINGLE_THREADED
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
#endif
return MEMORY_E; return MEMORY_E;
} }
#ifndef SINGLE_THREADED
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex)); wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
#endif
} }
#endif /* !WOLFSSL_STATIC_MEMORY_LEAN */ #endif /* !WOLFSSL_STATIC_MEMORY_LEAN */
#ifdef WOLFSSL_HEAP_TEST #ifdef WOLFSSL_HEAP_TEST

View File

@ -793,9 +793,13 @@ int wc_LoadStaticMemory(WOLFSSL_HEAP_HINT** pHint,
void wc_UnloadStaticMemory(WOLFSSL_HEAP_HINT* heap) void wc_UnloadStaticMemory(WOLFSSL_HEAP_HINT* heap)
{ {
WOLFSSL_ENTER("wc_UnloadStaticMemory"); WOLFSSL_ENTER("wc_UnloadStaticMemory");
#ifndef SINGLE_THREADED
if (heap != NULL && heap->memory != NULL) { if (heap != NULL && heap->memory != NULL) {
wc_FreeMutex(&heap->memory->memory_mutex); wc_FreeMutex(&heap->memory->memory_mutex);
} }
#else
(void)heap;
#endif
} }
#ifndef WOLFSSL_STATIC_MEMORY_LEAN #ifndef WOLFSSL_STATIC_MEMORY_LEAN