Skip to content

Commit

Permalink
Fix new RuntimeError integration with the LLVM-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Apr 27, 2020
1 parent 2bbe340 commit cffdb38
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 20 deletions.
1 change: 0 additions & 1 deletion examples/callback-guest/callback-guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ extern "C" {
fn host_callback() -> u32;
}

#[no_mangle]
fn test_callback() -> u32 {
42
}
Expand Down
Binary file modified examples/callback-guest/callback-guest.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/llvm-backend/cpp/object_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ MemoryManager::~MemoryManager() {
callbacks.dealloc_memory(read_section.base, read_section.size);
callbacks.dealloc_memory(readwrite_section.base, readwrite_section.size);
}
void unwinding_setjmp(jmp_buf stack_out, void (*func)(void *), void *userdata) {
void unwinding_setjmp(jmp_buf &stack_out, void (*func)(void *), void *userdata) {
if (setjmp(stack_out)) {

} else {
Expand Down
4 changes: 3 additions & 1 deletion lib/llvm-backend/cpp/object_loader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ enum WasmTrapType {

extern "C" void callback_trampoline(void *, void *);

extern "C" void copy_runtime_error(runtime_error_t src, runtime_error_t dst);

struct MemoryManager : llvm::RuntimeDyld::MemoryManager {
public:
MemoryManager(callbacks_t callbacks) : callbacks(callbacks) {}
Expand Down Expand Up @@ -159,7 +161,7 @@ public:
runtime_error_t error_data;

virtual void write_error(WasmErrorSink &out) const noexcept override {
out.user_error = error_data;
copy_runtime_error(error_data, out.user_error);
}
};

Expand Down
10 changes: 10 additions & 0 deletions lib/llvm-backend/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ extern "C" {
) -> bool;
}

#[no_mangle]
pub unsafe extern "C" fn copy_runtime_error(
src: *mut Option<RuntimeError>,
dst: *mut Option<RuntimeError>,
) {
assert_eq!(src as usize % mem::align_of::<Option<RuntimeError>>(), 0);
assert_eq!(dst as usize % mem::align_of::<Option<RuntimeError>>(), 0);
ptr::copy::<Option<RuntimeError>>(src, dst, 1);
}

/// `invoke_trampoline` is a wrapper around `cxx_invoke_trampoline`, for fixing up the obsoleted
/// `trap_out` in the C++ part.
unsafe extern "C" fn invoke_trampoline(
Expand Down
2 changes: 0 additions & 2 deletions lib/llvm-backend/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,10 +944,8 @@ pub unsafe extern "C" fn callback_trampoline(
b: *mut Option<RuntimeError>,
callback: *mut BreakpointHandler,
) {
dbg!("In llvm's callback_trampoline");
let callback = Box::from_raw(callback);
let result: Result<(), RuntimeError> = callback(BreakpointInfo { fault: None });
dbg!(&*b);
match result {
Ok(()) => *b = None,
Err(e) => *b = Some(e),
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime-core/src/fault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ extern "C" fn signal_trap_handler(
let mut should_unwind = false;
let mut unwind_result: Option<Box<RuntimeError>> = None;
let get_unwind_result = |uw_result: Option<Box<RuntimeError>>| -> Box<RuntimeError> {
dbg!(uw_result)
uw_result
.unwrap_or_else(|| Box::new(RuntimeError::InvokeError(InvokeError::FailedWithNoError)))
};

Expand Down
6 changes: 4 additions & 2 deletions lib/runtime-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,10 @@ pub(crate) fn call_func_with_index_inner(
if success {
Ok(())
} else {
let error: RuntimeError = dbg!(error_out)
.map_or_else(|| RuntimeError::InvokeError(InvokeError::FailedWithNoError), Into::into);
let error: RuntimeError = error_out.map_or_else(
|| RuntimeError::InvokeError(InvokeError::FailedWithNoError),
Into::into,
);
dbg!(&error);
Err(error.into())
}
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime-core/src/typed_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ macro_rules! impl_traits {
) {
Ok(Rets::from_ret_array(rets))
} else {
Err(dbg!(error_out).map_or_else(|| RuntimeError::InvokeError(InvokeError::FailedWithNoError), Into::into))
Err(error_out.map_or_else(|| RuntimeError::InvokeError(InvokeError::FailedWithNoError), Into::into))
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions tests/high_level_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,7 @@ wasmer_backends! {

let result = foo.call();

dbg!(&result);
if let Err(e) = &result {
dbg!(&e);
eprintln!("{}", &e);
}
match &result {
Err(RuntimeError::User(e)) => { dbg!("USER", &e); } ,
Err(e) => {dbg!("GENERIC",&e); } ,
_ => { dbg!("OKaY!"); },
}
if let Err(RuntimeError::User(e)) = result {
dbg!("WAT");
let exit_code = e.downcast::<ExitCode>().unwrap();
assert_eq!(exit_code.code, 42);
} else {
Expand Down

0 comments on commit cffdb38

Please sign in to comment.