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

Merged
BastienRodz merged 1 commit from upstream-pr/941 into main 2026-11-25 00:15:36 +00:00
BastienRodz commented 2026-11-24 14:23:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by BastienRodz on 2026-11-24T14:23:55Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6583961860 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/941). Originally posted by [BastienRodz](https://github.com/BastienRodz) on 2026-11-24T14:23:55Z. --- ## 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://github.com/Marvisak/VIctory/pull/941#issuecomment-7140963859)): 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)
Zipettte commented 2026-11-24 16:05:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T15:40:44Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7136429417 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/941#issuecomment-7136429417). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T15:40:44Z. --- 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)._
mercuryVM commented 2026-11-24 17:02:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T16:26:28Z.

Review: APPROVED


Tested 60d2cf9 on Windows 11, RTX 5070, MSVC: builds, cargo test --workspace --no-fail-fast 576 passed, 0 failed (on our fork main), with victory-shader single-threaded. a_clwb_at_the_end_of_a_page_is_decoded passes, including the two emulate_amd_only cases at the end of a page followed by a PAGE_NOACCESS page.

  • Every byte clwb_len reads now goes through first/get, including the prefix, the 0f ae pair, ModRM and SIB. The final len <= code.len() covers displacements cut off by the page end, so a truncated encoding returns None instead of a length past readable memory.
  • Decoding is unchanged otherwise: the old cases pass as slices.
  • emulate_amd_only passes exactly the n bytes it copied, so there's no read past the page.

Merged into our fork's main.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6043222596 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/941#pullrequestreview-6043222596). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T16:26:28Z. **Review:** APPROVED --- Tested 60d2cf9 on Windows 11, RTX 5070, MSVC: builds, `cargo test --workspace --no-fail-fast` 576 passed, 0 failed (on our fork main), with victory-shader single-threaded. `a_clwb_at_the_end_of_a_page_is_decoded` passes, including the two `emulate_amd_only` cases at the end of a page followed by a `PAGE_NOACCESS` page. - Every byte `clwb_len` reads now goes through `first`/`get`, including the prefix, the `0f ae` pair, ModRM and SIB. The final `len <= code.len()` covers displacements cut off by the page end, so a truncated encoding returns `None` instead of a length past readable memory. - Decoding is unchanged otherwise: the old cases pass as slices. - `emulate_amd_only` passes exactly the `n` bytes it copied, so there's no read past the page. Merged into our fork's main. AI-assisted (Claude Code).
hadrianhola commented 2026-11-24 19:04:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by hadrianhola on 2026-11-24T18:39:55Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140963859 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/941#issuecomment-7140963859). Originally posted by [hadrianhola](https://github.com/hadrianhola) on 2026-11-24T18:39:55Z. --- 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.
upstream-sync closed this pull request 2026-11-25 01:04:58 +00:00
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!941
No description provided.