Tests: the lib test binary sometimes ends with 0xc0000005 during eh::tests::thread_dumps_while_threads_run_and_exit #3586

Closed
opened 2026-12-01 16:14:03 +00:00 by fromthemoonx · 2 comments
fromthemoonx commented 2026-12-01 16:14:03 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-01T16:14:03Z.


In full parallel suite runs, the lib test binary sometimes ends with 0xc0000005 (STATUS_ACCESS_VIOLATION) while eh::tests::thread_dumps_while_threads_run_and_exit is taking its dumps. That test came with #253, which fixed #2689. No test is named as failing, so the run looks like an unexplained crash of the whole binary. I have seen it on two trees here.

What happened

  • main 6bfb5c5 (today): cargo test --workspace --no-fail-fast -- --test-threads=4 ended with process didn't exit successfully: ...xcl-<hash>.exe --test-threads=4 (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION).
  • #1692's head merged onto main b2b1f10 (earlier today): the same exit, in the same place.
  • In both logs:
    • The binary printed 8 xcl: thread dump (N threads) blocks and then ended. A passing run of the test prints 40.
    • The last dump lists the test's 4 churn_stack threads, deep in their recursion.
    • The tests running beside it were GPU tests. In today's run these were gpu::agility::tests::create_device_sets_dred_first (inside wintrust / CRYPT32, called from nvwgf2umx.dll), gpu::bvh::tests::scene_trace_matches_brute_force and gpu::dxil::compact_tests::a_large_program_that_repeats_itself_takes_the_compact_form. NVIDIA driver threads were in the process too.

What did not reproduce it

  • The test alone (--exact eh::tests::thread_dumps_while_threads_run_and_exit): 30 of 30 passed.
  • eh::tests::, gpu::agility::tests::, gpu::bvh::tests:: and gpu::dxil::compact_tests:: together at --test-threads=8: 20 of 20 passed.
  • The whole lib binary 3 more times on 6bfb5c5 at --test-threads=4: no access violation. One run hit the known xma2_source_voice_decodes_and_manages_lifetime assertion, and one ended at the coherent window could not be restored stop from #3187.
  • So far that is 2 access violations in at least 13 whole-lib runs on this machine today.

Not found
I did not find the cause. The process ends without a crash message, so the faulting thread and address are unknown. #253's notes list "a dump that reads deeper than the copy" as not covered. Another candidate is a module unloading between the copy and RtlLookupFunctionEntry / RtlVirtualUnwind. A driver thread that ends after it is resumed, for example, could let its module unload. Neither has been checked.

It matters outside the tests, because XCL_THREAD_DUMP_AT and the thread dump taken when a process ends with a non-zero status both call the same dump_threads.

Hardware: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300, debug build.

AI use

  • Claude Code (Claude Opus 5.5) made the runs on the machine above, read the logs and eh.rs, and wrote this issue.
<!-- greyfinch-sync:ivall/xcl:issue:6724967941 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/3586). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-12-01T16:14:03Z. --- In full parallel suite runs, the lib test binary sometimes ends with `0xc0000005` (STATUS_ACCESS_VIOLATION) while `eh::tests::thread_dumps_while_threads_run_and_exit` is taking its dumps. That test came with #253, which fixed #2689. No test is named as failing, so the run looks like an unexplained crash of the whole binary. I have seen it on two trees here. **What happened** - `main` `6bfb5c5` (today): `cargo test --workspace --no-fail-fast -- --test-threads=4` ended with `process didn't exit successfully: ...xcl-<hash>.exe --test-threads=4 (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)`. - #1692's head merged onto `main` `b2b1f10` (earlier today): the same exit, in the same place. - In both logs: - The binary printed 8 `xcl: thread dump (N threads)` blocks and then ended. A passing run of the test prints 40. - The last dump lists the test's 4 `churn_stack` threads, deep in their recursion. - The tests running beside it were GPU tests. In today's run these were `gpu::agility::tests::create_device_sets_dred_first` (inside `wintrust` / `CRYPT32`, called from `nvwgf2umx.dll`), `gpu::bvh::tests::scene_trace_matches_brute_force` and `gpu::dxil::compact_tests::a_large_program_that_repeats_itself_takes_the_compact_form`. NVIDIA driver threads were in the process too. **What did not reproduce it** - The test alone (`--exact eh::tests::thread_dumps_while_threads_run_and_exit`): 30 of 30 passed. - `eh::tests::`, `gpu::agility::tests::`, `gpu::bvh::tests::` and `gpu::dxil::compact_tests::` together at `--test-threads=8`: 20 of 20 passed. - The whole lib binary 3 more times on `6bfb5c5` at `--test-threads=4`: no access violation. One run hit the known `xma2_source_voice_decodes_and_manages_lifetime` assertion, and one ended at the `coherent window could not be restored` stop from #3187. - So far that is 2 access violations in at least 13 whole-lib runs on this machine today. **Not found** I did not find the cause. The process ends without a crash message, so the faulting thread and address are unknown. #253's notes list "a dump that reads deeper than the copy" as not covered. Another candidate is a module unloading between the copy and `RtlLookupFunctionEntry` / `RtlVirtualUnwind`. A driver thread that ends after it is resumed, for example, could let its module unload. Neither has been checked. It matters outside the tests, because `XCL_THREAD_DUMP_AT` and the thread dump taken when a process ends with a non-zero status both call the same `dump_threads`. Hardware: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300, debug build. **AI use** - Claude Code (Claude Opus 5.5) made the runs on the machine above, read the logs and `eh.rs`, and wrote this issue.
fromthemoonx commented 2026-12-01 17:23:48 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-01T17:23:48Z.


Two more sightings today, both on #1852's head (1fbdea9, which changes only gpu/dxil) merged onto main 6bfb5c5. Two whole-suite runs in a row ended the lib binary with 0xc0000005, after 7 and after 5 of the test's 40 dumps. As before, gpu::agility::tests::create_device_sets_dred_first and gpu::bvh::tests::* were running beside it.

The lib binary run on its own afterwards (--test-threads=4) did not hit it: 0 of 4 runs on main, 0 of 4 on the merge. So far: 4 crashes in about 23 whole-lib runs here. All 4 came in a cargo test --workspace run, which starts the lib binary right after the build.

AI use: Claude Code (Claude Opus 5.5) made the runs and wrote this comment.

Update, 17:55 UTC: one more, on #3476's merge (sparse.rs only), after 7 dumps. Today's tally here: 5 of the 22 cargo test --workspace runs I made today ended this way, against 0 of 19 runs of the lib binary started on its own. In every crash create_device_sets_dred_first was running beside the dump test. In 3 of the 5 last dumps its stack was inside the NVIDIA driver (nvwgf2umx.dll), and in 1 of them inside wintrust / CRYPT32 too.

Update, end of day (counted from the saved logs, not from memory): 8 of the 44 cargo test --workspace runs here today ended this way, each after 5 to 10 of the 40 dumps, each with create_device_sets_dred_first beside it. The lib binary started on its own did it too, once in 20 runs (on #3475's merge, after 5 dumps). So it is not only a suite-start effect, as my earlier line suggested.

<!-- greyfinch-sync:ivall/xcl:comment:7319231757 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/3586#issuecomment-7319231757). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-12-01T17:23:48Z. --- Two more sightings today, both on #1852's head (`1fbdea9`, which changes only `gpu/dxil`) merged onto `main` `6bfb5c5`. Two whole-suite runs in a row ended the lib binary with `0xc0000005`, after 7 and after 5 of the test's 40 dumps. As before, `gpu::agility::tests::create_device_sets_dred_first` and `gpu::bvh::tests::*` were running beside it. The lib binary run on its own afterwards (`--test-threads=4`) did not hit it: 0 of 4 runs on `main`, 0 of 4 on the merge. So far: 4 crashes in about 23 whole-lib runs here. All 4 came in a `cargo test --workspace` run, which starts the lib binary right after the build. AI use: Claude Code (Claude Opus 5.5) made the runs and wrote this comment. Update, 17:55 UTC: one more, on #3476's merge (`sparse.rs` only), after 7 dumps. Today's tally here: 5 of the 22 `cargo test --workspace` runs I made today ended this way, against 0 of 19 runs of the lib binary started on its own. In every crash `create_device_sets_dred_first` was running beside the dump test. In 3 of the 5 last dumps its stack was inside the NVIDIA driver (`nvwgf2umx.dll`), and in 1 of them inside `wintrust` / `CRYPT32` too. Update, end of day (counted from the saved logs, not from memory): 8 of the 44 `cargo test --workspace` runs here today ended this way, each after 5 to 10 of the 40 dumps, each with `create_device_sets_dred_first` beside it. The lib binary started on its own did it too, once in 20 runs (on #3475's merge, after 5 dumps). So it is not only a suite-start effect, as my earlier line suggested.
fromthemoonx commented 2026-12-02 19:16:13 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-02T19:16:13Z.


Cause found: a DLL is unloaded while the dump is unwinding a frame inside it. The fix below works here. I cannot open it as a PR myself: it changes the runtime, and this machine has no retail title for the smoke pair. Anyone who has one is welcome to take it as is.

How I reproduced it. Each run started the lib test binary from a freshly named copy, with eh::tests::thread_dumps gpu::agility gpu::bvh gpu::dxil::compact gpu::dxil::cfg --test-threads=4. On main 817eb1f 4 of 104 runs ended with the access violation (64 of those runs had the recorder below built in).

Where it faults. A temporary exception handler on the dumping thread recorded two of them, both in eh::unwind:

  1. At our own read of the module's UNWIND_INFO (*info.add(3)). The address was no longer readable, and the frame's address was not in any loaded module.
  2. Inside RtlLookupFunctionEntry in ntdll.dll, again for a frame that was not in any loaded module.

The stack is copied while the thread is stopped, but the thread then runs on, and the DLL its frame is in can be unloaded before the unwind data is read. This fits what every crash had in common: create_device_sets_dred_first was running beside it, with driver and system DLLs on its stack. XCL_THREAD_DUMP_AT, and the dump when a process ends with an error, use the same code.

Fix. unwind takes a reference to the module before it reads a frame's unwind data and gives it back afterwards. If the address is in image memory that is no longer a module, the walk of that thread ends there. Title images are VirtualAlloc memory, so their frames are kept.

main with the fix
the runs above 4 of 104 crashed 0 of 68 crashed
the new test, alone 14 of 100 crashed 0 of 100 crashed

cargo test --workspace --no-fail-fast -- --test-threads=4 with it, on c53e969: 3005 passed, 0 failed, 52 ignored, 25 SKIPPED (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps).

Limits. The new test is a race, so without the fix it fails only about 1 run in 7, by crashing the test binary. If the dump ends up holding the last reference, the DLL's unload runs on the dumping thread. Not tested: a dump in a real title. #2923 changes the same function for a different fault and does not hold the module, so the two would need merging by hand.

Patch against c53e969 (crates/xcl/src/eh.rs, +62)
diff --git a/crates/xcl/src/eh.rs b/crates/xcl/src/eh.rs
index 073e51d9..21eec7de 100644
--- a/crates/xcl/src/eh.rs
+++ b/crates/xcl/src/eh.rs
@@ -883,6 +883,34 @@ unsafe fn copy_stack(rsp: u64, buf: &mut [u8]) -> Option<usize> {
     (n >= 8 && ReadProcessMemory(cur, rsp as *const c_void, buf.as_mut_ptr() as *mut c_void, n, &mut read) != 0 && read == n).then_some(n)
 }
 
+/// A reference to the DLL an address is in, so that it stays loaded while its unwind data is read
+/// (the thread whose frame it is runs on, and may unload it: #3586).
+struct HeldModule(windows_sys::Win32::Foundation::HMODULE);
+
+impl HeldModule {
+    /// `None`: `addr` is in an image that is no module (any more). Memory that is no image (the
+    /// title's code) has nothing to hold.
+    unsafe fn of(addr: usize) -> Option<Self> {
+        use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleExW, GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS};
+        use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_IMAGE};
+        let mut h = std::ptr::null_mut();
+        if GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, addr as *const u16, &mut h) != 0 {
+            return Some(Self(h));
+        }
+        let mut mbi: MEMORY_BASIC_INFORMATION = std::mem::zeroed();
+        let known = VirtualQuery(addr as *const c_void, &mut mbi, std::mem::size_of::<MEMORY_BASIC_INFORMATION>()) != 0;
+        (known && mbi.State != windows_sys::Win32::System::Memory::MEM_FREE && mbi.Type != MEM_IMAGE).then_some(Self(std::ptr::null_mut()))
+    }
+}
+
+impl Drop for HeldModule {
+    fn drop(&mut self) {
+        if !self.0.is_null() {
+            unsafe { windows_sys::Win32::Foundation::FreeLibrary(self.0) };
+        }
+    }
+}
+
 /// The return addresses of the frames from `ctx` on (its RIP first), unwound with the
 /// functions' unwind data (registered for the title's images too); up to `n`. `stack` is the
 /// thread's stack from `ctx.Rsp` on, copied while it was stopped: the thread has gone on (or
@@ -908,6 +936,7 @@ unsafe fn unwind(ctx: &mut windows_sys::Win32::System::Diagnostics::Debug::CONTE
             break;
         }
         out.push(rip as usize);
+        let Some(_module) = HeldModule::of(rip as usize) else { break };
         let mut base = 0u64;
         let f = RtlLookupFunctionEntry(rip, &mut base, std::ptr::null_mut());
         if f.is_null() {
@@ -1305,6 +1334,39 @@ mod tests {
         busy.into_iter().for_each(|t| { t.join().unwrap(); });
     }
 
+    /// A frame in a DLL that is unloaded while the dump unwinds it (#3586): the module stays
+    /// loaded while its unwind data is read.
+    #[test]
+    fn a_frame_in_a_dll_being_unloaded_is_unwound_or_left_out() {
+        use std::sync::atomic::AtomicBool;
+        use windows_sys::Win32::Foundation::FreeLibrary;
+        use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA};
+        static STOP: AtomicBool = AtomicBool::new(false);
+        static RIP: AtomicUsize = AtomicUsize::new(0);
+        let churn = std::thread::spawn(|| unsafe {
+            while !STOP.load(Ordering::Relaxed) {
+                let h = LoadLibraryA(c"cabinet.dll".as_ptr() as *const u8);
+                assert!(!h.is_null());
+                RIP.store(GetProcAddress(h, c"FDICreate".as_ptr() as *const u8).unwrap() as usize + 1, Ordering::Relaxed);
+                FreeLibrary(h);
+            }
+        });
+        let stack = vec![0u8; 0x2000];
+        let end = std::time::Instant::now() + std::time::Duration::from_secs(1);
+        while std::time::Instant::now() < end {
+            let rip = RIP.load(Ordering::Relaxed);
+            if rip != 0 {
+                unsafe {
+                    let mut c: windows_sys::Win32::System::Diagnostics::Debug::CONTEXT = std::mem::zeroed();
+                    (c.Rip, c.Rsp) = (rip as u64, stack.as_ptr() as u64);
+                    unwind(&mut c, 2, &stack[..0x1000]);
+                }
+            }
+        }
+        STOP.store(true, Ordering::Relaxed);
+        churn.join().unwrap();
+    }
+
     #[test]
     fn the_process_ends_while_another_thread_holds_the_heap() {
         static HELD: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);

Hardware: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300, debug build.

AI use: Claude Code (Claude Opus 5.5) found the cause, wrote the patch and the test, made the runs and wrote this comment.

<!-- greyfinch-sync:ivall/xcl:comment:7355894281 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/3586#issuecomment-7355894281). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-12-02T19:16:13Z. --- Cause found: a DLL is unloaded while the dump is unwinding a frame inside it. The fix below works here. I cannot open it as a PR myself: it changes the runtime, and this machine has no retail title for the smoke pair. Anyone who has one is welcome to take it as is. **How I reproduced it.** Each run started the lib test binary from a freshly named copy, with `eh::tests::thread_dumps gpu::agility gpu::bvh gpu::dxil::compact gpu::dxil::cfg --test-threads=4`. On `main` `817eb1f` 4 of 104 runs ended with the access violation (64 of those runs had the recorder below built in). **Where it faults.** A temporary exception handler on the dumping thread recorded two of them, both in `eh::unwind`: 1. At our own read of the module's `UNWIND_INFO` (`*info.add(3)`). The address was no longer readable, and the frame's address was not in any loaded module. 2. Inside `RtlLookupFunctionEntry` in `ntdll.dll`, again for a frame that was not in any loaded module. The stack is copied while the thread is stopped, but the thread then runs on, and the DLL its frame is in can be unloaded before the unwind data is read. This fits what every crash had in common: `create_device_sets_dred_first` was running beside it, with driver and system DLLs on its stack. `XCL_THREAD_DUMP_AT`, and the dump when a process ends with an error, use the same code. **Fix.** `unwind` takes a reference to the module before it reads a frame's unwind data and gives it back afterwards. If the address is in image memory that is no longer a module, the walk of that thread ends there. Title images are `VirtualAlloc` memory, so their frames are kept. | | `main` | with the fix | |---|---|---| | the runs above | 4 of 104 crashed | 0 of 68 crashed | | the new test, alone | 14 of 100 crashed | 0 of 100 crashed | `cargo test --workspace --no-fail-fast -- --test-threads=4` with it, on `c53e969`: 3005 passed, 0 failed, 52 ignored, 25 SKIPPED (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps). **Limits.** The new test is a race, so without the fix it fails only about 1 run in 7, by crashing the test binary. If the dump ends up holding the last reference, the DLL's unload runs on the dumping thread. Not tested: a dump in a real title. #2923 changes the same function for a different fault and does not hold the module, so the two would need merging by hand. <details><summary>Patch against <code>c53e969</code> (crates/xcl/src/eh.rs, +62)</summary> ```diff diff --git a/crates/xcl/src/eh.rs b/crates/xcl/src/eh.rs index 073e51d9..21eec7de 100644 --- a/crates/xcl/src/eh.rs +++ b/crates/xcl/src/eh.rs @@ -883,6 +883,34 @@ unsafe fn copy_stack(rsp: u64, buf: &mut [u8]) -> Option<usize> { (n >= 8 && ReadProcessMemory(cur, rsp as *const c_void, buf.as_mut_ptr() as *mut c_void, n, &mut read) != 0 && read == n).then_some(n) } +/// A reference to the DLL an address is in, so that it stays loaded while its unwind data is read +/// (the thread whose frame it is runs on, and may unload it: #3586). +struct HeldModule(windows_sys::Win32::Foundation::HMODULE); + +impl HeldModule { + /// `None`: `addr` is in an image that is no module (any more). Memory that is no image (the + /// title's code) has nothing to hold. + unsafe fn of(addr: usize) -> Option<Self> { + use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleExW, GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS}; + use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_IMAGE}; + let mut h = std::ptr::null_mut(); + if GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, addr as *const u16, &mut h) != 0 { + return Some(Self(h)); + } + let mut mbi: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); + let known = VirtualQuery(addr as *const c_void, &mut mbi, std::mem::size_of::<MEMORY_BASIC_INFORMATION>()) != 0; + (known && mbi.State != windows_sys::Win32::System::Memory::MEM_FREE && mbi.Type != MEM_IMAGE).then_some(Self(std::ptr::null_mut())) + } +} + +impl Drop for HeldModule { + fn drop(&mut self) { + if !self.0.is_null() { + unsafe { windows_sys::Win32::Foundation::FreeLibrary(self.0) }; + } + } +} + /// The return addresses of the frames from `ctx` on (its RIP first), unwound with the /// functions' unwind data (registered for the title's images too); up to `n`. `stack` is the /// thread's stack from `ctx.Rsp` on, copied while it was stopped: the thread has gone on (or @@ -908,6 +936,7 @@ unsafe fn unwind(ctx: &mut windows_sys::Win32::System::Diagnostics::Debug::CONTE break; } out.push(rip as usize); + let Some(_module) = HeldModule::of(rip as usize) else { break }; let mut base = 0u64; let f = RtlLookupFunctionEntry(rip, &mut base, std::ptr::null_mut()); if f.is_null() { @@ -1305,6 +1334,39 @@ mod tests { busy.into_iter().for_each(|t| { t.join().unwrap(); }); } + /// A frame in a DLL that is unloaded while the dump unwinds it (#3586): the module stays + /// loaded while its unwind data is read. + #[test] + fn a_frame_in_a_dll_being_unloaded_is_unwound_or_left_out() { + use std::sync::atomic::AtomicBool; + use windows_sys::Win32::Foundation::FreeLibrary; + use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA}; + static STOP: AtomicBool = AtomicBool::new(false); + static RIP: AtomicUsize = AtomicUsize::new(0); + let churn = std::thread::spawn(|| unsafe { + while !STOP.load(Ordering::Relaxed) { + let h = LoadLibraryA(c"cabinet.dll".as_ptr() as *const u8); + assert!(!h.is_null()); + RIP.store(GetProcAddress(h, c"FDICreate".as_ptr() as *const u8).unwrap() as usize + 1, Ordering::Relaxed); + FreeLibrary(h); + } + }); + let stack = vec![0u8; 0x2000]; + let end = std::time::Instant::now() + std::time::Duration::from_secs(1); + while std::time::Instant::now() < end { + let rip = RIP.load(Ordering::Relaxed); + if rip != 0 { + unsafe { + let mut c: windows_sys::Win32::System::Diagnostics::Debug::CONTEXT = std::mem::zeroed(); + (c.Rip, c.Rsp) = (rip as u64, stack.as_ptr() as u64); + unwind(&mut c, 2, &stack[..0x1000]); + } + } + } + STOP.store(true, Ordering::Relaxed); + churn.join().unwrap(); + } + #[test] fn the_process_ends_while_another_thread_holds_the_heap() { static HELD: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0); ``` </details> Hardware: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300, debug build. AI use: Claude Code (Claude Opus 5.5) found the cause, wrote the patch and the test, made the runs and wrote this comment.
Sign in to join this conversation.
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
xcl-dev/xcl#3586
No description provided.