Loader: guard clwb_loop shortcut against u64 overflow and wraparound

(cherry picked from commit 274f6988c9779b0612186c3448123e5e157d56e4)
This commit is contained in:
Tex authored and local committed 2026-11-25 01:03:03 -06:00
1 parent e9b4302e8f
commit e394a216f9
1 file changed
+59 -8
+59 -8
View File
@@ -171,16 +171,14 @@ unsafe fn emulate_amd_only(info: *mut EXCEPTION_POINTERS) -> bool {
let mut bytes = [0u8; 32];
let n = room.min(bytes.len());
std::ptr::copy_nonoverlapping(code, bytes.as_mut_ptr(), n);
if let Some(pass) = clwb_loop(&bytes[..n]) {
if let Some(pass) = clwb_loop(&bytes[..n])
&& let Some(end) =
clwb_loop_end(*gpr(context, pass.base), *gpr(context, pass.limit))
{
// A whole write-back loop in one exception (see `clwb_loop`).
let limit = *gpr(context, pass.limit);
let base = gpr(context, pass.base);
*base = if *base <= limit {
base.wrapping_add(((limit - *base) / 64 + 1) * 64)
} else {
base.wrapping_add(64)
};
let flags = cmp_flags(*base, limit);
*gpr(context, pass.base) = end;
let flags = cmp_flags(end, limit);
context.EFlags = context.EFlags & !CMP_FLAGS | flags;
context.Rip += pass.len as u64;
return true;
@@ -249,6 +247,19 @@ struct ClwbLoop {
len: usize,
}
/// The `base` value left after a `clwb_loop` finishes without wrapping past
/// `u64::MAX`, or `None` if `add base, 0x40` would overflow or wrap (in which
/// case we fall through to single-instruction `clwb` emulation).
fn clwb_loop_end(base: u64, limit: u64) -> Option<u64> {
let step = if base <= limit {
((limit - base) / 64 + 1).checked_mul(64)?
} else {
64
};
let end = base.checked_add(step)?;
(end > limit).then_some(end)
}
fn clwb_loop(code: &[u8]) -> Option<ClwbLoop> {
// clwb [base]: 66 [40|41] 0f ae, ModRM mod 00, reg 6, no SIB or displacement.
let (rex, at) = match code.get(1)? {
@@ -946,6 +957,46 @@ mod tests {
assert_eq!(cmp_flags(0x1000, 0x1000) & 0x40, 0x40);
}
#[test]
fn clwb_loop_wrapping_ranges_do_not_overflow_and_fall_back_to_single_clwb() {
assert_eq!(clwb_loop_end(0x1000, 0x1080), Some(0x10c0));
assert_eq!(clwb_loop_end(0x10c0, 0x1080), Some(0x1100));
assert_eq!(clwb_loop_end(0, u64::MAX - 10), None);
assert_eq!(clwb_loop_end(u64::MAX - 16, 0x100), None);
assert_eq!(clwb_loop_end(u64::MAX - 63, u64::MAX), None);
// clwb [rcx]; add rcx, 0x40; cmp rcx, rax; jbe top (13 bytes, clwb is 4 bytes).
let mut bytes = [0xcc_u8; 16];
bytes[..13].copy_from_slice(&[
0x66, 0x0f, 0xae, 0x31, 0x48, 0x83, 0xc1, 0x40, 0x48, 0x39, 0xc1, 0x76, 0xf3,
]);
let run = |base: u64, limit: u64| -> (u64, u64) {
let start = bytes.as_ptr() as u64;
// SAFETY: zeroed record/context are valid; `emulate_amd_only` only
// reads `bytes` and updates `context`.
unsafe {
let mut record: EXCEPTION_RECORD = std::mem::zeroed();
record.ExceptionCode = EXCEPTION_ILLEGAL_INSTRUCTION;
let mut context: CONTEXT = std::mem::zeroed();
context.Rip = start;
context.Rcx = base;
context.Rax = limit;
let mut pointers = EXCEPTION_POINTERS {
ExceptionRecord: &mut record,
ContextRecord: &mut context,
};
assert!(emulate_amd_only(&mut pointers));
(context.Rip - start, context.Rcx)
}
};
// Normal range completes the whole 13-byte loop in one exception.
assert_eq!(run(0x1000, 0x1080), (13, 0x10c0));
// Overflowing/wrapping ranges must not panic or skip the loop: emulate
// just the 4-byte `clwb` instruction and let the CPU run add/cmp/jbe.
assert_eq!(run(0, u64::MAX - 10), (4, 0));
assert_eq!(run(u64::MAX - 16, 0x100), (4, u64::MAX - 16));
}
#[test]
fn clwb_lengths() {
assert_eq!(clwb_len(&[0x66, 0x41, 0x0f, 0xae, 0x31]), Some(5)); // clwb [r9]