diff --git a/rust/src/binary_view.rs b/rust/src/binary_view.rs index 9b77b9d065..09e865cb11 100644 --- a/rust/src/binary_view.rs +++ b/rust/src/binary_view.rs @@ -2724,13 +2724,46 @@ impl BinaryView { } /// Retrieves a list of addresses pointed to by a given address. + /// + /// The instruction at `addr` is decoded with `func`'s architecture. That is wrong when the + /// instruction belongs to a block of another architecture inside `func`, such as a Thumb + /// block inside an `armv7` function, and no references are returned. Use + /// [`BinaryView::code_refs_from`] to give the instruction's architecture. pub fn code_refs_from_addr(&self, addr: u64, func: Option<&Function>) -> Vec { + let code_ref = CodeReference::new(addr, func.map(|f| f.to_owned()), func.map(|f| f.arch())); + self.code_refs_from(&code_ref) + } + + /// Retrieves a list of addresses pointed to by the instruction at `source`. + /// + /// The instruction is decoded with `source.arch`, so pass it the architecture the + /// instruction is in, which is not always that of `source.func`. A function can contain + /// blocks of more than one architecture, such as Thumb blocks inside an `armv7` function, + /// and [`BinaryView::code_refs_from_addr`] always uses the function's. The + /// [`CodeReference`]s returned by [`BinaryView::code_refs_to_addr`] and + /// [`Function::call_sites`] record the right architecture, so they can be passed back as + /// they are. For an address of your own, [`BasicBlock::arch`] of the block containing it + /// gives its architecture. + /// + /// [`BasicBlock::arch`]: crate::basic_block::BasicBlock::arch + /// + /// ```no_run + /// # use binaryninja::binary_view::BinaryView; + /// # use binaryninja::function::Function; + /// # fn example(view: &BinaryView, func: &Function) { + /// for call_site in &func.call_sites() { + /// // `call_site.arch` is the architecture of the calling instruction. + /// for target in view.code_refs_from(&call_site) { + /// println!("{:#x} -> {:#x}", call_site.address, target); + /// } + /// } + /// # } + /// ``` + pub fn code_refs_from(&self, source: &CodeReference) -> Vec { + let mut raw_source = CodeReference::into_owned_raw(source); + let mut count = 0; unsafe { - let mut count = 0; - let code_ref = - CodeReference::new(addr, func.map(|f| f.to_owned()), func.map(|f| f.arch())); - let mut raw_code_ref = CodeReference::into_owned_raw(&code_ref); - let addresses = BNGetCodeReferencesFrom(self.handle, &mut raw_code_ref, &mut count); + let addresses = BNGetCodeReferencesFrom(self.handle, &mut raw_source, &mut count); let res = std::slice::from_raw_parts(addresses, count).to_vec(); BNFreeAddressList(addresses); res diff --git a/rust/tests/binary_view.rs b/rust/tests/binary_view.rs index 54d1f2f9cd..0aeda6747f 100644 --- a/rust/tests/binary_view.rs +++ b/rust/tests/binary_view.rs @@ -11,6 +11,7 @@ use binaryninja::headless::Session; use binaryninja::main_thread::execute_on_main_thread_and_wait; use binaryninja::platform::Platform; use binaryninja::rc::Ref; +use binaryninja::references::CodeReference; use binaryninja::segment::SegmentBuilder; use binaryninja::symbol::{Symbol, SymbolBuilder, SymbolType}; use binaryninja::Endianness; @@ -226,6 +227,59 @@ fn test_binary_tags() { .expect("Failed to get tag type"); } +#[test] +fn test_code_refs_from_mixed_arch() { + let _session = Session::new().expect("Failed to initialize session"); + // An armv7 function that switches to Thumb with `bx` and calls 0x100 from Thumb code: + // 0x0: add r0, pc, #1 (arm) + // 0x4: bx r0 (arm) + // 0x8: bl 0x100 (thumb) + // 0xc: bx lr (thumb) + // 0x100: bx lr (thumb) + let mut data = vec![0u8; 0x104]; + data[..0xe].copy_from_slice(&[ + 0x01, 0x00, 0x8f, 0xe2, 0x10, 0xff, 0x2f, 0xe1, 0x00, 0xf0, 0x7a, 0xf8, 0x70, 0x47, + ]); + data[0x100..].copy_from_slice(&[0x70, 0x47, 0xc0, 0x46]); + let view = BinaryView::from_data(&FileMetadata::new(), &data); + let armv7 = Platform::by_name("linux-armv7").expect("Failed to get platform"); + let func = view + .add_user_function_with_platform(0x0, &armv7) + .expect("Failed to add function"); + view.update_analysis_and_wait(); + + let thumb_arch = func + .basic_blocks() + .iter() + .find(|block| block.start() == 0x8) + .expect("No block at the Thumb address") + .arch(); + assert_eq!(func.arch().name(), "armv7"); + assert_eq!(thumb_arch.name(), "thumb2"); + + // The function's architecture can't decode the Thumb call. + assert_eq!( + view.code_refs_from_addr(0x8, Some(&func)), + Vec::::new() + ); + + // The call site records the architecture of the calling instruction. + let call_site = func + .call_sites() + .iter() + .find(|call_site| call_site.address == 0x8) + .expect("No call site at the Thumb address"); + assert_eq!( + call_site.arch.map(|arch| arch.name()).as_deref(), + Some("thumb2") + ); + assert_eq!(view.code_refs_from(&call_site), vec![0x100]); + + // A reference built from the block's architecture works the same way. + let source = CodeReference::new(0x8, Some(func.clone()), Some(thumb_arch)); + assert_eq!(view.code_refs_from(&source), vec![0x100]); +} + // These are the target files present in OUT_DIR // Add the files to fixtures/bin static TARGET_FILES: [&str; 2] = ["atox.obj", "atof.obj"];