From 20780eab24eb180068bc38be18b7371c3c0ab13d Mon Sep 17 00:00:00 2001 From: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:35:49 +0530 Subject: [PATCH] loader: clear the upper half of a 32-bit MOVSX destination (#65) The native DBT translator sign-extended to 64 bits unconditionally and stored the full 64-bit result, so a 32-bit MOVSX destination kept the sign extension in bits 63:32 instead of having them cleared. Before, both MOVSX paths did this: asm.sxtb_64(X9, X9); // or sxth_64 asm.str_uoff_64(X9, X19, (reg_idx as u32) * 8); `sxtb_64` is `SBFM Xd, Xn, #0, #7`, which sign-extends the byte to 64 bits, and `str_uoff_64` writes all 64 bits of the destination register. That is correct for the REX.W form `movsbq rax, bl`, but the Intel SDM requires a 32-bit result for the 32-bit destination form `movsbl eax, bl`. The translator therefore left `rax = 0xFFFFFFFFFFFFFFFF` where the architecture requires `rax = 0x00000000FFFFFFFF`. Select the sign-extension width from REX.W: - REX.W set: keep the 64-bit extension, which is already correct. - REX.W clear: sign-extend to 32 bits, which also clears bits 63:32 of the destination because the 32-bit form writes a W register. This needs two new assembler instructions, `sxtb_32` and `sxth_32`, which are the 32-bit forms of the existing `sxtb_64` and `sxth_64`: sxtb_32: SBFM Wd, Wn, #0, #7 (0x13001c00) sxth_32: SBFM Wd, Wn, #0, #15 (0x13003c00) Both encodings were confirmed against LLVM 23.1.6 `llvm-mc -triple=aarch64 -show-encoding`. The MOVZX paths are unchanged: a zero extension produces the same value at both destination widths, because the 32-bit form already clears the upper half. Add three regression tests in the dbt test module covering the register and memory source forms and the byte and word sources, each at both destination widths. Every encoding was produced by LLVM 23.1.6 llvm-mc -show-encoding and the expected values follow the Intel SDM rules for MOVSX. All three tests fail against the unmodified translator and pass with this change. Closes #65 Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com> --- crates/loader/src/arm64.rs | 16 +++++ crates/loader/src/dbt.rs | 118 +++++++++++++++++++++++++++++++++++-- 2 files changed, 130 insertions(+), 4 deletions(-) diff --git a/crates/loader/src/arm64.rs b/crates/loader/src/arm64.rs index 9d79182..94a8536 100644 --- a/crates/loader/src/arm64.rs +++ b/crates/loader/src/arm64.rs @@ -183,11 +183,27 @@ impl Arm64Assembler { self.emit(0x93401c00 | ((wn as u32 & 0x1f) << 5) | (xd as u32 & 0x1f)); } + /// SXTB (32-bit/sign-extended): SXTB Wd, Wn (SBFM Wd, Wn, #0, #7) + /// + /// Writing the 32-bit destination form clears bits 63:32 of the + /// destination register, which is what a 32-bit x86 destination requires. + pub fn sxtb_32(&mut self, wd: u8, wn: u8) { + self.emit(0x13001c00 | ((wn as u32 & 0x1f) << 5) | (wd as u32 & 0x1f)); + } + /// SXTH (64-bit/sign-extended): SXTH Xd, Wn (SBFM Xd, Xn, #0, #15) pub fn sxth_64(&mut self, xd: u8, wn: u8) { self.emit(0x93403c00 | ((wn as u32 & 0x1f) << 5) | (xd as u32 & 0x1f)); } + /// SXTH (32-bit/sign-extended): SXTH Wd, Wn (SBFM Wd, Wn, #0, #15) + /// + /// Writing the 32-bit destination form clears bits 63:32 of the + /// destination register, which is what a 32-bit x86 destination requires. + pub fn sxth_32(&mut self, wd: u8, wn: u8) { + self.emit(0x13003c00 | ((wn as u32 & 0x1f) << 5) | (wd as u32 & 0x1f)); + } + /// ADD (extended/shifted register, 32-bit): ADD Wd, Wn, Wm pub fn add_reg_32(&mut self, wd: u8, wn: u8, wm: u8) { self.emit( diff --git a/crates/loader/src/dbt.rs b/crates/loader/src/dbt.rs index 85db022..bcbd8cf 100644 --- a/crates/loader/src/dbt.rs +++ b/crates/loader/src/dbt.rs @@ -1245,7 +1245,13 @@ impl DbtEngine { if mode == 3 { asm.ldr_uoff_64(X9, X19, (rm_idx as u32) * 8); - asm.sxtb_64(X9, X9); + if rex_w { + asm.sxtb_64(X9, X9); + } else { + // A 32-bit destination holds a 32-bit + // result, so the upper half is cleared. + asm.sxtb_32(X9, X9); + } asm.str_uoff_64(X9, X19, (reg_idx as u32) * 8); } else { Self::emit_effective_address( @@ -1265,7 +1271,13 @@ impl DbtEngine { asm.load_imm_64(X4, helper_fn); asm.blr(X4); emit_fault_guard(asm, &mut fault_guards, insn_count - 1, insn_rip); - asm.sxtb_64(X0, X0); + if rex_w { + asm.sxtb_64(X0, X0); + } else { + // A 32-bit destination holds a 32-bit + // result, so the upper half is cleared. + asm.sxtb_32(X0, X0); + } asm.str_uoff_64(X0, X19, (reg_idx as u32) * 8); } } @@ -1283,7 +1295,13 @@ impl DbtEngine { if mode == 3 { asm.ldr_uoff_64(X9, X19, (rm_idx as u32) * 8); - asm.sxth_64(X9, X9); + if rex_w { + asm.sxth_64(X9, X9); + } else { + // A 32-bit destination holds a 32-bit + // result, so the upper half is cleared. + asm.sxth_32(X9, X9); + } asm.str_uoff_64(X9, X19, (reg_idx as u32) * 8); } else { Self::emit_effective_address( @@ -1303,7 +1321,13 @@ impl DbtEngine { asm.load_imm_64(X4, helper_fn); asm.blr(X4); emit_fault_guard(asm, &mut fault_guards, insn_count - 1, insn_rip); - asm.sxth_64(X0, X0); + if rex_w { + asm.sxth_64(X0, X0); + } else { + // A 32-bit destination holds a 32-bit + // result, so the upper half is cleared. + asm.sxth_32(X0, X0); + } asm.str_uoff_64(X0, X19, (reg_idx as u32) * 8); } } @@ -1781,6 +1805,92 @@ mod tests { assert_eq!(memory.read_u64(STORED_ADDR).unwrap(), STORED_VALUE); } + /// A 32-bit MOVSX destination holds a 32-bit result, so the upper half of + /// the register must be cleared. + /// + /// The encodings are `movsbl eax, bl` (0F BE C3) and + /// `movsbq rdx, bl` (48 0F BE D3), as produced by LLVM 23.1.6 + /// `llvm-mc -show-encoding`. With `bl = 0xFF` the 32-bit destination holds + /// 0x00000000ffffffff while the 64-bit destination holds the sign + /// extension across all 64 bits. + /// + /// Per Intel SDM Volume 1, "Address Calculations in 64-Bit Mode": "32-bit + /// operands generate a 32-bit result, zero-extended to a 64-bit result in + /// the destination general-purpose register." + #[test] + fn movsx_to_a_32_bit_destination_clears_the_upper_half() { + // mov rbx, 0xffffffffffffffff (48 BB ) + // movsbl eax, bl (0F BE C3) + // movsbq rdx, bl (48 0F BE D3) + let mut code = vec![0x48, 0xBB]; + code.extend_from_slice(&u64::MAX.to_le_bytes()); + code.extend_from_slice(&[0x0F, 0xBE, 0xC3]); + code.extend_from_slice(&[0x48, 0x0F, 0xBE, 0xD3]); + code.push(0xF4); // hlt + + let (mut engine, mut cpu, mut memory) = fixture(&code); + cpu.set(Register::Rbx, u64::MAX); + let result = engine.run(&mut cpu, &mut memory, 16); + + assert_eq!(result.exit_reason, ExitReason::Halt); + assert_eq!(result.final_state.get(Register::Rax), 0x0000_0000_ffff_ffff); + assert_eq!(result.final_state.get(Register::Rdx), 0xffff_ffff_ffff_ffff); + } + + /// A 32-bit MOVSX destination from a 16-bit source also holds a 32-bit + /// result. + /// + /// The encodings are `movswl eax, bx` (0F BF C3) and + /// `movswq rdx, bx` (48 0F BF D3), as produced by LLVM 23.1.6 + /// `llvm-mc -show-encoding`. + #[test] + fn movsx_from_a_word_to_a_32_bit_destination_clears_the_upper_half() { + // mov rbx, 0xffffffffffffffff (48 BB ) + // movswl eax, bx (0F BF C3) + // movswq rdx, bx (48 0F BF D3) + let mut code = vec![0x48, 0xBB]; + code.extend_from_slice(&u64::MAX.to_le_bytes()); + code.extend_from_slice(&[0x0F, 0xBF, 0xC3]); + code.extend_from_slice(&[0x48, 0x0F, 0xBF, 0xD3]); + code.push(0xF4); // hlt + + let (mut engine, mut cpu, mut memory) = fixture(&code); + cpu.set(Register::Rbx, u64::MAX); + let result = engine.run(&mut cpu, &mut memory, 16); + + assert_eq!(result.exit_reason, ExitReason::Halt); + assert_eq!(result.final_state.get(Register::Rax), 0x0000_0000_ffff_ffff); + assert_eq!(result.final_state.get(Register::Rdx), 0xffff_ffff_ffff_ffff); + } + + /// The same 32-bit destination rule applies to the memory source forms. + /// + /// The encodings are `movsbl eax, byte [rdi]` (0F BE 07) and + /// `movsbq rdx, byte [rdi]` (48 0F BE 17), as produced by LLVM 23.1.6 + /// `llvm-mc -show-encoding`. + #[test] + fn movsx_from_memory_to_a_32_bit_destination_clears_the_upper_half() { + // mov rdi, 0x4000 (48 BF ) + // movsbl eax, byte [rdi] (0F BE 07) + // movsbq rdx, byte [rdi] (48 0F BE 17) + let mut code = vec![0x48, 0xBF]; + code.extend_from_slice(&0x4000u64.to_le_bytes()); + code.extend_from_slice(&[0x0F, 0xBE, 0x07]); + code.extend_from_slice(&[0x48, 0x0F, 0xBE, 0x17]); + code.push(0xF4); // hlt + + let (mut engine, mut cpu, mut memory) = fixture(&code); + memory + .map_region(0x4000, 0x100, MemoryPermissions::READ_WRITE) + .unwrap(); + memory.write_u8(0x4000, 0xFF).unwrap(); + let result = engine.run(&mut cpu, &mut memory, 16); + + assert_eq!(result.exit_reason, ExitReason::Halt); + assert_eq!(result.final_state.get(Register::Rax), 0x0000_0000_ffff_ffff); + assert_eq!(result.final_state.get(Register::Rdx), 0xffff_ffff_ffff_ffff); + } + #[test] fn indirect_call_target_fault_leaves_guest_stack_unchanged() { // call qword ptr [rip + 0x1000]; hlt. The IAT slot is outside the