Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion crates/xcl/src/eh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,10 @@ unsafe fn emulate_console_only(ctx: &mut windows_sys::Win32::System::Diagnostics
std::hint::spin_loop();
}
ConsoleOnly::Clzero => {
let line = (ctx.Rax as usize) & !63;
// with an address-size prefix (0x67, among the prefixes before 0F 01 FC) the line
// is at EAX, zero-extended – as a Zen 5 runs it
let a32 = code[..len - 3].contains(&0x67);
let line = (if a32 { ctx.Rax & 0xFFFF_FFFF } else { ctx.Rax } as usize) & !63;
if is_writable(line, 64) {
std::ptr::write_bytes(line as *mut u8, 0, 64);
} else {
Expand Down Expand Up @@ -910,4 +913,37 @@ mod tests {
assert_eq!(find_gs_check(&console), Some(29));
assert_eq!(find_gs_check(&[0x48; 64]), None);
}

/// `67 0F 01 FC`: CLZERO at EAX. rax's upper half set, and memory mapped at both the 64- and
/// the 32-bit address: only the 32-bit one's line is zeroed.
#[test]
fn clzero_with_an_address_size_prefix_zeroes_at_eax() {
use std::sync::{atomic::AtomicBool, Arc};
use windows_sys::Win32::System::Memory::{VirtualAlloc, MEM_COMMIT, MEM_RESERVE, PAGE_READWRITE};
let alloc = |at: usize| unsafe { VirtualAlloc(at as *const _, 0x1_0000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) as usize };
let low = (1..64).map(|n| alloc(n << 26)).find(|&p| p != 0).expect("a buffer below 4 GiB");
let high = (1..256).map(|n| alloc(low + (n << 32))).find(|&p| p != 0).expect("a buffer at low + n << 32");
let code = alloc(0);
modules::register_mapped(Arc::new(modules::MappedModule {
name: "clzero_a32.exe".into(), path: "clzero_a32.exe".into(), base: code, size: 0x1_0000, entry_rva: 0, is_dll: false,
exports: Default::default(), tls_callbacks: Mutex::new(Vec::new()), no_thread_calls: AtomicBool::new(false),
initialized: AtomicBool::new(true), stack_reserve: 0,
}));
// alone, and as the first of a loop to rdx (add rax, 0x40; cmp rax, rdx; jb back to the 0F)
for bytes in [&[0x67u8, 0x0F, 0x01, 0xFC][..], &[0x67, 0x0F, 0x01, 0xFC, 0x48, 0x83, 0xC0, 0x40, 0x48, 0x39, 0xD0, 0x72, 0xF4]] {
unsafe {
std::ptr::copy_nonoverlapping(bytes.as_ptr(), code as *mut u8, bytes.len());
std::ptr::write_bytes(low as *mut u8, 0xA5, 0x100);
std::ptr::write_bytes(high as *mut u8, 0xA5, 0x100);
let mut ctx: windows_sys::Win32::System::Diagnostics::Debug::CONTEXT = std::mem::zeroed();
(ctx.Rip, ctx.Rax, ctx.Rdx) = (code as u64, (high + 0x47) as u64, (high + 0x100) as u64);
assert_eq!(emulate_console_only(&mut ctx), -1);
assert_eq!(ctx.Rip, code as u64 + 4);
let (l, h) = (std::slice::from_raw_parts(low as *const u8, 0x100), std::slice::from_raw_parts(high as *const u8, 0x100));
assert!(l[0x40..0x80].iter().all(|&b| b == 0) && l[..0x40].iter().chain(&l[0x80..]).all(|&b| b == 0xA5), "the line at EAX");
assert!(h.iter().all(|&b| b == 0xA5), "nothing at RAX");
}
}
modules::unregister_mapped(code);
}
}
5 changes: 5 additions & 0 deletions crates/xcl/src/insn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ pub fn decode(code: &[u8]) -> Option<(ConsoleOnly, usize)> {
match (*code.get(i + 1)?, code.get(i + 2).copied()) {
(0x01, Some(0xFC)) => {
// clzero (0F 01 FC); add rax, 0x40 (48 83 C0 40); cmp rax, <reg> ([48|4C] 39 modrm); jb -12 (72 F4)
// (not after an 0x67: that CLZERO is at EAX, and the jb goes back to the 0F, past the
// prefix – it is stepped alone, the loop is taken from the next one)
if code.len() >= i + 12
&& !code[..i].contains(&0x67)
&& code[i + 3..i + 7] == [0x48, 0x83, 0xC0, 0x40]
&& (code[i + 7] == 0x48 || code[i + 7] == 0x4C)
&& code[i + 8] == 0x39
Expand Down Expand Up @@ -192,6 +195,8 @@ mod tests {
decode(&[0x0F, 0x01, 0xFC, 0x48, 0x83, 0xC0, 0x40, 0x48, 0x39, 0xD0, 0x72, 0xF4]),
Some((ConsoleOnly::ClzeroLoop { end_reg: 2 }, 12))
);
// the same loop after an address-size prefix: that one CLZERO alone
assert_eq!(decode(&[0x67, 0x0F, 0x01, 0xFC, 0x48, 0x83, 0xC0, 0x40, 0x48, 0x39, 0xD0, 0x72, 0xF4]), Some((ConsoleOnly::Clzero, 4)));
assert_eq!(decode(&[0x0F, 0x01, 0xFD]), Some((ConsoleOnly::Rdpru, 3)));
assert_eq!(decode(&[0x0F, 0x01, 0xFA]), Some((ConsoleOnly::Monitorx, 3)));
assert_eq!(decode(&[0x0F, 0x01, 0xFB]), Some((ConsoleOnly::Mwaitx, 3)));
Expand Down
Loading