clzero on a guarded line faults into the guard instead of ending the run #1380

Manually merged
github merged 1 commit from github-bot/gh-1380 into gh-pr/1380/base 2026-11-27 01:04:10 +00:00 AGit
Owner

@⁠arbelonson-source on GitHub · 2026-11-26 19:38 UTC · original
arbelonson-source:clzero-protected-lines → main · +151 −13 · 1 files · 1 commits · merged 2026-11-26 23:06 UTC

Scenario

The clzero emulation in console_insn.rs declines a line that writable refuses ("the real instruction faults there too"). On the console that fault is an access violation, and the write-watch guard's vectored handler (watch.rs, first in the chain) resolves it for a guarded block like any other store of the title. Here the exception is STATUS_ILLEGAL_INSTRUCTION, which the guard never sees, so a clzero on a guarded line is left unhandled and the run ends.

Seen as 0xc000001d on the 12-byte loop clzero; add rax, 40h; cmp rax, r10; jb (0f 01 fc 48 83 c0 40 4c 39 d0 72 f4), 22 minutes into the opening, on an Intel i9-9900K (no CLZERO). That run used a build derived from this tree (Kelvaroq 0.2.0, which has its own copy of the emulation with the same early return); the code path here is the same.

Change

  • A line that is committed or reserved but not writable is zeroed with ordinary stores from the handler (zero_through_fault). They fault, the guard opens the block, and they complete. writable_to's remembered regions already rely on this ("the write then faults inside the handler, which the guard on view memory opens").
  • The 12-byte loop zeroes that first line this way and goes on at the next one. The 15-byte loop leaves it to the lone instruction.
  • Free memory is still declined, so the exception stays the title's.
  • The five-second line counts these lines ("lines zeroed through a fault").

Proof

New test clzero_on_guarded_or_uncommitted_lines_faults_into_the_runtime_s_handlers: a read-only line, a reserved line, and the 12-byte loop starting on reserved memory, with a stand-in handler at the front of the chain that opens the test's pages. Free memory is still refused.

  • With the change: victory-loader tests 28/28 pass.
  • With zero_through_fault forced to refuse (the old behaviour): the new test fails at its first emulation.
  • cargo clippy -p victory-loader --all-targets: no warnings.

Tested by cross-building with cargo xwin (Rust 1.98.1, x86_64-pc-windows-msvc) and running the test binary under Wine 11.22 on Linux. It has not been run on Windows.

In the derived build, the same fix (to its own copy of the emulation) ran a 22-minute session in which 77 lines went through the fault; each had been a crash before.

**@⁠arbelonson-source** on GitHub · 2026-11-26 19:38 UTC · [original](https://github.com/fxceless/victory-fork/pull/1380) `arbelonson-source:clzero-protected-lines` → `main` · +151 −13 · 1 files · 1 commits · merged 2026-11-26 23:06 UTC ## Scenario The `clzero` emulation in `console_insn.rs` declines a line that `writable` refuses ("the real instruction faults there too"). On the console that fault is an access violation, and the write-watch guard's vectored handler (`watch.rs`, first in the chain) resolves it for a guarded block like any other store of the title. Here the exception is `STATUS_ILLEGAL_INSTRUCTION`, which the guard never sees, so a `clzero` on a guarded line is left unhandled and the run ends. Seen as `0xc000001d` on the 12-byte loop `clzero; add rax, 40h; cmp rax, r10; jb` (`0f 01 fc 48 83 c0 40 4c 39 d0 72 f4`), 22 minutes into the opening, on an Intel i9-9900K (no CLZERO). That run used a build derived from this tree (Kelvaroq 0.2.0, which has its own copy of the emulation with the same early return); the code path here is the same. ## Change - A line that is committed or reserved but not writable is zeroed with ordinary stores from the handler (`zero_through_fault`). They fault, the guard opens the block, and they complete. `writable_to`'s remembered regions already rely on this ("the write then faults inside the handler, which the guard on view memory opens"). - The 12-byte loop zeroes that first line this way and goes on at the next one. The 15-byte loop leaves it to the lone instruction. - Free memory is still declined, so the exception stays the title's. - The five-second line counts these lines ("lines zeroed through a fault"). ## Proof New test `clzero_on_guarded_or_uncommitted_lines_faults_into_the_runtime_s_handlers`: a read-only line, a reserved line, and the 12-byte loop starting on reserved memory, with a stand-in handler at the front of the chain that opens the test's pages. Free memory is still refused. - With the change: `victory-loader` tests 28/28 pass. - With `zero_through_fault` forced to refuse (the old behaviour): the new test fails at its first emulation. - `cargo clippy -p victory-loader --all-targets`: no warnings. Tested by cross-building with `cargo xwin` (Rust 1.98.1, `x86_64-pc-windows-msvc`) and running the test binary under Wine 11.22 on Linux. It has not been run on Windows. In the derived build, the same fix (to its own copy of the emulation) ran a 22-minute session in which 77 lines went through the fault; each had been a crash before.
The emulation of `clzero` (a lone one, the 12-byte loop and the 15-byte
loop) declines a line `writable` refuses: "the real instruction faults
there too". It does, but on the console that fault is an access violation,
which the write-watch guard's vectored handler (first in the chain) resolves
for a guarded block like any store of the title. Here the exception is an
illegal instruction the guard never sees, so a `clzero` on a guarded line
was left unhandled and ended the run (0xc000001d at the 12-byte loop
`clzero; add rax, 40h; cmp rax, r10; jb`, 22 minutes into the opening on
a CPU without CLZERO, in a build derived from this tree).

A line that is committed or reserved but not writable is now zeroed with
ordinary stores from the handler: they fault, the guard opens the block,
and they complete, as `writable_to`'s remembered regions already rely on.
The 12-byte loop zeroes that first line so and goes on at the next one;
the 15-byte loop leaves it to the lone instruction. Free memory is still
declined. The five-second line counts these lines.

Test: a read-only line, a reserved line and the 12-byte loop starting on
reserved memory, with a stand-in handler that opens the test's pages; free
memory is still refused. It fails without the change (the first emulation
is declined) and passes with it; the crate's other 27 tests pass.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:47:30 +00:00
@ -229,0 +255,4 @@
for i in 0..8 {
// SAFETY: see the function's contract; a fault here is resolved by the runtime's
// handlers or ends the run, as the title's own store would.
unsafe { words.add(i).write_volatile(0) };
Author
Owner

@⁠JayFoxRox on GitHub · 2026-11-26 20:11 UTC · original

I believe this PR attempts to generate a fault for the emulated clzero instruction for writes into guarded pages (as other subsystems like the GPU buffer update depend on it)?

I think there's an issue with the planned instruction emulation where they will cause more page faults than a real CPU would.
A single 64 byte line clear (which would be a single page fault) must be emulated using multiple accesses (each of which could fail with its own page fault).

In #1305 I propose 2 views on most memory, where an all-access page could be used for every access except one, which goes through the normal page. That individual access could be used to trigger a real page fault (or just pass, no special handling necessary to separate the guard page case).

**@⁠JayFoxRox** on GitHub · 2026-11-26 20:11 UTC · [original](https://github.com/fxceless/victory-fork/pull/1380#discussion_r4712982063) I believe this PR attempts to generate a fault for the emulated `clzero` instruction for writes into guarded pages (as other subsystems like the GPU buffer update depend on it)? I think there's an issue with the planned instruction emulation where they will cause more page faults than a real CPU would. A single 64 byte line clear (which would be a single page fault) must be emulated using multiple accesses (each of which could fail with its own page fault). In #1305 I propose 2 views on most memory, where an all-access page could be used for every access except one, which goes through the normal page. That individual access could be used to trigger a real page fault (or just pass, no special handling necessary to separate the guard page case).
@ -396,4 +429,2 @@
let a32 = code[..len - 3].contains(&0x67);
let line = (if a32 { ctx.Rax & 0xFFFF_FFFF } else { ctx.Rax } as usize) & !63;
if !writable(line, 64) {
return 0; // the real instruction faults there too
Author
Owner

@⁠JayFoxRox on GitHub · 2026-11-26 20:15 UTC · original · edited 2026-11-26 20:17 UTC

I think this entire PR bascially boils down to "this writable check must be removed"?
Just let the second fault handler deal with the page faults?

Why is all the other new code suddenly necessary?
It pretty much looks like the zero_through_fault is the same as unsafe { std::ptr::write_bytes(line as *mut u8, 0, 64) }; with extra steps and bloat.

If there's an issue with double-faults, then look into #1363 which would avoid it (as emulated instructions would get patched into the game code, and run outside the exception handler/state).

**@⁠JayFoxRox** on GitHub · 2026-11-26 20:15 UTC · [original](https://github.com/fxceless/victory-fork/pull/1380#discussion_r4713003460) · edited 2026-11-26 20:17 UTC I think this entire PR bascially boils down to "this `writable` check must be removed"? Just let the second fault handler deal with the page faults? Why is all the other new code suddenly necessary? It pretty much looks like the `zero_through_fault` is the same as `unsafe { std::ptr::write_bytes(line as *mut u8, 0, 64) };` with extra steps and bloat. If there's an issue with double-faults, then look into #1363 which would avoid it (as emulated instructions would get patched into the game code, and run outside the exception handler/state).
github-bot changed target branch from main to gh-pr/1380/base 2026-11-27 01:04:09 +00:00
github manually merged commit 95a2034a21 into gh-pr/1380/base 2026-11-27 01:04:10 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.