loader: stop clzero loop emulation before address wraparound #3254

Open
cloph-dsp wants to merge 1 commit from upstream-pr/3256 into main
cloph-dsp commented 2026-11-26 14:46:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T14:46:43Z.


Depends on: #1633 (claim CLZERO-WRAPAROUND; earlier PR #2555 closed unmerged).

The emulated clzero loops advanced their line pointer with wrapping_add(0x40). If a loop's limit was near u64::MAX, the pointer wrapped to a small address and the emulator kept zeroing lines instead of ending the pass. A small advance_line helper now uses checked_add and both loops stop when the next line would wrap.

Test: crash::tests::clzero_loop_advance_stops_before_u64_wraparound checks the valid boundary (u64::MAX - 0x40 → u64::MAX) and that u64::MAX - 0x3f and u64::MAX are rejected, without touching invalid memory.

Validation (Windows, Rust 1.100.0/MSVC, worktree-local CARGO_TARGET_DIR):

  • cargo test --locked -p victory-loader --lib -- clzero — 8 passed, 0 failed (existing loop tests unchanged).
  • git diff --check — clean.
  • Negative control: the new test name is absent from origin/main.

AI-assisted (Hy4 flash). Synthetic inputs only; no game assets, extracted shaders, logs or DRM material were used or uploaded.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5512132540 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3256). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T14:46:43Z. --- Depends on: #1633 (claim `CLZERO-WRAPAROUND`; earlier PR #2555 closed unmerged). The emulated `clzero` loops advanced their line pointer with `wrapping_add(0x40)`. If a loop's limit was near `u64::MAX`, the pointer wrapped to a small address and the emulator kept zeroing lines instead of ending the pass. A small `advance_line` helper now uses `checked_add` and both loops stop when the next line would wrap. Test: `crash::tests::clzero_loop_advance_stops_before_u64_wraparound` checks the valid boundary (`u64::MAX - 0x40` → `u64::MAX`) and that `u64::MAX - 0x3f` and `u64::MAX` are rejected, without touching invalid memory. Validation (Windows, Rust 1.100.0/MSVC, worktree-local `CARGO_TARGET_DIR`): - `cargo test --locked -p victory-loader --lib -- clzero` — 8 passed, 0 failed (existing loop tests unchanged). - `git diff --check` — clean. - Negative control: the new test name is absent from `origin/main`. AI-assisted (Hy4 flash). Synthetic inputs only; no game assets, extracted shaders, logs or DRM material were used or uploaded.
AurumCarlos commented 2026-11-26 15:05:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T15:05:32Z.

Review: COMMENTED


Two things:

  • Same case as #2555, which triage proposed to close: the cursor is a user-mode address the title zeroes, so it can't come within 64 bytes of u64::MAX (reeledMarek closed it on that basis). Unless something has changed since, this guards an unreachable case; worth answering that triage note in the description.
  • The doc comments got swapped: advance_line is inserted between clzero's doc comment (/// # Safety …) and unsafe fn clzero, so the Safety section now documents advance_line and clzero has none. Moving the helper above that comment block fixes it.
<!-- greyfinch-sync:Marvisak/VIctory:review:6076434001 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3256#pullrequestreview-6076434001). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-26T15:05:32Z. **Review:** COMMENTED --- Two things: - **Same case as #2555, which triage proposed to close**: the cursor is a user-mode address the title zeroes, so it can't come within 64 bytes of `u64::MAX` (reeledMarek closed it on that basis). Unless something has changed since, this guards an unreachable case; worth answering that triage note in the description. - **The doc comments got swapped**: `advance_line` is inserted between `clzero`'s doc comment (`/// # Safety …`) and `unsafe fn clzero`, so the Safety section now documents `advance_line` and `clzero` has none. Moving the helper above that comment block fixes it.
Dukestar69 commented 2026-11-26 15:31:04 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64:

  • Using advance_line (checked_add(0x40)) in both clzero loop emulation paths cleanly stops before u64 address wraparound instead of wrapping to low memory.
  • cargo test -p victory-loader --lib crash:: -- --test-threads=1: 25 passed, 0 failed (including clzero_loop_advance_stops_before_u64_wraparound).
  • cargo clippy -p victory-loader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6076796874 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3256#pullrequestreview-6076796874). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T15:31:04Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64: - Using `advance_line` (`checked_add(0x40)`) in both `clzero` loop emulation paths cleanly stops before `u64` address wraparound instead of wrapping to low memory. - `cargo test -p victory-loader --lib crash:: -- --test-threads=1`: 25 passed, 0 failed (including `clzero_loop_advance_stops_before_u64_wraparound`). - `cargo clippy -p victory-loader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Jackster commented 2026-11-26 22:29:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T22:29:07Z.


Windows 11, 63bcca4 + this PR:

  • cargo test -p victory-loader: 36 passed / 0 failed (lib; unpatched 63bcca4 has 35), plus the integration suites (1 and 2 passed).
  • New test: crash::tests::clzero_loop_advance_stops_before_u64_wraparound.
  • cargo clippy -p victory-loader --all-targets -- -D warnings: clean.

The lib suite includes the earlier clzero_loop_wrapping_ranges_do_not_overflow_and_fall_back_to_single_clwb and the emulated-loop tests, which still pass with advance_line returning None at the wrap. Synthetic only. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204719603 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3256#issuecomment-7204719603). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T22:29:07Z. --- Windows 11, `63bcca4` + this PR: - `cargo test -p victory-loader`: 36 passed / 0 failed (lib; unpatched `63bcca4` has 35), plus the integration suites (1 and 2 passed). - New test: `crash::tests::clzero_loop_advance_stops_before_u64_wraparound`. - `cargo clippy -p victory-loader --all-targets -- -D warnings`: clean. The lib suite includes the earlier `clzero_loop_wrapping_ranges_do_not_overflow_and_fall_back_to_single_clwb` and the emulated-loop tests, which still pass with `advance_line` returning `None` at the wrap. Synthetic only. AI-assisted.
fuzeofficlal commented 2026-11-27 00:28:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T00:28:30Z.

Review: CHANGES_REQUESTED


Tested d990cd7abea3637964677d18bb68551e795377e3 with cargo xtask policy, cargo clippy --tests -p victory-loader -- -D warnings, and cargo test --locked -p victory-loader (40/40 pass).

Two issues in crates/victory-loader/src/crash.rs need to be addressed before merging:

  1. Second clzero loop skips the final in-bounds cache line on wraparound, and both loops exit with inconsistent register/EFlags state (crates/victory-loader/src/crash.rs:414-426, 444-459)

    • In the second loop (top: mov rax, a; add a, 0x40; clzero; cmp a, b; jb top, lines 444–459), suppose next = *gpr(context, a) = u64::MAX - 0x3f and limit = u64::MAX. Line 447 sees next < limit, line 450 overwrites context.Rax = next, and line 451 breaks on advance_line(next) == None before zero(next) ever runs, silently skipping the final in-bounds cache line ([u64::MAX - 0x3f ..= u64::MAX]) — asymmetric with the first loop, which calls zero() before advance_line().
    • In both loops, breaking on None still executes context.Rip += 12 (line 425) / context.Rip += 8 (line 458) to exit past jb top, while leaving context.Rax / *gpr(context, a) un-advanced and setting EFlags with CF = 1 (cmp_flags(x, limit) where x < limit), which is impossible after a non-taken jb.
    • Compare with this file's existing clwb wraparound convention (crash.rs:1083-1120, clwb_loop_wrapping_ranges_do_not_overflow_and_fall_back_to_single_clwb): when a loop's + 0x40 step would wrap, fall back to emulating just the single clzero instruction (zero(context.Rax); context.Rip += 3; return true; — or break out to context.Rip += 3 before mutating context.Rax / a / EFlags) so the CPU executes add; cmp; jb natively with exact architectural state instead of fabricating a loop exit.
  2. Detached /// # Safety doc comment (crates/victory-loader/src/crash.rs:391-399)

    • fn advance_line(rax: u64) was inserted directly between unsafe fn clzero's doc comment (lines 375–393, including /// # Safety) and unsafe fn clzero (line 399), attaching clzero's documentation and # Safety contract to the safe helper advance_line and leaving unsafe fn clzero undocumented. Please place advance_line above clzero's doc comment (or after clzero).

Also, clzero_loop_advance_stops_before_u64_wraparound (crash.rs:1304-1309) only exercises u64::checked_add(0x40) directly rather than the clzero loop fallback behavior.

<!-- greyfinch-sync:Marvisak/VIctory:review:6081963054 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3256#pullrequestreview-6081963054). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-27T00:28:30Z. **Review:** CHANGES_REQUESTED --- Tested `d990cd7abea3637964677d18bb68551e795377e3` with `cargo xtask policy`, `cargo clippy --tests -p victory-loader -- -D warnings`, and `cargo test --locked -p victory-loader` (40/40 pass). Two issues in `crates/victory-loader/src/crash.rs` need to be addressed before merging: 1. **Second `clzero` loop skips the final in-bounds cache line on wraparound, and both loops exit with inconsistent register/`EFlags` state (`crates/victory-loader/src/crash.rs:414-426, 444-459`)** - In the second loop (`top: mov rax, a; add a, 0x40; clzero; cmp a, b; jb top`, lines 444–459), suppose `next = *gpr(context, a) = u64::MAX - 0x3f` and `limit = u64::MAX`. Line 447 sees `next < limit`, line 450 overwrites `context.Rax = next`, and line 451 breaks on `advance_line(next) == None` **before `zero(next)` ever runs**, silently skipping the final in-bounds cache line (`[u64::MAX - 0x3f ..= u64::MAX]`) — asymmetric with the first loop, which calls `zero()` before `advance_line()`. - In both loops, breaking on `None` still executes `context.Rip += 12` (line 425) / `context.Rip += 8` (line 458) to exit past `jb top`, while leaving `context.Rax` / `*gpr(context, a)` un-advanced and setting `EFlags` with `CF = 1` (`cmp_flags(x, limit)` where `x < limit`), which is impossible after a non-taken `jb`. - Compare with this file's existing `clwb` wraparound convention (`crash.rs:1083-1120`, `clwb_loop_wrapping_ranges_do_not_overflow_and_fall_back_to_single_clwb`): when a loop's `+ 0x40` step would wrap, fall back to emulating just the single `clzero` instruction (`zero(context.Rax); context.Rip += 3; return true;` — or break out to `context.Rip += 3` before mutating `context.Rax` / `a` / `EFlags`) so the CPU executes `add; cmp; jb` natively with exact architectural state instead of fabricating a loop exit. 2. **Detached `/// # Safety` doc comment (`crates/victory-loader/src/crash.rs:391-399`)** - `fn advance_line(rax: u64)` was inserted directly between `unsafe fn clzero`'s doc comment (lines 375–393, including `/// # Safety`) and `unsafe fn clzero` (line 399), attaching `clzero`'s documentation and `# Safety` contract to the safe helper `advance_line` and leaving `unsafe fn clzero` undocumented. Please place `advance_line` above `clzero`'s doc comment (or after `clzero`). Also, `clzero_loop_advance_stops_before_u64_wraparound` (`crash.rs:1304-1309`) only exercises `u64::checked_add(0x40)` directly rather than the `clzero` loop fallback behavior.
futurepro62 commented 2026-11-27 23:31:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-27T23:31:13Z.


NVIDIA full-suite run, to go with the reports above by Dukestar69, Jackster, since none of them names a GPU run. Tested exact head d990cd7 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 867 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: clzero_loop_advance_stops_before_u64_wraparound.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7230203600 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3256#issuecomment-7230203600). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-27T23:31:13Z. --- NVIDIA full-suite run, to go with the reports above by Dukestar69, Jackster, since none of them names a GPU run. Tested exact head `d990cd7` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **867 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `clzero_loop_advance_stops_before_u64_wraparound`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
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/3256:upstream-pr/3256'
git switch 'upstream-pr/3256'

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/3256'
git switch 'upstream-pr/3256'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3256'
git switch 'upstream-pr/3256'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3256'
git switch 'main'
git merge --squash 'upstream-pr/3256'
git switch 'main'
git merge --ff-only 'upstream-pr/3256'
git switch 'main'
git merge 'upstream-pr/3256'
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!3254
No description provided.