Loader: clzero stubs in a region within reach of each loop (and a test that failed 1 run in 7 on Intel) #572
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/trustno1-coder--VIctory-NVIDIA!572
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-572"
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?
@kibagami-nc on GitHub · 2026-11-28 01:06 UTC · original
kibagami-nc:fix/clzero-stubs-per-reach→main· +69 −9 · 1 files · 1 commits · openThe problem
On a CPU without
clzero(Intel),patch_clzero(crates/victory-loader/src/console_insn.rs) replaces eachclzeroloop of the title with a jump to a stub of plain stores. The stubs lived in one region, allocated near the first loop replaced.A
jmp rel32reaches 2 GiB. So a loop farther than that from the region was never replaced:jumpgaveNone,patch_clzeroreturned false, and the loop raised an exception at every pass from then on. That is the cost the patch exists to avoid (25,000 exceptions a second in the dorm, per the comment onpatches_clzero). Loops in two images more than 2 GiB apart hit this.It also makes
cargo test -p victory-loaderfail about one run in seven on Intel CPUs:crash::tests::clzero_loops_end_like_their_last_passruns a realclzeroloop (inlineasm!) in the test program's image. When it was replaced first, the region sat near the image (0x7ff6…). The loop ofa_clzero_loop_becomes_plain_storesis in a page fromVirtualAlloc(0x1c3…), more than 2 GiB away, so it was left as it was. I traced this with a temporary log inpatch_clzero: the first patch went at0x7ff6fbcae217, and the next one, at0x1c33a840010, had no jump.The change
STUBSholds a list of regions. A loop takes its stub from the first region within reach that has room. If there is none, it gets a new region fromnear(within ±1.75 GiB of the loop), as the first one was made. The checks of the loop's form and of the jump's alignment now come before any room is taken, so a loop that cannot be replaced allocates nothing.write_sites.rskeeps its own region per image and is not changed.Tests
console_insn::tests::clzero_loops_far_apart_are_each_replaced:MEM_TOP_DOWN.0xe9at the loop), then run. Its lines are zeroed and nothing else.cargo test -p victory-loader --locked: 44 passed in the library, plus 3 in the integration tests.a_clzero_loop_becomes_plain_storesfailed in 2 of 15 runs of the whole suite.cargo clippy -p victory-loader --all-targets -- -D warnings, with and without--all-features: nothing inconsole_insn.rs.missing_transmute_annotationserrors inwrite_sites.rs. That file is unchanged here and has them onmain.No game files, dumps or extracted data are included.
patch_clzero put the stubs that replace clzero loops in one region, allocated near the first loop replaced. A jmp rel32 reaches 2 GiB, so a loop farther than that from the region (in another image) was never replaced, and was emulated with an exception at every pass from then on. The stubs now go in the first region within reach of the loop that has room, or in a new one near the loop. The form and alignment checks come before any room is taken. This also fixes a test that fails about one run in seven on Intel CPUs: crash::tests::clzero_loops_end_like_their_last_pass runs a real clzero loop in the test program's image, and when it was replaced first, the region sat near the image, so a_clzero_loop_becomes_plain_stores, whose loop is in memory of its own more than 2 GiB away, found its loop not replaced ("a jump", left: 15, right: 233). New test clzero_loops_far_apart_are_each_replaced: two loops more than 4 GiB apart (one page bottom-up, one MEM_TOP_DOWN), each replaced and run. It fails on every run before the change.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.