-
Notifications
You must be signed in to change notification settings - Fork 298
[ObjC] Enable removal of more memory management functions #8524
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
Closed
AngeloD2022
wants to merge
2
commits into
Vector35:dev
from
AngeloD2022:objc-additional-mem-management-funcs
+70
−3
Closed
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| use crate::activities::util; | ||
| use crate::{error::ILLevel, metadata::GlobalState, Error}; | ||
| use binaryninja::{ | ||
| architecture::{Architecture as _, CoreRegister, Register as _, RegisterInfo as _}, | ||
| binary_view::BinaryView, | ||
|
|
@@ -11,12 +13,11 @@ use binaryninja::{ | |
| lifting::LowLevelILLabel, | ||
| LowLevelILRegisterKind, | ||
| }, | ||
| symbol::Symbol, | ||
| variable::PossibleValueSet, | ||
| 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]] = &[ | ||
|
|
@@ -29,8 +30,51 @@ const IGNORABLE_MEMORY_MANAGEMENT_FUNCTIONS: &[&[u8]] = &[ | |
| b"_objc_retainAutoreleasedReturnValue", | ||
| b"_objc_retainBlock", | ||
| b"_objc_unsafeClaimAutoreleasedReturnValue", | ||
| b"_objc_claimAutoreleasedReturnValue", | ||
| ]; | ||
|
|
||
| fn is_objc_rt_symbol_dscview(view: &BinaryView, symbol: &Symbol) -> bool { | ||
| if view.view_type() != "DSCView" { | ||
| return false; | ||
| } | ||
|
|
||
| let addr = symbol.address(); | ||
|
|
||
| let Some(view_section_name) = view.sections().iter().find_map(|sect| { | ||
| (sect.start()..sect.end()) | ||
| .contains(&addr) | ||
| .then(|| sect.name()) | ||
| }) else { | ||
| return false; | ||
| }; | ||
|
|
||
| let Ok(view_section_name) = view_section_name.to_str() else { | ||
| return false; | ||
| }; | ||
|
|
||
| // Ensure that the symbol lies within the text segment and section of libobjc._.dylib by | ||
| // parsing the view section name: | ||
|
|
||
| let Some((image, mem_fqid)) = view_section_name.split_once("::") else { | ||
| return false; | ||
| }; | ||
|
|
||
| if mem_fqid != "__TEXT.__text" { | ||
| return false; | ||
| } | ||
|
|
||
| let mut imgparts = image.split("."); | ||
| matches!( | ||
| ( | ||
| imgparts.next(), | ||
| imgparts.next(), | ||
| imgparts.next(), | ||
| imgparts.next() | ||
| ), | ||
| (Some("libobjc"), Some(_), Some("dylib"), None) | ||
| ) | ||
| } | ||
|
|
||
| fn is_call_to_ignorable_memory_management_function<'func>( | ||
| view: &binaryninja::binary_view::BinaryView, | ||
| instr: &'func LowLevelILInstruction<'func, Mutable, NonSSA>, | ||
|
|
@@ -57,7 +101,17 @@ fn is_call_to_ignorable_memory_management_function<'func>( | |
| // 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) | ||
| // Normalize the name to also include register-specific functions (e.g. _objc_release_x19). | ||
| let symbol_name = util::strip_arc_reg_suffix(symbol_name); | ||
|
Contributor
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. |
||
|
|
||
| let name_test = IGNORABLE_MEMORY_MANAGEMENT_FUNCTIONS.contains(&symbol_name); | ||
|
|
||
| if view.view_type() == "DSCView" { | ||
| // verify that the section of the symbol in question lies within the memory of the runtime image | ||
| name_test && is_objc_rt_symbol_dscview(view, &symbol) | ||
| } else { | ||
| name_test | ||
| } | ||
| } | ||
|
|
||
| fn process_instruction( | ||
|
|
||
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
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.
Can you clarify what the purpose of this function is? What is it guarding against?
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.
I added that check just to be as precise as possible and reduce the reliance on only symbol names– because if I'm not mistaken, it's possible in principle for multiple different symbols with the same name to exist in a binary view. So although it's unlikely– if Apple includes a function in one of their shared cache libraries with the same name as a runtime memory management function, it won't be hidden like one.