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

Open
adritoyalser wants to merge 1 commit from upstream-pr/870 into main
adritoyalser commented 2026-11-25 11:12:19 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by adritoyalser on 2026-11-25T11:12:19Z.


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.
<!-- greyfinch-sync:fxceless/victory-fork:issue:6603178084 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/870). Originally posted by [adritoyalser](https://github.com/adritoyalser) on 2026-11-25T11:12:19Z. --- ### 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.
MamaLeVrai commented 2026-11-25 13:06:00 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by MamaLeVrai on 2026-11-25T12:23:18Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7162304733 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/870#issuecomment-7162304733). Originally posted by [MamaLeVrai](https://github.com/MamaLeVrai) on 2026-11-25T12:23:18Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/870:upstream-pr/870'
git switch 'upstream-pr/870'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/870'
git switch 'upstream-pr/870'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/870'
git switch 'upstream-pr/870'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/870'
git switch 'main'
git merge --squash 'upstream-pr/870'
git switch 'main'
git merge --ff-only 'upstream-pr/870'
git switch 'main'
git merge 'upstream-pr/870'
git push origin 'main'
Sign in to join this conversation.
No reviewers
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
fxceless/victory-fork!869
No description provided.