Loader: guard null C++ exception pointers in crash::thrown and Rsp overflow/alignment in unwind_into #2552

Open
reeledMarek wants to merge 1 commit from upstream-pr/2554 into main
reeledMarek commented 2026-11-25 15:40:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T15:40:15Z.


Problem

  1. In crash::thrown, MSVC C++ rethrows (throw;) pass p[1] == 0 and p[2] == 0, and malformed exception metadata can have zero RVAs or count == 0. thrown unconditionally dereferenced p[2] + 12, name(0), and p[1] + 8, causing a secondary access violation inside the unhandled crash reporter.
  2. In crash::unwind_into, rsp + 8 > high overflowed if a crashing thread corrupted Rsp near u64::MAX, and (rsp as *const u64).read() caused aligned-read UB if Rsp was misaligned.

Fix

  • Validate p.len() >= 4, non-null p[2]/p[3], non-zero RVAs, bounded non-zero catchable type count, non-null p[1] for std::exception, and read_unaligned in thrown.
  • Use rsp.checked_add(8) and read_unaligned in unwind_into.

Testing

  • Added thrown_and_unwind_handle_rethrow_and_corrupted_rsp in crates/victory-loader/src/crash.rs.
  • Verified cargo test -p xtask --locked passes asset policy.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6608189162 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2554). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T15:40:15Z. --- ### Problem 1. In `crash::thrown`, MSVC C++ rethrows (`throw;`) pass `p[1] == 0` and `p[2] == 0`, and malformed exception metadata can have zero RVAs or `count == 0`. `thrown` unconditionally dereferenced `p[2] + 12`, `name(0)`, and `p[1] + 8`, causing a secondary access violation inside the unhandled crash reporter. 2. In `crash::unwind_into`, `rsp + 8 > high` overflowed if a crashing thread corrupted `Rsp` near `u64::MAX`, and `(rsp as *const u64).read()` caused aligned-read UB if `Rsp` was misaligned. ### Fix - Validate `p.len() >= 4`, non-null `p[2]`/`p[3]`, non-zero RVAs, bounded non-zero catchable type count, non-null `p[1]` for `std::exception`, and `read_unaligned` in `thrown`. - Use `rsp.checked_add(8)` and `read_unaligned` in `unwind_into`. ### Testing - Added `thrown_and_unwind_handle_rethrow_and_corrupted_rsp` in `crates/victory-loader/src/crash.rs`. - Verified `cargo test -p xtask --locked` passes asset policy.
Marvisak commented 2026-11-25 19:06:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T18:37:25Z.


Automated triage for the maintainers.

Hardens the crash reporter in crates/victory-loader/src/crash.rs. thrown now returns "unknown" for a C++ rethrow (throw; raises the MSVC exception with null object/ThrowInfo pointers, and main read p[2] + 12 and faulted inside the reporter). It also returns "unknown" for zero RVAs or an empty type list, and skips the std::exception message when the object pointer is null. unwind_into now uses checked_add for rsp + 8; on main a corrupted Rsp near u64::MAX wrapped past the bounds check. It also reads the return address with read_unaligned, since a misaligned read() is UB and panics in debug builds. I checked the call site (report, which only requires NumberParameters >= 4 and the magic) and the loop exit on Rip == 0. The new test's expectations ([0x1000], [0x2000]) match that code. Reporting only, with no change to emulation behaviour.

CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7171164680 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2554#issuecomment-7171164680). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T18:37:25Z. --- <!-- victory-triage v1 head=06cebbf704c71ff449fa81be05a90f0157c27b25 verdict=merge-candidate at=2026-11-25T18:37:15Z --> Automated triage for the maintainers. Hardens the crash reporter in `crates/victory-loader/src/crash.rs`. `thrown` now returns "unknown" for a C++ rethrow (`throw;` raises the MSVC exception with null object/ThrowInfo pointers, and `main` read `p[2] + 12` and faulted inside the reporter). It also returns "unknown" for zero RVAs or an empty type list, and skips the `std::exception` message when the object pointer is null. `unwind_into` now uses `checked_add` for `rsp + 8`; on `main` a corrupted `Rsp` near `u64::MAX` wrapped past the bounds check. It also reads the return address with `read_unaligned`, since a misaligned `read()` is UB and panics in debug builds. I checked the call site (`report`, which only requires `NumberParameters >= 4` and the magic) and the loop exit on `Rip == 0`. The new test's expectations (`[0x1000]`, `[0x2000]`) match that code. Reporting only, with no change to emulation behaviour. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
fuzeofficlal commented 2026-11-26 01:04:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T00:52:48Z.


Code review and Windows 11 verification at head 06cebbf704c7 merged cleanly onto main (63bcca43c6a0):

  • Code review (crates/victory-loader/src/crash.rs:595-652, 754-796):
    • thrown safely rejects p.len() < 4 || p[2] == 0 || p[3] == 0 (covering C++ throw; rethrows), checks RVA non-zero and p[3].checked_add(...), guards count == 0, and reads integer/message values with p[1] != 0 and read_unaligned().
    • unwind_into prevents u64::MAX wraparound via rsp.checked_add(8).is_none_or(|end| end > high) and uses read_unaligned() on the leaf (entry.is_null()) return-address read.
    • Minor non-blocking note: when a crashing frame does have a RUNTIME_FUNCTION (!entry.is_null()), a misaligned context.Rsp is still passed directly to RtlVirtualUnwind, and a few small fixed offsets inside thrown (p[2] + 12, types + 4 + 4 * i, descriptor + 16) still use plain +. Still a solid improvement over main.
  • Verified (Windows 11, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy -p victory-loader --all-targets --locked -- -D warnings (default + --all-features): 0 warnings
    • cargo test -p victory-loader --locked: 36/36 unit + 4/4 integration passed; --all-features: 38/38 unit + 4/4 integration passed (including thrown_and_unwind_handle_rethrow_and_corrupted_rsp).

LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179205869 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2554#issuecomment-7179205869). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T00:52:48Z. --- Code review and Windows 11 verification at head `06cebbf704c7` merged cleanly onto `main` (`63bcca43c6a0`): - **Code review (`crates/victory-loader/src/crash.rs:595-652, 754-796`)**: - `thrown` safely rejects `p.len() < 4 || p[2] == 0 || p[3] == 0` (covering C++ `throw;` rethrows), checks RVA non-zero and `p[3].checked_add(...)`, guards `count == 0`, and reads integer/message values with `p[1] != 0` and `read_unaligned()`. - `unwind_into` prevents `u64::MAX` wraparound via `rsp.checked_add(8).is_none_or(|end| end > high)` and uses `read_unaligned()` on the leaf (`entry.is_null()`) return-address read. - Minor non-blocking note: when a crashing frame *does* have a `RUNTIME_FUNCTION` (`!entry.is_null()`), a misaligned `context.Rsp` is still passed directly to `RtlVirtualUnwind`, and a few small fixed offsets inside `thrown` (`p[2] + 12`, `types + 4 + 4 * i`, `descriptor + 16`) still use plain `+`. Still a solid improvement over `main`. - **Verified (Windows 11, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy -p victory-loader --all-targets --locked -- -D warnings` (default + `--all-features`): 0 warnings - `cargo test -p victory-loader --locked`: **36/36 unit + 4/4 integration passed**; `--all-features`: **38/38 unit + 4/4 integration passed** (including `thrown_and_unwind_handle_rethrow_and_corrupted_rsp`). LGTM.
futurepro62 commented 2026-11-26 02:07:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-26T01:35:49Z.


NVIDIA full-suite run, to go with the reports above by fuzeofficlal, since none of them names a GPU run. Tested exact head 06cebbf (on its base 67e8ae1).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 774 passed, 0 failed, 9 ignored.
  • The new tests ran and passed: thrown_and_unwind_handle_rethrow_and_corrupted_rsp.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7179931227 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2554#issuecomment-7179931227). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-26T01:35:49Z. --- NVIDIA full-suite run, to go with the reports above by fuzeofficlal, since none of them names a GPU run. Tested exact head `06cebbf` (on its base `67e8ae1`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **774 passed, 0 failed, 9 ignored**. - The new tests ran and passed: `thrown_and_unwind_handle_rethrow_and_corrupted_rsp`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
fuzeofficlal commented 2026-11-26 16:04:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T15:38:03Z.

Review: APPROVED


Reviewed 06cebbf704c71ff449fa81be05a90f0157c27b25 (merged cleanly onto 63bcca43c6a00a758510398fa43096ce7e4f7df0) and verified on Windows 11 across dev, --all-features, and --release builds.

Audit

  • C++ exception metadata guards (crates/victory-loader/src/crash.rs:584, :585, :600):
    • Correctly handles short exception parameter arrays and MSVC throw; rethrows (p.len() < 4 || p[2] == 0 || p[3] == 0), validates non-zero types_rva / entry_rva / desc_rva with checked_add, caps count at 64 and guards count == 0, avoids dereferencing a null object pointer (p[1] == 0) for std::exception, and uses read_unaligned() for raw pointer reads.
  • Stack unwind bounds & alignment (crates/victory-loader/src/crash.rs:753, :762):
    • rsp.checked_add(8).is_none_or(|end| end > high) closes the u64 wraparound near u64::MAX that previously bypassed the upper stack bound, and (rsp as *const u64).read_unaligned() removes misaligned-read UB on corrupted leaf frames.

Verification

  • Clean merge onto 63bcca43c6a00a758510398fa43096ce7e4f7df0 + cargo run -p xtask -- policy (191 files ok)
  • cargo clippy -p victory-loader --all-targets -- -D warnings (default & --all-features)
  • cargo test -p victory-loader: 40/40 passed (dev), 42/42 passed (--all-features), 40/40 passed (--release), including thrown_and_unwind_handle_rethrow_and_corrupted_rsp.
  • Also ran a synthetic boundary probe covering p.len() < 4, null p[2]/p[3], zero types_rva/entry_rva/desc_rva, count == 0 and count > 64, null p[1] with .?AVexception@std@@, and unwind_into at u64::MAX, u64::MAX - 7, u64::MAX - 6, and high - 7 — all returned safely without faulting.
<!-- greyfinch-sync:Marvisak/VIctory:review:6076887659 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2554#pullrequestreview-6076887659). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T15:38:03Z. **Review:** APPROVED --- Reviewed `06cebbf704c71ff449fa81be05a90f0157c27b25` (merged cleanly onto `63bcca43c6a00a758510398fa43096ce7e4f7df0`) and verified on Windows 11 across `dev`, `--all-features`, and `--release` builds. ### Audit - **C++ exception metadata guards** (`crates/victory-loader/src/crash.rs:584`, `:585`, `:600`): - Correctly handles short exception parameter arrays and MSVC `throw;` rethrows (`p.len() < 4 || p[2] == 0 || p[3] == 0`), validates non-zero `types_rva` / `entry_rva` / `desc_rva` with `checked_add`, caps `count` at `64` and guards `count == 0`, avoids dereferencing a null object pointer (`p[1] == 0`) for `std::exception`, and uses `read_unaligned()` for raw pointer reads. - **Stack unwind bounds & alignment** (`crates/victory-loader/src/crash.rs:753`, `:762`): - `rsp.checked_add(8).is_none_or(|end| end > high)` closes the `u64` wraparound near `u64::MAX` that previously bypassed the upper stack bound, and `(rsp as *const u64).read_unaligned()` removes misaligned-read UB on corrupted leaf frames. ### Verification - Clean merge onto `63bcca43c6a00a758510398fa43096ce7e4f7df0` + `cargo run -p xtask -- policy` (`191 files ok`) - `cargo clippy -p victory-loader --all-targets -- -D warnings` (default & `--all-features`) - `cargo test -p victory-loader`: **40/40 passed** (`dev`), **42/42 passed** (`--all-features`), **40/40 passed** (`--release`), including `thrown_and_unwind_handle_rethrow_and_corrupted_rsp`. - Also ran a synthetic boundary probe covering `p.len() < 4`, null `p[2]`/`p[3]`, zero `types_rva`/`entry_rva`/`desc_rva`, `count == 0` and `count > 64`, null `p[1]` with `.?AVexception@std@@`, and `unwind_into` at `u64::MAX`, `u64::MAX - 7`, `u64::MAX - 6`, and `high - 7` — all returned safely without faulting.
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/2554:upstream-pr/2554'
git switch 'upstream-pr/2554'

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/2554'
git switch 'upstream-pr/2554'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2554'
git switch 'upstream-pr/2554'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2554'
git switch 'main'
git merge --squash 'upstream-pr/2554'
git switch 'main'
git merge --ff-only 'upstream-pr/2554'
git switch 'main'
git merge 'upstream-pr/2554'
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
Marvisak/VIctory!2552
No description provided.