fix for staticmemory and singlethreaded build

pull/7737/head
JacobBarthelmeh 2024-07-10 09:44:10 -06:00
parent 00e42151ca
commit 6703a58c51
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
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

View File

@ -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