Fix mem_track.h compile failure on multi-threaded non-Linux builds#10453
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a build break in wolfssl/wolfcrypt/mem_track.h for multi-threaded, non-Linux/macOS/Zephyr targets when WOLFSSL_TRACK_MEMORY + USE_WOLFSSL_MEMORY are enabled (and !WOLFSSL_STATIC_MEMORY). It replaces a pthread-only mutex with wolfSSL’s portable mutex API and adds a lightweight preprocessor-level regression compile test plus CI coverage.
Changes:
- Replace
pthread_mutex_tusage in the memory tracker withwolfSSL_Mutex+wc_{Init,Lock,UnLock,Free}Mutexunder a guard that matches all call sites. - Initialize the mutex before installing tracking allocators; add cleanup logic after restoring default allocators.
- Add a FreeRTOS-class “compile-only” reproducer (stub headers +
-U__linux__style simulation) and a GitHub Actions workflow to run it.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
wolfssl/wolfcrypt/mem_track.h |
Switches mem tracking lock from pthreads to wolfSSL portable mutex API; aligns guard conditions and adds init/cleanup sequencing. |
tests/include.am |
Ships the new regression reproducer files in EXTRA_DIST. |
tests/freertos-mem-track-repro/user_settings.h |
Minimal configuration to trigger the previously broken build path. |
tests/freertos-mem-track-repro/repro.c |
Compile-only reproducer that includes settings.h and mem_track.h. |
tests/freertos-mem-track-repro/FreeRTOS.h |
Clean-room stub header to satisfy the FreeRTOS include chain for compilation. |
tests/freertos-mem-track-repro/semphr.h |
Stub header to satisfy mutex typedef include requirements. |
tests/freertos-mem-track-repro/task.h |
Stub header for the FreeRTOS task include. |
tests/freertos-mem-track-repro/run.sh |
Script compiling the reproducer while suppressing host platform autodefines. |
.github/workflows/freertos-mem-track.yml |
CI job that runs the reproducer compile as a regression check. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The memLock mutex and #include <pthread.h> in mem_track.h were
declared under #ifdef DO_MEM_LIST (Linux/macOS/Zephyr only), but
referenced under the broader guard
!defined(SINGLE_THREADED) && \
(defined(DO_MEM_LIST) || defined(DO_MEM_STATS))
Since DO_MEM_STATS is defined whenever WOLFSSL_TRACK_MEMORY +
USE_WOLFSSL_MEMORY are set without WOLFSSL_STATIC_MEMORY, any
non-Linux/Mac/Zephyr multi-threaded build failed to compile with
implicit pthread_mutex_lock declarations and undeclared memLock.
Replace the raw pthread mutex with wolfSSL's portable mutex API
(wc_InitMutex / wc_LockMutex / wc_UnLockMutex / wc_FreeMutex) so
locking works on every platform wolfSSL already ports to.
InitMemoryTracker now calls wc_InitMutex before
wolfSSL_SetAllocators installs TrackMalloc, guarded by a
memLockInit flag for idempotency. CleanupMemoryTracker calls
wc_FreeMutex after restoring the default allocators so no
in-flight allocation races a freed mutex. The four mutex guards
in TrackMalloc/TrackFree and the two in InitMemoryTracker/
ShowMemoryTracker are unified on the same condition as the
memLock declaration itself.
ZD #21763
Member
Author
|
Jenkins retest this please, FIPS test fixed |
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #10453
No scan targets match the changed files in this PR. Review skipped.
SparkiDev
approved these changes
May 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The memLock mutex and #include <pthread.h> in mem_track.h were declared under #ifdef DO_MEM_LIST (Linux/macOS/Zephyr only), but referenced under the broader guard
Since DO_MEM_STATS is defined whenever WOLFSSL_TRACK_MEMORY + USE_WOLFSSL_MEMORY are set without WOLFSSL_STATIC_MEMORY, any non-Linux/Mac/Zephyr multi-threaded build failed to compile with implicit pthread_mutex_lock declarations and undeclared memLock.
Replace the raw pthread mutex with wolfSSL's portable mutex API (wc_InitMutex / wc_LockMutex / wc_UnLockMutex / wc_FreeMutex) so locking works on every platform wolfSSL already ports to. InitMemoryTracker now calls wc_InitMutex before
wolfSSL_SetAllocators installs TrackMalloc, guarded by a memLockInit flag for idempotency. CleanupMemoryTracker calls wc_FreeMutex after restoring the default allocators so no in-flight allocation races a freed mutex. The four mutex guards in TrackMalloc/TrackFree and the two in InitMemoryTracker/ ShowMemoryTracker are unified on the same condition as the memLock declaration itself.
ZD #21763