Skip to content

Commit

Permalink
Merge pull request #7737 from JacobBarthelmeh/staticmemory-singlethre…
Browse files Browse the repository at this point in the history
…aded

fix for staticmemory and singlethreaded build
  • Loading branch information
SparkiDev authored Jul 10, 2024
2 parents 0979fe8 + 6703a58 commit 3cc7bbe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */

Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand All @@ -7255,15 +7263,19 @@ 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;
}
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) {
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions wolfcrypt/src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3cc7bbe

Please sign in to comment.