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

Merged
arbelonson-source merged 1 commit from upstream-pr/1380 into main 2026-11-26 23:06:33 +00:00
arbelonson-source commented 2026-11-26 19:38:03 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by arbelonson-source on 2026-11-26T19:38:03Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:issue:5516124613 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1380). Originally posted by [arbelonson-source](https://github.com/arbelonson-source) on 2026-11-26T19:38:03Z. --- ## 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.
JayFoxRox commented 2026-11-26 20:11:49 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by JayFoxRox on 2026-11-26T20:11:49Z.

Code review location: crates/victory-loader/src/console_insn.rs; line 258

@@ -226,6 +229,38 @@ fn writable_to(va: usize, end: usize) -> Option<usize> {
     Some(to.min(end))
 }
 
+/// Zeroes the 64-byte line at `line` with ordinary stores, for a line [`writable`] refused that
+/// is committed or reserved all the same: write-protected on purpose, as the write-watch guards
+/// over the host copies of view memory make it (`watch.rs`), whose vectored handler is first in
+/// the chain and opens a guarded block for the title's own stores. The console's `clzero`
+/// faults into such a handler as any store does; here it is an illegal instruction the handler
+/// never sees, so refusing the line left that exception unhandled and ended the run. These
+/// stores fault inside this handler instead, the guard opens the block, and the stores
+/// complete (a fault nothing resolves ends the run, as the title's own store would). False,
+/// with nothing written, for free memory: the exception stays the title's.
+///
+/// # Safety
+/// The title owns the line, as it owns the line of the instruction being emulated.
+unsafe fn zero_through_fault(line: usize) -> bool {
+    use windows_sys::Win32::System::Memory::{MEM_COMMIT, MEM_RESERVE, MEMORY_BASIC_INFORMATION, VirtualQuery};
+    // SAFETY: plain data filled in by the query.
+    let mut m: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() };
+    // SAFETY: a valid out pointer of the right size.
+    let found = unsafe { VirtualQuery(line as *const _, &mut m, size_of_val(&m)) } != 0;
+    ASKED.fetch_add(1, Relaxed);
+    if !found || (m.State != MEM_COMMIT && m.State != MEM_RESERVE) {
+        return false;
+    }
+    let words = line as *mut u64;
+    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) };

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

<!-- greyfinch-sync:fxceless/victory-fork:review-comment:4712982063 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1380#discussion_r4712982063). Originally posted by [JayFoxRox](https://github.com/JayFoxRox) on 2026-11-26T20:11:49Z. **Code review location:** `crates/victory-loader/src/console_insn.rs`; line 258 ~~~~diff @@ -226,6 +229,38 @@ fn writable_to(va: usize, end: usize) -> Option<usize> { Some(to.min(end)) } +/// Zeroes the 64-byte line at `line` with ordinary stores, for a line [`writable`] refused that +/// is committed or reserved all the same: write-protected on purpose, as the write-watch guards +/// over the host copies of view memory make it (`watch.rs`), whose vectored handler is first in +/// the chain and opens a guarded block for the title's own stores. The console's `clzero` +/// faults into such a handler as any store does; here it is an illegal instruction the handler +/// never sees, so refusing the line left that exception unhandled and ended the run. These +/// stores fault inside this handler instead, the guard opens the block, and the stores +/// complete (a fault nothing resolves ends the run, as the title's own store would). False, +/// with nothing written, for free memory: the exception stays the title's. +/// +/// # Safety +/// The title owns the line, as it owns the line of the instruction being emulated. +unsafe fn zero_through_fault(line: usize) -> bool { + use windows_sys::Win32::System::Memory::{MEM_COMMIT, MEM_RESERVE, MEMORY_BASIC_INFORMATION, VirtualQuery}; + // SAFETY: plain data filled in by the query. + let mut m: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() }; + // SAFETY: a valid out pointer of the right size. + let found = unsafe { VirtualQuery(line as *const _, &mut m, size_of_val(&m)) } != 0; + ASKED.fetch_add(1, Relaxed); + if !found || (m.State != MEM_COMMIT && m.State != MEM_RESERVE) { + return false; + } + let words = line as *mut u64; + 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) }; ~~~~ --- 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 commented 2026-11-26 20:15:05 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by JayFoxRox on 2026-11-26T20:15:05Z.

Code review location: crates/victory-loader/src/console_insn.rs; line 399

@@ -395,11 +428,12 @@ pub(crate) unsafe fn emulate(ctx: &mut CONTEXT) -> i32 {
             // with an address-size prefix (0x67) the line is at EAX, zero-extended
             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

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

<!-- greyfinch-sync:fxceless/victory-fork:review-comment:4713003460 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1380#discussion_r4713003460). Originally posted by [JayFoxRox](https://github.com/JayFoxRox) on 2026-11-26T20:15:05Z. **Code review location:** `crates/victory-loader/src/console_insn.rs`; line 399 ~~~~diff @@ -395,11 +428,12 @@ pub(crate) unsafe fn emulate(ctx: &mut CONTEXT) -> i32 { // with an address-size prefix (0x67) the line is at EAX, zero-extended 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 ~~~~ --- 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).
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
fxceless/victory-fork!1380
No description provided.