Skip to content

Commit a02fbcd

Browse files
HEAP_HINT has refcount in free and alloc
Static blocks now keep a ref count and only free when all holders are done holding.
1 parent 09fb499 commit a02fbcd

6 files changed

Lines changed: 343 additions & 134 deletions

File tree

‎src/internal.c‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5930,13 +5930,6 @@ void InitX509(WOLFSSL_X509* x509, int dynamicFlag, void* heap)
59305930
(void)ret;
59315931
}
59325932
#endif
5933-
#ifdef WOLFSSL_STATIC_MEMORY
5934-
/* A dynamic X509 can outlive the SSL whose heap hint it uses; dropped in
5935-
* ExternalFreeX509() */
5936-
if (dynamicFlag) {
5937-
wc_HeapHint_Ref(heap);
5938-
}
5939-
#endif
59405933
}
59415934

59425935

@@ -8893,6 +8886,7 @@ static int InitSSL_StaticMemory(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
88938886
if (ctx->heap != NULL) {
88948887
WOLFSSL_HEAP_HINT* ssl_hint;
88958888
WOLFSSL_HEAP_HINT* ctx_hint;
8889+
int ret = 0;
88968890

88978891
/* avoid dereferencing a test value */
88988892
#ifdef WOLFSSL_HEAP_TEST
@@ -8911,13 +8905,24 @@ static int InitSSL_StaticMemory(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
89118905
ctx_hint = ((WOLFSSL_HEAP_HINT*)(ctx->heap));
89128906

89138907
ssl_hint->memory = ctx_hint->memory;
8914-
ssl_hint->refCount = 1; /* the SSL's own reference */
8908+
/* the SSL's ref; each allocated block holds another */
8909+
wolfSSL_RefInit(&ssl_hint->refCount, &ret);
8910+
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
8911+
if (ret != 0) {
8912+
WOLFSSL_MSG("Error setting up heap hint reference mutex");
8913+
XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
8914+
ssl->heap = NULL;
8915+
return ret;
8916+
}
8917+
#endif
8918+
(void)ret;
89158919
ssl_hint->sslOwned = 1;
89168920
#ifndef WOLFSSL_STATIC_MEMORY_LEAN
89178921
#ifndef SINGLE_THREADED
89188922
/* lock and check IO count / handshake count */
89198923
if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) {
89208924
WOLFSSL_MSG("Bad memory_mutex lock");
8925+
wolfSSL_RefFree(&ssl_hint->refCount);
89218926
XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
89228927
ssl->heap = NULL; /* free and set to NULL for IO counter */
89238928
WOLFSSL_ERROR_VERBOSE(BAD_MUTEX_E);
@@ -8930,6 +8935,7 @@ static int InitSSL_StaticMemory(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
89308935
#ifndef SINGLE_THREADED
89318936
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
89328937
#endif
8938+
wolfSSL_RefFree(&ssl_hint->refCount);
89338939
XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
89348940
ssl->heap = NULL; /* free and set to NULL for IO counter */
89358941
return MEMORY_E;
@@ -8941,6 +8947,7 @@ static int InitSSL_StaticMemory(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
89418947
#ifndef SINGLE_THREADED
89428948
wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
89438949
#endif
8950+
wolfSSL_RefFree(&ssl_hint->refCount);
89448951
XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
89458952
ssl->heap = NULL; /* free and set to NULL for IO counter */
89468953
return MEMORY_E;
@@ -10303,8 +10310,7 @@ static void FreeSSL_StaticMemory(WOLFSSL* ssl)
1030310310
WOLFSSL_MSG("Bad memory_mutex lock");
1030410311
}
1030510312
#endif
10306-
/* detach stats under the lock: sessions and X509s holding a reference
10307-
* may still allocate through this hint from other threads */
10313+
/* detach under the lock: live blocks may still use the hint */
1030810314
if (ctx_heap->flag & WOLFMEM_TRACK_STATS) {
1030910315
stats = ssl_hint->stats;
1031010316
ssl_hint->stats = NULL;
@@ -10328,8 +10334,8 @@ static void FreeSSL_StaticMemory(WOLFSSL* ssl)
1032810334
* ssl->heap, which the SSL's reference still keeps alive here */
1032910335
XFREE(stats, heap, DYNAMIC_TYPE_SSL);
1033010336
#endif
10331-
/* drop the SSL's reference; other holders may keep the hint alive */
10332-
wc_HeapHint_Unref(ssl->heap);
10337+
/* drop the SSL's ref; the last block's free may free the hint */
10338+
wc_HeapHint_Release(ssl->heap);
1033310339
ssl->heap = NULL;
1033410340
#ifdef WOLFSSL_HEAP_TEST
1033510341
}

‎src/ssl_sess.c‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4042,9 +4042,6 @@ WOLFSSL_SESSION* wolfSSL_NewSession(void* heap)
40424042
#endif
40434043
ret->type = WOLFSSL_SESSION_TYPE_HEAP;
40444044
ret->heap = heap;
4045-
#ifdef WOLFSSL_STATIC_MEMORY
4046-
wc_HeapHint_Ref(heap);
4047-
#endif
40484045
#ifdef WOLFSSL_CHECK_MEM_ZERO
40494046
wc_MemZero_Add("SESSION master secret", ret->masterSecret, SECRET_LEN);
40504047
wc_MemZero_Add("SESSION id", ret->sessionID, ID_LEN);
@@ -4494,15 +4491,8 @@ void wolfSSL_FreeSession(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* session)
44944491
ForceZero(session->sessionID, ID_LEN);
44954492

44964493
if (session->type == WOLFSSL_SESSION_TYPE_HEAP) {
4497-
#ifdef WOLFSSL_STATIC_MEMORY
4498-
void* heap = session->heap;
4499-
#endif
45004494
/* // NOLINTNEXTLINE(clang-analyzer-unix.Malloc) */
45014495
XFREE(session, session->heap, DYNAMIC_TYPE_SESSION);
4502-
#ifdef WOLFSSL_STATIC_MEMORY
4503-
/* after the XFREE: it may free the hint the session came from */
4504-
wc_HeapHint_Unref(heap);
4505-
#endif
45064496
}
45074497
}
45084498

‎src/x509.c‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,15 +4281,8 @@ static void ExternalFreeX509(WOLFSSL_X509* x509)
42814281
if (doFree)
42824282
#endif /* OPENSSL_EXTRA_X509_SMALL || OPENSSL_EXTRA */
42834283
{
4284-
#ifdef WOLFSSL_STATIC_MEMORY
4285-
void* heap = x509->heap;
4286-
#endif
42874284
FreeX509(x509);
42884285
XFREE(x509, x509->heap, DYNAMIC_TYPE_X509);
4289-
#ifdef WOLFSSL_STATIC_MEMORY
4290-
/* after the XFREE: it may free the hint x509 came from */
4291-
wc_HeapHint_Unref(heap);
4292-
#endif
42934286
}
42944287
}
42954288
else {

0 commit comments

Comments
 (0)