Start implementing exception handling by adding frame descriptor entry processing.

- Soon, we should be able basic exceptions.
This commit is contained in:
Lachlan Sneff
2019-03-02 10:56:02 -08:00
parent 87ddf4f855
commit 5760f6006a
7 changed files with 68 additions and 49 deletions

View File

@ -59,6 +59,7 @@ struct Callbacks {
dealloc_memory: extern "C" fn(*mut u8, usize) -> LLVMResult,
lookup_vm_symbol: extern "C" fn(*const c_char, usize) -> *const vm::Func,
visit_fde: extern "C" fn(*mut u8, usize, extern "C" fn(*mut u8)),
}
extern "C" {
@ -165,11 +166,18 @@ fn get_callbacks() -> Callbacks {
}
}
extern "C" fn visit_fde(fde: *mut u8, size: usize, visitor: extern "C" fn(*mut u8)) {
unsafe {
crate::platform::visit_fde(fde, size, visitor);
}
}
Callbacks {
alloc_memory,
protect_memory,
dealloc_memory,
lookup_vm_symbol,
visit_fde,
}
}

View File

@ -14,6 +14,7 @@ use wasmparser::{self, WasmDecoder};
mod backend;
mod code;
mod intrinsics;
mod platform;
mod read_info;
mod state;
mod trampolines;

View File

@ -1,8 +1,7 @@
#[cfg(target_family = "unix")]
#[cfg(unix)]
mod unix;
#[cfg(target_family = "unix")]
#[cfg(unix)]
pub use self::unix::*;
#[cfg(target_family = "windows")]
compile_error!("windows not yet supported for the llvm-based compiler backend");
compile_error!("windows not yet supported for the llvm-based compiler backend");

View File

@ -0,0 +1,35 @@
/// `__register_frame` and `__deregister_frame` on macos take a single fde as an
/// argument, so we need to parse the fde table here.
///
/// This is a pretty direct port of llvm's fde handling code:
/// https://llvm.org/doxygen/RTDyldMemoryManager_8cpp_source.html.
#[cfg(target_os = "macos")]
pub unsafe fn visit_fde(addr: *mut u8, size: usize, visitor: extern "C" fn(*mut u8)) {
unsafe fn process_fde(entry: *mut u8, visitor: extern "C" fn(*mut u8)) -> *mut u8 {
let mut p = entry;
let length = (p as *const u32).read_unaligned();
p = p.add(4);
let offset = (p as *const u32).read_unaligned();
if offset != 0 {
visitor(entry);
}
p.add(length as usize)
}
let mut p = addr;
let end = p.add(size);
loop {
if p >= end {
break;
}
p = process_fde(p, visitor);
}
}
#[cfg(not(target_os = "macos"))]
pub unsafe fn visit_fde(addr: *mut u8, size: usize, visitor: extern "C" fn(*mut u8)) {
visitor(addr);
}

View File

@ -1,40 +0,0 @@
extern "C" {
fn __register_frame(frame: *const u8);
fn __deregister_frame(frame: *const u8);
}
pub unsafe fn register_eh_frames(eh_frames: *const u8, num_bytes: usize) {
visit_frame_desc_entries(eh_frames, num_bytes, |frame| __register_frame(frame));
}
unsafe fn visit_frame_desc_entries<F>(eh_frames: *const u8, num_bytes: usize, visitor: F)
where
F: Fn(*const u8),
{
let mut next = eh_frames;
let mut end = eh_frames.add(num_bytes);
loop {
if next >= end {
break;
}
let cfi = next;
let mut cfi_num_bytes = (next as *const u32).read_unaligned() as u64;
assert!(cfi_num_bytes != 0);
next = next.add(4);
if num_bytes == 0xffffffff {
let cfi_num_bytes64 = (next as *const u64).read_unaligned();
cfi_num_bytes = cfi_num_bytes64;
next = next.add(8);
}
let cie_offset = (next as *const u32).read_unaligned();
if cie_offset != 0 {
visitor(cfi);
}
next = next.add(cfi_num_bytes as usize);
}
}