Repository navigation
1118ed7 chaos28 - VLD chaos tests and fixes (gate soak) - #74
Merged
Merged
Conversation
VLDGetLeaksCount holds g_heapMapLock, which every allocation hook takes, while it resolves leak call stacks with dbghelp. dbghelp loads a module's symbols on first use, and that load can need the loader lock (with a symbol server, symsrv/wininet load DLLs). A thread running DLL_THREAD_ATTACH holds the loader lock and blocks in VLD's allocation hook on g_heapMapLock, so the two threads deadlock. Seen in a dump of ignore_modules_getaddrinfo_test run with _NT_SYMBOL_PATH=srv*... The test makes that ordering deterministic without a symbol server: a dbghelp callback on the deferred symbol load of a helper DLL starts a thread whose DLL_THREAD_ATTACH in the helper allocates, waits until that thread is inside thread attach, then calls LoadLibrary. A watchdog prints where both threads are and terminates the process after 30 s (exit 3); ctest TIMEOUT is 60 s. It fails today in Debug and RelWithDebInfo (x64). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
GetLeaksCount, GetThreadLeaksCount, ReportLeaks and ReportThreadLeaks resolve leak call stacks while holding g_heapMapLock, which every allocation takes. dbghelp loads a module's symbols on first use and that load can take the loader lock (a symbol server path loads symsrv, wininet and their dependencies), while a thread in DLL_THREAD_ATTACH holds the loader lock and waits for g_heapMapLock in VLD's allocation hook: deadlock (heapmaplock_deadlock_test). loadLeakSymbols collects the code pages of the unreported leaks' call stacks under g_heapMapLock, releases it, and makes dbghelp load the symbols of each module those pages belong to. The locked pass then only does in-memory lookups. Modules first seen in leaks allocated in between are still loaded under the lock. ResolveCallstacks and GetAllocationResolveResults already take the loader lock first and are unchanged; so is the report from the destructor. heapmaplock_deadlock_test now passes on x64 and x86 (local), ARM64 native and x64 under emulation (Windows 26300 ARM64), Debug and RelWithDebInfo; all other tests unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
…(review) The symbols are loaded without g_heapMapLock, so a leak allocated in between could bring a module whose symbols are still deferred. The four APIs now take g_heapMapLock through LeakSymbolsHeapMapLock, which checks under the lock (SymGetModuleInfoW64, no loading) that no module in the unreported leaks' call stacks is still deferred, and otherwise releases the lock, loads them and retries (up to 8 times). GetLeaksCount and GetThreadLeaksCount only resolve call stacks with SkipCrtStartupLeaks, so they no longer load symbols without it. The loads deliberately do not hold the loader lock either: tried, and heapmaplock_deadlock_test deadlocked again, because with the loader lock held LoadLibrary waits for a thread that is initializing and needs that lock. (ResolveCallstacks and GetAllocationResolveResults resolve under the loader lock and are exposed to the same deadlock; not changed here.) The test no longer insists that its thread reaches DLL_THREAD_ATTACH (VLD holding the loader lock would keep it out, which is not a deadlock), joins it from main instead of from the dbghelp callback, and its watchdog only writes static strings. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
Two holes left by the check-and-retry: - after the last attempt the locked pass could still trigger a deferred load. Now, while a thread holds g_heapMapLock through LeakSymbolsHeapMapLock, getFunctionName and the line lookup skip a module whose symbols are still deferred (SymGetModuleInfoW64, no load) and show the frame as an address; the retry loop only makes that rare. - a second leak query could hold g_heapMapLock while waiting for g_DbgHelp held by a thread loading symbols, closing a cycle with a thread in DLL_THREAD_ATTACH. g_leakQueryLock now serializes the four leak queries before any other lock. Not changed: with StackWalkMethod = safe the allocation hook walks the stack under g_DbgHelp, so a thread in DLL_THREAD_ATTACH can still wait for a thread loading symbols, as before this series. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
A leak query from a thread that already owns g_heapMapLock (e.g. from a report hook while a report runs) took g_leakQueryLock after g_heapMapLock (lock order inversion), loaded symbols while the outer g_heapMapLock was still held, and cleared the outer query's no-load guard when it returned. Such a query now takes no lock and loads nothing; it only sets the guard and restores the previous guard value when it returns. The ownership check uses CRITICAL_SECTION::OwningThread (CriticalSection::IsOwnedByCurrentThread), because the existing IsLockedByCurrentThread is cleared by any nested Leave. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
…iew) The constructor read the previous guard value before waiting for the locks, so a thread could later restore the ID of a query that had already finished and forbid symbol loading outside any leak query. The value now comes from the InterlockedExchange that sets the guard. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
New src/tests/chaos suite: stress and race tests that try to make VLD hang, crash or misreport. A shared watchdog turns hangs into failures; tests share one output directory, vld.ini and a helper DLL. chaos_heapdestroy_deadlock: VLD's HeapDestroy hook reports the heap's remaining blocks (reportHeapLeaks) while holding g_heapMapLock and resolved their call stacks there, the same symbol load under g_heapMapLock that deadlocks against a thread in DLL_THREAD_ATTACH (as fixed for the leak queries). It hung on x64 Debug and RelWithDebInfo. reportHeapLeaks now uses LeakSymbolsHeapMapLock, which loads the symbols first and only for that heap's blocks. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
CallReportHook walked g_pReportHooks without any lock while VLDSetReportHook changed it under m_optionsLock, so a leak report racing a hook install/remove could walk a set node being freed. The set now has its own lock (g_reportHooksLock); CallReportHook copies the hooks under it and calls them after releasing it, so a hook may itself install or remove hooks, and allocating in a hook cannot invert locks with g_heapMapLock. The destructor clears the set under the same lock. At most MAX_REPORT_HOOKS (64) hooks can be installed. chaos_report_hook_race installs and removes hooks on three threads while two threads print leak reports. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
chaos_report_hook_race hung in Debug: a thread in VLDReportLeaks held g_heapMapLock and, in Report's _vsnwprintf_s, waited for the debug CRT heap lock (the debug CRT's free takes it even to free nothing), while an exiting thread's FLS cleanup in VLD's own static debug CRT held that lock and freed through VLD's HeapFree hook, which waited for g_heapMapLock. VLD's own CRT heap calls reach the hooks through kernel32's patched import slots. Report now formats with ntdll's _vsnwprintf, which uses neither the heap nor locks, and the memory dumps format bytes by hand. The one Report format using %zu, which ntdll does not support, uses %Iu. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
CallStack::resolveFunction took fmt::format_to_n's untruncated size as the line length, so a frame line longer than MAXREPORTLENGTH (long source path, module and function names) wrote its terminating NUL past the static stack_line buffer and lost its newline: the next frame was appended to the same report line, and the resolved length overcounted. The length is now what was written, and a truncated line ends with a newline. chaos_long_frame leaks from a function with a 252-character name in a DLL with a 138-character file name built from a 111-character source file name, and checks the report. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
Fail() formatted with _snprintf_s; a test whose hung thread holds a CRT lock made the watchdog hang too, so the hang was reported as a CTest timeout. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
Report hooks run on the reporting thread with g_heapMapLock held, in the middle of a walk over a heap's block map. A hook that freed a tracked block erased a map entry (Tree::erase moves the successor's contents, invalidating the walk) and deleted the blockinfo_t under it: chaos_hook_reenter crashed with _purecall in reportLeaks in RelWithDebInfo every run. LeakSymbolsHeapMapLock now marks a block walk. While the walking thread frees, reallocates in place, maps over an address or destroys a heap, the entry is marked reported and its erasure, the old call stack or the heap unmap is deferred to the end of the outermost walk. reportLeaks reads the CRT request id and the data to dump before its first Report call, since the hook can free the block being reported (Debug hit the CRT block size assert reading the freed header). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
The previous commit was staged from zero-context hunks, which put beginBlockWalk, endBlockWalk, the m_vld member and VldSymbolLoadForbidden's opening lines at the wrong places, so it did not build. Move them where they belong. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
VLDResolveCallstacks and VLDGetAllocationResolveResults took the loader lock and g_heapMapLock and then resolved symbols, so dbghelp could call LoadLibrary with g_heapMapLock held. That waits for the loader's load owner, and an exiting thread that owns the loader frees FLS data into unmapBlock, which waits for g_heapMapLock: chaos_api_storm hung in Debug. Both now use LeakSymbolsHeapMapLock like the leak report: symbols are loaded before g_heapMapLock is taken and never under it, and the loader lock is not taken. A frame whose symbols are still pending is shown as an address, so GetAllocationResolveResults no longer asserts that everything resolved. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
RefreshModules held the loader lock while attachToLoadedModules waited for g_DbgHelp. A thread loading symbols holds g_DbgHelp while dbghelp calls LoadLibrary, which waits for the loader's load owner; a starting thread that owned the loader waited for the loader lock. chaos_api_storm hung on that cycle once ResolveCallstacks stopped holding g_heapMapLock. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
A realloc made while VLD is disabled, or from an excluded module, did not update the block map. When the block moved, its old address stayed mapped although the heap had freed it; the next allocation there printed "New allocation at already allocated address" and replaced it. Unmap the old address when the block moves. chaos_api_storm toggles every control API (Disable/Enable/Restore, GlobalDisable/Enable, SetOptions, counts, ResolveCallstacks, RefreshModules) while threads allocate, reallocate and free, then checks that tracking is still exact. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
chaos_crt_matrix allocates through every CRT entry point (malloc, calloc, realloc, _recalloc, _aligned_*, _expand, new[], strdup family) from several threads and checks the leak count stays exact. chaos_heap_churn creates, uses and destroys private heaps from several threads, leaking on purpose into heaps that are then destroyed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
Short-lived threads allocate in DLL_THREAD_ATTACH and free in DLL_THREAD_DETACH of a VLD-linked DLL while other threads load and unload DLLs and count leaks. The leak count must match the blocks still held by threads that are alive (thread pool and loader workers). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
With AggregateDuplicates on, eraseDuplicates counted every block with the same size and call stack, ignoring the thread filter of ReportThreadLeaks and the blocks already marked as reported. VLDReportThreadLeaks for one thread counted another thread's duplicates (chaos_aggregate_threads: 5 instead of 3). Apply the same filters as reportLeaks. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
A heap destroyed during a block walk (by a report hook) is unmapped at the end of the walk, by handle. When the hook then created a heap that got the same handle, its blocks went into the destroyed heap's block map and were dropped with it: chaos_hook_heap_ops lost the new heap's block in 15 of 20 runs. A heap destroyed during a walk is now flagged and deferred with its heapinfo_t. Mapping the same handle again (HeapCreate, or the first block of a heap VLD did not see created) swaps in new information in place and endBlockWalk frees the old one; it unmaps the handle only if it still maps to the deferred information. Deferred blocks are erased through their own heap information instead of a handle lookup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
HEAP_NO_SERIALIZE heaps, HEAP_GENERATE_EXCEPTIONS with a failing allocation, HEAP_REALLOC_IN_PLACE_ONLY that fails and that succeeds, zero-size blocks and reallocations to zero, and HeapLock/HeapWalk, HeapCompact and HeapValidate while other threads allocate from the process heap. Leak counts must stay exact. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
ExitProcess kills threads inside VLD's hooks, reports and symbol loading, then VLD reports from DLL_PROCESS_DETACH. The process must still exit with main's exit code, without crashing or hanging. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
SafeCallStack::getStackTrace took g_heapMapLock and then g_DbgHelp around StackWalk64. The walk only uses dbghelp, which g_DbgHelp serializes, but StackWalk64 can load symbols and dbghelp then calls LoadLibrary, which waits for the loader's load owner. An exiting thread that owned the loader freed FLS data into unmapBlock and waited for g_heapMapLock: chaos_api_storm hung with the safe stack walk enabled. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
dbghelp loads symsrv.dll from its own directory every time it meets a symbol server element of the search path (from _NT_SYMBOL_PATH) or probes a directory as a symbol store. VLD's dbghelp has no symsrv.dll beside it, so every symbol load made a failing LoadLibrary that waited for the loader's load owner with g_DbgHelp held; a thread starting or exiting under the loader then waited for VLD's locks. chaos_api_storm (Debug) hung in about 4% of runs on x64, x64 emulated on ARM64 and native ARM64. When symsrv.dll is missing, drop the srv*, symsrv* and cache* elements from the search path and set SYMOPT_DISABLE_SYMSRV_AUTODETECT. They could never be used without symsrv.dll. 0 hangs in 150 runs afterwards. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
VLDSetReportOptions closed the report file without telling the report writer, which kept writing to the closed FILE, and never applied the new stdout and debugger settings unless a file was opened. It also opened and closed files while holding m_optionsLock; that allocates and frees through VLD's hooks, and the allocation hooks take m_optionsLock. A thread switching the destination waited for g_heapMapLock in fclose while a thread attach allocating under the loader lock waited for m_optionsLock. setReportDestination now swaps the report file, its encoding and the stdout and debugger settings at once (SwapReportFile, under an SRW lock that writes to the file share), then closes the previous file and opens the new one with no VLD lock held. The file buffer is allocated at open, so writes under the SRW lock never allocate. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
wcsncpy_s with a NULL source raised an invalid parameter: a fast fail in release builds, an assertion dialog in debug builds. chaos_report_options switches the report destination between files and the debugger, including from a report hook and from several threads while others report, checks that a file gets nothing after reporting moved away from it, and passes NULL strings to the APIs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
HEAP_REALLOC_IN_PLACE_ONLY may fail. On ARM64 the shrink of a block in a HEAP_NO_SERIALIZE heap returns NULL; only a moved block is wrong. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
RtlReAllocateHeap and the CRT realloc free the old block before VLD remaps it under g_heapMapLock, so a leak walk can read a block whose memory was already freed and decommitted (AV in resolveStacks found by chaos_api_storm). Read CRT headers and leak dump data through an SEH-guarded copy and treat an unreadable block as freed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
Reverts the order from edbe5f4. With g_DbgHelp held first, RefreshModules waited for the loader while a thread attach owned the loader and waited for the CRT heap lock, whose holder was in a hook waiting for g_DbgHelp. The order loader then g_DbgHelp matches RegisterLoadedModule and attachToLoadedModules; dbghelp no longer loads symsrv under g_DbgHelp (f7d82d5), which was the reason for the reversed order. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
The heap frees the old block inside the real realloc, before VLD unmaps the old address. Another thread can be handed that address and map it first; the late unmap then erased that thread's live block, so real leaks went missing. This explains the dynamic_app MultithreadLoadingTests under-count (1150 of 1152) on the x64 on ARM64 gate. The realloc hooks now record the serial number mapped at the old address before the real call, and every unmap of the old address after a move checks it. Serial numbers skip 0, which now means no block was mapped. The ARM64 early return in ~CaptureContext also unmaps the old address of a moved block, as the other disabled paths do. chaos_realloc_reuse lost leaked blocks in 8 of 10 runs before the fix and in none of 25 after. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
anporumb
marked this pull request as ready for review
November 13, 2026 03:31
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
anporumb
enabled auto-merge (squash)
November 13, 2026 03:41
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Dan Cristoloveanu (dcristoloveanu)
approved these changes
Nov 14, 2026
Dan Cristoloveanu (dcristoloveanu)
left a comment
Member
There was a problem hiding this comment.
I have read the core changes and the tests behind each claimed fix. The 1 (one) that matters:
- callstack.cpp:374: a symbol cache miss under g_heapMapLock latches NOTSTARTUPCRT and a hex m_resolved; a FreeLibrary racing a leak query turns CRT startup blocks into reported leaks for good.
The realloc serial fix and the ordinal fix look right. The rest is should fix / consider. Will sign trusting that comments will be addressed.
…mment Product fixes: - A symbol cache miss no longer latches a call stack's startup decision or its resolved text; a partial rendering is resolved again once its frames are cached. Earlier renderings stay alive with the call stack, since VldInternalGetAllocationResolveResults hands them out. - Hooks preserve the thread's last error (getTls and CaptureContext). - An unopenable report file falls back to debugger output. - Ordinal names are handled when matching GetProcAddress requests. - ValidateHeapFree reports print after g_heapMapLock is released; call stack resolution no longer uses static scratch buffers. - Blocks are read with ReadProcessMemory, so unreadable or guard pages raise no exceptions; a CRT block whose header the application made inaccessible is still counted and reported. - DumpMemoryW does not read past the block; NULL checks after VLD's new in the symbol cache. - VLDSetReportOptions callers are serialized. - StackWalkMethod=safe fallbacks to RtlCaptureStackBackTrace are counted and reported once. - Docs: report hook locking and the 64 hook limit; stale comments fixed. Tests and pipeline: - New src/tests/review74 tests, each failing without its fix. - Chaos tests: permanent hook checked after churn, a CRT header guess that changes between queries, paced leakers and enabled moves in realloc_reuse. - getaddrinfo_missing_free_test comments and name match what it checks. - Failed job attempts publish their build under a per-attempt artifact name. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d251f4de-84a5-4357-87c1-0d7158123d9f
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Dan Cristoloveanu (dcristoloveanu)
approved these changes
Nov 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.