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

Open
legayclightning22 wants to merge 1 commit from upstream-pr/3278 into main
legayclightning22 commented 2026-11-26 15:36:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by legayclightning22 on 2026-11-26T15:36:58Z.


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
<!-- greyfinch-sync:Marvisak/VIctory:issue:6630063271 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3278). Originally posted by [legayclightning22](https://github.com/legayclightning22) on 2026-11-26T15:36:58Z. --- ## 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` ```
Dukestar69 commented 2026-11-26 15:49:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T15:49:25Z.

Review: COMMENTED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6077034506 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3278#pullrequestreview-6077034506). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T15:49:25Z. **Review:** COMMENTED --- 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).
cloph-dsp commented 2026-11-26 16:00:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T16:00:35Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7197055778 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3278#issuecomment-7197055778). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T16:00:35Z. --- 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.
legayclightning22 commented 2026-11-26 16:08:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by legayclightning22 on 2026-11-26T16:08:43Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7197241626 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3278#issuecomment-7197241626). Originally posted by [legayclightning22](https://github.com/legayclightning22) on 2026-11-26T16:08:43Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3278:upstream-pr/3278'
git switch 'upstream-pr/3278'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3278'
git switch 'upstream-pr/3278'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3278'
git switch 'upstream-pr/3278'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3278'
git switch 'main'
git merge --squash 'upstream-pr/3278'
git switch 'main'
git merge --ff-only 'upstream-pr/3278'
git switch 'main'
git merge 'upstream-pr/3278'
git push origin 'main'
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
Marvisak/VIctory!3276
No description provided.