use crate::prelude::*;
use crate::runtime::vm::SendSyncPtr;
use core::ptr::NonNull;
pub struct UnwindRegistration {
registrations: TryVec<SendSyncPtr<u8>>,
}
cfg_if::cfg_if! {
if #[cfg(target_arch = "arm")] {
unsafe extern "C" fn __register_frame(_: *const u8) {}
unsafe extern "C" fn __deregister_frame(_: *const u8) {}
unsafe extern "C" fn wasmtime_using_libunwind() -> bool {
false
}
} else {
unsafe extern "C" {
fn __register_frame(fde: *const u8);
fn __deregister_frame(fde: *const u8);
#[wasmtime_versioned_export_macros::versioned_link]
fn wasmtime_using_libunwind() -> bool;
}
}
}
fn using_libunwind() -> bool {
cfg!(target_os = "macos") || unsafe { wasmtime_using_libunwind() }
}
impl UnwindRegistration {
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> {
#[cfg(has_virtual_memory)]
debug_assert_eq!(
unwind_info as usize % crate::runtime::vm::host_page_size(),
0,
"The unwind info must always be aligned to a page"
);
let mut registrations = TryVec::new();
unsafe {
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());
}
}
}
}