Skip to content

Commit c8acc28

Browse files
committed
Avoid conditionally-destructuring struct field
Instead, we now attempt to parse /proc/self/maps upfront.
1 parent adca7f1 commit c8acc28

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

src/symbolize/gimli/libs_dl_iterate_phdr.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,43 @@ use super::mystd::borrow::ToOwned;
66
use super::mystd::env;
77
use super::mystd::ffi::{CStr, OsStr};
88
use super::mystd::os::unix::prelude::*;
9-
use super::{Library, LibrarySegment, OsString, Vec};
9+
use super::{parse_running_mmaps, Library, LibrarySegment, OsString, Vec};
1010
use core::slice;
1111

1212
struct CallbackData {
1313
libs: Vec<Library>,
14-
#[cfg(target_os = "android")]
15-
maps: Option<Vec<super::parse_running_mmaps::MapsEntry>>,
14+
maps: Option<Vec<parse_running_mmaps::MapsEntry>>,
1615
}
1716
pub(super) fn native_libraries() -> Vec<Library> {
1817
let mut cb_data = CallbackData {
1918
libs: Vec::new(),
20-
#[cfg(target_os = "android")]
21-
maps: super::parse_running_mmaps::parse_maps().ok(),
19+
#[cfg(not(target_os = "hurd"))]
20+
maps: parse_running_mmaps::parse_maps().ok(),
21+
#[cfg(target_os = "hurd")]
22+
maps: None,
2223
};
2324
unsafe {
2425
libc::dl_iterate_phdr(Some(callback), core::ptr::addr_of_mut!(cb_data).cast());
2526
}
2627
cb_data.libs
2728
}
2829

29-
fn infer_current_exe(base_addr: usize) -> OsString {
30-
cfg_if::cfg_if! {
31-
if #[cfg(not(target_os = "hurd"))] {
32-
if let Ok(entries) = super::parse_running_mmaps::parse_maps() {
33-
let opt_path = entries
34-
.iter()
35-
.find(|e| e.ip_matches(base_addr) && e.pathname().len() > 0)
36-
.map(|e| e.pathname())
37-
.cloned();
38-
if let Some(path) = opt_path {
39-
return path;
40-
}
41-
}
30+
fn infer_current_exe(
31+
maps: &Option<Vec<parse_running_mmaps::MapsEntry>>,
32+
base_addr: usize,
33+
) -> OsString {
34+
#[cfg(not(target_os = "hurd"))]
35+
if let Some(entries) = maps {
36+
let opt_path = entries
37+
.iter()
38+
.find(|e| e.ip_matches(base_addr) && e.pathname().len() > 0)
39+
.map(|e| e.pathname())
40+
.cloned();
41+
if let Some(path) = opt_path {
42+
return path;
4243
}
4344
}
45+
4446
env::current_exe().map(|e| e.into()).unwrap_or_default()
4547
}
4648

@@ -59,11 +61,7 @@ unsafe extern "C" fn callback(
5961
let dlpi_phdr = unsafe { (*info).dlpi_phdr };
6062
let dlpi_phnum = unsafe { (*info).dlpi_phnum };
6163
// SAFETY: We assured this.
62-
let CallbackData {
63-
libs,
64-
#[cfg(target_os = "android")]
65-
maps,
66-
} = unsafe { &mut *data.cast::<CallbackData>() };
64+
let CallbackData { libs, maps } = unsafe { &mut *data.cast::<CallbackData>() };
6765
// most implementations give us the main program first
6866
let is_main = libs.is_empty();
6967
// we may be statically linked, which means we are main and mostly one big blob of code
@@ -76,7 +74,7 @@ unsafe extern "C" fn callback(
7674
// don't try to look up our name from /proc/self/maps, it'll get silly
7775
env::current_exe().unwrap_or_default().into_os_string()
7876
} else if is_main && no_given_name {
79-
infer_current_exe(dlpi_addr as usize)
77+
infer_current_exe(&maps, dlpi_addr as usize)
8078
} else {
8179
// this fallback works even if we are main, because some platforms give the name anyways
8280
if dlpi_name.is_null() {

0 commit comments

Comments
 (0)