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

Manually merged
github merged 1 commit from gh-pr/1539/head into gh-pr/1539/base 2026-11-26 04:35:36 +00:00
Owner

@⁠Texbirds on GitHub · 2026-11-24 18:58 UTC · original
Texbirds:fix/loader-clwb-loop-overflow → main · +59 −8 · 1 files · 1 commits · merged 2026-11-25 00:14 UTC

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).
**@⁠Texbirds** on GitHub · 2026-11-24 18:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/1539) `Texbirds:fix/loader-clwb-loop-overflow` → `main` · +59 −8 · 1 files · 1 commits · merged 2026-11-25 00:14 UTC ### 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`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:35:35 +00:00
github manually merged commit 274f6988c9 into gh-pr/1539/base 2026-11-26 04:35:36 +00:00
Author
Owner

@⁠acetonahard-code on GitHub · 2026-11-24 22:29 UTC · original

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.

**@⁠acetonahard-code** on GitHub · 2026-11-24 22:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/1539#issuecomment-7146289456) 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>
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1539
No description provided.