Skip to content

Fix mem_track.h compile failure on multi-threaded non-Linux builds#10453

Merged
SparkiDev merged 1 commit into
wolfSSL:masterfrom
LinuxJedi:fix-memtrack
May 12, 2026
Merged

Fix mem_track.h compile failure on multi-threaded non-Linux builds#10453
SparkiDev merged 1 commit into
wolfSSL:masterfrom
LinuxJedi:fix-memtrack

Conversation

@LinuxJedi

Copy link
Copy Markdown
Member

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

Copilot AI review requested due to automatic review settings May 11, 2026 15:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_t usage in the memory tracker with wolfSSL_Mutex + wc_{Init,Lock,UnLock,Free}Mutex under 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.

Comment thread wolfssl/wolfcrypt/mem_track.h Outdated
Comment thread wolfssl/wolfcrypt/mem_track.h Outdated
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
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@LinuxJedi

Copy link
Copy Markdown
Member Author

Jenkins retest this please, FIPS test fixed

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #10453

No scan targets match the changed files in this PR. Review skipped.

@SparkiDev
SparkiDev merged commit 4438615 into wolfSSL:master May 12, 2026
444 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants