Loader: a clzero loop the stub cannot take reserves no room, and far-apart loops are tested #572
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/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?
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by kibagami-nc on 2026-11-28T01:06:15Z.
Rebased on main (
923e38c), and smaller now. Main fixed what this PR first fixed: sincec73b6c84,STUBSholds several regions, and each loop takes its stub from one within reach of it. What is left here is what main lacks: the loop's form is checked before any room is taken, and two tests. The first version's text is below the line.What main still does
patch_clzero(crates/victory-loader/src/console_insn.rs) checks the loop's form only throughclzero_stub, once per region. The decoder takes anycmp rax, regof the48 39/4C 39opcodes as aclzeroloop.clzero_stubrefusescmp rax, raxand forms whose operand is not a register.For such a loop, no region fits, so a new 64 KiB region is reserved (
near), and then the stub cannot be made there either. The loop is emulated, faults again at its next pass, and the next try reserves another region: one 64 KiB region a pass, for a loop that passes thousands of times a second.The change
The form is checked first, as this PR's first version did:
clzero_stub(at, at, cmp).is_none()returns before the regions are looked at. Main's choice of region is unchanged.Tests
console_insn::tests::a_loop_the_stub_cannot_take_reserves_no_room. The loopclzero; add rax, 40h; cmp rax, rax; jb(the decoder takes it as a loop against rax) is tried 50 times. On main:51 regions reserved for a loop that was not replaced. With the check: none. A margin of 9 is left for regions other tests reserve meanwhile.console_insn::tests::clzero_loops_far_apart_are_each_replaced. Two loops more than 4 GiB apart are each replaced and run as the loop does. It passes on main's choice of region too, so it guards that fix.cargo test -p victory-loader --locked: 48 passed in the library, plus the integration tests.--all-targets, default and--all-features): nothing in the changed lines. Main has findings of its own elsewhere.No game files, dumps or extracted data are included.
The first version's text (the region per reach, now in main)
The 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.