From 5a06604d40b8bc4257429a3bbd4741cca779999a Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Sat, 11 May 2024 07:31:49 -0300 Subject: [PATCH] add Function::{reg_value_at, reg_value_after, reg_value_at_exit} methods --- rust/src/function.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/rust/src/function.rs b/rust/src/function.rs index 1a6452211..7473e4672 100644 --- a/rust/src/function.rs +++ b/rust/src/function.rs @@ -848,6 +848,60 @@ impl Function { let display = self.int_display_type(instr_addr, value, operand, Some(arch)); (display, name) } + + /// Get the value the provided string register address corresponding to the given virtual address + /// + /// * `addr` - virtual address of the instruction to query + /// * `reg` - string value of native register to query + /// * `arch` - (optional) Architecture for the given function + /// + /// # Example + /// ```no_run + /// # use binaryninja::architecture::{ArchitectureExt, Register}; + /// # let fun: binaryninja::function::Function = todo!(); + /// let reg = fun.arch().register_by_name("rdi").unwrap(); + /// let value = fun.reg_value_at(0x400dbe, reg.id(), None); + /// ``` + pub fn reg_value_at( + &self, + addr: u64, + reg: u32, + arch: Option, + ) -> RegisterValue { + let arch = arch.unwrap_or_else(|| self.arch()); + let register = unsafe { BNGetRegisterValueAtInstruction(self.handle, arch.0, addr, reg) }; + register.into() + } + + /// Gets the value instruction address corresponding to the given virtual address + /// + /// * `addr` - virtual address of the instruction to query + /// * `reg` - string value of native register to query + /// * `arch` - (optional) Architecture for the given function + /// + /// # Example + /// ```no_run + /// # use binaryninja::architecture::{ArchitectureExt, Register}; + /// # let fun: binaryninja::function::Function = todo!(); + /// let reg = fun.arch().register_by_name("rdi").unwrap(); + /// let value = fun.reg_value_after(0x400dbe, reg.id(), None); + /// ``` + pub fn reg_value_after( + &self, + addr: u64, + reg: u32, + arch: Option, + ) -> RegisterValue { + let arch = arch.unwrap_or_else(|| self.arch()); + let register = + unsafe { BNGetRegisterValueAfterInstruction(self.handle, arch.0, addr, reg) }; + register.into() + } + + pub fn reg_value_at_exit(&self, reg: u32) -> Conf { + let register = unsafe { BNGetFunctionRegisterValueAtExit(self.handle, reg) }; + Conf::new(register.value.into(), register.confidence) + } } impl fmt::Debug for Function {