Loader: a clwb in the last bytes of a page is emulated instead of faulting #941

Manually merged
github merged 1 commit from gh-pr/941/head into gh-pr/941/base 2026-11-26 04:14:35 +00:00
Owner

@⁠BastienRodz on GitHub · 2026-11-24 14:23 UTC · original
BastienRodz:loader/clwb-near-page-end → main · +68 −28 · 1 files · 1 commits · merged 2026-11-25 00:15 UTC

What's wrong

As #924 describes, emulate_amd_only (crates/victory-loader/src/crash.rs) copies only the bytes left in the faulting instruction's page, then calls clwb_len only when at least 15 were copied, because clwb_len took a [u8; 15]:

if n >= 15
    && let Some(len) = clwb_len(bytes[..15].try_into().unwrap_or([0; 15]))

A clwb m8 is 4 to 10 bytes. One that starts in the last 14 bytes of a page is not emulated, and on Intel CPUs before Ice Lake the title dies with STATUS_ILLEGAL_INSTRUCTION. The same happens to a write-back loop that straddles the page end: clwb_loop cannot match the cut bytes, and the single-instruction fallback is skipped too.

What the fix does

clwb_len takes &[u8], as clwb_loop already does. It reads every byte through first/get, so a prefix, ModRM, SIB or displacement past the slice gives None instead of a panic. It returns Some(len) only when len <= code.len(). emulate_amd_only drops the n >= 15 guard and passes &bytes[..n]. The decoding is otherwise unchanged.

Tests

  • crash::tests::clwb_lengths: the existing cases, now passing slices.
  • crash::tests::a_clwb_at_the_end_of_a_page_is_decoded, which has three parts:
    • Exact-length 4- and 5-byte slices decode.
    • Truncated forms give None without panicking: disp32 6 of 8 bytes, disp8 4 of 5, SIB missing, SIB with disp32 cut short, ModRM missing, 1 byte, and empty.
    • emulate_amd_only on a 4-byte and a 5-byte clwb placed at the very end of a page, followed by a PAGE_NOACCESS page, steps over each one.
  • On main, the page-end emulate_amd_only case fails at assert!(emulate_amd_only(&mut pointers)): n = 4 < 15 skips clwb_len, and the handler returns false.

On GitHub's windows-latest with the upstream CI's commands (clippy -D warnings with and without --all-features, then cargo test --workspace --locked), everything passes, and victory-loader has 23 passed, 0 failed: https://github.com/BastienRodz/VIctory/actions/runs/45413158495

On a real pre-Ice Lake CPU (@⁠hadrianhola, comment): i7-5820K (Haswell-E, no clwb), Windows 10, a real clwb raising #UD through the installed handler, placed in the last 4 and 5 bytes of a page. On main the process dies with 0xc000001d. With this PR it passes, 10 of 10 runs. The control, the same clwb mid-page, passes on both.

No game data used. AI-assisted: implemented with Gemini 4 Neon, reviewed and verified with Claude Code.

Fixes #924.

🤖 Generated with Claude Code

**@⁠BastienRodz** on GitHub · 2026-11-24 14:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/941) `BastienRodz:loader/clwb-near-page-end` → `main` · +68 −28 · 1 files · 1 commits · merged 2026-11-25 00:15 UTC ## What's wrong As #924 describes, `emulate_amd_only` (`crates/victory-loader/src/crash.rs`) copies only the bytes left in the faulting instruction's page, then calls `clwb_len` only when at least 15 were copied, because `clwb_len` took a `[u8; 15]`: ```rust if n >= 15 && let Some(len) = clwb_len(bytes[..15].try_into().unwrap_or([0; 15])) ``` A `clwb m8` is 4 to 10 bytes. One that starts in the last 14 bytes of a page is not emulated, and on Intel CPUs before Ice Lake the title dies with `STATUS_ILLEGAL_INSTRUCTION`. The same happens to a write-back loop that straddles the page end: `clwb_loop` cannot match the cut bytes, and the single-instruction fallback is skipped too. ## What the fix does `clwb_len` takes `&[u8]`, as `clwb_loop` already does. It reads every byte through `first`/`get`, so a prefix, ModRM, SIB or displacement past the slice gives `None` instead of a panic. It returns `Some(len)` only when `len <= code.len()`. `emulate_amd_only` drops the `n >= 15` guard and passes `&bytes[..n]`. The decoding is otherwise unchanged. ## Tests - `crash::tests::clwb_lengths`: the existing cases, now passing slices. - `crash::tests::a_clwb_at_the_end_of_a_page_is_decoded`, which has three parts: - Exact-length 4- and 5-byte slices decode. - Truncated forms give `None` without panicking: disp32 6 of 8 bytes, disp8 4 of 5, SIB missing, SIB with disp32 cut short, ModRM missing, 1 byte, and empty. - `emulate_amd_only` on a 4-byte and a 5-byte `clwb` placed at the very end of a page, followed by a `PAGE_NOACCESS` page, steps over each one. - On `main`, the page-end `emulate_amd_only` case fails at `assert!(emulate_amd_only(&mut pointers))`: `n = 4 < 15` skips `clwb_len`, and the handler returns `false`. On GitHub's `windows-latest` with the upstream CI's commands (clippy `-D warnings` with and without `--all-features`, then `cargo test --workspace --locked`), everything passes, and `victory-loader` has 23 passed, 0 failed: https://github.com/BastienRodz/VIctory/actions/runs/45413158495 **On a real pre-Ice Lake CPU** (@⁠hadrianhola, [comment](https://git.gtahub.st/github/Marvisak--VIctory/pulls/941)): i7-5820K (Haswell-E, no `clwb`), Windows 10, a real `clwb` raising `#UD` through the installed handler, placed in the last 4 and 5 bytes of a page. On `main` the process dies with `0xc000001d`. With this PR it passes, 10 of 10 runs. The control, the same `clwb` mid-page, passes on both. No game data used. AI-assisted: implemented with Gemini 4 Neon, reviewed and verified with Claude Code. Fixes #924. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
emulate_amd_only copies only the bytes left in the faulting page and
gated clwb_len on n >= 15 because clwb_len took a fixed [u8; 15]. A
4- to 10-byte clwb starting in the last 14 bytes of a page (or falling
back from a clwb_loop that straddles the page end) was therefore
skipped, leaving pre-Ice Lake Intel CPUs with an unhandled
STATUS_ILLEGAL_INSTRUCTION.

Change clwb_len to take &[u8], bounds-checking each byte read and
returning Some(len) only when len <= code.len(), and drop the n >= 15
guard in emulate_amd_only.

Fixes #924.

Co-Authored-By: Gemini 4 Neon <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:34 +00:00
github manually merged commit 60d2cf9cbc into gh-pr/941/base 2026-11-26 04:14:35 +00:00
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 15:40 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 60d2cf9: cargo test -p victory-loader passes, 26 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 15:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/941#issuecomment-7136429417) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 60d2cf9: `cargo test -p victory-loader` passes, 26 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠hadrianhola on GitHub · 2026-11-24 18:39 UTC · original

Tested on a real pre-Ice Lake CPU: Intel Core i7-5820K (Haswell-E; CPUID leaf 7 EBX bit 24 = 0, so no clwb), Windows 10 22H2 (19045.7846), Rust 1.98.1, debug. No game data used.

The tests in this PR call emulate_amd_only with a built context, so I added a local test (not part of this PR) that runs a real clwb and lets the CPU raise #UD through the installed handler. Two PAGE_EXECUTE_READWRITE pages; the clwb (66 0f ae 30 and 66 41 0f ae 31) sits in the last 4/5 bytes of the first page, with ret as the first byte of the second, and is called on a local variable:

result
main ffe310e, the same clwb at page offset 0x800 (control) passes
main ffe310e, clwb in the last bytes of the page process dies, 0xc000001d (STATUS_ILLEGAL_INSTRUCTION)
this PR (60d2cf9, merged onto ffe310e together with #214 and #362), same case passes, 10 of 10 runs

cargo test -p victory-loader --lib on that merge: everything else passes except unwinds_like_the_system_walk, which fails the same way on main here (#742).

The local test (in crash.rs's mod tests)
#[test]
fn local_native_clwb_at_page_end() {
    report_crashes();
    use windows_sys::Win32::System::Memory::{
        MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_EXECUTE_READWRITE, VirtualAlloc, VirtualFree,
    };
    let mut target = 0u64;
    let at = (&raw mut target) as u64;
    // SAFETY: two executable pages of ours; the code runs clwb on our variable, then returns.
    unsafe {
        let page = VirtualAlloc(std::ptr::null(), 0x2000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)
            .cast::<u8>();
        assert!(!page.is_null());
        for insn in [&[0x66, 0x0f, 0xae, 0x30][..], &[0x66, 0x41, 0x0f, 0xae, 0x31][..]] {
            let end = page.add(0x1000 - insn.len());
            end.copy_from_nonoverlapping(insn.as_ptr(), insn.len());
            *page.add(0x1000) = 0xc3; // ret, first byte of the next page
            std::arch::asm!("call {f}", f = in(reg) end, in("rax") at, in("r9") at,
                clobber_abi("C"));
        }
        VirtualFree(page.cast(), 0, MEM_RELEASE);
    }
}

The control run is the same test with page.add(0x800) for the clwb and the ret right after it.

AI-assisted (Claude Code); run on my machine.

**@⁠hadrianhola** on GitHub · 2026-11-24 18:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/941#issuecomment-7140963859) Tested on a real pre-Ice Lake CPU: Intel Core i7-5820K (Haswell-E; CPUID leaf 7 EBX bit 24 = 0, so no `clwb`), Windows 10 22H2 (19045.7846), Rust 1.98.1, debug. No game data used. The tests in this PR call `emulate_amd_only` with a built context, so I added a local test (not part of this PR) that runs a real `clwb` and lets the CPU raise `#UD` through the installed handler. Two `PAGE_EXECUTE_READWRITE` pages; the `clwb` (`66 0f ae 30` and `66 41 0f ae 31`) sits in the last 4/5 bytes of the first page, with `ret` as the first byte of the second, and is called on a local variable: | | result | |---|---| | `main` `ffe310e`, the same `clwb` at page offset `0x800` (control) | passes | | `main` `ffe310e`, `clwb` in the last bytes of the page | process dies, `0xc000001d` (`STATUS_ILLEGAL_INSTRUCTION`) | | this PR (`60d2cf9`, merged onto `ffe310e` together with #214 and #362), same case | passes, 10 of 10 runs | `cargo test -p victory-loader --lib` on that merge: everything else passes except `unwinds_like_the_system_walk`, which fails the same way on `main` here (#742). <details><summary>The local test (in <code>crash.rs</code>'s <code>mod tests</code>)</summary> ```rust #[test] fn local_native_clwb_at_page_end() { report_crashes(); use windows_sys::Win32::System::Memory::{ MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_EXECUTE_READWRITE, VirtualAlloc, VirtualFree, }; let mut target = 0u64; let at = (&raw mut target) as u64; // SAFETY: two executable pages of ours; the code runs clwb on our variable, then returns. unsafe { let page = VirtualAlloc(std::ptr::null(), 0x2000, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE) .cast::<u8>(); assert!(!page.is_null()); for insn in [&[0x66, 0x0f, 0xae, 0x30][..], &[0x66, 0x41, 0x0f, 0xae, 0x31][..]] { let end = page.add(0x1000 - insn.len()); end.copy_from_nonoverlapping(insn.as_ptr(), insn.len()); *page.add(0x1000) = 0xc3; // ret, first byte of the next page std::arch::asm!("call {f}", f = in(reg) end, in("rax") at, in("r9") at, clobber_abi("C")); } VirtualFree(page.cast(), 0, MEM_RELEASE); } } ``` The control run is the same test with `page.add(0x800)` for the `clwb` and the `ret` right after it. </details> AI-assisted (Claude Code); run on my machine.
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/Marvisak--VIctory!941
No description provided.