Skip to content

xcl run: thread dumps unwind a copy of each stopped thread's stack - #253

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/thread-dump-stack-copy
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/thread-dump-stack-copy

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Refs #106 (the crash in eh::dump_threads, with XCL_THREAD_DUMP_AT: an access violation in RtlVirtualUnwind2 reading 0xf8).

dump_threads resumed each thread right after GetThreadContext, then unwound its stack from live memory. By that time the thread has either gone on, so the saved registers and return addresses are overwritten, or ended, so its stack is freed. A stale frame register sends RtlVirtualUnwind to a near-null address: the report's read. The fault is inside RtlVirtualUnwind, so no check after the call can catch it. Now, while the thread is stopped, the dump also copies its stack, and the unwinding reads the copy.

Seen in a run: a long run of a retail title on a local diagnostic build (main's dump_threads with other local changes; this PR was not in it) crashed in ntdll's unwinder (ntdll+0x20047, a read at 0xfb0, between RtlVirtualUnwind2 and RtlVirtualUnwind) 75 ms into a thread dump the stalled-heartbeat check took, on the thread taking it. Its registers point into an Opus-decoding thread (MSOpusDecoder.dll), which dump_threads had already resumed when it unwound its stack. Two earlier long runs of builds with the same dump_threads took 11 dumps each without a crash.

What is in it

Tested

  • RTX 5070 Ti, NVIDIA 610.74, Windows 11 Pro 26200; MSVC stable, debug.
  • cargo test --workspace -- --test-threads=4 on f67e35a (this PR, rebased onto main 930e8b4 without conflicts; the same patch as 4d05d43): 752 passed, 0 failed, 41 ignored (the xcl library: 708 passed, 21 ignored), eh::tests::thread_dumps_while_threads_run_and_exit among them. SKIPPED GPU tests: 2, both no wave64 (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware).
  • The earlier head 4d05d43, on main dd2ae9a: 691 passed, 0 failed, 41 ignored, the same two SKIPPED.
  • cargo test -p xcl --lib -- --exact eh::tests::thread_dumps_while_threads_run_and_exit on 4d05d43, 3 runs: 3 of 3 passed (4.1–4.3 s).
  • The new test, --exact, CPU only (no device), under a debugger that passes first-chance exceptions on and catches the second-chance one with its thread and stack. 20 runs each, interleaved:
    • main 60565c4 with the test added: 15 of 20 crashed, all from eh::unwind ← dump_threads. 4 were at ntdll+0x1ff2a in RtlVirtualUnwind2, reading a near-null address: the instruction in the report. 11 were at ntdll+0x1ff9f, reading other threads' stacks.
    • this change (built on 60565c4; the same code as 12328ac): 20 of 20 passed.
  • The dumps still show whole stacks, down to RtlUserThreadStart.
  • Independent run by @jleybourne (xcl run: thread dumps unwind a copy of each stopped thread's stack #253 (comment)): RTX 4070 Ti (NVIDIA 610.88), i7-10700KF, Windows 11 26300, Rust 1.100.0, debug, 12328ac. scripts/test.ps1: 652 passed, 0 failed, 40 ignored; SKIPPED only the 2 no wave64 tests; the e2e test title test passed.
  • The report's case, a retail title with XCL_THREAD_DUMP_AT, run by @Natakout (xcl run: thread dumps unwind a copy of each stopped thread's stack #253 (comment)): RTX 3060 12 GB (NVIDIA 621.14), Windows 11 25H2, release, 4d05d43 (the same patch as this head) and main 47efa2e, dumps at 140, 170, 200, 230, 250 and 260 s. On main the run dies at 200.13 s on the dumping thread, during the third dump (0xc0000005 in ntdll.dll+0x79a67, a read at 0x376). With this PR all six dumps are written (168–170 threads each).
  • The same case run by @Anon64 (xcl run: thread dumps unwind a copy of each stopped thread's stack #253 (comment)): RTX 3060 12 GB (NVIDIA 581.80), Windows 11 26200, release, this head f67e35a and main 930e8b4, a dump every 5 s from 70 s. On main the second dump (75.17 s) faults on the dumping thread in ntdll.dll+0x20191 (a read at 0xffffffffffffffff) and the process hangs. With this PR all 9 dumps are written (70–105 s, 173–176 threads each). Their smoke pair of the same two commits: 1571 / 1585 frames (99 %), peak commit 22979 / 20665 MB (111 %), no crash or device removal.

Regression run

Our own runs, one after the other on the machine under Tested (RTX 5070 Ti), release builds, no uncommitted changes: main at 930e8b4 (this PR's base), then this PR's head. 98 % of main's frames, 99 % of its peak commit.

main:

xcl-smoke v1
commit: 930e8b49c1c2ec683d22c724a398b39111cd0e56
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1317 (PresentX 1318)
presents per 2 s (last 3): 39 51 10
device removals: 0
crashes: 0
peak commit: 28215 MB

PR:

xcl-smoke v1
commit: f67e35aeb09291534ff47a627e42c1f6a91c67b3
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1296 (PresentX 1296)
presents per 2 s (last 3): 43 46 7
device removals: 0
crashes: 0
peak commit: 27964 MB

Not tested

  • AMD GPUs; a dump taken at the moment of a crash.
  • Wine.
  • Function tables registered with RtlInstallFunctionTableCallback.
  • One case is still open: a stack deeper than the 256 KB copy, with a frame of more than 64 KB at the copy's end. RtlVirtualUnwind reads that frame's return address before the bound check, so it could read past the buffer and its 64 KB slack.
  • macOS / Linux: not built.

AI use

  • Claude Code, Claude Opus 5.5: reproduced the crash with a test, wrote the change, the test and this description; the runs above are its own.

🤖 Generated with Claude Code

@Kalmarv
Kalmarv force-pushed the fix/thread-dump-stack-copy branch from 5c77a39 to 12328ac Compare November 23, 2026 22:05
@Kalmarv Kalmarv changed the title Diagnostics: thread dumps unwind a copy of the stopped thread's stack xcl run: thread dumps unwind a copy of each stopped thread's stack Nov 23, 2026
@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Regression run wanted. We have no title on our machine. If you can run scripts/smoke.ps1 -Game <your title folder> twice, once built from main (9ab7d76) and once from this PR's head (12328ac), please paste both xcl-smoke blocks here. I'll copy them into the description under Regression run, with your name and hardware.

🤖 Claude Opus 5.5

@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of 4d05d437d8c9d47a3f0f39fd9a17fada072f4536 on AMD RDNA4. I merged it onto current main 64b3f35 locally (clean merge) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace + cargo test --workspace -- --test-threads=4 on the merge: 702 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 661 passed, 0 failed, 21 ignored
  • 0 SKIPPED; the eh tests pass (no thread dump was triggered here).

Review: Copying each stopped thread's stack (from rsp to the end of its committed region, at most 256 KiB) before resuming it removes the read of a live stack that crashed in #106. A question I could not settle from the diff: frames that use RBP as a frame pointer, and any pointer the unwinder follows from the context, still hold addresses of the original stack, so RtlVirtualUnwind may read the live stack there rather than the copy. If the unwind only ever reads relative to the copied RSP that is fine; otherwise the context registers that point into the stack would need the same rebase.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the PR's head on its old base was not run, only its merge onto main.

Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@MesshM Thanks for the RDNA4 run. On the question: the unwind already moves those registers to the copy (eh.rs, unwind, 4d05d43):

  • Before the first frame, each of the 16 general registers in the context whose value falls in the copied range of the original stack (from the stopped thread's RSP up the copy's length) is moved by the same offset into the copy. RSP and RBP are among them.
  • After each RtlVirtualUnwind, the same is done again. A frame register or a saved RBP that the unwinder restored from the copy still holds an address of the original stack, and it's moved into the copy before the next frame.
  • Before unwinding a frame with a frame register (UNWIND_INFO byte 3, low nibble), the dump checks that this register points into the copy. If it doesn't, for example when the frame lies beyond the 256 KiB copied, the dump stops there and doesn't read the live stack. It also stops when RSP leaves the copy.

So the unwinder reads only the copy, plus a 64 KiB margin after it in the same buffer, never the stopped thread's live stack. What it doesn't fix up is a pointer to the stack that sits in memory outside the stack: an unwind of x64 code doesn't follow one.

🤖 Claude Opus 5.5

dump_threads resumed each thread right after GetThreadContext and only
then unwound its stack, from the live memory: by then the thread had
gone on (its saved registers and return addresses overwritten) or ended
(its stack freed). RtlVirtualUnwind followed a stale frame register to
a near-null address, or read a freed stack: an access violation in the
dump thread (ivall#106).

While the thread is stopped, the dump now also copies its stack (from
RSP, up to 256 KB, with system calls only); the unwinding reads the
copy, with addresses into the stack moved to it, and stops where a frame
would leave it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Kalmarv
Kalmarv force-pushed the fix/thread-dump-stack-copy branch from 4d05d43 to f67e35a Compare November 24, 2026 15:20
@Natakout

Copy link
Copy Markdown

Test report: thread dumps no longer crash the run (RTX 3060)

Commit tested: this PR's head 4d05d43, against main 47efa2e.

Tested

Not tested

  • AMD GPUs.
  • Dumps taken at the very moment of a crash.
  • The PR is based on f4ce27c, so the title's shaders were recompiled for this run. Shader cache state therefore differs from the main run; it does not affect the dump path.

AI use
Claude (claude-opus-5-5, through Hermes) ran the builds and the title runs on the hardware above over SSH, read the logs and drafted this report.

@Anon64

Anon64 commented Nov 24, 2026

Copy link
Copy Markdown

Regression run, as asked: this PR's head f67e35a and main 930e8b4 (its merge base). RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, XCL_NETWORK=0, A pressed every 4 s from 20 s, scripts/smoke.ps1 on a retail title for 60 s.

main 930e8b4:

xcl-smoke v1
commit: 930e8b49c1c2ec683d22c724a398b39111cd0e56
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1585 (PresentX 1585)
presents per 2 s (last 3): 60 46 45
device removals: 0
crashes: 0
peak commit: 20665 MB

this PR:

xcl-smoke v1
commit: f67e35aeb09291534ff47a627e42c1f6a91c67b3
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1571 (PresentX 1571)
presents per 2 s (last 3): 43 52 22
device removals: 0
crashes: 0
peak commit: 22979 MB

No device removals or crashes; 99 % of main's frames, 111 % of its peak commit.

The crash this PR fixes, reproduced on main 930e8b4: a 150 s world-loading run with XCL_THREAD_DUMP_AT every 5 s from 70 s took the first dump (143 threads) and then, at the second (75.17 s), faulted on the dumping thread in ntdll.dll+0x20191, a read at 0xffffffffffffffff. The process did not end; it hung until I stopped it.

The same run on this PR's head: all 9 dumps were written (70 s to 105 s, 173–176 threads each), no fault in the unwinder. The run ended at 105 s in the title's own hang watchdog during world loading, which main hits on this card without dumps too (104–116 s in earlier runs).

Not tested: the whole suite; AMD.

Written with Claude Code (Claude Opus 5.5) from runs on this machine.

@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@Natakout, @Anon64 thank you. These are the first runs of the case from the report: a title with XCL_THREAD_DUMP_AT, crashing on main inside the unwinder and writing every dump with this PR. Both runs are now in the description under Tested, Anon64's smoke pair included, and "a title with XCL_THREAD_DUMP_AT" is no longer under Not tested.

AI use: Claude Code (Claude Opus 5.5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants