-
Notifications
You must be signed in to change notification settings - Fork 283
Rust bindings needed for custom function lifters #8197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zznop
wants to merge
2
commits into
dev
Choose a base branch
from
test_tms320c6x
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,14 +26,15 @@ use crate::{ | |
| calling_convention::CoreCallingConvention, | ||
| data_buffer::DataBuffer, | ||
| disassembly::InstructionTextToken, | ||
| function::Function, | ||
| function::{Function, Location, NativeBlock}, | ||
| platform::Platform, | ||
| rc::*, | ||
| relocation::CoreRelocationHandler, | ||
| string::{IntoCStr, *}, | ||
| types::{NameAndType, Type}, | ||
| Endianness, | ||
| }; | ||
| use std::collections::{HashMap, HashSet}; | ||
| use std::ops::Deref; | ||
| use std::{ | ||
| borrow::Borrow, | ||
|
|
@@ -47,7 +48,9 @@ use std::ptr::NonNull; | |
| use crate::function_recognizer::FunctionRecognizer; | ||
| use crate::relocation::{CustomRelocationHandlerHandle, RelocationHandler}; | ||
|
|
||
| use crate::basic_block::BasicBlock; | ||
| use crate::confidence::Conf; | ||
| use crate::logger::Logger; | ||
| use crate::low_level_il::expression::ValueExpr; | ||
| use crate::low_level_il::lifting::{ | ||
| get_default_flag_cond_llil, get_default_flag_write_llil, LowLevelILFlagWriteOp, | ||
|
|
@@ -591,13 +594,133 @@ pub trait ArchitectureWithFunctionContext: Architecture { | |
|
|
||
| pub struct FunctionLifterContext { | ||
| pub(crate) handle: *mut BNFunctionLifterContext, | ||
| pub function: *mut BNLowLevelILFunction, | ||
| pub platform: Ref<Platform>, | ||
| pub logger: Ref<Logger>, | ||
| pub blocks: Vec<Ref<BasicBlock<NativeBlock>>>, | ||
| pub no_return_calls: HashSet<Location>, | ||
| pub contextual_returns: HashMap<Location, bool>, | ||
| pub inlined_remapping: HashMap<Location, Location>, | ||
| pub user_indirect_branches: HashMap<Location, HashSet<Location>>, | ||
| pub auto_indirect_branches: HashMap<Location, HashSet<Location>>, | ||
| //pub inlined_calls: HashSet<u64>, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not used anymore? |
||
| } | ||
|
|
||
| unsafe fn lifter_context_slice<'a, T>(ptr: *const T, len: usize) -> &'a [T] { | ||
| if len == 0 { | ||
| &[] | ||
| } else { | ||
| debug_assert!(!ptr.is_null()); | ||
| unsafe { std::slice::from_raw_parts(ptr, len) } | ||
| } | ||
| } | ||
|
|
||
| impl FunctionLifterContext { | ||
| pub unsafe fn from_raw(handle: *mut BNFunctionLifterContext) -> Self { | ||
| pub unsafe fn from_raw( | ||
| function: *mut BNLowLevelILFunction, | ||
| handle: *mut BNFunctionLifterContext, | ||
| ) -> Self { | ||
| debug_assert!(!function.is_null()); | ||
| debug_assert!(!handle.is_null()); | ||
| let flc_ref = &*handle; | ||
| let platform = unsafe { Platform::ref_from_raw(BNNewPlatformReference(flc_ref.platform)) }; | ||
| let logger = unsafe { Logger::ref_from_raw(BNNewLoggerReference(flc_ref.logger)) }; | ||
|
|
||
| let mut blocks = Vec::new(); | ||
| for i in 0..flc_ref.basicBlockCount { | ||
| let block = unsafe { | ||
| Some(BasicBlock::ref_from_raw( | ||
| BNNewBasicBlockReference(*flc_ref.basicBlocks.add(i)), | ||
| NativeBlock::new(), | ||
| )) | ||
| }; | ||
|
|
||
| blocks.push(block.unwrap()); | ||
| } | ||
|
|
||
| let raw_no_return_calls: &[BNArchitectureAndAddress] = | ||
| lifter_context_slice(flc_ref.noReturnCalls, flc_ref.noReturnCallsCount); | ||
| let no_return_calls: HashSet<Location> = | ||
| raw_no_return_calls.iter().map(Location::from).collect(); | ||
|
|
||
| let raw_contextual_return_locs: &[BNArchitectureAndAddress] = unsafe { | ||
| lifter_context_slice( | ||
| flc_ref.contextualFunctionReturnLocations, | ||
| flc_ref.contextualFunctionReturnCount, | ||
| ) | ||
| }; | ||
| let raw_contextual_return_vals: &[bool] = unsafe { | ||
| lifter_context_slice( | ||
| flc_ref.contextualFunctionReturnValues, | ||
| flc_ref.contextualFunctionReturnCount, | ||
| ) | ||
| }; | ||
| let contextual_returns: HashMap<Location, bool> = raw_contextual_return_locs | ||
| .iter() | ||
| .map(Location::from) | ||
| .zip(raw_contextual_return_vals.iter().copied()) | ||
| .collect(); | ||
|
|
||
| let inlined_remapping: HashMap<Location, Location> = { | ||
| let raw_inline_remap_locs: &[BNArchitectureAndAddress] = lifter_context_slice( | ||
| flc_ref.inlinedRemappingKeys, | ||
| flc_ref.inlinedRemappingEntryCount, | ||
| ); | ||
|
|
||
| let raw_inline_remap_dests: &[BNArchitectureAndAddress] = lifter_context_slice( | ||
| flc_ref.inlinedRemappingValues, | ||
| flc_ref.inlinedRemappingEntryCount, | ||
| ); | ||
|
|
||
| raw_inline_remap_locs | ||
| .iter() | ||
| .map(Location::from) | ||
| .zip(raw_inline_remap_dests.iter().map(Location::from)) | ||
| .collect() | ||
| }; | ||
|
|
||
| FunctionLifterContext { handle } | ||
| let mut user_indirect_branches: HashMap<Location, HashSet<Location>> = HashMap::new(); | ||
| let mut auto_indirect_branches: HashMap<Location, HashSet<Location>> = HashMap::new(); | ||
| for i in 0..flc_ref.indirectBranchesCount { | ||
| let entry = unsafe { *flc_ref.indirectBranches.add(i) }; | ||
| let src = Location::new( | ||
| Some(CoreArchitecture::from_raw(entry.sourceArch)), | ||
| entry.sourceAddr, | ||
| ); | ||
| let dest = Location::new( | ||
| Some(CoreArchitecture::from_raw(entry.destArch)), | ||
| entry.destAddr, | ||
| ); | ||
| if entry.autoDefined { | ||
| auto_indirect_branches.entry(src).or_default().insert(dest); | ||
| } else { | ||
| user_indirect_branches.entry(src).or_default().insert(dest); | ||
| } | ||
| } | ||
|
|
||
| FunctionLifterContext { | ||
| handle, | ||
| function: BNNewLowLevelILFunctionReference(function), | ||
| platform, | ||
| logger, | ||
| blocks, | ||
| no_return_calls, | ||
| contextual_returns, | ||
| inlined_remapping, | ||
| user_indirect_branches, | ||
| auto_indirect_branches, | ||
| } | ||
| } | ||
|
|
||
| pub fn prepare_block_translation( | ||
| &self, | ||
| func: &LowLevelILMutableFunction, | ||
| arch: &CoreArchitecture, | ||
| address: u64, | ||
| ) { | ||
| unsafe { | ||
| BNPrepareBlockTranslation(func.handle, arch.handle, address); | ||
| } | ||
| } | ||
|
|
||
| pub fn get_function_arch_context<A: ArchitectureWithFunctionContext>( | ||
|
|
@@ -615,6 +738,14 @@ impl FunctionLifterContext { | |
| } | ||
| } | ||
|
|
||
| impl Drop for FunctionLifterContext { | ||
| fn drop(&mut self) { | ||
| if !self.function.is_null() { | ||
| unsafe { BNFreeLowLevelILFunction(self.function) }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO: WTF?!?!?!? | ||
| pub struct CoreArchitectureList(*mut *mut BNArchitecture, usize); | ||
|
|
||
|
|
@@ -1622,12 +1753,12 @@ where | |
| A: 'static + Architecture<Handle = CustomArchitectureHandle<A>> + Send + Sync, | ||
| { | ||
| let custom_arch = unsafe { &*(ctxt as *mut A) }; | ||
| let function = unsafe { | ||
| let llil = unsafe { | ||
| LowLevelILMutableFunction::from_raw_with_arch(function, Some(*custom_arch.as_ref())) | ||
| }; | ||
| let mut context: FunctionLifterContext = | ||
| unsafe { FunctionLifterContext::from_raw(context) }; | ||
| custom_arch.lift_function(function, &mut context) | ||
|
|
||
| let mut ctx = unsafe { FunctionLifterContext::from_raw(function, context) }; | ||
| custom_arch.lift_function(llil, &mut ctx) | ||
| } | ||
|
|
||
| extern "C" fn cb_reg_name<A>(ctxt: *mut c_void, reg: u32) -> *mut c_char | ||
|
|
@@ -2622,7 +2753,6 @@ where | |
|
|
||
| unsafe { | ||
| let res = BNRegisterArchitecture(name.as_ptr(), &mut custom_arch as *mut _); | ||
|
|
||
| assert!(!res.is_null()); | ||
|
|
||
| (*raw).arch.assume_init_mut() | ||
|
|
||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems strange to expose the
functionas a raw pointer, can we ref count it like everything else?