Loader: guard clwb_loop shortcut against u64 overflow and wraparound #1538

Merged
Texbirds merged 1 commit from upstream-pr/1539 into main 2026-11-25 00:14:36 +00:00
Texbirds commented 2026-11-24 18:58:25 +00:00 (Migrated from github.com)

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


Problem

In crates/victory-loader/src/crash.rs, emulate_amd_only's whole-loop clwb_loop shortcut computes:
base.wrapping_add(((limit - *base) / 64 + 1) * 64) when *base <= limit, and base.wrapping_add(64) otherwise.

  1. When *base <= limit and limit - *base >= u64::MAX - 63 (e.g. base = 0, limit = u64::MAX - 10), ((limit - base) / 64 + 1) * 64 overflows u64 (panicking in debug builds and wrapping to 0 in release).
  2. When limit >= u64::MAX - 63 or when *base > limit wraps add base, 0x40 past u64::MAX back to <= limit, hardware jbe continues looping instead of exiting.

Fix

  • Extract clwb_loop_end(base, limit) -> Option<u64> using checked arithmetic (checked_mul, checked_add) and requiring end > limit.
  • When clwb_loop_end returns None, fall through to single-instruction clwb_len emulation so the CPU executes add/cmp/jbe natively with exact wraparound semantics.
  • Add regression test clwb_loop_wrapping_ranges_do_not_overflow_and_fall_back_to_single_clwb.

Verification

  • Pending windows-latest CI (cargo clippy default + --all-features and cargo test --workspace --locked).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6589175025 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1539). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-24T18:58:25Z. --- ### Problem In `crates/victory-loader/src/crash.rs`, `emulate_amd_only`'s whole-loop `clwb_loop` shortcut computes: `base.wrapping_add(((limit - *base) / 64 + 1) * 64)` when `*base <= limit`, and `base.wrapping_add(64)` otherwise. 1. When `*base <= limit` and `limit - *base >= u64::MAX - 63` (e.g. `base = 0, limit = u64::MAX - 10`), `((limit - base) / 64 + 1) * 64` overflows `u64` (panicking in debug builds and wrapping to `0` in release). 2. When `limit >= u64::MAX - 63` or when `*base > limit` wraps `add base, 0x40` past `u64::MAX` back to `<= limit`, hardware `jbe` continues looping instead of exiting. ### Fix - Extract `clwb_loop_end(base, limit) -> Option<u64>` using checked arithmetic (`checked_mul`, `checked_add`) and requiring `end > limit`. - When `clwb_loop_end` returns `None`, fall through to single-instruction `clwb_len` emulation so the CPU executes `add`/`cmp`/`jbe` natively with exact wraparound semantics. - Add regression test `clwb_loop_wrapping_ranges_do_not_overflow_and_fall_back_to_single_clwb`. ### Verification - Pending `windows-latest` CI (`cargo clippy` default + `--all-features` and `cargo test --workspace --locked`).
catminou4 commented 2026-11-24 20:02:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T19:20:58Z.

Review: COMMENTED


Static review only: crash.rs is Windows-only (EXCEPTION_POINTERS/CONTEXT), so this cannot be exercised on macOS. The logic is sound: clwb_loop_end uses checked_mul/checked_add and returns None when the loop would overflow or wrap past u64::MAX or fail to pass limit, in which case emulation falls back to the single-instruction clwb path (Rip advances 4, registers untouched) — matching the test's expectations. EFlags are recomputed from the final base/limit as before. Please run the new clwb_loop_wrapping_ranges... test on Windows before merge; the overflow cases (0..u64::MAX-10, u64::MAX-16..0x100, u64::MAX-63..u64::MAX) are exactly the ones that would have panicked or wrapped before.

<!-- greyfinch-sync:Marvisak/VIctory:review:6045812246 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1539#pullrequestreview-6045812246). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T19:20:58Z. **Review:** COMMENTED --- Static review only: `crash.rs` is Windows-only (`EXCEPTION_POINTERS`/`CONTEXT`), so this cannot be exercised on macOS. The logic is sound: `clwb_loop_end` uses `checked_mul`/`checked_add` and returns `None` when the loop would overflow or wrap past `u64::MAX` or fail to pass `limit`, in which case emulation falls back to the single-instruction `clwb` path (Rip advances 4, registers untouched) — matching the test's expectations. EFlags are recomputed from the final base/limit as before. Please run the new `clwb_loop_wrapping_ranges...` test on Windows before merge; the overflow cases (`0..u64::MAX-10`, `u64::MAX-16..0x100`, `u64::MAX-63..u64::MAX`) are exactly the ones that would have panicked or wrapped before.
acetonahard-code commented 2026-11-24 23:05:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by acetonahard-code on 2026-11-24T22:29:18Z.


Tested on Windows 11 with the repo toolchain, merged into current main (70564ae):

cargo test -p victory-loader passed failed ignored
main 25 0 0
main + this PR 26 0 0
main, --all-features 26 0 -
main + this PR, --all-features 27 0 -
  • The new test passes.
  • cargo clippy --all-targets -- -D warnings, with and without --all-features: clean.

I didn't test with game data.


Tested locally with an AI-assisted script (Claude Code); I checked the results before posting.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7146289456 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1539#issuecomment-7146289456). Originally posted by [acetonahard-code](https://github.com/acetonahard-code) on 2026-11-24T22:29:18Z. --- Tested on Windows 11 with the repo toolchain, merged into current `main` (70564ae): | `cargo test -p victory-loader` | passed | failed | ignored | |---|---|---|---| | `main` | 25 | 0 | 0 | | `main` + this PR | 26 | 0 | 0 | | `main`, `--all-features` | 26 | 0 | - | | `main` + this PR, `--all-features` | 27 | 0 | - | - The new test passes. - `cargo clippy --all-targets -- -D warnings`, with and without `--all-features`: clean. I didn't test with game data. --- <sub>Tested locally with an AI-assisted script (Claude Code); I checked the results before posting.</sub>
upstream-sync closed this pull request 2026-11-25 01:06:48 +00:00
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!1538
No description provided.