From 0c91c03116e4491339d554bbfad783efa3b88e6b Mon Sep 17 00:00:00 2001 From: raffclar Date: Mon, 23 Nov 2026 23:30:15 +0000 Subject: [PATCH 1/2] Loader: test the AMD-only instruction emulation on any CPU The test for the emulation runs the real instructions. AMD CPUs, and most CI runners, execute them natively, so the emulation was tested only on Intel CPUs. The new tests give the emulation instruction bytes and saved registers directly, so they work on any CPU. They cover clzero, monitorx/mwaitx, the immediate and register forms of extrq/insertq, xmm8 to xmm15, length 0, and the instructions and exceptions it must not emulate. The old test stays: on an Intel CPU it is the only test of the full path through the vectored handler. --- crates/victory-loader/src/crash.rs | 154 ++++++++++++++++++++++++++++- 1 file changed, 153 insertions(+), 1 deletion(-) diff --git a/crates/victory-loader/src/crash.rs b/crates/victory-loader/src/crash.rs index eb6bb5e..e68ab1d 100644 --- a/crates/victory-loader/src/crash.rs +++ b/crates/victory-loader/src/crash.rs @@ -433,7 +433,7 @@ fn module_of(address: usize) -> Option { mod tests { use super::*; use windows_sys::Win32::System::Diagnostics::Debug::{ - RtlCaptureContext, RtlCaptureStackBackTrace, + EXCEPTION_RECORD, RtlCaptureContext, RtlCaptureStackBackTrace, }; #[test] @@ -457,6 +457,9 @@ mod tests { assert_eq!(ours, system); } + /// On an Intel CPU, the real instructions go through the vectored + /// handler. On an AMD CPU they run natively, and the tests below test + /// the emulation. #[test] fn clzero_zeroes_its_line_on_any_cpu() { report_crashes(); @@ -488,4 +491,153 @@ mod tests { in("rax") at, in("ecx") 0, in("edx") 0) }; } + + fn reg(low: u64, high: u64) -> M128A { + M128A { + Low: low, + High: high as i64, + } + } + + /// Gives `code` to the emulation as if it faulted with `exception` at + /// its first byte, with `rax` and `xmm` as the saved registers. The + /// emulation does not execute `code`, so this works on any CPU. Returns + /// the number of bytes that the emulation skipped, or `None` if it did + /// not emulate the instruction. `xmm` gets the saved registers after. + fn emulate_as(exception: i32, code: &[u8], rax: u64, xmm: &mut [M128A; 16]) -> Option { + // `int3` after the instruction, so that a decoder that reads too + // far sees no valid bytes. + let mut bytes = [0xcc_u8; 16]; + bytes[..code.len()].copy_from_slice(code); + let start = bytes.as_ptr() as u64; + // SAFETY: a zeroed record and context are valid; the emulation + // reads only `bytes` and writes only `context` (and memory at + // `rax` for clzero, which the caller owns). + unsafe { + let mut record: EXCEPTION_RECORD = std::mem::zeroed(); + record.ExceptionCode = exception; + let mut context: CONTEXT = std::mem::zeroed(); + context.Rip = start; + context.Rax = rax; + context.Anonymous.FltSave.XmmRegisters = *xmm; + let mut pointers = EXCEPTION_POINTERS { + ExceptionRecord: &mut record, + ContextRecord: &mut context, + }; + let emulated = emulate_amd_only(&mut pointers); + *xmm = context.Anonymous.FltSave.XmmRegisters; + emulated.then(|| context.Rip - start) + } + } + + fn emulate(code: &[u8], rax: u64, xmm: &mut [M128A; 16]) -> Option { + emulate_as(EXCEPTION_ILLEGAL_INSTRUCTION, code, rax, xmm) + } + + fn low(xmm: &[M128A; 16], i: usize) -> u64 { + xmm[i].Low + } + + #[test] + fn emulated_clzero_zeroes_its_line() { + #[repr(align(64))] + struct Line([u8; 128]); + let mut line = Line([0xff; 128]); + let at = line.0.as_mut_ptr().wrapping_add(5) as u64; // inside the first line + let mut xmm = [reg(0, 0); 16]; + assert_eq!(emulate(&[0x0f, 0x01, 0xfc], at, &mut xmm), Some(3)); + assert!(line.0[..64].iter().all(|&b| b == 0)); + assert!(line.0[64..].iter().all(|&b| b == 0xff)); + } + + #[test] + fn emulated_monitorx_and_mwaitx_are_skipped() { + let mut xmm = [reg(1, 2); 16]; + assert_eq!(emulate(&[0x0f, 0x01, 0xfa], 0, &mut xmm), Some(3)); + assert_eq!(emulate(&[0x0f, 0x01, 0xfb], 0, &mut xmm), Some(3)); + assert!(xmm.iter().all(|r| r.Low == 1 && r.High == 2)); + } + + #[test] + fn emulated_insertq_and_extrq_with_immediates() { + let mut xmm = [reg(0, 0); 16]; + xmm[0] = reg(0x1111_2222_3333_4444, 0x5555); + xmm[1] = reg(0xabcd, 0x6666); + // insertq xmm0, xmm1, length 16, index 16 + let insertq = [0xf2, 0x0f, 0x78, 0xc1, 0x10, 0x10]; + assert_eq!(emulate(&insertq, 0, &mut xmm), Some(6)); + assert_eq!(low(&xmm, 0), 0x1111_2222_abcd_4444); + assert_eq!((low(&xmm, 1), xmm[1].High), (0xabcd, 0x6666)); + // extrq xmm0, length 8, index 20 + let extrq = [0x66, 0x0f, 0x78, 0xc0, 0x08, 0x14]; + assert_eq!(emulate(&extrq, 0, &mut xmm), Some(6)); + assert_eq!(low(&xmm, 0), 0xbc); + } + + #[test] + fn emulated_insertq_and_extrq_with_fields_in_a_register() { + let mut xmm = [reg(0, 0); 16]; + // insertq xmm0, xmm1: length and index in xmm1's high half. + xmm[0] = reg(0x1111_2222_3333_4444, 0); + xmm[1] = reg(0xabcd, 0x10_10); + assert_eq!(emulate(&[0xf2, 0x0f, 0x79, 0xc1], 0, &mut xmm), Some(4)); + assert_eq!(low(&xmm, 0), 0x1111_2222_abcd_4444); + // extrq xmm0, xmm1: length and index in xmm1's low half. + xmm[1] = reg(0x14_08, 0); + assert_eq!(emulate(&[0x66, 0x0f, 0x79, 0xc1], 0, &mut xmm), Some(4)); + assert_eq!(low(&xmm, 0), 0xbc); + } + + #[test] + fn emulated_sse4a_reaches_xmm8_to_xmm15() { + let mut xmm = [reg(0, 0); 16]; + xmm[8] = reg(0x1111_2222_3333_4444, 0); + xmm[9] = reg(0xabcd, 0); + // insertq xmm8, xmm9, length 16, index 16 (REX.R and REX.B) + let insertq = [0xf2, 0x45, 0x0f, 0x78, 0xc1, 0x10, 0x10]; + assert_eq!(emulate(&insertq, 0, &mut xmm), Some(7)); + assert_eq!(low(&xmm, 8), 0x1111_2222_abcd_4444); + assert_eq!((low(&xmm, 0), low(&xmm, 1)), (0, 0)); + // extrq xmm9, length 8, index 4 (REX.B) + let extrq = [0x66, 0x41, 0x0f, 0x78, 0xc1, 0x08, 0x04]; + assert_eq!(emulate(&extrq, 0, &mut xmm), Some(7)); + assert_eq!(low(&xmm, 9), 0xbc); + assert_eq!(low(&xmm, 1), 0); + } + + #[test] + fn emulated_sse4a_length_0_is_64_bits() { + let mut xmm = [reg(0, 0); 16]; + xmm[0] = reg(0x1122_3344_5566_7788, 0); + // extrq xmm0, length 0 (64), index 8 + assert_eq!( + emulate(&[0x66, 0x0f, 0x78, 0xc0, 0x00, 0x08], 0, &mut xmm), + Some(6) + ); + assert_eq!(low(&xmm, 0), 0x0011_2233_4455_6677); + // insertq xmm0, xmm1, length 0 (64), index 0 + xmm[1] = reg(0xdead_beef_cafe_f00d, 0); + assert_eq!( + emulate(&[0xf2, 0x0f, 0x78, 0xc1, 0x00, 0x00], 0, &mut xmm), + Some(6) + ); + assert_eq!(low(&xmm, 0), 0xdead_beef_cafe_f00d); + } + + #[test] + fn other_instructions_and_exceptions_are_not_emulated() { + let mut xmm = [reg(0, 0); 16]; + assert_eq!(emulate(&[0x0f, 0x0b], 0, &mut xmm), None); // ud2 + // extrq with a memory operand, which does not exist + assert_eq!(emulate(&[0x66, 0x0f, 0x79, 0x00], 0, &mut xmm), None); + // extrq with immediates needs ModRM.reg 0 + let bad_extrq = [0x66, 0x0f, 0x78, 0xc8, 0x08, 0x14]; + assert_eq!(emulate(&bad_extrq, 0, &mut xmm), None); + // a clzero that did not cause an illegal instruction + let clzero = [0x0f, 0x01, 0xfc]; + assert_eq!( + emulate_as(EXCEPTION_ACCESS_VIOLATION, &clzero, 0, &mut xmm), + None + ); + } } From f00caec1cf577673d8f2a6e9def49db35a7562e5 Mon Sep 17 00:00:00 2001 From: raffclar Date: Mon, 23 Nov 2026 23:34:49 +0000 Subject: [PATCH 2/2] Loader: test 64-bit extrq at index 0, not in AMD's undefined range --- crates/victory-loader/src/crash.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/victory-loader/src/crash.rs b/crates/victory-loader/src/crash.rs index e68ab1d..5d6260f 100644 --- a/crates/victory-loader/src/crash.rs +++ b/crates/victory-loader/src/crash.rs @@ -609,12 +609,13 @@ mod tests { fn emulated_sse4a_length_0_is_64_bits() { let mut xmm = [reg(0, 0); 16]; xmm[0] = reg(0x1122_3344_5566_7788, 0); - // extrq xmm0, length 0 (64), index 8 + // extrq xmm0, length 0 (64), index 0. A non-zero index with + // length 64 goes past bit 63, and AMD leaves that result undefined. assert_eq!( - emulate(&[0x66, 0x0f, 0x78, 0xc0, 0x00, 0x08], 0, &mut xmm), + emulate(&[0x66, 0x0f, 0x78, 0xc0, 0x00, 0x00], 0, &mut xmm), Some(6) ); - assert_eq!(low(&xmm, 0), 0x0011_2233_4455_6677); + assert_eq!(low(&xmm, 0), 0x1122_3344_5566_7788); // insertq xmm0, xmm1, length 0 (64), index 0 xmm[1] = reg(0xdead_beef_cafe_f00d, 0); assert_eq!(