-
Notifications
You must be signed in to change notification settings - Fork 261
Add support for symbolicating APK/ZIP-embedded libraries on Android #662
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
workingjubilee
merged 1 commit into
rust-lang:master
from
sudoBash418:pr/android-apk-symbolication
Sep 9, 2024
Merged
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
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 |
---|---|---|
|
@@ -6,51 +6,62 @@ use super::mystd::borrow::ToOwned; | |
use super::mystd::env; | ||
use super::mystd::ffi::{CStr, OsStr}; | ||
use super::mystd::os::unix::prelude::*; | ||
use super::{Library, LibrarySegment, OsString, Vec}; | ||
use super::{parse_running_mmaps, Library, LibrarySegment, OsString, Vec}; | ||
use core::slice; | ||
|
||
struct CallbackData { | ||
libs: Vec<Library>, | ||
maps: Option<Vec<parse_running_mmaps::MapsEntry>>, | ||
} | ||
pub(super) fn native_libraries() -> Vec<Library> { | ||
let mut ret = Vec::new(); | ||
let mut cb_data = CallbackData { | ||
libs: Vec::new(), | ||
#[cfg(not(target_os = "hurd"))] | ||
maps: parse_running_mmaps::parse_maps().ok(), | ||
#[cfg(target_os = "hurd")] | ||
maps: None, | ||
}; | ||
unsafe { | ||
libc::dl_iterate_phdr(Some(callback), core::ptr::addr_of_mut!(ret).cast()); | ||
libc::dl_iterate_phdr(Some(callback), core::ptr::addr_of_mut!(cb_data).cast()); | ||
} | ||
ret | ||
cb_data.libs | ||
} | ||
|
||
fn infer_current_exe(base_addr: usize) -> OsString { | ||
cfg_if::cfg_if! { | ||
if #[cfg(not(target_os = "hurd"))] { | ||
if let Ok(entries) = super::parse_running_mmaps::parse_maps() { | ||
let opt_path = entries | ||
.iter() | ||
.find(|e| e.ip_matches(base_addr) && e.pathname().len() > 0) | ||
.map(|e| e.pathname()) | ||
.cloned(); | ||
if let Some(path) = opt_path { | ||
return path; | ||
} | ||
} | ||
fn infer_current_exe( | ||
maps: &Option<Vec<parse_running_mmaps::MapsEntry>>, | ||
base_addr: usize, | ||
) -> OsString { | ||
#[cfg(not(target_os = "hurd"))] | ||
if let Some(entries) = maps { | ||
let opt_path = entries | ||
.iter() | ||
.find(|e| e.ip_matches(base_addr) && e.pathname().len() > 0) | ||
.map(|e| e.pathname()) | ||
.cloned(); | ||
if let Some(path) = opt_path { | ||
return path; | ||
} | ||
} | ||
|
||
env::current_exe().map(|e| e.into()).unwrap_or_default() | ||
} | ||
|
||
/// # Safety | ||
/// `info` must be a valid pointer. | ||
/// `vec` must be a valid pointer to `Vec<Library>` | ||
/// `data` must be a valid pointer to `CallbackData`. | ||
#[forbid(unsafe_op_in_unsafe_fn)] | ||
unsafe extern "C" fn callback( | ||
info: *mut libc::dl_phdr_info, | ||
_size: libc::size_t, | ||
vec: *mut libc::c_void, | ||
data: *mut libc::c_void, | ||
) -> libc::c_int { | ||
// SAFETY: We are guaranteed these fields: | ||
let dlpi_addr = unsafe { (*info).dlpi_addr }; | ||
let dlpi_name = unsafe { (*info).dlpi_name }; | ||
let dlpi_phdr = unsafe { (*info).dlpi_phdr }; | ||
let dlpi_phnum = unsafe { (*info).dlpi_phnum }; | ||
// SAFETY: We assured this. | ||
let libs = unsafe { &mut *vec.cast::<Vec<Library>>() }; | ||
let CallbackData { libs, maps } = unsafe { &mut *data.cast::<CallbackData>() }; | ||
// most implementations give us the main program first | ||
let is_main = libs.is_empty(); | ||
// we may be statically linked, which means we are main and mostly one big blob of code | ||
|
@@ -63,7 +74,7 @@ unsafe extern "C" fn callback( | |
// don't try to look up our name from /proc/self/maps, it'll get silly | ||
env::current_exe().unwrap_or_default().into_os_string() | ||
} else if is_main && no_given_name { | ||
infer_current_exe(dlpi_addr as usize) | ||
infer_current_exe(&maps, dlpi_addr as usize) | ||
} else { | ||
// this fallback works even if we are main, because some platforms give the name anyways | ||
if dlpi_name.is_null() { | ||
|
@@ -73,6 +84,19 @@ unsafe extern "C" fn callback( | |
OsStr::from_bytes(unsafe { CStr::from_ptr(dlpi_name) }.to_bytes()).to_owned() | ||
} | ||
}; | ||
#[cfg(target_os = "android")] | ||
let zip_offset: Option<u64> = { | ||
// only check for ZIP-embedded file if we have data from /proc/self/maps | ||
maps.as_ref().and_then(|maps| { | ||
// check if file is embedded within a ZIP archive by searching for `!/` | ||
super::extract_zip_path_android(&name).and_then(|_| { | ||
// find MapsEntry matching library's base address and get its file offset | ||
maps.iter() | ||
.find(|m| m.ip_matches(dlpi_addr as usize)) | ||
.map(|m| m.offset()) | ||
Comment on lines
+93
to
+96
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. Note to self: reread the MapsEntry struct and its FromStr if you somehow develop another question about this. |
||
}) | ||
}) | ||
}; | ||
let headers = if dlpi_phdr.is_null() || dlpi_phnum == 0 { | ||
&[] | ||
} else { | ||
|
@@ -81,6 +105,8 @@ unsafe extern "C" fn callback( | |
}; | ||
libs.push(Library { | ||
name, | ||
#[cfg(target_os = "android")] | ||
zip_offset, | ||
segments: headers | ||
.iter() | ||
.map(|header| LibrarySegment { | ||
|
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.
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.