From ae3963d6de5f926196cc9d968db72e7619639d27 Mon Sep 17 00:00:00 2001 From: BastienRdz <58186763+BastienRodz@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:20:53 +0000 Subject: [PATCH] Loader: a clwb in the last bytes of a page is emulated instead of faulting 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 Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 60d2cf9cbcf5a2abc773d5de10c3c892e77d3340) --- crates/viport-loader/src/crash.rs | 96 ++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 28 deletions(-) diff --git a/crates/viport-loader/src/crash.rs b/crates/viport-loader/src/crash.rs index 9f71ecb3..9350b538 100644 --- a/crates/viport-loader/src/crash.rs +++ b/crates/viport-loader/src/crash.rs @@ -277,9 +277,7 @@ unsafe fn emulate_amd_only(info: *mut EXCEPTION_POINTERS) -> bool { context.Rip += pass.len as u64; return true; } - if n >= 15 - && let Some(len) = clwb_len(bytes[..15].try_into().unwrap_or([0; 15])) - { + if let Some(len) = clwb_len(&bytes[..n]) { // A write-back hint: the PC's memory is coherent, so it has nothing to do. context.Rip += len as u64; return true; @@ -423,12 +421,12 @@ unsafe fn recover_line_fault(info: *mut EXCEPTION_POINTERS) -> bool { /// `clwb m8` (`66 [rex] 0f ae /6`, a memory operand), which Intel CPUs before /// Ice Lake lack: its length, or `None` for any other instruction. -fn clwb_len(code: [u8; 15]) -> Option { - let at = if code[1] & 0xf0 == 0x40 { 2 } else { 1 }; - if code[0] != 0x66 || code[at] != 0x0f || code[at + 1] != 0xae { +fn clwb_len(code: &[u8]) -> Option { + let at = if *code.get(1)? & 0xf0 == 0x40 { 2 } else { 1 }; + if *code.first()? != 0x66 || code.get(at..at + 2)? != [0x0f, 0xae] { return None; } - let modrm = code[at + 2]; + let modrm = *code.get(at + 2)?; let (mode, rm) = (modrm >> 6, modrm & 7); if mode == 3 || (modrm >> 3) & 7 != 6 { return None; @@ -436,7 +434,7 @@ fn clwb_len(code: [u8; 15]) -> Option { let mut len = at + 3; let mut base = rm; if rm == 4 { - base = code[len] & 7; + base = *code.get(len)? & 7; len += 1; } len += match mode { @@ -445,7 +443,7 @@ fn clwb_len(code: [u8; 15]) -> Option { 2 => 4, _ => 0, }; - Some(len) + (len <= code.len()).then_some(len) } /// A `clwb` write-back loop as the title's compilers build it, one cache line a @@ -1276,26 +1274,68 @@ mod tests { #[test] fn clwb_lengths() { - let pad = |b: &[u8]| { - let mut c = [0u8; 15]; - c[..b.len()].copy_from_slice(b); - c + assert_eq!(clwb_len(&[0x66, 0x41, 0x0f, 0xae, 0x31]), Some(5)); // clwb [r9] + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0x30]), Some(4)); // clwb [rax] + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0x70, 0x40]), Some(5)); // clwb [rax+0x40] + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0xb0, 0, 1, 0, 0]), Some(8)); // [rax+disp32] + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0x34, 0x24]), Some(5)); // clwb [rsp] + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0x35, 0, 0, 0, 0]), Some(8)); // [rip+disp32] + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0x38]), None); // clflushopt, /7 + assert_eq!(clwb_len(&[0x0f, 0xae, 0x30]), None); // xsaveopt, no 66 + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0xf0]), None); // register form + } + + #[test] + fn a_clwb_at_the_end_of_a_page_is_decoded() { + // Exact-length slices, as at the end of a page. + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0x30]), Some(4)); + assert_eq!(clwb_len(&[0x66, 0x41, 0x0f, 0xae, 0x31]), Some(5)); + // Cut short by the slice: returns None without panicking. + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0xb0, 0, 1]), None); // disp32, 6 of 8 bytes + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0x70]), None); // disp8, 4 of 5 bytes + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0x34]), None); // SIB missing + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae, 0x34, 0x25, 0, 0]), None); // SIB disp32 cut short + assert_eq!(clwb_len(&[0x66, 0x0f, 0xae]), None); // ModRM missing + assert_eq!(clwb_len(&[0x66]), None); + assert_eq!(clwb_len(&[]), None); + + // Emulation at the last 4 and 5 bytes of a page with an unreadable page after it. + use windows_sys::Win32::System::Memory::{ + MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_NOACCESS, PAGE_READWRITE, VirtualAlloc, + VirtualFree, VirtualProtect, }; - assert_eq!(clwb_len(pad(&[0x66, 0x41, 0x0f, 0xae, 0x31])), Some(5)); // clwb [r9] - assert_eq!(clwb_len(pad(&[0x66, 0x0f, 0xae, 0x30])), Some(4)); // clwb [rax] - assert_eq!(clwb_len(pad(&[0x66, 0x0f, 0xae, 0x70, 0x40])), Some(5)); // clwb [rax+0x40] - assert_eq!( - clwb_len(pad(&[0x66, 0x0f, 0xae, 0xb0, 0, 1, 0, 0])), - Some(8) - ); // [rax+disp32] - assert_eq!(clwb_len(pad(&[0x66, 0x0f, 0xae, 0x34, 0x24])), Some(5)); // clwb [rsp] - assert_eq!( - clwb_len(pad(&[0x66, 0x0f, 0xae, 0x35, 0, 0, 0, 0])), - Some(8) - ); // [rip+disp32] - assert_eq!(clwb_len(pad(&[0x66, 0x0f, 0xae, 0x38])), None); // clflushopt, /7 - assert_eq!(clwb_len(pad(&[0x0f, 0xae, 0x30])), None); // xsaveopt, no 66 - assert_eq!(clwb_len(pad(&[0x66, 0x0f, 0xae, 0xf0])), None); // register form + // SAFETY: two pages of ours, the second unreadable so reading past the first faults. + unsafe { + let page = VirtualAlloc( + std::ptr::null(), + 0x2000, + MEM_RESERVE | MEM_COMMIT, + PAGE_READWRITE, + ) + .cast::(); + assert!(!page.is_null()); + let mut old = 0; + let guard = page.add(0x1000).cast(); + assert_ne!(VirtualProtect(guard, 0x1000, PAGE_NOACCESS, &mut old), 0); + 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()); + let mut record: EXCEPTION_RECORD = std::mem::zeroed(); + record.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION; + let mut context: CONTEXT = std::mem::zeroed(); + context.Rip = end as u64; + let mut pointers = EXCEPTION_POINTERS { + ExceptionRecord: &mut record, + ContextRecord: &mut context, + }; + assert!(emulate_amd_only(&mut pointers)); + assert_eq!(context.Rip, end as u64 + insn.len() as u64); + } + VirtualFree(page.cast(), 0, MEM_RELEASE); + } } #[test]