Loader: guard clwb_loop shortcut against u64 overflow and wraparound #1538
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1539"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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-loopclwb_loopshortcut computes:base.wrapping_add(((limit - *base) / 64 + 1) * 64)when*base <= limit, andbase.wrapping_add(64)otherwise.*base <= limitandlimit - *base >= u64::MAX - 63(e.g.base = 0, limit = u64::MAX - 10),((limit - base) / 64 + 1) * 64overflowsu64(panicking in debug builds and wrapping to0in release).limit >= u64::MAX - 63or when*base > limitwrapsadd base, 0x40pastu64::MAXback to<= limit, hardwarejbecontinues looping instead of exiting.Fix
clwb_loop_end(base, limit) -> Option<u64>using checked arithmetic (checked_mul,checked_add) and requiringend > limit.clwb_loop_endreturnsNone, fall through to single-instructionclwb_lenemulation so the CPU executesadd/cmp/jbenatively with exact wraparound semantics.clwb_loop_wrapping_ranges_do_not_overflow_and_fall_back_to_single_clwb.Verification
windows-latestCI (cargo clippydefault +--all-featuresandcargo test --workspace --locked).Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T19:20:58Z.
Review: COMMENTED
Static review only:
crash.rsis Windows-only (EXCEPTION_POINTERS/CONTEXT), so this cannot be exercised on macOS. The logic is sound:clwb_loop_enduseschecked_mul/checked_addand returnsNonewhen the loop would overflow or wrap pastu64::MAXor fail to passlimit, in which case emulation falls back to the single-instructionclwbpath (Rip advances 4, registers untouched) — matching the test's expectations. EFlags are recomputed from the final base/limit as before. Please run the newclwb_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.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-loadermainmain+ this PRmain,--all-featuresmain+ this PR,--all-featurescargo 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.