Skip to content

Commit

Permalink
Merge pull request #1516 from ltratt/dladdr_can_fail
Browse files Browse the repository at this point in the history
`dladdr` can fail for sensible reasons so make sure we deal with it.
  • Loading branch information
vext01 authored Dec 16, 2024
2 parents 9032c42 + a0ba138 commit 5ab08b1
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions ykaddr/src/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,25 @@ pub fn dladdr(vaddr: usize) -> Option<DLInfo> {
/// and the byte offset.
#[cached]
pub fn vaddr_to_obj_and_off(vaddr: usize) -> Option<(PathBuf, u64)> {
// Find the object file from which the virtual address was loaded.
let info = dladdr(vaddr).unwrap();
let containing_obj = PathBuf::from(info.dli_fname.unwrap().to_str().unwrap());

// Find the corresponding byte offset of the virtual address in the object.
for obj in PHDR_OBJECT_CACHE.iter() {
let obj_name = obj.name();
let obj_name: &Path = if unsafe { *obj_name.as_ptr() } == 0 {
SELF_BIN_PATH.as_path()
} else {
Path::new(obj_name.to_str().unwrap())
};
if obj_name != containing_obj {
continue;
// Find the object file from which the virtual address was loaded. This may fail for several
// reasons e.g. if we traced into code generated by another JIT (e.g. PCRE's JIT), `dladdr`
// won't be able to tell us what's going on.
if let Some(info) = dladdr(vaddr) {
let containing_obj = PathBuf::from(info.dli_fname.unwrap().to_str().unwrap());

// Find the corresponding byte offset of the virtual address in the object.
for obj in PHDR_OBJECT_CACHE.iter() {
let obj_name = obj.name();
let obj_name: &Path = if unsafe { *obj_name.as_ptr() } == 0 {
SELF_BIN_PATH.as_path()
} else {
Path::new(obj_name.to_str().unwrap())
};
if obj_name != containing_obj {
continue;
}
return Some((containing_obj, u64::try_from(vaddr).unwrap() - obj.addr()));
}
return Some((containing_obj, u64::try_from(vaddr).unwrap() - obj.addr()));
}
None
}
Expand Down

0 comments on commit 5ab08b1

Please sign in to comment.