Skip to content
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

Move create_apk_elf_path() helper into new apk module #1032

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/apk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::ffi::OsStr;
use std::path::Path;
use std::path::PathBuf;

use crate::Error;
use crate::Result;


pub(crate) fn create_apk_elf_path(apk: &Path, elf: &Path) -> Result<PathBuf> {
let mut extension = apk
.extension()
.unwrap_or_else(|| OsStr::new("apk"))
.to_os_string();
// Append '!' to indicate separation from archive internal contents
// that follow. This is an Android convention.
let () = extension.push("!");

let mut apk = apk.to_path_buf();
if !apk.set_extension(extension) {
return Err(Error::with_invalid_data(format!(
"path {} is not valid",
apk.display()
)))
}

let path = apk.join(elf);
Ok(path)
}


#[cfg(test)]
mod tests {
use super::*;

use crate::ErrorKind;


/// Check that we can create a path to an ELF inside an APK as expected.
#[test]
fn elf_apk_path_creation() {
let apk = Path::new("/root/test.apk");
let elf = Path::new("subdir/libc.so");
let path = create_apk_elf_path(apk, elf).unwrap();
assert_eq!(path, Path::new("/root/test.apk!/subdir/libc.so"));

let err = create_apk_elf_path(Path::new(""), elf).unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidData);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ extern crate test;

#[macro_use]
mod cfg;
#[cfg(feature = "apk")]
mod apk;
#[cfg(feature = "breakpad")]
mod breakpad;
#[cfg(feature = "dwarf")]
Expand Down
37 changes: 2 additions & 35 deletions src/symbolize/symbolizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;

#[cfg(feature = "apk")]
use crate::apk::create_apk_elf_path;
#[cfg(feature = "breakpad")]
use crate::breakpad::BreakpadResolver;
use crate::elf::ElfParser;
Expand Down Expand Up @@ -114,29 +116,6 @@ impl Debug for DebugMapsEntry<'_> {
}


#[cfg(feature = "apk")]
fn create_apk_elf_path(apk: &Path, elf: &Path) -> Result<PathBuf> {
let mut extension = apk
.extension()
.unwrap_or_else(|| OsStr::new("apk"))
.to_os_string();
// Append '!' to indicate separation from archive internal contents
// that follow. This is an Android convention.
let () = extension.push("!");

let mut apk = apk.to_path_buf();
if !apk.set_extension(extension) {
return Err(Error::with_invalid_data(format!(
"path {} is not valid",
apk.display()
)))
}

let path = apk.join(elf);
Ok(path)
}


/// Demangle a symbol name using the demangling scheme for the given language.
#[cfg(feature = "demangle")]
fn maybe_demangle(name: Cow<'_, str>, language: SrcLang) -> Cow<'_, str> {
Expand Down Expand Up @@ -1527,18 +1506,6 @@ mod tests {
});
}

/// Check that we can create a path to an ELF inside an APK as expected.
#[test]
fn elf_apk_path_creation() {
let apk = Path::new("/root/test.apk");
let elf = Path::new("subdir/libc.so");
let path = create_apk_elf_path(apk, elf).unwrap();
assert_eq!(path, Path::new("/root/test.apk!/subdir/libc.so"));

let err = create_apk_elf_path(Path::new(""), elf).unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidData);
}

/// Check that we can correctly construct the source code path to a symbol.
#[test]
fn symbol_source_code_path() {
Expand Down