Skip to content

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

Open
Tyrannys wants to merge 2 commits into
ProjectLeonida:mainfrom
Tyrannys:claude/xmem-heap-deadlock
Open

Tyrannys wants to merge 2 commits into
ProjectLeonida:mainfrom
Tyrannys:claude/xmem-heap-deadlock

Conversation

@Tyrannys

@Tyrannys Tyrannys commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-XMEM-HEAP-LOCK-DEADLOCK (#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 tests: xmem commit-on-touch checks read the test's own view, not system commit (TEST-XMEM-COMMIT-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 and others added 2 commits November 25, 2026 03:47
…e debug CRT

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 <noreply@anthropic.com>
…ss heap

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 <noreply@anthropic.com>

@abnalstlll-byte abnalstlll-byte 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.

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.

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.

2 participants