@@ -142,9 +142,12 @@ static memoryStats ourMemStats;
142142WOLFSSL_API extern memoryStats * wc_MemStats_Ptr ;
143143
144144#ifdef DO_MEM_LIST
145- #include <pthread.h>
146145 static memoryList ourMemList ;
147- static pthread_mutex_t memLock = PTHREAD_MUTEX_INITIALIZER ;
146+ #endif
147+
148+ #if !defined(SINGLE_THREADED ) && (defined(DO_MEM_LIST ) || defined(DO_MEM_STATS ))
149+ static wolfSSL_Mutex memLock ;
150+ static int memLockInit = 0 ;
148151#endif
149152
150153#ifdef WOLFSSL_DEBUG_MEMORY
@@ -182,7 +185,7 @@ static WC_INLINE void* TrackMalloc(size_t sz)
182185#endif
183186#endif
184187#if !defined(SINGLE_THREADED ) && (defined(DO_MEM_LIST ) || defined(DO_MEM_STATS ))
185- if (pthread_mutex_lock (& memLock ) == 0 )
188+ if (wc_LockMutex (& memLock ) == 0 )
186189 {
187190#endif
188191
@@ -228,9 +231,9 @@ static WC_INLINE void* TrackMalloc(size_t sz)
228231 ourMemList .count ++ ;
229232#endif
230233#if !defined(SINGLE_THREADED ) && (defined(DO_MEM_LIST ) || defined(DO_MEM_STATS ))
231- pthread_mutex_unlock (& memLock );
234+ wc_UnLockMutex (& memLock );
232235 }
233- #endif /* DO_MEM_LIST */
236+ #endif /* !SINGLE_THREADED && ( DO_MEM_LIST || DO_MEM_STATS) */
234237
235238 return header -> thisMemory ;
236239}
@@ -255,7 +258,7 @@ static WC_INLINE void TrackFree(void* ptr)
255258 sz = header -> thisSize ;
256259
257260#if !defined(SINGLE_THREADED ) && (defined(DO_MEM_LIST ) || defined(DO_MEM_STATS ))
258- if (pthread_mutex_lock (& memLock ) == 0 )
261+ if (wc_LockMutex (& memLock ) == 0 )
259262 {
260263#endif
261264
@@ -289,7 +292,7 @@ static WC_INLINE void TrackFree(void* ptr)
289292#endif
290293
291294#if !defined(SINGLE_THREADED ) && (defined(DO_MEM_LIST ) || defined(DO_MEM_STATS ))
292- pthread_mutex_unlock (& memLock );
295+ wc_UnLockMutex (& memLock );
293296 }
294297#endif
295298
@@ -362,14 +365,27 @@ static WC_INLINE int InitMemoryTracker(void)
362365 if (ret < 0 ) {
363366 wc_mem_printf ("wolfSSL GetAllocators failed to get the defaults\n" );
364367 }
368+
369+ #if !defined(SINGLE_THREADED ) && (defined(DO_MEM_LIST ) || defined(DO_MEM_STATS ))
370+ /* Init the mutex before installing the tracking allocators, so the
371+ * mutex is ready as soon as another thread can enter TrackMalloc. */
372+ if (!memLockInit ) {
373+ if (wc_InitMutex (& memLock ) != 0 ) {
374+ wc_mem_printf ("wc_InitMutex failed for track memory\n" );
375+ return -1 ;
376+ }
377+ memLockInit = 1 ;
378+ }
379+ #endif
380+
365381 ret = wolfSSL_SetAllocators (TrackMalloc , TrackFree , TrackRealloc );
366382 if (ret < 0 ) {
367383 wc_mem_printf ("wolfSSL SetAllocators failed for track memory\n" );
368384 return ret ;
369385 }
370386
371- #ifdef DO_MEM_LIST
372- if (pthread_mutex_lock (& memLock ) == 0 )
387+ #if !defined( SINGLE_THREADED ) && (defined( DO_MEM_LIST ) || defined( DO_MEM_STATS ))
388+ if (wc_LockMutex (& memLock ) == 0 )
373389#endif
374390 {
375391 #ifdef DO_MEM_STATS
@@ -387,8 +403,11 @@ static WC_INLINE int InitMemoryTracker(void)
387403 #ifdef DO_MEM_LIST
388404 XMEMSET (& ourMemList , 0 , sizeof (ourMemList ));
389405 ourMemStats .memList = & ourMemList ;
406+ #endif
390407
391- pthread_mutex_unlock (& memLock );
408+ #if !defined(SINGLE_THREADED ) && \
409+ (defined(DO_MEM_LIST ) || defined(DO_MEM_STATS ))
410+ wc_UnLockMutex (& memLock );
392411 #endif
393412 }
394413
@@ -399,8 +418,8 @@ static WC_INLINE int InitMemoryTracker(void)
399418
400419static WC_INLINE void ShowMemoryTracker (void )
401420{
402- #ifdef DO_MEM_LIST
403- if (pthread_mutex_lock (& memLock ) == 0 )
421+ #if !defined( SINGLE_THREADED ) && (defined( DO_MEM_LIST ) || defined( DO_MEM_STATS ))
422+ if (wc_LockMutex (& memLock ) == 0 )
404423#endif
405424 {
406425 #ifdef DO_MEM_STATS
@@ -429,16 +448,25 @@ static WC_INLINE void ShowMemoryTracker(void)
429448 #endif
430449 }
431450 }
451+ #endif
432452
433- pthread_mutex_unlock (& memLock );
453+ #if !defined(SINGLE_THREADED ) && \
454+ (defined(DO_MEM_LIST ) || defined(DO_MEM_STATS ))
455+ wc_UnLockMutex (& memLock );
434456 #endif
435457 }
436458}
437459
438460static WC_INLINE int CleanupMemoryTracker (void )
439461{
440462 wc_MemStats_Ptr = NULL ;
441- /* restore default allocators */
463+ /* Restore default allocators. memLock is intentionally left
464+ * initialized for process lifetime (matching the prior static
465+ * PTHREAD_MUTEX_INITIALIZER behavior): SetAllocators stops new
466+ * entries into TrackMalloc/TrackFree but does not synchronize
467+ * with in-flight calls, so freeing the mutex here would be a
468+ * use-after-free hazard. The memLockInit flag keeps re-Init
469+ * idempotent across an Init/Cleanup/Init cycle. */
442470 return wolfSSL_SetAllocators (mfDefault , ffDefault , rfDefault );
443471}
444472#endif /* WOLFSSL_TRACK_MEMORY && USE_WOLFSSL_MEMORY && \
0 commit comments