You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(startup_trace): guard SEH unwind in VEH handler and tear down safely at exit - #2709
SEH fault inside VEH permanently silences thread tracing (in_handler stuck true): Under MSVC /EHsc, catch (...) in detail::handler() does not catch Windows SEH exceptions (e.g. an access violation in debug_text() on a bad DBG_PRINTEXCEPTION_C pointer or in stack_from() on a corrupt stack). When SEH unwinds out of detail::handler(), in_handler = false; is skipped and all subsequent debug_string and exception events on that thread are silently dropped. detail::handler() now delegates formatting to detail::log_exception() inside an MSVC __try / __except (EXCEPTION_EXECUTE_HANDLER) block (with an RAII reset guard on non-MSVC), guaranteeing in_handler is always cleared while returning EXCEPTION_CONTINUE_SEARCH.
Dangling VEH / destroyed detail::mutex on process exit:open() installed a process-wide VEH without registering shutdown cleanup, leaving detail::veh active after main() returns while static destruction destroyed detail::mutex and CRT exit closed FILE* streams. detail::mutex() is now an intentionally immortal singleton (new std::mutex), open() registers std::atexit(&close) once, and detail::on uses acquire/release ordering.
Regression Test (Fail-First)
Added raise_bad_dbg_print() (raising DBG_PRINTEXCEPTION_C with invalid pointer 0x1000, caught via __except) followed by OutputDebugStringA("after_bad_dbg_print") in runtime/test/startup_trace_test.cpp:
Before fix (Debug):startup_trace_test.exe failed 5 checks (24 check(s), 5 failed, EXIT=1) — both "after_bad_dbg_print" and the subsequent access violation were silently dropped because in_handler stayed true.
After fix (Debug + RelWithDebInfo): 0 compiler warnings; ctest -R startup_trace passes 1/1 in both Debug and RelWithDebInfo.
Code read of 0e70866b (I didn't build it). Looks right. handler() now holds no C++ objects with destructors, so the __try/__except is allowed there under /EHsc. A nested access violation (the bad DBG_PRINTEXCEPTION_C pointer) first re-enters the VEH, which returns early because in_handler is set, then lands in the __except, so in_handler is always cleared and the original exception continues the search. The immortal mutex and the release/acquire pairing on on (the handler installed before tracing turns on) look right too.
One low-risk caveat: under /EHsc an SEH unwind doesn't run destructors, so a fault after event() takes its lock_guard would leave the mutex locked. The faulting work (debug_text, stack_from) all happens before event() is called, so I don't see a path to it today. Worth keeping in mind if more formatting moves inside event().
Tested 0e70866b (on its base 49475796) on Windows 11 Pro build 26200, MSVC build of runtime/ (RelWithDebInfo, Ninja), AMD Radeon RX 9070 XT (driver 32.0.32029.2010), Vulkan validation on: pass.
startup_trace (the PR's updated startup_trace_test.cpp) passes.
Full ctest: 115/115 pass, 0 failures, 0 validation errors.
This shows the change builds and doesn't regress on Windows/MSVC. I didn't inject an SEH fault inside the VEH handler by hand, so the in_handler reset is covered only by what the PR's test exercises.
Tested 0e70866 merged onto mainec8ec4c (clean merge) on Windows 11 26200, MSVC 19.44 (VS 2022 Build Tools), Ninja, RelWithDebInfo: pass.
startup_trace_test: 24 checks, 0 failed, and 20 more back-to-back runs pass too.
Negative control: the PR's test with main's runtime/shims/startup_trace.h gives 24 check(s), 5 failed (exit 1) in RelWithDebInfo too, the same count the PR reports for Debug.
Full ctest -j 4 with LEONIDA_VK_DEVICE=NVIDIA (RTX 3080, 610.88): 119/119.
Full ctest -j 2 with LEONIDA_VK_DEVICE=AMD (Ryzen 5 7600X iGPU, gfx1036, AMD 24.30.66): 118/119. The one failure is d3d12_vulkan_device at command_queue(host.p, rt_desc) == S_OK, which main has on this AMD host in an elevated shell too (Tests: skip the host realtime-queue check when the host driver refuses it #2422). Nothing from this PR.
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
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.
Summary
Fixes two bugs in
runtime/shims/startup_trace.h:in_handlerstucktrue): Under MSVC/EHsc,catch (...)indetail::handler()does not catch Windows SEH exceptions (e.g. an access violation indebug_text()on a badDBG_PRINTEXCEPTION_Cpointer or instack_from()on a corrupt stack). When SEH unwinds out ofdetail::handler(),in_handler = false;is skipped and all subsequentdebug_stringandexceptionevents on that thread are silently dropped.detail::handler()now delegates formatting todetail::log_exception()inside an MSVC__try/__except (EXCEPTION_EXECUTE_HANDLER)block (with an RAII reset guard on non-MSVC), guaranteeingin_handleris always cleared while returningEXCEPTION_CONTINUE_SEARCH.detail::mutexon process exit:open()installed a process-wide VEH without registering shutdown cleanup, leavingdetail::vehactive aftermain()returns while static destruction destroyeddetail::mutexand CRT exit closedFILE*streams.detail::mutex()is now an intentionally immortal singleton (new std::mutex),open()registersstd::atexit(&close)once, anddetail::onuses acquire/release ordering.Regression Test (Fail-First)
Added
raise_bad_dbg_print()(raisingDBG_PRINTEXCEPTION_Cwith invalid pointer0x1000, caught via__except) followed byOutputDebugStringA("after_bad_dbg_print")inruntime/test/startup_trace_test.cpp:Debug):startup_trace_test.exefailed 5 checks (24 check(s), 5 failed,EXIT=1) — both"after_bad_dbg_print"and the subsequent access violation were silently dropped becausein_handlerstayedtrue.Debug+RelWithDebInfo): 0 compiler warnings;ctest -R startup_tracepasses 1/1 in bothDebugandRelWithDebInfo.