Skip to content

Commit d747ba3

Browse files
committed
Extract ZIP-embedded path splitting into a function
1 parent 960e60a commit d747ba3

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

src/symbolize/gimli.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::convert::TryInto;
1414
use core::mem;
1515
use core::u32;
1616
use libc::c_void;
17-
use mystd::ffi::OsString;
17+
use mystd::ffi::{OsStr, OsString};
1818
use mystd::fs::File;
1919
use mystd::path::Path;
2020
use mystd::prelude::v1::*;
@@ -322,6 +322,21 @@ fn create_mapping(lib: &Library) -> Option<Mapping> {
322322
}
323323
}
324324

325+
/// Try to extract the archive path from an "embedded" library path
326+
/// (e.g. `/path/to/my.apk` from `/path/to/my.apk!/mylib.so`).
327+
///
328+
/// Returns `None` if the path does not contain a `!/` separator.
329+
#[cfg(target_os = "android")]
330+
fn extract_zip_path_android(path: &OsStr) -> Option<&OsStr> {
331+
use mystd::os::unix::ffi::OsStrExt;
332+
333+
path.as_bytes()
334+
.windows(2)
335+
.enumerate()
336+
.find(|(_, chunk)| chunk == b"!/")
337+
.map(|(index, _)| OsStr::from_bytes(path.as_bytes().split_at(index).0))
338+
}
339+
325340
// unsafe because this is required to be externally synchronized
326341
pub unsafe fn clear_symbol_cache() {
327342
Cache::with_global(|cache| cache.mappings.clear());

src/symbolize/gimli/elf.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,7 @@ impl Mapping {
5959
pub fn new_android(path: &Path, zip_offset: Option<u64>) -> Option<Mapping> {
6060
fn map_embedded_library(path: &Path, zip_offset: u64) -> Option<Mapping> {
6161
// get path of ZIP archive (delimited by `!/`)
62-
let raw_path = path.as_os_str().as_bytes();
63-
let zip_path = raw_path
64-
.windows(2)
65-
.enumerate()
66-
.find(|(_, chunk)| chunk == b"!/")
67-
.map(|(index, _)| Path::new(OsStr::from_bytes(raw_path.split_at(index).0)))?;
62+
let zip_path = Path::new(super::extract_zip_path_android(path.as_os_str())?);
6863

6964
let file = fs::File::open(zip_path).ok()?;
7065
let len = file.metadata().ok()?.len();

src/symbolize/gimli/libs_dl_iterate_phdr.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,12 @@ unsafe extern "C" fn callback(
8989
// only check for ZIP-embedded file if we have data from /proc/self/maps
9090
maps.as_ref().and_then(|maps| {
9191
// check if file is embedded within a ZIP archive by searching for `!/`
92-
name.as_bytes()
93-
.windows(2)
94-
.find(|&chunk| chunk == b"!/")
95-
.and_then(|_| {
96-
// find MapsEntry matching library's base address
97-
maps.iter()
98-
.find(|m| m.ip_matches(dlpi_addr as usize))
99-
.and_then(|m| Some(m.offset()))
100-
})
92+
super::extract_zip_path_android(&name).and_then(|_| {
93+
// find MapsEntry matching library's base address and get its file offset
94+
maps.iter()
95+
.find(|m| m.ip_matches(dlpi_addr as usize))
96+
.map(|m| m.offset())
97+
})
10198
})
10299
};
103100
let headers = if dlpi_phdr.is_null() || dlpi_phnum == 0 {

0 commit comments

Comments
 (0)