Repository navigation
[Rust] Add BinaryView::code_refs_from to look up references with the instruction's architecture - #8736
Open
jrozner wants to merge 1 commit into
Open
[Rust] Add BinaryView::code_refs_from to look up references with the instruction's architecture#8736jrozner wants to merge 1 commit into
BinaryView::code_refs_from to look up references with the instruction's architecture#8736jrozner wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
BinaryView::code_refs_from_addr(addr, func)always builds itsBNReferenceSourcewithfunc.arch(). A function can contain blocks of more than one architecture, such as a Thumb block inside anarmv7function after abxinterworking switch. For an instruction in such a block, the core decodes it with the wrong architecture and the lookup silently returns nothing.BNGetCodeReferencesFromand Python'sget_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>usessource.arch. TheCodeReferences returned bycode_refs_to_addrandFunction::call_sitesalready record the instruction's architecture, so they can be passed back as they are.code_refs_from_addrkeeps its signature and behavior and now forwards tocode_refs_from. Its doc comment explains when the function's architecture is the wrong one and points to the new method. No newunsafebeyond the FFI call.I checked for other places that substitute
func.arch()for a caller's architecture:data_refs_from_addr:BNGetDataReferencesFromtakes no architecture, so nothing to change.Functionhelpers already takearch: Option<CoreArchitecture>and fall back to the function's only when it'sNone.BNGetCodeReferencesFromInRangehas no Rust binding yet, so I didn't add one here.plugins/warp/src/cache/guid.rscallscode_refs_from_addr(call_site.address, Some(function))with a call site that already has itsarch, so it has this problem for mixed-architecture functions. I left it unchanged because switching it tocode_refs_from(&call_site)could change the WARP constraints computed for those functions.Testing
New
test_code_refs_from_mixed_archinrust/tests/binary_view.rsbuilds a small raw view at test time: anarmv7function that switches to Thumb withadd r0, pc, #1; bx r0and makes a Thumbbl 0x100. It checks that the function isarmv7and the block at0x8isthumb2, thatcode_refs_from_addr(0x8, Some(&func))returns nothing, and thatcode_refs_fromreturns[0x100]both for the call site fromcall_sites()and for aCodeReferencebuilt with the block's architecture. It passes when run alone. The doc example oncode_refs_fromcompiles. The WARP plugin, the one existing caller, compiles unchanged.cargo fmtis clean, and clippy reports nothing on the new code.cargo clippy --all-targetsandcargo teststop on a compile error inexamples/high_level_il.rs(non-exhaustive match onPossibleValueSet), which also happens ondevwithout 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 ondev.