From c107f3a43f52816cde9b28690cd54b4d487416df Mon Sep 17 00:00:00 2001 From: ates Date: Tue, 24 Nov 2026 11:30:08 +0300 Subject: [PATCH] Preserve precise guest fault state and retired instructions Signed-off-by: ates --- crates/loader/src/cpu.rs | 9 ++++++ crates/loader/src/dbt.rs | 66 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/crates/loader/src/cpu.rs b/crates/loader/src/cpu.rs index d6a016a..5a3a5ef 100644 --- a/crates/loader/src/cpu.rs +++ b/crates/loader/src/cpu.rs @@ -246,6 +246,15 @@ impl<'a> Interpreter<'a> { /// Single-step execution. Returns Ok(Some(ExitReason)) on clean exit (HLT/SYSCALL). pub fn step(&mut self) -> Result, ExitReason> { + let start = self.cpu.rip; + let result = self.execute_instruction(); + if result.is_err() { + self.cpu.rip = start; + } + result + } + + fn execute_instruction(&mut self) -> Result, ExitReason> { let mut rex_w = false; let mut rex_r = false; let mut rex_b = false; diff --git a/crates/loader/src/dbt.rs b/crates/loader/src/dbt.rs index c7065bb..85db022 100644 --- a/crates/loader/src/dbt.rs +++ b/crates/loader/src/dbt.rs @@ -112,7 +112,19 @@ impl DbtEngine { // Look up or translate block at cpu.rip let block_limit = (max_instructions - instructions_count).min(MAX_BLOCK_INSTRUCTIONS); - let block_fn_ptr = match self.get_or_compile_block(cpu.rip, memory, block_limit) { + let mut block = self.get_or_compile_block(cpu.rip, memory, block_limit); + // A decode failure can belong to a later instruction in the block. + // Execute the current instruction alone so earlier effects retire + // and an earlier data fault takes precedence over speculative fetch. + if block_limit > 1 + && matches!( + block, + Err(ExitReason::IllegalInstruction(_) | ExitReason::MemoryFault(_)) + ) + { + block = self.get_or_compile_block(cpu.rip, memory, 1); + } + let block_fn_ptr = match block { Ok(ptr) => ptr, Err(reason) => { return ExecutionResult { @@ -1661,6 +1673,58 @@ mod tests { assert_eq!(unsupported.instructions_executed, 0); } + #[test] + fn completed_store_survives_the_next_fault_or_budget_boundary() { + for native in [false, true] { + for (tail, fault) in [ + (&[0xcc][..], ExitReason::IllegalInstruction(0xcc)), + ( + &[0xb8, 1][..], + ExitReason::MemoryFault(MemoryError::UnmappedAddress(ENTRY + 5)), + ), + ( + &[0x48, 0x8b, 0x02, 0xf4][..], + ExitReason::MemoryFault(MemoryError::UnmappedAddress(0x4000)), + ), + ] { + for budget in [0, 1, 16] { + // mov [rcx], rax; then an illegal, truncated or faulting instruction. + let code = [&[0x48, 0x89, 0x01][..], tail].concat(); + let (mut engine, mut cpu, mut memory) = fixture(&code); + memory + .map_region(0x3000, 8, MemoryPermissions::READ_WRITE) + .unwrap(); + cpu.set(Register::Rax, 0x1234); + cpu.set(Register::Rcx, 0x3000); + cpu.set(Register::Rdx, 0x4000); + let mut expected = cpu.clone(); + if budget > 0 { + expected.rip += 3; + } + let result = if native { + engine.run(&mut cpu, &mut memory, budget) + } else { + crate::cpu::Interpreter::new(cpu, &mut memory, budget).run() + }; + assert_eq!(result.instructions_executed, budget.min(1)); + assert_eq!( + result.exit_reason, + if budget <= 1 { + ExitReason::StepLimitExceeded + } else { + fault + } + ); + assert_eq!(result.final_state, expected); + assert_eq!( + memory.read_u64(0x3000).unwrap(), + if budget == 0 { 0 } else { 0x1234 } + ); + } + } + } + } + #[test] fn hlt_preserves_first_memory_fault() { let code = [