Skip to content

Commit

Permalink
fix(c-api) Cherry-pick manually from wasmerio#1657.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Oct 1, 2020
1 parent bcb8812 commit ecb79e2
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 47 deletions.
11 changes: 7 additions & 4 deletions lib/c-api/src/wasm_c_api/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ pub unsafe extern "C" fn wasm_func_new(
num_rets
];

let _traps = callback(processed_args.as_ptr(), results.as_mut_ptr());
// TODO: do something with `traps`
let trap = callback(processed_args.as_ptr(), results.as_mut_ptr());
if !trap.is_null() {
let trap: Box<wasm_trap_t> = Box::from_raw(trap);
RuntimeError::raise(Box::new(trap.inner));
}

let processed_results = results
.into_iter()
Expand Down Expand Up @@ -134,7 +137,7 @@ pub unsafe extern "C" fn wasm_func_call(
func: &wasm_func_t,
args: *const wasm_val_t,
results: *mut wasm_val_t,
) -> Option<NonNull<wasm_trap_t>> {
) -> Option<Box<wasm_trap_t>> {
let num_params = func.inner.ty().params().len();
let params: Vec<Val> = (0..num_params)
.map(|i| (&(*args.add(i))).try_into())
Expand All @@ -149,7 +152,7 @@ pub unsafe extern "C" fn wasm_func_call(
}
None
}
Err(e) => Some(NonNull::new_unchecked(Box::into_raw(Box::new(e)) as _)),
Err(e) => Some(Box::new(e.into())),
}
}

Expand Down
31 changes: 31 additions & 0 deletions lib/c-api/src/wasm_c_api/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,37 @@ macro_rules! wasm_declare_vec {
pub data: *mut [<wasm_ $name _t>],
}

impl<'a> From<Vec<[<wasm_ $name _t>]>> for [<wasm_ $name _vec_t>] {
fn from(other: Vec<[<wasm_ $name _t>]>) -> Self {
let mut boxed_slice = other.into_boxed_slice();
let size = boxed_slice.len();
let data = boxed_slice.as_mut_ptr();
::std::mem::forget(boxed_slice);
Self {
size,
data,
}
}
}

impl<'a, T: Into<[<wasm_ $name _t>]> + Clone> From<&'a [T]> for [<wasm_ $name _vec_t>] {
fn from(other: &'a [T]) -> Self {
let size = other.len();
let mut copied_data = other
.iter()
.cloned()
.map(Into::into)
.collect::<Vec<[<wasm_ $name _t>]>>()
.into_boxed_slice();
let data = copied_data.as_mut_ptr();
::std::mem::forget(copied_data);
Self {
size,
data,
}
}
}

impl [<wasm_ $name _vec_t>] {
pub unsafe fn into_slice(&self) -> Option<&[[<wasm_ $name _t>]]>{
if self.data.is_null() {
Expand Down
82 changes: 45 additions & 37 deletions lib/c-api/src/wasm_c_api/trap.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
use super::types::wasm_byte_vec_t;
use std::mem;
use std::ptr::NonNull;
use super::store::wasm_store_t;
use super::types::{wasm_byte_vec_t, wasm_frame_t, wasm_frame_vec_t, wasm_message_t};
use wasmer::RuntimeError;

// opaque type which is a `RuntimeError`
/// cbindgen:ignore
#[allow(non_camel_case_types)]
pub struct wasm_trap_t {}
pub struct wasm_trap_t {
pub(crate) inner: RuntimeError,
}

impl From<RuntimeError> for wasm_trap_t {
fn from(other: RuntimeError) -> Self {
Self { inner: other }
}
}

/// cbindgen:ignore
#[no_mangle]
pub unsafe extern "C" fn wasm_trap_delete(trap: Option<NonNull<wasm_trap_t>>) {
if let Some(t_inner) = trap {
let _ = Box::from_raw(t_inner.cast::<RuntimeError>().as_ptr());
}
pub unsafe extern "C" fn wasm_trap_new(
_store: &mut wasm_store_t,
message: &wasm_message_t,
) -> Option<Box<wasm_trap_t>> {
let message_bytes: &[u8] = message.into_slice()?;
let message_str = c_try!(std::str::from_utf8(message_bytes));
let runtime_error = RuntimeError::new(message_str);
let trap = runtime_error.into();

Some(Box::new(trap))
}

/// cbindgen:ignore
#[no_mangle]
pub unsafe extern "C" fn wasm_trap_message(
trap: *const wasm_trap_t,
out_ptr: *mut wasm_byte_vec_t,
) {
let re = &*(trap as *const RuntimeError);
// this code assumes no nul bytes appear in the message
let mut message = format!("{}\0", re);
message.shrink_to_fit();

// TODO use `String::into_raw_parts` when it gets stabilized
(*out_ptr).size = message.as_bytes().len();
(*out_ptr).data = message.as_mut_ptr();
mem::forget(message);
pub unsafe extern "C" fn wasm_trap_delete(_trap: Option<Box<wasm_trap_t>>) {}

/// cbindgen:ignore
#[no_mangle]
pub unsafe extern "C" fn wasm_trap_message(trap: &wasm_trap_t, out_ptr: &mut wasm_byte_vec_t) {
let message = trap.inner.message();
let byte_vec: wasm_byte_vec_t = message.into_bytes().into();
out_ptr.size = byte_vec.size;
out_ptr.data = byte_vec.data;
}

// in trap/RuntimeError we need to store
// 1. message
// 2. origin (frame); frame contains:
// 1. func index
// 2. func offset
// 3. module offset
// 4. which instance this was apart of

/*#[no_mangle]
pub unsafe extern "C" fn wasm_trap_trace(trap: *const wasm_trap_t, out_ptr: *mut wasm_frame_vec_t) {
let re = &*(trap as *const RuntimeError);
todo!()
}*/

//wasm_declare_ref!(trap);
//wasm_declare_ref!(foreign);
/// cbindgen:ignore
#[no_mangle]
pub unsafe extern "C" fn wasm_trap_origin(trap: &wasm_trap_t) -> Option<Box<wasm_frame_t>> {
trap.inner.trace().first().map(Into::into).map(Box::new)
}

/// cbindgen:ignore
#[no_mangle]
pub unsafe extern "C" fn wasm_trap_trace(trap: &wasm_trap_t, out_ptr: &mut wasm_frame_vec_t) {
let frames = trap.inner.trace();
let frame_vec: wasm_frame_vec_t = frames.into();

out_ptr.size = frame_vec.size;
out_ptr.data = frame_vec.data;
}
51 changes: 51 additions & 0 deletions lib/c-api/src/wasm_c_api/types/frame.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use super::super::instance::wasm_instance_t;
use wasmer::FrameInfo;

#[derive(Debug, Clone)]
#[repr(C)]
pub struct wasm_frame_t {
info: FrameInfo,
}

impl<'a> From<&'a FrameInfo> for wasm_frame_t {
fn from(other: &'a FrameInfo) -> Self {
other.clone().into()
}
}

impl From<FrameInfo> for wasm_frame_t {
fn from(other: FrameInfo) -> Self {
Self { info: other }
}
}

#[no_mangle]
pub unsafe extern "C" fn wasm_frame_copy(frame: &wasm_frame_t) -> Box<wasm_frame_t> {
Box::new(frame.clone())
}

#[no_mangle]
pub unsafe extern "C" fn wasm_frame_delete(_frame: Option<Box<wasm_frame_t>>) {}

#[no_mangle]
pub unsafe extern "C" fn wasm_frame_instance(frame: &wasm_frame_t) -> *const wasm_instance_t {
//todo!("wasm_frame_instance")
std::ptr::null()
}

#[no_mangle]
pub unsafe extern "C" fn wasm_frame_func_index(frame: &wasm_frame_t) -> u32 {
frame.info.func_index()
}

#[no_mangle]
pub unsafe extern "C" fn wasm_frame_func_offset(frame: &wasm_frame_t) -> usize {
frame.info.func_offset()
}

#[no_mangle]
pub unsafe extern "C" fn wasm_frame_module_offset(frame: &wasm_frame_t) -> usize {
frame.info.module_offset()
}

wasm_declare_vec!(frame);
12 changes: 6 additions & 6 deletions lib/c-api/src/wasm_c_api/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod export;
mod extern_;
mod frame;
mod function;
mod global;
mod import;
Expand All @@ -10,6 +11,7 @@ mod value;

pub use export::*;
pub use extern_::*;
pub use frame::*;
pub use function::*;
pub use global::*;
pub use import::*;
Expand All @@ -23,12 +25,6 @@ pub type wasm_byte_t = u8;

wasm_declare_vec!(byte);

#[derive(Debug)]
#[repr(C)]
pub struct wasm_frame_t {}

wasm_declare_vec!(frame);

/// cbindgen:ignore
#[allow(non_camel_case_types)]
pub type wasm_name_t = wasm_byte_vec_t;
Expand All @@ -37,3 +33,7 @@ pub type wasm_name_t = wasm_byte_vec_t;
/// cbindgen:ignore
#[allow(non_camel_case_types)]
pub struct wasm_ref_t;

/// cbindgen:ignore
#[allow(non_camel_case_types)]
pub type wasm_message_t = wasm_byte_vec_t;

0 comments on commit ecb79e2

Please sign in to comment.