Stop the unwind where RtlVirtualUnwind would read outside the stack #140

Closed
github-bot wants to merge 1 commit from github-bot/gh-140 into main AGit
Owner

@⁠Garu667 on GitHub · 2026-11-27 03:58 UTC · original
Garu667:fix/profiler-unwind-reach → main · +209 −0 · 1 files · 1 commits · closed without merging

What happened: a VICTORY_PROFILE run (RTX 3080, Series S, open world, standing) ended about a minute into sampling with an access violation in the profiler's own thread. It faulted inside RtlVirtualUnwind, called from crash::unwind_into while walking another thread, on a read of reserved, never-committed memory. The title itself was fine.

Why: unwind_into checked only that rsp lies in the sampled thread's stack. But RtlVirtualUnwind reads the whole frame up to the return address. Once the prolog has set the frame register, it reads from that register instead, and in another thread the register can hold anything. So a frame near the top of a stack, or a stray rbp, sends the read out of the stack.

Fix (crates/victory-loader/src/crash.rs only):

  • Before each step, read the function's UNWIND_INFO: pushes, small and large allocations, machine frames, the frame register (only once its SET_FPREG has run), and chained entries.
  • Work out the frame's base and the bytes the unwind will read from there.
  • Stop the walk if that range leaves low..high. Unknown versions or malformed data also stop it.
  • The crash report shares this walk, so it gets the same bound.

Tests:

  • Three new unit tests on hand-built unwind infos: plain frame, large allocation, frame register before and after the prolog sets it, chained entry, bad version, codes past the data.
  • The existing unwinds_like_the_system_walk still passes, so real stacks are not cut short.
  • cargo test -p victory-loader: 59 pass. Clippy with -D warnings and cargo xtask policy are clean.

In the game: the same profile run with this change went 7 minutes without a fault and wrote its profile.

🤖 Generated with Claude Code

**@⁠Garu667** on GitHub · 2026-11-27 03:58 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/140) `Garu667:fix/profiler-unwind-reach` → `main` · +209 −0 · 1 files · 1 commits · closed without merging **What happened:** a `VICTORY_PROFILE` run (RTX 3080, Series S, open world, standing) ended about a minute into sampling with an access violation in the profiler's own thread. It faulted inside `RtlVirtualUnwind`, called from `crash::unwind_into` while walking another thread, on a read of reserved, never-committed memory. The title itself was fine. **Why:** `unwind_into` checked only that `rsp` lies in the sampled thread's stack. But `RtlVirtualUnwind` reads the whole frame up to the return address. Once the prolog has set the frame register, it reads from that register instead, and in another thread the register can hold anything. So a frame near the top of a stack, or a stray `rbp`, sends the read out of the stack. **Fix (`crates/victory-loader/src/crash.rs` only):** - Before each step, read the function's `UNWIND_INFO`: pushes, small and large allocations, machine frames, the frame register (only once its `SET_FPREG` has run), and chained entries. - Work out the frame's base and the bytes the unwind will read from there. - Stop the walk if that range leaves `low..high`. Unknown versions or malformed data also stop it. - The crash report shares this walk, so it gets the same bound. **Tests:** - Three new unit tests on hand-built unwind infos: plain frame, large allocation, frame register before and after the prolog sets it, chained entry, bad version, codes past the data. - The existing `unwinds_like_the_system_walk` still passes, so real stacks are not cut short. - `cargo test -p victory-loader`: 59 pass. Clippy with `-D warnings` and `cargo xtask policy` are clean. **In the game:** the same profile run with this change went 7 minutes without a fault and wrote its profile. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The sampling profiler (VICTORY_PROFILE) ended a run 3 minutes in with an
access violation inside RtlVirtualUnwind, called from crash::unwind_into
for another thread: "read 0x7b38d01f28: reserved, never committed". The
walk checked only that rsp lay in the thread's stack, but the unwind
reads the whole frame up to the return address, and from the frame
register once the prolog has set it, which in another thread can hold
anything.

Each step now reads the function's unwind info (pushes, allocations,
the frame register, chained entries) and stops when the bytes the
unwind will read would leave the stack. The crash report shares the
walk and gets the same bound.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:59:01 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 04:07 UTC · original

Thanks for tracking this down to the profiler thread, @⁠Garu667. I reproduced all 59 loader tests passing at fa2f5fe. The seven-minute profiling run gives us a useful real-world check alongside the tests.

The next revision needs to keep the suspended-thread walk allocation-free. stacks.rs calls unwind_into between SuspendThread and ResumeThread, so the to_vec() calls in frame_reach and reach_of can wait on the allocator while the suspended thread holds it. A fixed scratch buffer and bounded reads would preserve that existing contract.

I’d also tighten the stack bounds around saved-register restores. The SAVE_NONVOL and SAVE_XMM128 cases currently advance the opcode cursor without accounting for the memory those restores read. Please include the saved offset and register width in the range check, and keep unwind-record reads bounded before handing the frame to Windows. A positive case and a boundary case for register restores would help cover this. Microsoft’s x64 unwind reference

**@⁠TaynanGT** on GitHub · 2026-11-27 04:07 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/140#issuecomment-7209542481) Thanks for tracking this down to the profiler thread, @⁠Garu667. I reproduced all 59 loader tests passing at `fa2f5fe`. The seven-minute profiling run gives us a useful real-world check alongside the tests. The next revision needs to keep the suspended-thread walk allocation-free. `stacks.rs` calls `unwind_into` between `SuspendThread` and `ResumeThread`, so the `to_vec()` calls in `frame_reach` and `reach_of` can wait on the allocator while the suspended thread holds it. A fixed scratch buffer and bounded reads would preserve that existing contract. I’d also tighten the stack bounds around saved-register restores. The `SAVE_NONVOL` and `SAVE_XMM128` cases currently advance the opcode cursor without accounting for the memory those restores read. Please include the saved offset and register width in the range check, and keep unwind-record reads bounded before handing the frame to Windows. A positive case and a boundary case for register restores would help cover this. [Microsoft’s x64 unwind reference](https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64?view=msvc-170)
github-bot closed this pull request 2026-11-27 04:26:49 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 04:22 UTC · original

Thanks for the profiling investigation, @⁠Garu667. The bounded walk is now on main through #151, with your authorship preserved. The follow-up keeps the sampled-thread path allocation-free and checks saved-register reads and guard pages. All 65 loader tests with diagnostics and the Windows workflow passed. A repeat of your longer sampling run on current main would give us the matching gameplay confirmation.

**@⁠TaynanGT** on GitHub · 2026-11-27 04:22 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/140#issuecomment-7209752671) Thanks for the profiling investigation, @⁠Garu667. The bounded walk is now on `main` through [#151](https://git.gtahub.st/github/TaynanGT--Vice-Tay/pulls/151), with your authorship preserved. The follow-up keeps the sampled-thread path allocation-free and checks saved-register reads and guard pages. All 65 loader tests with diagnostics and the Windows workflow passed. A repeat of your longer sampling run on current `main` would give us the matching gameplay confirmation.
Author
Owner

@⁠Garu667 on GitHub · 2026-11-27 04:24 UTC · original

Thanks for the careful review. Both points are fixed in eeb4419:

  • No allocation on the suspended-thread path. reach_of now borrows the unwind info in place: it takes slices, and chained infos come back as slices too, so there is no to_vec left. The header, codes and chained entry are read only after checking that they lie inside the image. The image size is SizeOfImage from its PE headers, and an image without valid headers stops the walk.
  • Saved-register restores are in the range check:
    • SAVE_NONVOL uses offset × 8 and 8 bytes; the _FAR form uses its unscaled offset.
    • SAVE_XMM128 uses offset × 16 and 16 bytes, with the same _FAR handling. Version 1's 6/7 are treated as XMM saves, and version 2's 6 as the epilog mark.
    • A machine frame counts at the current stack position, 40 bytes, or 48 with an error code.
    • A chained info's offsets start from where the infos before it left the stack pointer.
    • The bound is now max(frame up to the return address, farthest restore).
  • New test reach_covers_the_saved_registers_it_restores: a save inside the frame, one exactly at its boundary, one past it (which widens the range), an XMM save, and the far form.

cargo test -p victory-loader: 60 pass. clippy -D warnings and cargo xtask policy are clean. I'll repeat the in-game profile run with this revision and post the result.

🤖 Generated with Claude Code

**@⁠Garu667** on GitHub · 2026-11-27 04:24 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/140#issuecomment-7209766839) Thanks for the careful review. Both points are fixed in `eeb4419`: - **No allocation on the suspended-thread path.** `reach_of` now borrows the unwind info in place: it takes slices, and chained infos come back as slices too, so there is no `to_vec` left. The header, codes and chained entry are read only after checking that they lie inside the image. The image size is `SizeOfImage` from its PE headers, and an image without valid headers stops the walk. - **Saved-register restores are in the range check:** - `SAVE_NONVOL` uses offset × 8 and 8 bytes; the `_FAR` form uses its unscaled offset. - `SAVE_XMM128` uses offset × 16 and 16 bytes, with the same `_FAR` handling. Version 1's 6/7 are treated as XMM saves, and version 2's 6 as the epilog mark. - A machine frame counts at the current stack position, 40 bytes, or 48 with an error code. - A chained info's offsets start from where the infos before it left the stack pointer. - The bound is now max(frame up to the return address, farthest restore). - **New test `reach_covers_the_saved_registers_it_restores`:** a save inside the frame, one exactly at its boundary, one past it (which widens the range), an XMM save, and the far form. `cargo test -p victory-loader`: 60 pass. `clippy -D warnings` and `cargo xtask policy` are clean. I'll repeat the in-game profile run with this revision and post the result. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 04:25 UTC · original

Thanks for following up with the saved-register cases, @⁠Garu667. The merged build in #151 uses fixed buffers and checks the full restore width plus page readability. Your longer profile run would be most useful on the merged revision 02b693e, so it can validate the implementation people will use. Please include the elapsed sampling time and whether it wrote the profile.

**@⁠TaynanGT** on GitHub · 2026-11-27 04:25 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/140#issuecomment-7209789016) Thanks for following up with the saved-register cases, @⁠Garu667. The merged build in [#151](https://git.gtahub.st/github/TaynanGT--Vice-Tay/pulls/151) uses fixed buffers and checks the full restore width plus page readability. Your longer profile run would be most useful on the merged revision `02b693e`, so it can validate the implementation people will use. Please include the elapsed sampling time and whether it wrote the profile.
Author
Owner

@⁠Garu667 on GitHub · 2026-11-27 04:37 UTC · original

In-game check of eeb4419: same profile settings (VICTORY_PROFILE=120,300,5,<file>, RTX 3080, Series S, open world, standing). The run lasted about 9 minutes with no fault and wrote a 43 MB profile.

The new bound doesn't cut stacks short: the sample-weighted mean depth is 12.6 frames, against 12.0 with fa2f5fe.

🤖 Generated with Claude Code

**@⁠Garu667** on GitHub · 2026-11-27 04:37 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/140#issuecomment-7209957311) In-game check of `eeb4419`: same profile settings (`VICTORY_PROFILE=120,300,5,<file>`, RTX 3080, Series S, open world, standing). The run lasted about 9 minutes with no fault and wrote a 43 MB profile. The new bound doesn't cut stacks short: the sample-weighted mean depth is 12.6 frames, against 12.0 with `fa2f5fe`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 04:42 UTC · original

Thanks for the longer profile run, @⁠Garu667. About nine minutes without a fault, a 43 MB profile and a mean depth of 12.6 frames are the result for eeb4419. The remaining check is the same sampling run on current main with the #151 implementation. The elapsed time and whether it wrote a profile are enough.

**@⁠TaynanGT** on GitHub · 2026-11-27 04:42 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/140#issuecomment-7210028946) Thanks for the longer profile run, @⁠Garu667. About nine minutes without a fault, a 43 MB profile and a mean depth of 12.6 frames are the result for `eeb4419`. The remaining check is the same sampling run on current `main` with the #151 implementation. The elapsed time and whether it wrote a profile are enough.
Author
Owner

@⁠Garu667 on GitHub · 2026-11-27 06:23 UTC · original

Check of the #151 implementation, as you asked: main f944962, same settings (VICTORY_PROFILE=120,300,5,<file>, RTX 3080, Series S, the cell, standing).

  • The run lasted about 6 min 20 s (world at 176 s, closed at 380 s) with no fault.
  • It wrote the full 180 s window: a 26 MB profile with 723k samples.
  • The sample-weighted mean depth is 11.5 frames (the cell, against 12.6 in the open world for eeb4419).

🤖 Generated with Claude Code

**@⁠Garu667** on GitHub · 2026-11-27 06:23 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/140#issuecomment-7211482791) Check of the #151 implementation, as you asked: main `f944962`, same settings (`VICTORY_PROFILE=120,300,5,<file>`, RTX 3080, Series S, the cell, standing). - The run lasted about 6 min 20 s (world at 176 s, closed at 380 s) with no fault. - It wrote the full 180 s window: a 26 MB profile with 723k samples. - The sample-weighted mean depth is 11.5 frames (the cell, against 12.6 in the open world for `eeb4419`). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 06:40 UTC · original

@⁠Garu667 Thanks for checking the #151 implementation on f944962. The run completed the full 180 s sampling window and wrote 723k samples without an unwind fault. That confirms the stack-walk fix for the cell run on the RTX 3080. I’ll keep its 11.5-frame mean with the cell result, since the 12.6-frame run sampled the open world.

**@⁠TaynanGT** on GitHub · 2026-11-27 06:40 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/140#issuecomment-7211740472) @⁠Garu667 Thanks for checking the #151 implementation on `f944962`. The run completed the full 180 s sampling window and wrote 723k samples without an unwind fault. That confirms the stack-walk fix for the cell run on the RTX 3080. I’ll keep its 11.5-frame mean with the cell result, since the 12.6-frame run sampled the open world.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.