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

Closed
Tyrannys wants to merge 2 commits from upstream-pr/1725 into main
Tyrannys commented 2026-11-25 09:48:23 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-25T09:48:23Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5488935321 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1725). Originally posted by [Tyrannys](https://github.com/Tyrannys) on 2026-11-25T09:48:23Z. --- 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)
abnalstlll-byte commented 2026-11-25 10:55:01 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-25T10:55:01Z.

Review: APPROVED


### 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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6056257781 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1725#pullrequestreview-6056257781). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-25T10:55:01Z. **Review:** APPROVED --- ### 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.
zmasi commented 2026-11-28 13:07:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T13:07:18Z.


Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8, /BASE:0x140000000 /DYNAMICBASE:NO) under both Debug CRT (/Od /MDd) and Release CRT (/O2 /MD) against main d48a957bb + 7c0215dc29 (claim #2570#issuecomment-7239583640):

  • Line-by-line diff audit: Clean (runtime/test/d3d12_existing_heap_test.cpp, runtime/test/xmem_test.cpp); replaces _aligned_malloc / _aligned_free with HeapAlloc(heap, ...) / HeapFree(heap, ...) inside the HeapLock(heap)-holding fault toucher loops, eliminating the AB-BA lock-order inversion between the process heap lock and the Debug CRT's internal _HEAP_LOCK. check_no_game_data.py clean.
  • Red (d48a957bb under /Od /MDd Debug CRT): xmem_test_dbg.exe deadlocks (TIMEOUT after 20s) in test_heap_lock_faults.
  • Green (d48a957bb + 7c0215dc29 merge): Auto-merges cleanly (rc=0), introduces 0 new compiler warnings, and passes all 488 checks under both /Od /MDd (heap-lock faults: 16860 mappings, 9586 rounds holding the process heap's lock, 69480 first touches committed, 0 faults on unmapped pages: no deadlock, xmem_test: OK (488 checks), rc=0) and /O2 /MD (26739 mappings, 12519 rounds ... xmem_test: OK (488 checks), rc=0).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239706327 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1725#issuecomment-7239706327). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T13:07:18Z. --- Verified on Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8`, `/BASE:0x140000000 /DYNAMICBASE:NO`) under both Debug CRT (`/Od /MDd`) and Release CRT (`/O2 /MD`) against `main` `d48a957bb` + `7c0215dc29` (claim `#2570#issuecomment-7239583640`): - **Line-by-line diff audit**: Clean (`runtime/test/d3d12_existing_heap_test.cpp`, `runtime/test/xmem_test.cpp`); replaces `_aligned_malloc` / `_aligned_free` with `HeapAlloc(heap, ...)` / `HeapFree(heap, ...)` inside the `HeapLock(heap)`-holding fault toucher loops, eliminating the AB-BA lock-order inversion between the process heap lock and the Debug CRT's internal `_HEAP_LOCK`. `check_no_game_data.py` clean. - **Red (`d48a957bb` under `/Od /MDd` Debug CRT)**: `xmem_test_dbg.exe` deadlocks (`TIMEOUT after 20s`) in `test_heap_lock_faults`. - **Green (`d48a957bb` + `7c0215dc29` merge)**: Auto-merges cleanly (`rc=0`), introduces `0` new compiler warnings, and passes all 488 checks under both `/Od /MDd` (`heap-lock faults: 16860 mappings, 9586 rounds holding the process heap's lock, 69480 first touches committed, 0 faults on unmapped pages: no deadlock`, `xmem_test: OK (488 checks)`, `rc=0`) and `/O2 /MD` (`26739 mappings, 12519 rounds ... xmem_test: OK (488 checks)`, `rc=0`). AI-assisted.
enlight3d commented 2026-11-29 13:18:55 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-11-29T13:18:55Z.


RUN (Windows, GPU box): #1725 merges cleanly; the Debug deadlock reproduces on main and is gone with the PR

Heads: PR head 7c0215dc2, main 3535eb5c4. Merged locally (git merge --no-ff, no conflicts, merge commit f2474f6fe, not pushed). The PR touches only runtime/test/xmem_test.cpp and runtime/test/d3d12_existing_heap_test.cpp.

Commands (VS 2022 cmake + ninja, -G Ninja -DLEONIDA_REQUIRE_VULKAN=ON, --build -j 8 of the named targets only):

cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=<Debug|RelWithDebInfo> -DLEONIDA_REQUIRE_VULKAN=ON
cmake --build runtime/build -j 8 --target gta6rt xmem_test d3d12_existing_heap_test
ctest -R "^(xmem|xmem_pool_zero_read|xmem_no_write_watch|xmem_write_watch|d3d12_existing_heap|d3d12_host_prefix|d3d12_mapped_plane_async|d3d12_existing_heap_no_write_watch|d3d12_existing_heap_write_watch)$" --output-on-failure -j1

Red (kind: behavioural, in a Debug build). The hang needs the debug CRT, so RelWithDebInfo cannot show it: plain main, RelWithDebInfo, 9/9 pass, same as with the PR.

  • main 3535eb5c4, Debug: 6 of 9 fail. xmem, xmem_no_write_watch, xmem_write_watch stop with "xmem heap-lock faults: no progress for 30 s: deadlock", and d3d12_existing_heap, d3d12_existing_heap_no_write_watch, d3d12_existing_heap_write_watch with "vulkan slot-133 heap-lock faults: no progress for 30 s: deadlock". The other three (xmem_pool_zero_read, d3d12_host_prefix, d3d12_mapped_plane_async) pass (they do not run that section). Not a build failure: the PR adds no API, so the red is the plain test run on main.

Green:

  • main + PR (merge f2474f6fe), Debug: 9/9 pass (the six above take 3.8 to 9.5 s each).
  • main + PR, RelWithDebInfo: 9/9 pass (the same tests, 0.02 to 8.9 s).
  • Single runs only, no repeat loop.

Link: no new imports vs main (gta6rt.exe, RelWithDebInfo: 694 DLL!symbol entries from dumpbin /imports, identical to main's list). Expected, since the PR touches tests only.

Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44.

Not run: the full ctest suite; the other tests do not link the two changed files.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7256446580 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1725#issuecomment-7256446580). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-11-29T13:18:55Z. --- **RUN (Windows, GPU box): #1725 merges cleanly; the Debug deadlock reproduces on main and is gone with the PR** Heads: PR head `7c0215dc2`, main `3535eb5c4`. Merged locally (`git merge --no-ff`, no conflicts, merge commit `f2474f6fe`, not pushed). The PR touches only `runtime/test/xmem_test.cpp` and `runtime/test/d3d12_existing_heap_test.cpp`. Commands (VS 2022 cmake + ninja, `-G Ninja -DLEONIDA_REQUIRE_VULKAN=ON`, `--build -j 8` of the named targets only): ``` cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=<Debug|RelWithDebInfo> -DLEONIDA_REQUIRE_VULKAN=ON cmake --build runtime/build -j 8 --target gta6rt xmem_test d3d12_existing_heap_test ctest -R "^(xmem|xmem_pool_zero_read|xmem_no_write_watch|xmem_write_watch|d3d12_existing_heap|d3d12_host_prefix|d3d12_mapped_plane_async|d3d12_existing_heap_no_write_watch|d3d12_existing_heap_write_watch)$" --output-on-failure -j1 ``` Red (kind: behavioural, in a Debug build). The hang needs the debug CRT, so RelWithDebInfo cannot show it: plain main, RelWithDebInfo, 9/9 pass, same as with the PR. - main `3535eb5c4`, Debug: 6 of 9 fail. `xmem`, `xmem_no_write_watch`, `xmem_write_watch` stop with "xmem heap-lock faults: no progress for 30 s: deadlock", and `d3d12_existing_heap`, `d3d12_existing_heap_no_write_watch`, `d3d12_existing_heap_write_watch` with "vulkan slot-133 heap-lock faults: no progress for 30 s: deadlock". The other three (`xmem_pool_zero_read`, `d3d12_host_prefix`, `d3d12_mapped_plane_async`) pass (they do not run that section). Not a build failure: the PR adds no API, so the red is the plain test run on main. Green: - main + PR (merge `f2474f6fe`), Debug: 9/9 pass (the six above take 3.8 to 9.5 s each). - main + PR, RelWithDebInfo: 9/9 pass (the same tests, 0.02 to 8.9 s). - Single runs only, no repeat loop. Link: no new imports vs main (gta6rt.exe, RelWithDebInfo: 694 DLL!symbol entries from `dumpbin /imports`, identical to main's list). Expected, since the PR touches tests only. Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 Pro 10.0.26300, MSVC 19.44. Not run: the full ctest suite; the other tests do not link the two changed files.
aslan2142 commented 2026-12-01 16:37:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:37:39Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317919137 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1725#issuecomment-7317919137). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:37:39Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1725
No description provided.