use anyhow::Result;
pub struct UnwindRegistration {
registrations: Vec<usize>,
}
extern "C" {
fn __register_frame(fde: *const u8);
fn __deregister_frame(fde: *const u8);
}
impl UnwindRegistration {
pub const SECTION_NAME: &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 % wasmtime_runtime::page_size(),
0,
"The unwind info must always be aligned to a page"
);
let mut registrations = Vec::new();
if cfg!(any(
all(target_os = "linux", target_env = "gnu"),
target_os = "freebsd"
)) {
__register_frame(unwind_info);
registrations.push(unwind_info as usize);
} else {
let start = unwind_info;
let end = start.add(unwind_len - 4);
let mut current = start;
while current < end {
let len = std::ptr::read::<u32>(current as *const u32) as usize;
if current != start {
__register_frame(current);
registrations.push(current as usize);
}
current = current.add(len + 4);
}
}
Ok(UnwindRegistration { registrations })
}
}
impl Drop for UnwindRegistration {
fn drop(&mut self) {
unsafe {
for fde in self.registrations.iter().rev() {
__deregister_frame(*fde as *const _);
}
}
}
}