Skip to content

Commit

Permalink
Merge pull request #8180 from JacobBarthelmeh/staticmemory
Browse files Browse the repository at this point in the history
wc_UnloadStaticMemory should be used to free mutex
  • Loading branch information
dgarske authored Nov 15, 2024
2 parents 21bfcaf + f74e73e commit c06b5fa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
25 changes: 0 additions & 25 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2769,25 +2769,6 @@ void SSL_CtxResourceFree(WOLFSSL_CTX* ctx)
(void)heapAtCTXInit;
}

#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 */
&& heap != (void*)WOLFSSL_HEAP_TEST
#endif
) {
WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)heap;
WOLFSSL_HEAP* mem = hint->memory;
wc_FreeMutex(&mem->memory_mutex);
}
#else
(void)heap;
#endif
}
#endif /* WOLFSSL_STATIC_MEMORY */

void FreeSSL_Ctx(WOLFSSL_CTX* ctx)
{
Expand All @@ -2809,9 +2790,6 @@ void FreeSSL_Ctx(WOLFSSL_CTX* ctx)
if (ctx->err == WC_NO_ERR_TRACE(CTX_INIT_MUTEX_E)) {
SSL_CtxResourceFree(ctx);
XFREE(ctx, heap, DYNAMIC_TYPE_CTX);
#ifdef WOLFSSL_STATIC_MEMORY
SSL_CtxResourceFreeStaticMem(heap);
#endif
}
return;
}
Expand All @@ -2829,9 +2807,6 @@ void FreeSSL_Ctx(WOLFSSL_CTX* ctx)
#endif
wolfSSL_RefFree(&ctx->ref);
XFREE(ctx, heap, DYNAMIC_TYPE_CTX);
#ifdef WOLFSSL_STATIC_MEMORY
SSL_CtxResourceFreeStaticMem(heap);
#endif
}
else {
WOLFSSL_MSG("CTX ref count not 0 yet, no free");
Expand Down
42 changes: 42 additions & 0 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,47 @@ static int test_wc_LoadStaticMemory_ex(void)
}


static int test_wc_LoadStaticMemory_CTX(void)
{
EXPECT_DECLS;
#if defined(WOLFSSL_STATIC_MEMORY) && !defined(NO_WOLFSSL_CLIENT)
byte staticMemory[TEST_LSM_STATIC_SIZE];
word32 sizeList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_BUCKETS };
word32 distList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_DIST };
WOLFSSL_HEAP_HINT* heap;
WOLFSSL_CTX *ctx1 = NULL, *ctx2 = NULL;


/* Set the size of the static buffer to exactly the minimum size. */
heap = NULL;
ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
WOLFMEM_DEF_BUCKETS, sizeList, distList,
staticMemory, sizeof(staticMemory), 0, 1),
0);

/* Creating two WOLFSSL_CTX objects from the same heap hint and free'ing
* them should not cause issues. */
ExpectNotNull((ctx1 = wolfSSL_CTX_new_ex(wolfSSLv23_client_method_ex(heap),
heap)));
wolfSSL_CTX_free(ctx1);
ExpectNotNull((ctx2 = wolfSSL_CTX_new_ex(wolfSSLv23_client_method_ex(heap),
heap)));
wolfSSL_CTX_free(ctx2);

/* two CTX's at once */
ExpectNotNull((ctx1 = wolfSSL_CTX_new_ex(wolfSSLv23_client_method_ex(heap),
heap)));
ExpectNotNull((ctx2 = wolfSSL_CTX_new_ex(wolfSSLv23_client_method_ex(heap),
heap)));
wolfSSL_CTX_free(ctx1);
wolfSSL_CTX_free(ctx2);

wc_UnloadStaticMemory(heap);
#endif /* WOLFSSL_STATIC_MEMORY */
return EXPECT_RESULT();
}


/*----------------------------------------------------------------------------*
| Platform dependent function test
*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -99661,6 +99702,7 @@ TEST_CASE testCases[] = {
TEST_DECL(test_wolfCrypt_Init),

TEST_DECL(test_wc_LoadStaticMemory_ex),
TEST_DECL(test_wc_LoadStaticMemory_CTX),

/* Locking with Compat Mutex */
TEST_DECL(test_wc_SetMutexCb),
Expand Down

0 comments on commit c06b5fa

Please sign in to comment.