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

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
This commit is contained in:
Andrew Hutchings
2026-05-11 16:06:26 +01:00
parent 01ba609f0d
commit 7b89d82b35
9 changed files with 291 additions and 15 deletions
+35
View File
@@ -0,0 +1,35 @@
name: FreeRTOS mem_track.h compile regression
# START OF COMMON SECTION
on:
push:
branches: [ 'master', 'main', 'release/**' ]
pull_request:
branches: [ '*' ]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
# END OF COMMON SECTION
# Regression test for the memLock / pthread.h guard misalignment in
# wolfssl/wolfcrypt/mem_track.h that previously broke multi-threaded
# FreeRTOS-class builds with WOLFSSL_TRACK_MEMORY + USE_WOLFSSL_MEMORY
# + !WOLFSSL_STATIC_MEMORY. The bug was preprocessor-only, so the test
# simulates a non-Linux/Mac/Zephyr target by suppressing the host
# platform autodefines (-U__linux__ -U__MACH__ -U__ZEPHYR__) and using
# the clean-room FreeRTOS.h / semphr.h stubs under
# tests/freertos-mem-track-repro/. No cross compiler needed.
jobs:
freertos_mem_track:
name: mem_track.h non-Linux multi-threaded compile
if: github.repository_owner == 'wolfssl'
runs-on: ubuntu-24.04
timeout-minutes: 5
steps:
- name: Checkout wolfSSL
uses: actions/checkout@v4
- name: Run mem_track.h FreeRTOS reproducer
run: sh tests/freertos-mem-track-repro/run.sh