Skip to content

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

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
BastienRodz:loader/clwb-near-page-end
Nov 25, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
BastienRodz:loader/clwb-near-page-end

Conversation

@BastienRodz

@BastienRodz BastienRodz commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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

…lting

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 Marvisak#924.

Co-Authored-By: Gemini 4 Neon <noreply@google.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Zipettte

Copy link
Copy Markdown

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 added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
… page is emulated instead of faulting

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

yourmomsbasement pushed a commit to yourmomsbasement/VIctory_Dev that referenced this pull request Nov 24, 2026
Merges Marvisak#188 Marvisak#213 Marvisak#214 Marvisak#219 Marvisak#221 Marvisak#311 Marvisak#324 Marvisak#363 Marvisak#406 Marvisak#454 Marvisak#529 Marvisak#560 Marvisak#708 Marvisak#937
Marvisak#941 Marvisak#963 Marvisak#1111 Marvisak#1159 Marvisak#1304 Marvisak#1306 onto main (ffe310e), each head unchanged,
no conflicts.

One change of mine: the long-format import descriptor code Marvisak#963 adds nests
`if size == 0` in an `if let`, which clippy::collapsible_if rejects under
-D warnings; it is now one `if let ... && size == 0` (clippy --fix, no
change of behaviour).

cargo clippy --workspace --all-targets --locked -- -D warnings, with and
without --all-features, is clean and cargo test --workspace --locked passes
(388 passed, 7 ignored; main has 342 passed, 6 ignored).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@hadrianhola

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Loader: clwb_len() requires 15 remaining bytes in the page, causing unhandled #UD when clwb sits at page offset 0xFF2..0xFFB

5 participants