Loader: clzero stubs in a region within reach of each loop (and a test that failed 1 run in 7 on Intel) #572

Open
github-bot wants to merge 1 commit from github-bot/gh-572 into main AGit
Owner

@⁠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 · open

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.

**@⁠kibagami-nc** on GitHub · 2026-11-28 01:06 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/572) `kibagami-nc:fix/clzero-stubs-per-reach` → `main` · +69 −9 · 1 files · 1 commits · open ## 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.
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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 01:07:01 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/572/head:github-bot/gh-572'
git switch 'github-bot/gh-572'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-572'
git switch 'github-bot/gh-572'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-572'
git switch 'github-bot/gh-572'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-572'
git switch 'main'
git merge --squash 'github-bot/gh-572'
git switch 'main'
git merge --ff-only 'github-bot/gh-572'
git switch 'main'
git merge 'github-bot/gh-572'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.