tests: heap-lock touchers allocate from the process heap, not the debug CRT (RT-XMEM-HEAP-LOCK-DEADLOCK) #1725

Open
github-bot wants to merge 2 commits from github-bot/gh-1725 into main AGit
Owner

@⁠Tyrannys on GitHub · 2026-11-25 09:48 UTC · original
Tyrannys:claude/xmem-heap-deadlock → main · +14 −8 · 2 files · 2 commits · open

Claimed in the coordination issue as: RT-XMEM-HEAP-LOCK-DEADLOCK (ubiquitarianism/leonida#1608)

Item: RT-XMEM-HEAP-LOCK-DEADLOCK (kotekompotek's finding in #1608: xmem_test hangs in the "xmem heap-lock faults" section in a Debug build on Windows, all three variants).

Cause (measured, not xmem)

At the hang I walked every test thread's stack from the watchdog (temporary instrumentation, not committed):

  • a toucher holds HeapLock(GetProcessHeap()) and is in the debug CRT's _aligned_free → _free_dbg → EnterCriticalSection, waiting for the CRT's own heap lock;
  • another thread is in exception dispatch → vcruntime140d __CxxFrameHandler3 → _calloc_dbg, which holds that CRT lock, → RtlAllocateHeap, waiting for the process heap.

xmem's g_fault_lock isn't in the cycle, and the heap gate can't stop it because the CRT's internal calloc doesn't go through operator new. A release CRT's _aligned_malloc is just HeapAlloc and takes no lock of its own, which is why RelWithDebInfo passes, and why the title's XMemAlloc (its own release CRT) can't hit this. So the bug is in the test's emulation, not in the fault handler.

d3d12_existing_heap_test's check_heap_lock_faults has the same pattern and hangs the same way in Debug (all three variants: "vulkan slot-133 heap-lock faults: no progress for 30 s"). Its allocator threads take the CRT lock and then the heap lock directly.

Fix

The touchers in both tests HeapAlloc/HeapFree on the process heap while they hold its lock, instead of the test binary's _aligned_malloc/_aligned_free. They still write heap block headers under the heap's lock while faulting, which is what the section tests. Nothing is skipped and no check changes. The comments say why.

Tested

Windows 11 Pro (26300), MSVC 14.52, RTX 4070 SUPER, no game files:

  • Debug, before: xmem_test xmem exit 3 on every run (watchdog). After: no hang in 105 runs of the three xmem* variants. Debug ctest xmem* and d3d12_existing_heap*: 6/6 pass.
  • RelWithDebInfo: full ctest 400/402. The two failures were xmem_write_watch ("the reader of the views' last pages never faults or misreads") and an api_ref_state 30 s timeout. Both then passed --repeat until-fail:3 together with the other heap-lock tests (7/7 ×3).
  • Unrelated to this change but seen: two other xmem checks fail intermittently, both before the heap-lock section. "writing to 512 of them charges about those 512 pages" is the system-wide commit-charge check #1316 owns; it failed once in 30 quiet runs. "the reader of the views' last pages…" failed only while a parallel build loaded the machine. A full Debug build also fails on vk_presenter_sync_test.cpp (C1128, needs /bigobj), and the Debug run_tiny_* and replay_* tests fail on this machine. None of them link the two test files changed here.
  • python -I scripts/check_no_game_data.py on both files: clean.

djrolfes' Wine 11.22 read-only XMem findings in #1608 have a different cause (VirtualQuery state under Wine). This PR doesn't touch them, and the Wine skip of the heap-lock section stays as it is.

AI-assisted (Claude Code).

GPUs tested: NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11 Pro 26300

AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

**@⁠Tyrannys** on GitHub · 2026-11-25 09:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1725) `Tyrannys:claude/xmem-heap-deadlock` → `main` · +14 −8 · 2 files · 2 commits · open Claimed in the coordination issue as: RT-XMEM-HEAP-LOCK-DEADLOCK (ubiquitarianism/leonida#1608) Item: **RT-XMEM-HEAP-LOCK-DEADLOCK** (kotekompotek's finding in #1608: `xmem_test` hangs in the "xmem heap-lock faults" section in a Debug build on Windows, all three variants). ## Cause (measured, not xmem) At the hang I walked every test thread's stack from the watchdog (temporary instrumentation, not committed): - a toucher holds `HeapLock(GetProcessHeap())` and is in the **debug CRT's** `_aligned_free` → `_free_dbg` → `EnterCriticalSection`, waiting for the CRT's own heap lock; - another thread is in exception dispatch → vcruntime140d `__CxxFrameHandler3` → `_calloc_dbg`, which **holds that CRT lock**, → `RtlAllocateHeap`, waiting for the process heap. xmem's `g_fault_lock` isn't in the cycle, and the heap gate can't stop it because the CRT's internal `calloc` doesn't go through `operator new`. A release CRT's `_aligned_malloc` is just `HeapAlloc` and takes no lock of its own, which is why RelWithDebInfo passes, and why the title's `XMemAlloc` (its own release CRT) can't hit this. So the bug is in the test's emulation, not in the fault handler. `d3d12_existing_heap_test`'s `check_heap_lock_faults` has the same pattern and hangs the same way in Debug (all three variants: "vulkan slot-133 heap-lock faults: no progress for 30 s"). Its allocator threads take the CRT lock and then the heap lock directly. ## Fix The touchers in both tests `HeapAlloc`/`HeapFree` on the process heap while they hold its lock, instead of the test binary's `_aligned_malloc`/`_aligned_free`. They still write heap block headers under the heap's lock while faulting, which is what the section tests. Nothing is skipped and no check changes. The comments say why. ## Tested Windows 11 Pro (26300), MSVC 14.52, RTX 4070 SUPER, no game files: - Debug, before: `xmem_test xmem` exit 3 on every run (watchdog). After: no hang in 105 runs of the three `xmem*` variants. Debug ctest `xmem*` and `d3d12_existing_heap*`: 6/6 pass. - RelWithDebInfo: full `ctest` 400/402. The two failures were `xmem_write_watch` ("the reader of the views' last pages never faults or misreads") and an `api_ref_state` 30 s timeout. Both then passed `--repeat until-fail:3` together with the other heap-lock tests (7/7 ×3). - Unrelated to this change but seen: two other xmem checks fail intermittently, both before the heap-lock section. "writing to 512 of them charges about those 512 pages" is the system-wide commit-charge check #1316 owns; it failed once in 30 quiet runs. "the reader of the views' last pages…" failed only while a parallel build loaded the machine. A full Debug build also fails on `vk_presenter_sync_test.cpp` (C1128, needs `/bigobj`), and the Debug `run_tiny_*` and `replay_*` tests fail on this machine. None of them link the two test files changed here. - `python -I scripts/check_no_game_data.py` on both files: clean. djrolfes' Wine 11.22 read-only XMem findings in #1608 have a different cause (VirtualQuery state under Wine). This PR doesn't touch them, and the Wine skip of the heap-lock section stays as it is. AI-assisted (Claude Code). GPUs tested: NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11 Pro 26300 AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
In a Debug build the heap-lock section of xmem_test hung on every run: a
toucher held HeapLock and called the debug CRT's _aligned_free, which waits
for the CRT's own heap lock, while another thread held that lock in
_calloc_dbg (vcruntime's exception dispatch allocates there, past the heap
gate) and waited for the process heap. xmem's fault lock was not in the
cycle, and RelWithDebInfo passed.

The title's _aligned_malloc comes down to HeapAlloc, so the touchers now
HeapAlloc and HeapFree on the process heap while they hold its lock: the
block headers are still written under the lock, without the debug CRT's
lock order.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The same Debug-only cycle as xmem_test's: the touchers held HeapLock and
called the debug CRT's _aligned_malloc, which takes the CRT's heap lock,
while the allocator threads hold that lock and wait for the process heap.
The watchdog fired in all three variants in a Debug build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:02:39 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-25 10:55 UTC · original · approved these changes

### Clean-Room & Technical Verification

  • Mergeability: 3-way merge against upstream main (bcaa13dad) is 100% clean with zero merge conflicts.
  • Clean-Room Compliance: 100% clean-room. Test harness synchronization fix only; zero retail code, game data, or keys (check_no_game_data.py clean).
  • Technical Analysis & Root Cause:
    • In MSVC Debug builds, _aligned_malloc/_aligned_free route into _free_dbg/_calloc_dbg which acquires the debug CRT heap lock.
    • When test touchers held HeapLock(GetProcessHeap()) and invoked _aligned_malloc, this inverted the lock hierarchy with CRT exception dispatch threads (in __CxxFrameHandler3 / _calloc_dbg allocating from CRT heap lock and subsequently waiting on the process heap lock), leading to an unrecoverable deadlock in Debug builds (xmem_test and d3d12_existing_heap_test).
    • Replacing test touchers' allocation calls with direct Win32 HeapAlloc(heap, 0, ...) and HeapFree(heap, 0, ...) eliminates the CRT lock acquisition while maintaining valid heap header manipulation and fault induction under HeapLock.
    • Thoroughly tested under both Debug and RelWithDebInfo builds.

LGTM! Approved.

**@⁠abnalstlll-byte** on GitHub · 2026-11-25 10:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1725#pullrequestreview-6056257781) · **approved these changes** ### Clean-Room & Technical Verification - **Mergeability**: 3-way merge against upstream `main` (`bcaa13dad`) is 100% clean with zero merge conflicts. - **Clean-Room Compliance**: 100% clean-room. Test harness synchronization fix only; zero retail code, game data, or keys (`check_no_game_data.py` clean). - **Technical Analysis & Root Cause**: - In MSVC Debug builds, `_aligned_malloc`/`_aligned_free` route into `_free_dbg`/`_calloc_dbg` which acquires the debug CRT heap lock. - When test touchers held `HeapLock(GetProcessHeap())` and invoked `_aligned_malloc`, this inverted the lock hierarchy with CRT exception dispatch threads (in `__CxxFrameHandler3` / `_calloc_dbg` allocating from CRT heap lock and subsequently waiting on the process heap lock), leading to an unrecoverable deadlock in Debug builds (`xmem_test` and `d3d12_existing_heap_test`). - Replacing test touchers' allocation calls with direct Win32 `HeapAlloc(heap, 0, ...)` and `HeapFree(heap, 0, ...)` eliminates the CRT lock acquisition while maintaining valid heap header manipulation and fault induction under `HeapLock`. - Thoroughly tested under both Debug and RelWithDebInfo builds. LGTM! Approved.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1725/head:github-bot/gh-1725'
git switch 'github-bot/gh-1725'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1725'
git switch 'github-bot/gh-1725'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1725'
git switch 'github-bot/gh-1725'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1725'
git switch 'main'
git merge --squash 'github-bot/gh-1725'
git switch 'main'
git merge --ff-only 'github-bot/gh-1725'
git switch 'main'
git merge 'github-bot/gh-1725'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.