fix(loader): accelerate 15-byte CLZERO loops with page-readability caching #3278

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

@⁠legayclightning22 on GitHub · 2026-11-26 15:36 UTC · original
legayclightning22:fix/clzero-and-console-insn-emulation → main · +228 −5 · 1 files · 1 commits · open

Summary

Optimizes console-only CLZERO loop stepping in crates/victory-loader/src/crash.rs and ensures safe handling of unmapped pages without panicking.

Changes

  • Add per-thread page-readability caching and fast 15-byte CLZERO loop stepping (togglable via VICTORY_CLZERO_LOOP15=0).
  • Safely abort loop acceleration without panicking when encountering unmapped or non-writable pages, while keeping MONITORX/MWAITX, CLWB, and SSE4A INSERTQ/EXTRQ emulation intact.
  • Add unit tests covering VICTORY_CLZERO_LOOP15 flag parsing and unmapped-page loop stepping.

Verification

  • cargo test -p victory-loader --lib
  • cargo clippy --locked -p victory-loader --all-targets -- -D warnings
  • cargo xtask policy
**@⁠legayclightning22** on GitHub · 2026-11-26 15:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/3278) `legayclightning22:fix/clzero-and-console-insn-emulation` → `main` · +228 −5 · 1 files · 1 commits · open ## Summary Optimizes console-only `CLZERO` loop stepping in `crates/victory-loader/src/crash.rs` and ensures safe handling of unmapped pages without panicking. ## Changes - Add per-thread page-readability caching and fast 15-byte `CLZERO` loop stepping (togglable via `VICTORY_CLZERO_LOOP15=0`). - Safely abort loop acceleration without panicking when encountering unmapped or non-writable pages, while keeping `MONITORX`/`MWAITX`, `CLWB`, and SSE4A `INSERTQ`/`EXTRQ` emulation intact. - Add unit tests covering `VICTORY_CLZERO_LOOP15` flag parsing and unmapped-page loop stepping. ## Verification - `cargo test -p victory-loader --lib` - `cargo clippy --locked -p victory-loader --all-targets -- -D warnings` - `cargo xtask policy` ```
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 15:42:13 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 15:49 UTC · original · reviewed

Reviewed the diff and ran local verification on Windows 11 x64 (cargo test -p victory-loader --lib crash:: -- --test-threads=1: 28/28 passed; cargo clippy -p victory-loader --all-targets -- -D warnings: clean; cargo xtask policy: 191 files ok).

I cannot approve this as-is because clzero_page_writable introduces a serious correctness regression with the GPU write watch:

  1. Silently skips zeroing GPU write-watched (PAGE_READONLY) pages:
    victory-gpu's write watch (crates/victory-gpu/src/watch.rs) marks clean title-memory pages PAGE_READONLY so that a CPU write faults into the VEH, marks the page dirty, restores PAGE_READWRITE, and resumes. Previously, clzero's write_bytes(0, 64) triggered that write-watch fault normally and zeroed the memory. With clzero_page_writable, any PAGE_READONLY page returns false from VirtualQuery and clzero silently turns into a no-op — leaving stale non-zero bytes in title memory and never notifying the write watch.
  2. Performance impact:
    The 15-byte clzero loop was already emulated in clzero(); adding a VirtualQuery syscall per 4 KiB page on entry actually adds overhead (and in large title pools VirtualQuery walks page tables to the end of the region — see crates/victory-gpu/src/host.rs lines 2476-2480).

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 15:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/3278#pullrequestreview-6077034506) · **reviewed** Reviewed the diff and ran local verification on Windows 11 x64 (`cargo test -p victory-loader --lib crash:: -- --test-threads=1`: 28/28 passed; `cargo clippy -p victory-loader --all-targets -- -D warnings`: clean; `cargo xtask policy`: 191 files ok). I cannot approve this as-is because `clzero_page_writable` introduces a serious correctness regression with the GPU write watch: 1. **Silently skips zeroing GPU write-watched (`PAGE_READONLY`) pages:** `victory-gpu`'s write watch (`crates/victory-gpu/src/watch.rs`) marks clean title-memory pages `PAGE_READONLY` so that a CPU write faults into the VEH, marks the page dirty, restores `PAGE_READWRITE`, and resumes. Previously, `clzero`'s `write_bytes(0, 64)` triggered that write-watch fault normally and zeroed the memory. With `clzero_page_writable`, any `PAGE_READONLY` page returns `false` from `VirtualQuery` and `clzero` **silently turns into a no-op** — leaving stale non-zero bytes in title memory and never notifying the write watch. 2. **Performance impact:** The 15-byte `clzero` loop was already emulated in `clzero()`; adding a `VirtualQuery` syscall per 4 KiB page on entry actually adds overhead (and in large title pools `VirtualQuery` walks page tables to the end of the region — see `crates/victory-gpu/src/host.rs` lines 2476-2480). Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-26 16:00 UTC · original

Independent Windows verification. Head 15628a6, Rust 1.100.0/MSVC, worktree-local CARGO_TARGET_DIR (target-3278), no game data.

  • cargo test --locked -p victory-loader --lib -- --exact crash::tests::clzero_loop15_switch_is_off_only_for_zero crash::tests::clzero_page_cache_remembers_recent_pages crash::tests::clzero_does_not_write_an_unmapped_page crash::tests::emulated_clzero_loop15_skips_unmapped_pages_without_panicking -> 4 passed, 0 failed, 35 filtered, exit 0.
  • git diff --check origin/main HEAD clean; all four test names absent on merge base 63bcca4.
  • Caveat: Dukestar69's COMMENTED review raises a PAGE_READONLY / GPU write-watch correctness concern that these synthetic tests do not exercise. My run only confirms the added tests pass locally; it does not validate that concern.

AI-assisted (Hy4 flash). GPU/driver N/A (CPU-only loader tests). No game data.

**@⁠cloph-dsp** on GitHub · 2026-11-26 16:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/3278#issuecomment-7197055778) Independent Windows verification. Head 15628a6, Rust 1.100.0/MSVC, worktree-local CARGO_TARGET_DIR (target-3278), no game data. - `cargo test --locked -p victory-loader --lib -- --exact crash::tests::clzero_loop15_switch_is_off_only_for_zero crash::tests::clzero_page_cache_remembers_recent_pages crash::tests::clzero_does_not_write_an_unmapped_page crash::tests::emulated_clzero_loop15_skips_unmapped_pages_without_panicking` -> 4 passed, 0 failed, 35 filtered, exit 0. - `git diff --check origin/main HEAD` clean; all four test names absent on merge base 63bcca4. - Caveat: Dukestar69's COMMENTED review raises a PAGE_READONLY / GPU write-watch correctness concern that these synthetic tests do not exercise. My run only confirms the added tests pass locally; it does not validate that concern. AI-assisted (Hy4 flash). GPU/driver N/A (CPU-only loader tests). No game data.
github-bot force-pushed github-bot/gh-3278 from 15628a6949 to 1adf566d53 2026-11-26 16:10:25 +00:00 Compare
Author
Owner

@⁠legayclightning22 on GitHub · 2026-11-26 16:08 UTC · original

Pushed 1adf566 removing clzero_page_writable / VirtualQuery pre-checks so clzero writes through normally without skipping PAGE_READONLY write-watch pages or adding per-page VirtualQuery overhead, while keeping safe non-panicking handling and tests passing.

**@⁠legayclightning22** on GitHub · 2026-11-26 16:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/3278#issuecomment-7197241626) Pushed `1adf566` removing `clzero_page_writable` / `VirtualQuery` pre-checks so `clzero` writes through normally without skipping `PAGE_READONLY` write-watch pages or adding per-page `VirtualQuery` overhead, while keeping safe non-panicking handling and tests passing.
This pull request can be merged automatically.
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/3278/head:github-bot/gh-3278'
git switch 'github-bot/gh-3278'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3278'
git switch 'github-bot/gh-3278'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3278'
git switch 'github-bot/gh-3278'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3278'
git switch 'main'
git merge --squash 'github-bot/gh-3278'
git switch 'main'
git merge --ff-only 'github-bot/gh-3278'
git switch 'main'
git merge 'github-bot/gh-3278'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3278
No description provided.