Loader: guard clwb_loop shortcut against u64 overflow and wraparound #1539
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!1539
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1539/head"
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?
@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 UTCProblem
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).@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-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.