Skip to content

[Rust] Add BinaryView::code_refs_from to look up references with the instruction's architecture - #8736

Open
jrozner wants to merge 1 commit into
Vector35:devfrom
jrozner:rust-code-refs-from-arch
Open

jrozner wants to merge 1 commit into
Vector35:devfrom
jrozner:rust-code-refs-from-arch

Conversation

@jrozner

@jrozner jrozner commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Problem

BinaryView::code_refs_from_addr(addr, func) always builds its BNReferenceSource with func.arch(). A function can contain blocks of more than one architecture, such as a Thumb block inside an armv7 function after a bx interworking switch. For an instruction in such a block, the core decodes it with the wrong architecture and the lookup silently returns nothing. BNGetCodeReferencesFrom and Python's get_code_refs_from(addr, func=None, arch=None) both take the architecture, but the Rust bindings had no way to pass it.

Change

New BinaryView::code_refs_from(&self, source: &CodeReference) -> Vec<u64> uses source.arch. The CodeReferences returned by code_refs_to_addr and Function::call_sites already record the instruction's architecture, so they can be passed back as they are. code_refs_from_addr keeps its signature and behavior and now forwards to code_refs_from. Its doc comment explains when the function's architecture is the wrong one and points to the new method. No new unsafe beyond the FFI call.

I checked for other places that substitute func.arch() for a caller's architecture:

  • data_refs_from_addr: BNGetDataReferencesFrom takes no architecture, so nothing to change.
  • Function helpers already take arch: Option<CoreArchitecture> and fall back to the function's only when it's None.
  • BNGetCodeReferencesFromInRange has no Rust binding yet, so I didn't add one here.

plugins/warp/src/cache/guid.rs calls code_refs_from_addr(call_site.address, Some(function)) with a call site that already has its arch, so it has this problem for mixed-architecture functions. I left it unchanged because switching it to code_refs_from(&call_site) could change the WARP constraints computed for those functions.

Testing

New test_code_refs_from_mixed_arch in rust/tests/binary_view.rs builds a small raw view at test time: an armv7 function that switches to Thumb with add r0, pc, #1; bx r0 and makes a Thumb bl 0x100. It checks that the function is armv7 and the block at 0x8 is thumb2, that code_refs_from_addr(0x8, Some(&func)) returns nothing, and that code_refs_from returns [0x100] both for the call site from call_sites() and for a CodeReference built with the block's architecture. It passes when run alone. The doc example on code_refs_from compiles. The WARP plugin, the one existing caller, compiles unchanged.

cargo fmt is clean, and clippy reports nothing on the new code. cargo clippy --all-targets and cargo test stop on a compile error in examples/high_level_il.rs (non-exhaustive match on PossibleValueSet), which also happens on dev without this change.

On my machine, running the whole integration suite in parallel fails most tests with "Failed to get architecture" or "Failed to get platform", and a single-threaded run hung in binary_reader. I haven't checked whether that also happens on dev.

…e instruction's architecture

`code_refs_from_addr` always decodes the instruction with its function's
architecture, so a reference from a block of another architecture, such as a
Thumb block inside an armv7 function, is never found. `code_refs_from` takes a
`CodeReference` and uses its `arch`, so a call site or another recorded
reference can be passed back as it is. `code_refs_from_addr` keeps its
behavior and now forwards to it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant