From 596fe1f6aca8514da79b09543d290836566b9a0e Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Wed, 25 Nov 2026 02:38:55 -0600 Subject: [PATCH 1/2] tests: xmem heap-lock toucher allocates from the process heap, not the 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 --- runtime/test/xmem_test.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index 03944b1e16..291e7b99d9 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp @@ -1094,9 +1094,13 @@ void spin_for(LONGLONG us) { // its own or not, so whoever holds that lock exclusively (a map or an unmap of physical pages) must never wait for the // process heap meanwhile: it would wait for the faulting thread, which waits for it. While // - threads take the process heap's lock (HeapLock, as HeapAlloc does inside, and the heap gate above) and, holding -// it, _aligned_malloc and _aligned_free and write a word into every page of a window whose pages are mapped fresh +// it, HeapAlloc and HeapFree a block and write a word into every page of a window whose pages are mapped fresh // all the time (a first-touch commit, or a fault on a page that is being unmapped) and into a read-only page (a -// fault that isn't xmem's, as on a guarded shadow page); +// fault that isn't xmem's, as on a guarded shadow page). HeapAlloc, not this program's _aligned_malloc: the title's +// _aligned_malloc comes down to HeapAlloc, but a debug CRT's takes the CRT's own heap lock first, and any thread +// of this program that allocates through that CRT (vcruntime's exception dispatch does, past the heap gate) holds +// that lock while it waits for the process heap: holding HeapLock, the toucher would then deadlock with it +// without xmem in the cycle; // - threads allocate physical pages, map them into their part of the window (as a run, or page by page through the // scatter call), and unmap and free them, some freed while still mapped so that the unmap drops the last // reference to their section; @@ -1157,14 +1161,14 @@ void test_heap_lock_faults(const PhysicalApi& api) { bool round_read_only = false; AcquireSRWLockExclusive(&g_heap_gate); HeapLock(heap); - void* block = _aligned_malloc(64 + (round % 32) * 160, 16); + void* block = HeapAlloc(heap, 0, 64 + (round % 32) * 160); for (uint64_t p = 0; p < kPages; ++p) { auto* const word = static_cast(static_cast(window + p * kPage64k)) + t; round_missed += access_faults(word, true); } round_read_only = access_faults(static_cast(static_cast(read_only)), true); - _aligned_free(block); + HeapFree(heap, 0, block); HeapUnlock(heap); ReleaseSRWLockExclusive(&g_heap_gate); missed.fetch_add(round_missed, std::memory_order_relaxed); From 7c0215dc296478aedc97ba996bb1c2309b7a55e4 Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Wed, 25 Nov 2026 03:47:55 -0600 Subject: [PATCH 2/2] tests: d3d12_existing_heap heap-lock touchers allocate from the process 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 --- runtime/test/d3d12_existing_heap_test.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/runtime/test/d3d12_existing_heap_test.cpp b/runtime/test/d3d12_existing_heap_test.cpp index d84454fce9..6c96b13128 100644 --- a/runtime/test/d3d12_existing_heap_test.cpp +++ b/runtime/test/d3d12_existing_heap_test.cpp @@ -1606,8 +1606,10 @@ int check_rereserved(ID3D12Device* device) { // write-watched one (kept hot, so every submission asks about it) and a plain one (guarded), while submissions run back // to back and: // - threads take the process heap's lock (HeapLock, as HeapAlloc does inside, and the heap gate above: every -// allocation waits for them), _aligned_malloc and _aligned_free under it and write a guarded page: a fault, with -// the heap's lock held, at every submission; +// allocation waits for them), HeapAlloc and HeapFree a block under it and write a guarded page: a fault, with +// the heap's lock held, at every submission. HeapAlloc, not this program's _aligned_malloc: the title's comes +// down to HeapAlloc, but a debug CRT's takes the CRT's own heap lock first, which the threads below hold while +// they wait for the process heap, so a toucher holding HeapLock would deadlock with them without the handler; // - threads _aligned_malloc and _aligned_free blocks of every size class without it; // - a thread keeps the write-watched half hot; // - threads change the write-watch record (a reservation added and removed), the journal (mark), the alias set and @@ -1673,10 +1675,10 @@ int check_heap_lock_faults(ID3D12Device* device) { for (std::uint8_t round = 1; !stop.load(std::memory_order_relaxed); ++round) { AcquireSRWLockExclusive(&g_heap_gate); HeapLock(heap); - void* block = _aligned_malloc(64 + (round % 32) * 160, 16); + void* block = HeapAlloc(heap, 0, 64 + (round % 32) * 160); for (SIZE_T at = t * 64; at < committed; at += 0x10000) // a byte per block *reinterpret_cast(guarded + at) = round; - _aligned_free(block); + HeapFree(heap, 0, block); HeapUnlock(heap); ReleaseSRWLockExclusive(&g_heap_gate); progress.fetch_add(1, std::memory_order_relaxed);