|
|
@@ -185,9 +185,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;
|
|
|
@@ -206,12 +204,12 @@ unsafe fn emulate_amd_only(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<usize> {
|
|
|
- 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<usize> {
|
|
|
+ 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;
|
|
|
@@ -219,7 +217,7 @@ fn clwb_len(code: [u8; 15]) -> Option<usize> {
|
|
|
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 {
|
|
|
@@ -228,7 +226,7 @@ fn clwb_len(code: [u8; 15]) -> Option<usize> {
|
|
|
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
|
|
|
@@ -950,26 +948,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::<u8>();
|
|
|
+ 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]
|