GPU: guard BND epilogues and saved-register bounds in profile unwind check #870

Open
github-bot wants to merge 1 commit from github-bot/gh-870 into main AGit
Owner

@⁠adritoyalser on GitHub · 2026-11-25 11:12 UTC · original
adritoyalser:fix/profile-unwind-bounds → main · +255 −20 · 2 files · 1 commits · open

Summary

Hardens the pre-unwind safety checks in crates/victory-gpu/src/profile.rs (frame_fits and epilogue_fits) against four edge cases that can cause RtlVirtualUnwind to read outside the copied stack buffer (STATUS_ACCESS_VIOLATION) or overflow/read past unwind code arrays:

  1. Intel MPX bnd (0xf2) prefix in epilogue_fits: Windows RtlVirtualUnwind skips a single 0xf2 prefix before matching epilogue terminators (c3, c2, eb, e9, f3 c3, ff 25, rex ff /4). Previously epilogue_fits did not skip 0xf2, so sequences like pop rsp; bnd ret (5c f2 c3) or add rsp, <large>; bnd ret were treated as non-epilogues (leaves == false) and passed to RtlVirtualUnwind, faulting inside ntdll.
  2. Saved-register offsets in frame_fits: UWOP_SAVE_NONVOL (4), UWOP_SAVE_NONVOL_FAR (5), UWOP_SAVE_XMM128 (8), and UWOP_SAVE_XMM128_FAR (9) now update save_end, and reach = frame_size.saturating_add(64).max(save_end) ensures reads at those offsets stay within limit.
  3. Unwind code bounds & UWOP_PUSH_MACHFRAME: Multi-slot codes (1, 4, 5, 7, 8, 9) verify i + 2 <= count / i + 3 <= count before reading extra slots, UWOP_ALLOC_LARGE rejects op_info > 1, and UWOP_PUSH_MACHFRAME (10) is rejected when followed by more unwind codes or a chained parent (since it replaces Rsp from the stack).
  4. Checked arithmetic: Replaced unchecked image + finish - context.Rip, reach += ..., rsp + reach <= limit, base + reach <= limit, and rsp + 8 * (pops + 1) <= end with checked/saturating arithmetic.

Verification

  • Added frame_fits_accounts_for_saved_register_offsets_and_truncated_codes and epilogue_fits_recognizes_bnd_prefixed_epilogues in crates/victory-gpu/src/profile/tests.rs (including live RtlVirtualUnwind verification for both UWOP_SAVE_NONVOL and 0xf2 c3).
  • cargo test -q -p victory-gpu --lib --features diagnostics --locked profile -- --test-threads=1: 11 passed, 0 failed, 1 ignored (profile_cost).
  • cargo clippy -q -p victory-gpu --lib --tests --features diagnostics --locked: 0 warnings in profile.rs / profile/tests.rs.
  • rustfmt --edition 2024 --check crates/victory-gpu/src/profile.rs crates/victory-gpu/src/profile/tests.rs: clean.
**@⁠adritoyalser** on GitHub · 2026-11-25 11:12 UTC · [original](https://github.com/fxceless/victory-fork/pull/870) `adritoyalser:fix/profile-unwind-bounds` → `main` · +255 −20 · 2 files · 1 commits · open ### Summary Hardens the pre-unwind safety checks in `crates/victory-gpu/src/profile.rs` (`frame_fits` and `epilogue_fits`) against four edge cases that can cause `RtlVirtualUnwind` to read outside the copied stack buffer (`STATUS_ACCESS_VIOLATION`) or overflow/read past unwind code arrays: 1. **Intel MPX `bnd` (`0xf2`) prefix in `epilogue_fits`**: Windows `RtlVirtualUnwind` skips a single `0xf2` prefix before matching epilogue terminators (`c3`, `c2`, `eb`, `e9`, `f3 c3`, `ff 25`, `rex ff /4`). Previously `epilogue_fits` did not skip `0xf2`, so sequences like `pop rsp; bnd ret` (`5c f2 c3`) or `add rsp, <large>; bnd ret` were treated as non-epilogues (`leaves == false`) and passed to `RtlVirtualUnwind`, faulting inside `ntdll`. 2. **Saved-register offsets in `frame_fits`**: `UWOP_SAVE_NONVOL` (`4`), `UWOP_SAVE_NONVOL_FAR` (`5`), `UWOP_SAVE_XMM128` (`8`), and `UWOP_SAVE_XMM128_FAR` (`9`) now update `save_end`, and `reach = frame_size.saturating_add(64).max(save_end)` ensures reads at those offsets stay within `limit`. 3. **Unwind code bounds & `UWOP_PUSH_MACHFRAME`**: Multi-slot codes (`1`, `4`, `5`, `7`, `8`, `9`) verify `i + 2 <= count` / `i + 3 <= count` before reading extra slots, `UWOP_ALLOC_LARGE` rejects `op_info > 1`, and `UWOP_PUSH_MACHFRAME` (`10`) is rejected when followed by more unwind codes or a chained parent (since it replaces `Rsp` from the stack). 4. **Checked arithmetic**: Replaced unchecked `image + finish - context.Rip`, `reach += ...`, `rsp + reach <= limit`, `base + reach <= limit`, and `rsp + 8 * (pops + 1) <= end` with checked/saturating arithmetic. ### Verification - Added `frame_fits_accounts_for_saved_register_offsets_and_truncated_codes` and `epilogue_fits_recognizes_bnd_prefixed_epilogues` in `crates/victory-gpu/src/profile/tests.rs` (including live `RtlVirtualUnwind` verification for both `UWOP_SAVE_NONVOL` and `0xf2 c3`). - `cargo test -q -p victory-gpu --lib --features diagnostics --locked profile -- --test-threads=1`: **11 passed, 0 failed, 1 ignored** (`profile_cost`). - `cargo clippy -q -p victory-gpu --lib --tests --features diagnostics --locked`: 0 warnings in `profile.rs` / `profile/tests.rs`. - `rustfmt --edition 2024 --check crates/victory-gpu/src/profile.rs crates/victory-gpu/src/profile/tests.rs`: clean.
Before calling `RtlVirtualUnwind` on a copied stack, `profile::frame_fits`
and `profile::epilogue_fits` verify that the step cannot read outside the
stack copy or trailing zero slack. Four gaps let malformed or BND-prefixed
code slip through:

- `epilogue_fits` did not skip a single `0xf2` (`bnd`) prefix before
  epilogue terminators (`ret`, `ret imm16`, `jmp`, `rep ret`, `rex jmp`),
  which Windows `RtlVirtualUnwind` accepts; sequences such as
  `pop rsp; bnd ret` (`5c f2 c3`) or `add rsp, <large>; bnd ret` passed
  the guard and caused `STATUS_ACCESS_VIOLATION` inside `RtlVirtualUnwind`.
- `frame_fits` ignored the stack offsets of `UWOP_SAVE_NONVOL` (`4`),
  `UWOP_SAVE_NONVOL_FAR` (`5`), `UWOP_SAVE_XMM128` (`8`), and
  `UWOP_SAVE_XMM128_FAR` (`9`), underestimating `reach` when saved
  registers lie past `frame_size + 64`.
- Multi-slot unwind codes (`op` 1, 4, 5, 7, 8, 9) read `slot(i + 1)` /
  `slot(i + 2)` without checking `count` bounds, `UWOP_ALLOC_LARGE` did
  not reject `op_info > 1`, and `UWOP_PUSH_MACHFRAME` (`10`) allowed
  trailing codes or chained parents to read through the stack-loaded `Rsp`.
- `image + finish - context.Rip`, `reach` accumulation, and
  `rsp + reach <= limit` / `rsp + 8 * (pops + 1) <= end` used unchecked
  arithmetic.

Track `save_end` alongside `frame_size`, bounds-check multi-slot codes
and arithmetic, reject trailing/chained codes after `UWOP_PUSH_MACHFRAME`,
skip an optional `0xf2` prefix in `epilogue_fits`, and add unit tests
covering both functions (including live `RtlVirtualUnwind` checks).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:52:52 +00:00
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-25 12:23 UTC · original

Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head d36d089 into main f85101b in a throwaway tree (no conflict). cargo clippy --workspace --all-targets --locked -- -D warnings fails with exactly the errors main itself has on this machine (placed_filler_twins, depth_written, a sort; PR #841 fixes them), also with --all-features; this PR adds none. cargo test --workspace --locked: run 1: 555 passed, 0 failed, 11 ignored; run 2: stopped at indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields (the one that fails on main itself most runs here (PR #849 and #847 fix its assertion)) (main alone: 555 passed, 0 failed, 11 ignored). The title was not run, so this says nothing about in-game effect. No game data.

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-25 12:23 UTC · [original](https://github.com/fxceless/victory-fork/pull/870#issuecomment-7162304733) Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head `d36d089` into `main` `f85101b` in a throwaway tree (no conflict). `cargo clippy --workspace --all-targets --locked -- -D warnings` fails with exactly the errors `main` itself has on this machine (`placed_filler_twins`, `depth_written`, a sort; PR #841 fixes them), also with `--all-features`; this PR adds none. `cargo test --workspace --locked`: run 1: 555 passed, 0 failed, 11 ignored; run 2: stopped at `indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields` (the one that fails on `main` itself most runs here (PR #849 and #847 fix its assertion)) (`main` alone: 555 passed, 0 failed, 11 ignored). The title was not run, so this says nothing about in-game effect. No game data. AI-assisted (Claude Code); no game data.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/870/head:github-bot/gh-870'
git switch 'github-bot/gh-870'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-870'
git switch 'github-bot/gh-870'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-870'
git switch 'github-bot/gh-870'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-870'
git switch 'main'
git merge --squash 'github-bot/gh-870'
git switch 'main'
git merge --ff-only 'github-bot/gh-870'
git switch 'main'
git merge 'github-bot/gh-870'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.