Merge pull request #38 from raffclar/emulation-tests

Loader: test the AMD-only instruction emulation on any CPU (including AMD!)
This commit is contained in:
Marvis authored and GitHub committed 2026-11-24 01:32:45 +01:00
commit cbd71a9bde
1 file changed
+154 -1
+154 -1
View File
@@ -583,7 +583,7 @@ fn module_of(address: usize) -> Option<String> {
mod tests {
use super::*;
use windows_sys::Win32::System::Diagnostics::Debug::{
RtlCaptureContext, RtlCaptureStackBackTrace,
EXCEPTION_RECORD, RtlCaptureContext, RtlCaptureStackBackTrace,
};
#[test]
@@ -607,6 +607,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 clwb_loops() {
// clwb [rcx]; add rcx, 0x40; cmp rcx, rax; jbe top
@@ -695,4 +698,154 @@ 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<u64> {
// `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<u64> {
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 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, 0x00], 0, &mut xmm),
Some(6)
);
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!(
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
);
}
}