Loader: a clzero loop the stub cannot take reserves no room, and far-apart loops are tested #572

Merged
kibagami-nc merged 1 commit from upstream-pr/572 into main 2026-11-29 08:16:59 +00:00
kibagami-nc commented 2026-11-28 01:06:15 +00:00 (Migrated from github.com)

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: since c73b6c84, STUBS holds 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 through clzero_stub, once per region. The decoder takes any cmp rax, reg of the 48 39 / 4C 39 opcodes as a clzero loop. clzero_stub refuses cmp rax, rax and 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

  • New console_insn::tests::a_loop_the_stub_cannot_take_reserves_no_room. The loop clzero; 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.
  • New 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.
  • Clippy (workspace, --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 each clzero loop 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 rel32 reaches 2 GiB. So a loop farther than that from the region was never replaced: jump gave None, patch_clzero returned 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 on patches_clzero). Loops in two images more than 2 GiB apart hit this.

It also makes cargo test -p victory-loader fail about one run in seven on Intel CPUs:

---- console_insn::tests::a_clzero_loop_becomes_plain_stores ----
assertion `left == right` failed: a jump
  left: 15
 right: 233

crash::tests::clzero_loops_end_like_their_last_pass runs a real clzero loop (inline asm!) in the test program's image. When it was replaced first, the region sat near the image (0x7ff6…). The loop of a_clzero_loop_becomes_plain_stores is in a page from VirtualAlloc (0x1c3…), more than 2 GiB away, so it was left as it was. I traced this with a temporary log in patch_clzero: the first patch went at 0x7ff6fbcae217, and the next one, at 0x1c33a840010, had no jump.

The change

STUBS holds 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 from near (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.rs keeps its own region per image and is not changed.

Tests

  • New console_insn::tests::clzero_loops_far_apart_are_each_replaced:
    • The setup. Two loops more than 4 GiB apart: one page bottom-up, one MEM_TOP_DOWN.
    • The check. Each loop is replaced (0xe9 at the loop), then run. Its lines are zeroed and nothing else.
    • Before / after. It fails on every run before the change, at the second loop, and passes after it.
  • cargo test -p victory-loader --locked: 44 passed in the library, plus 3 in the integration tests.
    • Before the change: a_clzero_loop_becomes_plain_stores failed in 2 of 15 runs of the whole suite.
    • After the change: 0 failures in 25 runs (Intel Core Ultra 7 265KF).
  • cargo clippy -p victory-loader --all-targets -- -D warnings, with and without --all-features: nothing in console_insn.rs.
    • It still stops on 4 missing_transmute_annotations errors in write_sites.rs. That file is unchanged here and has them on main.

No game files, dumps or extracted data are included.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5535210301 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/572). Originally posted by [kibagami-nc](https://github.com/kibagami-nc) on 2026-11-28T01:06:15Z. --- **Rebased on main (923e38c), and smaller now.** Main fixed what this PR first fixed: since c73b6c84, `STUBS` holds 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 through `clzero_stub`, once per region. The decoder takes any `cmp rax, reg` of the `48 39` / `4C 39` opcodes as a `clzero` loop. `clzero_stub` refuses `cmp rax, rax` and 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 - **New `console_insn::tests::a_loop_the_stub_cannot_take_reserves_no_room`.** The loop `clzero; 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. - **New `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. - Clippy (workspace, `--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. --- <details><summary>The first version's text (the region per reach, now in main)</summary> ## The problem On a CPU without `clzero` (Intel), `patch_clzero` (`crates/victory-loader/src/console_insn.rs`) replaces each `clzero` loop 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 rel32` reaches 2 GiB. So a loop farther than that from the region was never replaced: `jump` gave `None`, `patch_clzero` returned 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 on `patches_clzero`). Loops in two images more than 2 GiB apart hit this. It also makes `cargo test -p victory-loader` fail about one run in seven on Intel CPUs: ``` ---- console_insn::tests::a_clzero_loop_becomes_plain_stores ---- assertion `left == right` failed: a jump left: 15 right: 233 ``` `crash::tests::clzero_loops_end_like_their_last_pass` runs a real `clzero` loop (inline `asm!`) in the test program's image. When it was replaced first, the region sat near the image (`0x7ff6…`). The loop of `a_clzero_loop_becomes_plain_stores` is in a page from `VirtualAlloc` (`0x1c3…`), more than 2 GiB away, so it was left as it was. I traced this with a temporary log in `patch_clzero`: the first patch went at `0x7ff6fbcae217`, and the next one, at `0x1c33a840010`, had no jump. ## The change `STUBS` holds 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 from `near` (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.rs` keeps its own region per image and is not changed. ## Tests - New `console_insn::tests::clzero_loops_far_apart_are_each_replaced`: - **The setup.** Two loops more than 4 GiB apart: one page bottom-up, one `MEM_TOP_DOWN`. - **The check.** Each loop is replaced (`0xe9` at the loop), then run. Its lines are zeroed and nothing else. - **Before / after.** It fails on every run before the change, at the second loop, and passes after it. - `cargo test -p victory-loader --locked`: 44 passed in the library, plus 3 in the integration tests. - Before the change: `a_clzero_loop_becomes_plain_stores` failed in 2 of 15 runs of the whole suite. - After the change: 0 failures in 25 runs (Intel Core Ultra 7 265KF). - `cargo clippy -p victory-loader --all-targets -- -D warnings`, with and without `--all-features`: nothing in `console_insn.rs`. - It still stops on 4 `missing_transmute_annotations` errors in `write_sites.rs`. That file is unchanged here and has them on `main`. No game files, dumps or extracted data are included. </details>
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
trustno1-coder/VIctory-NVIDIA!572
No description provided.