Loader: a clzero on a guarded line goes through the guard, not out of the run (Intel 0xc000001d; port of fxceless#1380) #698

Manually merged
github merged 1 commit from github-bot/gh-698 into main 2026-11-29 08:20:58 +00:00 AGit
Owner

@⁠kibagami-nc on GitHub · 2026-11-28 03:39 UTC · original
kibagami-nc:fix/clzero-on-guarded-line → main · +131 −6 · 1 files · 1 commits · merged 2026-11-29 08:16 UTC

The problem

On a CPU without clzero (Intel), the emulation in crates/victory-loader/src/console_insn.rs declines a line that writable refuses. The code reads return 0; // the real instruction faults there too.

On the console that fault is an access violation. The write watch's vectored handler resolves it for a guarded block, like any store of the title. Here the exception is an illegal instruction, which the guard never sees, so a clzero on a write-protected line of title memory was left unhandled and the run ended.

This was seen as 0xc000001d on the 12-byte loop clzero; add rax, 40h; cmp rax, r10; jb, 22 minutes into the opening, on an Intel i9-9900K. The report is in fxceless/victory-fork#1380, on a tree with the same early return.

The change

zero_through_fault(line) handles a line that is committed or reserved but not writable. It zeroes the line with ordinary stores from the handler: they fault, the guard opens the block, and they complete. writable_to's remembered regions already rely on that ("the write then faults inside the handler, which the guard on view memory opens").

  • The lone instruction does this for its line.
  • The 12-byte loop does it for its first line, then goes on at the next one, where the same instruction runs again.
  • On free memory nothing is written, and the exception stays the title's, as before.

This is a port of the fix in fxceless/victory-fork#1380 (arbelonson-source). It is cut to what this tree's emulation needs. That fork's console_insn.rs has more that this one does not, such as per-kind counts and the 15-byte loop taken whole.

Tests

  • New console_insn::tests::clzero_on_a_guarded_line_goes_through_the_guard. It uses a read-only page and a vectored handler that opens the page on a write, as the guard does. It covers:

    • a lone clzero on that page: the line is zeroed and nothing else is touched;
    • the 12-byte loop with its first line there: that line is zeroed and rax moves on to the next;
    • free memory: the emulation leaves it, as before.

    With the two early returns put back, it fails at the first case.

  • cargo test -p victory-loader --locked --lib: 46 passed, three runs.

  • cargo clippy -p victory-loader --all-targets -- -D warnings, with and without --all-features: no warnings.

  • It merges cleanly with #572, which changes the same file elsewhere (the stubs' regions).

Checked by others

nick0451 (i9-14900K, RTX 3090, Windows 11) merged this onto main at 63b902f:

  • Tests: cargo test -p victory-loader --lib --locked passed 46 tests in 5 of 5 runs, and clippy was clean. With the early returns put back, the new test fails at its first case.
  • In the game: a release build, Series S, 5 minutes in the world: no crash, and nothing new in the log.
  • How often the new path runs: a local log line showed it was taken 0 times in those 5 minutes. On this tree the 12-byte loop is patched with a stub at its first fault, and the stub's stores fault into the guard like any other store. So this path matters where the patch is not made (stubs out of reach, VICTORY_PATCH_CLZERO=0) and for a lone clzero. That fits the crash 22 minutes in, rather than one in every run.

No game files, dumps or extracted data are included.

**@⁠kibagami-nc** on GitHub · 2026-11-28 03:39 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/698) `kibagami-nc:fix/clzero-on-guarded-line` → `main` · +131 −6 · 1 files · 1 commits · merged 2026-11-29 08:16 UTC ## The problem On a CPU without `clzero` (Intel), the emulation in `crates/victory-loader/src/console_insn.rs` declines a line that `writable` refuses. The code reads `return 0; // the real instruction faults there too`. On the console that fault is an access violation. The write watch's vectored handler resolves it for a guarded block, like any store of the title. Here the exception is an illegal instruction, which the guard never sees, so a `clzero` on a write-protected line of title memory was left unhandled and the run ended. This was seen as `0xc000001d` on the 12-byte loop `clzero; add rax, 40h; cmp rax, r10; jb`, 22 minutes into the opening, on an Intel i9-9900K. The report is in fxceless/victory-fork#1380, on a tree with the same early return. ## The change `zero_through_fault(line)` handles a line that is committed or reserved but not writable. It zeroes the line with ordinary stores from the handler: they fault, the guard opens the block, and they complete. `writable_to`'s remembered regions already rely on that ("the write then faults inside the handler, which the guard on view memory opens"). - The lone instruction does this for its line. - The 12-byte loop does it for its first line, then goes on at the next one, where the same instruction runs again. - On free memory nothing is written, and the exception stays the title's, as before. This is a port of the fix in [fxceless/victory-fork#1380](https://github.com/fxceless/victory-fork/pull/1380) (arbelonson-source). It is cut to what this tree's emulation needs. That fork's `console_insn.rs` has more that this one does not, such as per-kind counts and the 15-byte loop taken whole. ## Tests - New `console_insn::tests::clzero_on_a_guarded_line_goes_through_the_guard`. It uses a read-only page and a vectored handler that opens the page on a write, as the guard does. It covers: - a lone `clzero` on that page: the line is zeroed and nothing else is touched; - the 12-byte loop with its first line there: that line is zeroed and `rax` moves on to the next; - free memory: the emulation leaves it, as before. With the two early returns put back, it fails at the first case. - `cargo test -p victory-loader --locked --lib`: 46 passed, three runs. - `cargo clippy -p victory-loader --all-targets -- -D warnings`, with and without `--all-features`: no warnings. - It merges cleanly with #572, which changes the same file elsewhere (the stubs' regions). ## Checked by others **nick0451** (i9-14900K, RTX 3090, Windows 11) merged this onto `main` at `63b902f`: - **Tests:** `cargo test -p victory-loader --lib --locked` passed 46 tests in 5 of 5 runs, and clippy was clean. With the early returns put back, the new test fails at its first case. - **In the game:** a release build, Series S, 5 minutes in the world: no crash, and nothing new in the log. - **How often the new path runs:** a local log line showed it was taken 0 times in those 5 minutes. On this tree the 12-byte loop is patched with a stub at its first fault, and the stub's stores fault into the guard like any other store. So this path matters where the patch is not made (stubs out of reach, `VICTORY_PATCH_CLZERO=0`) and for a lone `clzero`. That fits the crash 22 minutes in, rather than one in every run. No game files, dumps or extracted data are included.
The clzero emulation declined a line that writable() refuses ("the real
instruction faults there too"). On the console that fault is an access
violation, which the write watch's vectored handler resolves for a guarded
block like any store of the title. Here the exception is an illegal
instruction, which the guard never sees, so a clzero on a write-protected
line of title memory was left unhandled and the run ended: 0xc000001d on
the 12-byte loop, 22 minutes into the opening on an Intel i9-9900K (no
CLZERO).

A line that is committed or reserved but not writable is now zeroed with
ordinary stores from the handler (zero_through_fault): they fault, the
guard opens the block, they complete, as writable_to's remembered regions
already rely on. The lone instruction does it for its line; the 12-byte
loop does it for its first line and goes on at the next. Free memory is
still the title's exception.

Port of the fix in fxceless/victory-fork#1380 (arbelonson-source), cut to
what this tree's emulation needs: that fork's console_insn.rs has more
(per-kind counts, the 15-byte loop taken whole) that this one does not.

Test clzero_on_a_guarded_line_goes_through_the_guard: a read-only page and
a vectored handler that opens it on a write, as the guard does; a lone
clzero on it, then the loop with its first line there, then free memory.
Before the change the emulation refused the guarded line.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 03:43:42 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-28 04:26 UTC · original · approved these changes

LGTM — verified on Windows:

  • cargo test -p victory-loader passed (46/46 unit tests including clzero_on_a_guarded_line_goes_through_the_guard + 3/3 integration tests)
  • cargo clippy -p victory-loader --all-targets -- -D warnings passed with 0 warnings
  • cargo xtask policy passed (294 files ok)

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

**@⁠Dukestar69** on GitHub · 2026-11-28 04:26 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/698#pullrequestreview-6095802753) · **approved these changes** LGTM — verified on Windows: - `cargo test -p victory-loader` passed (46/46 unit tests including `clzero_on_a_guarded_line_goes_through_the_guard` + 3/3 integration tests) - `cargo clippy -p victory-loader --all-targets -- -D warnings` passed with 0 warnings - `cargo xtask policy` passed (`294 files ok`) Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠nick0451 on GitHub · 2026-11-28 07:19 UTC · original

Tested on Intel: i9-14900K, RTX 3090, Windows 11. This PR merged onto main at 63b902f.

  • cargo test -p victory-loader --lib --locked: 46 passed in 5 of 5 runs (default parallel threads). cargo clippy -p victory-loader --all-targets --locked -- -D warnings: clean.
  • With the two early returns put back, clzero_on_a_guarded_line_goes_through_the_guard fails at its first case (a clzero on a guarded line), as the description says. So the test covers the change.
  • In the game: a release build, Series S, my own save, 5 minutes in the world, walking around. No crash and nothing new in the log.
  • I added a local log line in zero_through_fault (not part of this PR) to see whether the path is taken in play. It was taken 0 times in those 5 minutes. On this tree the 12-byte loop is replaced by the stub at its first fault (ClzeroLoop { end_reg: 10 } was emulated once, then patched), and the stub's plain stores fault into the guard like any other store. So the new path should only matter where the patch is not made (stubs out of reach, VICTORY_PATCH_CLZERO=0) or for a lone clzero. That fits a crash 22 minutes in rather than one in every run.

The code reads right to me: the loop advances rax by one line and runs the same instruction again, by which time the guard has opened the block, and free memory still returns 0 as before. Looks good to merge.

Tested with an AI assistant (Claude Code); results are from this PC.

**@⁠nick0451** on GitHub · 2026-11-28 07:19 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/698#issuecomment-7235859101) Tested on Intel: i9-14900K, RTX 3090, Windows 11. This PR merged onto main at `63b902f`. - `cargo test -p victory-loader --lib --locked`: 46 passed in 5 of 5 runs (default parallel threads). `cargo clippy -p victory-loader --all-targets --locked -- -D warnings`: clean. - With the two early returns put back, `clzero_on_a_guarded_line_goes_through_the_guard` fails at its first case (`a clzero on a guarded line`), as the description says. So the test covers the change. - In the game: a release build, Series S, my own save, 5 minutes in the world, walking around. No crash and nothing new in the log. - I added a local log line in `zero_through_fault` (not part of this PR) to see whether the path is taken in play. It was taken 0 times in those 5 minutes. On this tree the 12-byte loop is replaced by the stub at its first fault (`ClzeroLoop { end_reg: 10 }` was emulated once, then patched), and the stub's plain stores fault into the guard like any other store. So the new path should only matter where the patch is not made (stubs out of reach, `VICTORY_PATCH_CLZERO=0`) or for a lone `clzero`. That fits a crash 22 minutes in rather than one in every run. The code reads right to me: the loop advances `rax` by one line and runs the same instruction again, by which time the guard has opened the block, and free memory still returns 0 as before. Looks good to merge. Tested with an AI assistant (Claude Code); results are from this PC.
github manually merged commit 664b300207 into main 2026-11-29 08:20:58 +00:00
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/trustno1-coder--VIctory-NVIDIA!698
No description provided.