-
Notifications
You must be signed in to change notification settings - Fork 261
[ObjC] Add support for removing runtime calls that perform retain count operations #7440
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
16595bf
[Rust] Add support for activity eligibility predicates involving plat…
bdash 63e4f9e
[ObjC] Silence some more unnecessary logging
bdash c3225e7
Fix Rust formatting
bdash 82e8eac
[ObjC] Add support for removing runtime calls that perform retain cou…
bdash 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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod inline_stubs; | ||
| pub mod objc_msg_send_calls; | ||
| pub mod remove_memory_management; | ||
| pub mod super_init; |
192 changes: 192 additions & 0 deletions
192
plugins/workflow_objc/src/activities/remove_memory_management.rs
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 |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| use binaryninja::{ | ||
| architecture::{Architecture as _, CoreRegister, Register as _, RegisterInfo as _}, | ||
| binary_view::{BinaryView, BinaryViewExt as _}, | ||
| low_level_il::{ | ||
| expression::{ExpressionHandler, LowLevelILExpressionKind}, | ||
| function::{LowLevelILFunction, Mutable, NonSSA}, | ||
| instruction::{ | ||
| InstructionHandler, LowLevelILInstruction, LowLevelILInstructionKind, | ||
| LowLevelInstructionIndex, | ||
| }, | ||
| lifting::LowLevelILLabel, | ||
| LowLevelILRegisterKind, | ||
| }, | ||
| workflow::AnalysisContext, | ||
| }; | ||
|
|
||
| use crate::{error::ILLevel, metadata::GlobalState, Error}; | ||
|
|
||
| // TODO: We should also handle `objc_retain_x` / `objc_release_x` variants | ||
| // that use a custom calling convention. | ||
| const IGNORABLE_MEMORY_MANAGEMENT_FUNCTIONS: &[&[u8]] = &[ | ||
| b"_objc_autorelease", | ||
| b"_objc_autoreleaseReturnValue", | ||
| b"_objc_release", | ||
| b"_objc_retain", | ||
| b"_objc_retainAutorelease", | ||
| b"_objc_retainAutoreleaseReturnValue", | ||
| b"_objc_retainAutoreleasedReturnValue", | ||
| b"_objc_retainBlock", | ||
| b"_objc_unsafeClaimAutoreleasedReturnValue", | ||
| ]; | ||
|
|
||
| fn is_call_to_ignorable_memory_management_function<'func>( | ||
| view: &binaryninja::binary_view::BinaryView, | ||
| instr: &'func LowLevelILInstruction<'func, Mutable, NonSSA>, | ||
| ) -> bool { | ||
| let target = match instr.kind() { | ||
| LowLevelILInstructionKind::Call(call) | LowLevelILInstructionKind::TailCall(call) => { | ||
| let LowLevelILExpressionKind::ConstPtr(address) = call.target().kind() else { | ||
| return false; | ||
| }; | ||
| address.value() | ||
| } | ||
| LowLevelILInstructionKind::Goto(target) => target.address(), | ||
| _ => return false, | ||
| }; | ||
| let Some(symbol) = view.symbol_by_address(target) else { | ||
| return false; | ||
| }; | ||
|
|
||
| let symbol_name = symbol.full_name(); | ||
| let symbol_name = symbol_name.to_bytes(); | ||
|
|
||
| // Remove any j_ prefix that the shared cache workflow adds to stub functions. | ||
| let symbol_name = symbol_name.strip_prefix(b"j_").unwrap_or(symbol_name); | ||
|
|
||
| IGNORABLE_MEMORY_MANAGEMENT_FUNCTIONS.contains(&symbol_name) | ||
| } | ||
|
|
||
| fn process_instruction( | ||
| bv: &BinaryView, | ||
| llil: &LowLevelILFunction<Mutable, NonSSA>, | ||
| insn: &LowLevelILInstruction<Mutable, NonSSA>, | ||
| link_register: LowLevelILRegisterKind<CoreRegister>, | ||
| link_register_size: usize, | ||
| ) -> Result<bool, &'static str> { | ||
| if !is_call_to_ignorable_memory_management_function(bv, insn) { | ||
| return Ok(false); | ||
| } | ||
|
|
||
| // TODO: Removing calls to `objc_release` can sometimes leave behind a load of a struct field | ||
| // that appears to be unused. It's not clear whether we should be trying to detect and remove | ||
| // those here, or if some later analysis pass should be cleaning them up but isn't. | ||
|
|
||
| match insn.kind() { | ||
| LowLevelILInstructionKind::TailCall(_) => unsafe { | ||
| llil.set_current_address(insn.address()); | ||
| llil.replace_expression( | ||
| insn.expr_idx(), | ||
| llil.ret(llil.reg(link_register_size, link_register)), | ||
| ); | ||
| }, | ||
| LowLevelILInstructionKind::Call(_) => unsafe { | ||
| // The memory management functions that are currently supported either return void | ||
| // or return their first argument. For arm64, the first argument is passed in `x0` | ||
| // and results are returned in `x0`, so we can replace the call with a nop. We'll need | ||
| // to revisit this to support other architectures, and to support the `objc_retain_x` | ||
| // `objc_release_x` functions that accept their argument in a different register. | ||
| llil.set_current_address(insn.address()); | ||
| llil.replace_expression(insn.expr_idx(), llil.nop()); | ||
| }, | ||
| LowLevelILInstructionKind::Goto(_) if insn.index.0 == 0 => unsafe { | ||
| // If the `objc_retain` is the first instruction in the function, this function | ||
| // can only contain the call to the memory management function since when the | ||
| // memory management function returns, it will return to this function's caller. | ||
| llil.set_current_address(insn.address()); | ||
| llil.replace_expression( | ||
| insn.expr_idx(), | ||
| llil.ret(llil.reg(link_register_size, link_register)), | ||
| ); | ||
| }, | ||
| LowLevelILInstructionKind::Goto(_) => { | ||
| // The shared cache workflow inlines calls to stub functions, which causes them | ||
| // to show up as a `lr = <next instruction>; goto <stub function instruction>;` | ||
| // sequence. We need to remove the load of `lr` and update the `goto` to jump | ||
| // to the next instruction. | ||
|
|
||
| let Some(prev) = | ||
| llil.instruction_from_index(LowLevelInstructionIndex(insn.index.0 - 1)) | ||
| else { | ||
| return Ok(false); | ||
| }; | ||
|
|
||
| let target = match prev.kind() { | ||
| LowLevelILInstructionKind::SetReg(op) if op.dest_reg() == link_register => { | ||
| let LowLevelILExpressionKind::ConstPtr(value) = op.source_expr().kind() else { | ||
| return Ok(false); | ||
| }; | ||
| value.value() | ||
| } | ||
| _ => return Ok(false), | ||
| }; | ||
|
|
||
| let Some(LowLevelInstructionIndex(target_idx)) = llil.instruction_index_at(target) | ||
| else { | ||
| return Ok(false); | ||
| }; | ||
|
|
||
| // TODO: Manually creating a label like this is fragile and relies on a) knowledge of | ||
| // how labels are used by core, and b) that the target is the first instruction in | ||
| // a basic block. We should do this differently. | ||
| let mut label = LowLevelILLabel::new(); | ||
| label.operand = target_idx; | ||
|
|
||
| unsafe { | ||
| llil.set_current_address(prev.address()); | ||
| llil.replace_expression(prev.expr_idx(), llil.nop()); | ||
| llil.set_current_address(insn.address()); | ||
| llil.replace_expression(insn.expr_idx(), llil.goto(&mut label)); | ||
| } | ||
| } | ||
| _ => return Ok(false), | ||
| } | ||
|
|
||
| Ok(true) | ||
| } | ||
|
|
||
| pub fn process(ac: &AnalysisContext) -> Result<(), Error> { | ||
| let view = ac.view(); | ||
| if GlobalState::should_ignore_view(&view) { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let func = ac.function(); | ||
|
|
||
| let Some(link_register) = func.arch().link_reg() else { | ||
| return Ok(()); | ||
| }; | ||
| let link_register_size = link_register.info().size(); | ||
| let link_register = LowLevelILRegisterKind::Arch(link_register); | ||
|
|
||
| let Some(llil) = (unsafe { ac.llil_function() }) else { | ||
| return Err(Error::MissingIL { | ||
| level: ILLevel::Low, | ||
| func_start: func.start(), | ||
| }); | ||
| }; | ||
|
|
||
| let mut function_changed = false; | ||
| for block in llil.basic_blocks().iter() { | ||
| for insn in block.iter() { | ||
| match process_instruction(&view, &llil, &insn, link_register, link_register_size) { | ||
| Ok(true) => function_changed = true, | ||
| Ok(_) => {} | ||
| Err(err) => { | ||
| log::error!( | ||
| "Error processing instruction at {:#x}: {}", | ||
| insn.address(), | ||
| err | ||
| ); | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if function_changed { | ||
| // Regenerate SSA form after modifications | ||
| llil.generate_ssa_form(); | ||
| } | ||
| Ok(()) | ||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.