use crate::SendSyncPtr;
use anyhow::Result;
use std::ptr::{self, NonNull};
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
pub struct UnwindRegistration {
registrations: Vec<SendSyncPtr<u8>>,
}
extern "C" {
fn __register_frame(fde: *const u8);
fn __deregister_frame(fde: *const u8);
}
fn using_libunwind() -> bool {
static USING_LIBUNWIND: AtomicUsize = AtomicUsize::new(LIBUNWIND_UNKNOWN);
const LIBUNWIND_UNKNOWN: usize = 0;
const LIBUNWIND_YES: usize = 1;
const LIBUNWIND_NO: usize = 2;
if cfg!(target_os = "macos") {
return true;
}
match USING_LIBUNWIND.load(Relaxed) {
LIBUNWIND_YES => true,
LIBUNWIND_NO => false,
LIBUNWIND_UNKNOWN => {
let looks_like_libunwind = unsafe {
!libc::dlsym(ptr::null_mut(), "__unw_add_dynamic_fde\0".as_ptr().cast()).is_null()
};
USING_LIBUNWIND.store(
if looks_like_libunwind {
LIBUNWIND_YES
} else {
LIBUNWIND_NO
},
Relaxed,
);
looks_like_libunwind
}
_ => unreachable!(),
}
}
impl UnwindRegistration {
#[allow(missing_docs)]
pub const SECTION_NAME: &'static str = ".eh_frame";
pub unsafe fn new(
_base_address: *const u8,
unwind_info: *const u8,
unwind_len: usize,
) -> Result<UnwindRegistration> {
debug_assert_eq!(
unwind_info as usize % crate::page_size(),
0,
"The unwind info must always be aligned to a page"
);
let mut registrations = Vec::new();
if using_libunwind() {
let start = unwind_info;
let end = start.add(unwind_len - 4);
let mut current = start;
while current < end {
let len = current.cast::<u32>().read_unaligned() as usize;
if current != start {
__register_frame(current);
let cur = NonNull::new(current.cast_mut()).unwrap();
registrations.push(SendSyncPtr::new(cur));
}
current = current.add(len + 4);
}
} else {
__register_frame(unwind_info);
let info = NonNull::new(unwind_info.cast_mut()).unwrap();
registrations.push(SendSyncPtr::new(info));
}
Ok(UnwindRegistration { registrations })
}
}
impl Drop for UnwindRegistration {
fn drop(&mut self) {
unsafe {
for fde in self.registrations.iter().rev() {
__deregister_frame(fde.as_ptr());
}
}
}
}