From 707714dd381d741f5291d929b69373a292a8a378 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Fri, 10 Jun 2016 15:35:02 -0600 Subject: [PATCH] threaded fixes with static memory --- src/internal.c | 16 ++++++++++++++-- wolfcrypt/src/memory.c | 6 ++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/internal.c b/src/internal.c index 9a6cf63ee..a55ecba9a 100755 --- a/src/internal.c +++ b/src/internal.c @@ -3135,16 +3135,16 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx) if (ctx_hint->memory->maxHa > 0 && ctx_hint->memory->maxHa <= ctx_hint->memory->curHa) { WOLFSSL_MSG("At max number of handshakes for static memory"); + UnLockMutex(&(ctx_hint->memory->memory_mutex)); return MEMORY_E; } - ctx_hint->memory->curHa++; 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"); + UnLockMutex(&(ctx_hint->memory->memory_mutex)); return MEMORY_E; } - ctx_hint->memory->curIO++; UnLockMutex(&(ctx_hint->memory->memory_mutex)); ssl->heap = (WOLFSSL_HEAP_HINT*)XMALLOC(sizeof(WOLFSSL_HEAP_HINT), @@ -3173,17 +3173,29 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx) return BAD_MUTEX_E; } if (SetFixedIO(ctx_hint->memory, &(ssl_hint->inBuf)) != 1) { + UnLockMutex(&(ctx_hint->memory->memory_mutex)); return MEMORY_E; } if (SetFixedIO(ctx_hint->memory, &(ssl_hint->outBuf)) != 1) { + UnLockMutex(&(ctx_hint->memory->memory_mutex)); return MEMORY_E; } if (ssl_hint->outBuf == NULL || ssl_hint->inBuf == NULL) { WOLFSSL_MSG("Not enough memory to create fixed IO buffers"); + UnLockMutex(&(ctx_hint->memory->memory_mutex)); return MEMORY_E; } UnLockMutex(&(ctx_hint->memory->memory_mutex)); } + + /* increment counters at end of setting up memory */ + if (LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) { + WOLFSSL_MSG("Bad memory_mutex lock"); + return BAD_MUTEX_E; + } + ctx_hint->memory->curHa++; + ctx_hint->memory->curIO++; + UnLockMutex(&(ctx_hint->memory->memory_mutex)); #ifdef WOLFSSL_HEAP_TEST } #endif diff --git a/wolfcrypt/src/memory.c b/wolfcrypt/src/memory.c index 54333534c..53b341fd1 100644 --- a/wolfcrypt/src/memory.c +++ b/wolfcrypt/src/memory.c @@ -493,8 +493,10 @@ void wolfSSL_Free(void *ptr, void* heap, int type) /* get memory struct and add it to available list */ pt = (wc_Memory*)((byte*)ptr - sizeof(wc_Memory) - padSz); - LockMutex(&(mem->memory_mutex)); - + if (LockMutex(&(mem->memory_mutex)) != 0) { + WOLFSSL_MSG("Bad memory_mutex lock"); + return; + } /* case of using fixed IO buffers */ if (mem->flag & WOLFMEM_IO_POOL_FIXED &&