crash: prevent secondary faults and overflow panics in VEH and exception reporter #679

Open
MasterWolf wants to merge 1 commit from MasterWolf/Darner:pr/post-merge-crash-handler-safety into post-merge
First-time contributor

Not run in the game (verified via cargo test, cargo clippy, and cargo build on 85107c7; VEH/stack-unwind/C++-throw edge cases covered by unit tests).

Contributor role (optional)

  • Fluxer ID: 1577824307575267331

What it changes

  • The problem on post-merge: read from crates/darner-loader/src/crash.rs (28a9b42):
    1. emulate_amd_only reads 3 bytes at context.Rip (code.cast::<[u8; 3]>().read_unaligned()) before checking room to the end of the page, skips clwb_len when n < 15, passes the raw pointer to sse4a without bounds checks, and computes ((limit - *base) / 64 + 1) * 64 (panics in debug when *base == 0 && limit == u64::MAX). A fault near a page end before an unmapped page causes a secondary AV inside the VEH.
    2. unwind_into checks rsp + 8 > high without checked addition; corrupted Rsp near usize::MAX panics in debug across the VEH/UEF FFI boundary or wraps in release and reads rsp.
    3. thrown dereferences p[2] + 12, types, p[1], and descriptor strings with raw pointers; MSVC C++ rethrows (throw; where p[1] == 0 && p[2] == 0) or invalid exception records crash inside the exception reporter before logs are written.

This PR:

  • Copies n = room.min(bytes.len()) page-bounded bytes first in emulate_amd_only, matches bytes.get(..3), uses .wrapping_mul(64), and passes &bytes[..n] to bounds-checked slice versions of clwb_len and sse4a (removing the n >= 15 gate on clwb_len).
  • Guards unwind_into with rsp.checked_add(8).is_none_or(|end| end > high).
  • Hardens thrown using read_memory-backed read_u32 / read_usize / read_cstr helpers, guarding p.len() < 4 || p[2] == 0, count == 0, and p[1] != 0.
  • Adds 3 unit tests (unwind_into_stops_cleanly_when_rsp_near_usize_max, clwb_len_and_sse4a_handle_short_page_tail_slices, thrown_handles_rethrow_and_invalid_pointers_safely).

How it was tested

  • Tested revision: 85107c7 (on post-merge 28a9b42)
  • Commands and results:
    • cargo test --offline -p darner-loader --lib crash — 3 passed; 0 failed (0.00s)
    • cargo clippy --offline -p darner-loader --tests — passed (0 new warnings; 4 pre-existing)
    • cargo build --offline -p darner — passed (7.25s)
  • Changes a default: no
  • A/B result: not applicable (crash/VEH safety fix).
  • Checks run on that revision: cargo build, cargo test, cargo clippy
  • Gameplay, when relevant: not run in the game
  • Hardware dependence: likely general
  • Coverage gaps: verified with unit tests rather than an in-game launch.

Checklist

  • I ran this change in the game, or say below why it can't be (not run in the game; verified via unit tests, cargo clippy, and cargo build).
  • This PR contains one change based on post-merge.
  • The diff contains no game files, code or assets.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none found
  • Replaces or overlaps: none found
**Not run in the game** (verified via `cargo test`, `cargo clippy`, and `cargo build` on `85107c7`; VEH/stack-unwind/C++-throw edge cases covered by unit tests). ## Contributor role (optional) - Fluxer ID: 1577824307575267331 ## What it changes - The problem on `post-merge`: read from `crates/darner-loader/src/crash.rs` (`28a9b42`): 1. `emulate_amd_only` reads 3 bytes at `context.Rip` (`code.cast::<[u8; 3]>().read_unaligned()`) *before* checking `room` to the end of the page, skips `clwb_len` when `n < 15`, passes the raw pointer to `sse4a` without bounds checks, and computes `((limit - *base) / 64 + 1) * 64` (panics in debug when `*base == 0 && limit == u64::MAX`). A fault near a page end before an unmapped page causes a secondary AV inside the VEH. 2. `unwind_into` checks `rsp + 8 > high` without checked addition; corrupted `Rsp` near `usize::MAX` panics in debug across the VEH/UEF FFI boundary or wraps in release and reads `rsp`. 3. `thrown` dereferences `p[2] + 12`, `types`, `p[1]`, and descriptor strings with raw pointers; MSVC C++ rethrows (`throw;` where `p[1] == 0 && p[2] == 0`) or invalid exception records crash inside the exception reporter before logs are written. This PR: - Copies `n = room.min(bytes.len())` page-bounded bytes first in `emulate_amd_only`, matches `bytes.get(..3)`, uses `.wrapping_mul(64)`, and passes `&bytes[..n]` to bounds-checked slice versions of `clwb_len` and `sse4a` (removing the `n >= 15` gate on `clwb_len`). - Guards `unwind_into` with `rsp.checked_add(8).is_none_or(|end| end > high)`. - Hardens `thrown` using `read_memory`-backed `read_u32` / `read_usize` / `read_cstr` helpers, guarding `p.len() < 4 || p[2] == 0`, `count == 0`, and `p[1] != 0`. - Adds 3 unit tests (`unwind_into_stops_cleanly_when_rsp_near_usize_max`, `clwb_len_and_sse4a_handle_short_page_tail_slices`, `thrown_handles_rethrow_and_invalid_pointers_safely`). ## How it was tested - Tested revision: `85107c7` (on `post-merge` `28a9b42`) - Commands and results: - `cargo test --offline -p darner-loader --lib crash` — `3 passed; 0 failed` (`0.00s`) - `cargo clippy --offline -p darner-loader --tests` — passed (`0` new warnings; 4 pre-existing) - `cargo build --offline -p darner` — passed (`7.25s`) - Changes a default: no - A/B result: not applicable (crash/VEH safety fix). - Checks run on that revision: `cargo build`, `cargo test`, `cargo clippy` - Gameplay, when relevant: not run in the game - Hardware dependence: likely general - Coverage gaps: verified with unit tests rather than an in-game launch. ## Checklist - [ ] I ran this change in the game, or say below why it can't be (not run in the game; verified via unit tests, `cargo clippy`, and `cargo build`). - [x] This PR contains one change based on `post-merge`. - [x] The diff contains no game files, code or assets. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none found - Replaces or overlaps: none found
TrustNo1 changed title from [pr-fix][loader] crash: prevent secondary faults and overflow panics in VEH and exception reporter to crash: prevent secondary faults and overflow panics in VEH and exception reporter 2026-12-06 03:25:11 +00:00
This pull request can be merged automatically.
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 'pr/post-merge-crash-handler-safety:MasterWolf-pr/post-merge-crash-handler-safety'
git switch 'MasterWolf-pr/post-merge-crash-handler-safety'

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 'post-merge'
git merge --no-ff 'MasterWolf-pr/post-merge-crash-handler-safety'
git switch 'MasterWolf-pr/post-merge-crash-handler-safety'
git rebase 'post-merge'
git switch 'post-merge'
git merge --ff-only 'MasterWolf-pr/post-merge-crash-handler-safety'
git switch 'MasterWolf-pr/post-merge-crash-handler-safety'
git rebase 'post-merge'
git switch 'post-merge'
git merge --no-ff 'MasterWolf-pr/post-merge-crash-handler-safety'
git switch 'post-merge'
git merge --squash 'MasterWolf-pr/post-merge-crash-handler-safety'
git switch 'post-merge'
git merge --ff-only 'MasterWolf-pr/post-merge-crash-handler-safety'
git switch 'post-merge'
git merge 'MasterWolf-pr/post-merge-crash-handler-safety'
git push origin 'post-merge'
Sign in to join this conversation.
No reviewers
No milestone
No project
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.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!679
No description provided.