Skip to content

Commit

Permalink
Merge #379
Browse files Browse the repository at this point in the history
379: Fix small return types. r=syrusakbary a=lachlansneff

This fixes returning integers smaller than 32bits from imported functions. Also some work on fixing returning errors from functions.

Co-authored-by: Lachlan Sneff <[email protected]>
Co-authored-by: Syrus <[email protected]>
  • Loading branch information
3 people committed Apr 22, 2019
2 parents 6478500 + 3729c4a commit a42b7d3
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 213 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All PRs to the Wasmer repository must add to this file.
Blocks of changes will separated by version increments.

## **[Unreleased]**
- [#379](https://github.com/wasmerio/wasmer/pull/379) Fix small return types from imported functions.
- [#371](https://github.com/wasmerio/wasmer/pull/371) Add more Debug impl for WASI types
- [#368](https://github.com/wasmerio/wasmer/pull/368) Fix issue with write buffering
- [#343](https://github.com/wasmerio/wasmer/pull/343) Implement preopened files for WASI and fix aligment issue when accessing WASI memory
Expand Down
2 changes: 1 addition & 1 deletion lib/clif-backend/src/signal/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> R
*jmp_buf = prev_jmp_buf;

if let Some(data) = super::TRAP_EARLY_DATA.with(|cell| cell.replace(None)) {
Err(RuntimeError::Panic { data })
Err(RuntimeError::Error { data })
} else {
let (faulting_addr, inst_ptr) = CAUGHT_ADDRESSES.with(|cell| cell.get());

Expand Down
13 changes: 5 additions & 8 deletions lib/emscripten/src/varargs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::mem;
use wasmer_runtime_core::{
types::{Type, WasmExternType},
vm::Ctx,
};
use wasmer_runtime_core::{types::WasmExternType, vm::Ctx};

#[repr(transparent)]
#[derive(Copy, Clone)]
Expand All @@ -19,12 +16,12 @@ impl VarArgs {
}

unsafe impl WasmExternType for VarArgs {
const TYPE: Type = Type::I32;
type Native = i32;

fn to_bits(self) -> u64 {
self.pointer as u64
fn to_native(self) -> Self::Native {
self.pointer as _
}
fn from_bits(n: u64) -> Self {
fn from_native(n: Self::Native) -> Self {
Self { pointer: n as u32 }
}
}
10 changes: 3 additions & 7 deletions lib/runtime-core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::{FuncSig, GlobalDescriptor, MemoryDescriptor, TableDescriptor, Type, Value};
use crate::types::{FuncSig, GlobalDescriptor, MemoryDescriptor, TableDescriptor, Type};
use core::borrow::Borrow;
use std::any::Any;

Expand Down Expand Up @@ -121,8 +121,7 @@ impl std::error::Error for LinkError {}
/// Comparing two `RuntimeError`s always evaluates to false.
pub enum RuntimeError {
Trap { msg: Box<str> },
Exception { data: Box<[Value]> },
Panic { data: Box<dyn Any> },
Error { data: Box<dyn Any> },
}

impl PartialEq for RuntimeError {
Expand All @@ -137,10 +136,7 @@ impl std::fmt::Display for RuntimeError {
RuntimeError::Trap { ref msg } => {
write!(f, "WebAssembly trap occured during runtime: {}", msg)
}
RuntimeError::Exception { ref data } => {
write!(f, "Uncaught WebAssembly exception: {:?}", data)
}
RuntimeError::Panic { data } => {
RuntimeError::Error { data } => {
let msg = if let Some(s) = data.downcast_ref::<String>() {
s
} else if let Some(s) = data.downcast_ref::<&str>() {
Expand Down
62 changes: 29 additions & 33 deletions lib/runtime-core/src/typed_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use crate::{
error::RuntimeError,
export::{Context, Export, FuncPointer},
import::IsExport,
types::{FuncSig, Type, WasmExternType},
types::{FuncSig, NativeWasmType, Type, WasmExternType},
vm::{self, Ctx},
};
use std::{
any::Any,
convert::Infallible,
ffi::c_void,
fmt,
marker::PhantomData,
Expand Down Expand Up @@ -120,25 +121,28 @@ pub trait TrapEarly<Rets>
where
Rets: WasmTypeList,
{
fn report(self) -> Result<Rets, Box<dyn Any>>;
type Error: 'static;
fn report(self) -> Result<Rets, Self::Error>;
}

impl<Rets> TrapEarly<Rets> for Rets
where
Rets: WasmTypeList,
{
fn report(self) -> Result<Rets, Box<dyn Any>> {
type Error = Infallible;
fn report(self) -> Result<Rets, Infallible> {
Ok(self)
}
}

impl<Rets, E> TrapEarly<Rets> for Result<Rets, E>
where
Rets: WasmTypeList,
E: Any,
E: 'static,
{
fn report(self) -> Result<Rets, Box<dyn Any>> {
self.map_err(|err| Box::new(err) as Box<dyn Any>)
type Error = E;
fn report(self) -> Result<Rets, E> {
self
}
}

Expand Down Expand Up @@ -213,22 +217,24 @@ impl<A: WasmExternType> WasmTypeList for (A,) {
type CStruct = S1<A>;
type RetArray = [u64; 1];
fn from_ret_array(array: Self::RetArray) -> Self {
(WasmExternType::from_bits(array[0]),)
(WasmExternType::from_native(NativeWasmType::from_bits(
array[0],
)),)
}
fn empty_ret_array() -> Self::RetArray {
[0u64]
}
fn from_c_struct(c_struct: Self::CStruct) -> Self {
let S1(a) = c_struct;
(a,)
(WasmExternType::from_native(a),)
}
fn into_c_struct(self) -> Self::CStruct {
#[allow(unused_parens, non_snake_case)]
let (a,) = self;
S1(a)
S1(WasmExternType::to_native(a))
}
fn types() -> &'static [Type] {
&[A::TYPE]
&[A::Native::TYPE]
}
#[allow(non_snake_case)]
unsafe fn call<Rets: WasmTypeList>(
Expand All @@ -237,11 +243,8 @@ impl<A: WasmExternType> WasmTypeList for (A,) {
wasm: Wasm,
ctx: *mut Ctx,
) -> Result<Rets, WasmTrapInfo> {
// type Trampoline = extern "C" fn(*mut Ctx, *const c_void, *const u64, *mut u64);
// type Invoke = extern "C" fn(Trampoline, *mut Ctx, *const c_void, *const u64, *mut u64, &mut WasmTrapInfo) -> bool;

let (a,) = self;
let args = [a.to_bits()];
let args = [a.to_native().to_bits()];
let mut rets = Rets::empty_ret_array();
let mut trap = WasmTrapInfo::Unknown;

Expand Down Expand Up @@ -277,40 +280,37 @@ where
macro_rules! impl_traits {
( [$repr:ident] $struct_name:ident, $( $x:ident ),* ) => {
#[repr($repr)]
pub struct $struct_name <$( $x ),*> ( $( $x ),* );
pub struct $struct_name <$( $x: WasmExternType ),*> ( $( <$x as WasmExternType>::Native ),* );

impl< $( $x: WasmExternType, )* > WasmTypeList for ( $( $x ),* ) {
type CStruct = $struct_name<$( $x ),*>;
type RetArray = [u64; count_idents!( $( $x ),* )];
fn from_ret_array(array: Self::RetArray) -> Self {
#[allow(non_snake_case)]
let [ $( $x ),* ] = array;
( $( WasmExternType::from_bits($x) ),* )
( $( WasmExternType::from_native(NativeWasmType::from_bits($x)) ),* )
}
fn empty_ret_array() -> Self::RetArray {
[0; count_idents!( $( $x ),* )]
}
fn from_c_struct(c_struct: Self::CStruct) -> Self {
#[allow(non_snake_case)]
let $struct_name ( $( $x ),* ) = c_struct;
( $( $x ),* )
( $( WasmExternType::from_native($x) ),* )
}
fn into_c_struct(self) -> Self::CStruct {
#[allow(unused_parens, non_snake_case)]
let ( $( $x ),* ) = self;
$struct_name ( $( $x ),* )
$struct_name ( $( WasmExternType::to_native($x) ),* )
}
fn types() -> &'static [Type] {
&[$( $x::TYPE, )*]
&[$( $x::Native::TYPE, )*]
}
#[allow(non_snake_case)]
unsafe fn call<Rets: WasmTypeList>(self, f: NonNull<vm::Func>, wasm: Wasm, ctx: *mut Ctx) -> Result<Rets, WasmTrapInfo> {
// type Trampoline = extern "C" fn(*mut Ctx, *const c_void, *const u64, *mut u64);
// type Invoke = extern "C" fn(Trampoline, *mut Ctx, *const c_void, *const u64, *mut u64, &mut WasmTrapInfo) -> bool;

#[allow(unused_parens)]
let ( $( $x ),* ) = self;
let args = [ $( $x.to_bits() ),* ];
let args = [ $( $x.to_native().to_bits() ),* ];
let mut rets = Rets::empty_ret_array();
let mut trap = WasmTrapInfo::Unknown;

Expand All @@ -319,12 +319,6 @@ macro_rules! impl_traits {
} else {
Err(trap)
}

// let f: extern fn(*mut Ctx $( ,$x )*) -> Rets::CStruct = mem::transmute(f);
// #[allow(unused_parens)]
// let ( $( $x ),* ) = self;
// let c_struct = f(ctx $( ,$x )*);
// Rets::from_c_struct(c_struct)
}
}

Expand All @@ -335,15 +329,17 @@ macro_rules! impl_traits {

/// This is required for the llvm backend to be able to unwind through this function.
#[cfg_attr(nightly, unwind(allowed))]
extern fn wrap<$( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly<Rets>, FN: Fn( &mut Ctx $( ,$x )* ) -> Trap>( ctx: &mut Ctx $( ,$x: $x )* ) -> Rets::CStruct {
extern fn wrap<$( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly<Rets>, FN: Fn( &mut Ctx $( ,$x )* ) -> Trap>( ctx: &mut Ctx $( ,$x: <$x as WasmExternType>::Native )* ) -> Rets::CStruct {
let f: FN = unsafe { mem::transmute_copy(&()) };

let err = match panic::catch_unwind(panic::AssertUnwindSafe(|| {
let res = f( ctx $( ,$x )* ).report();
res
f( ctx $( ,WasmExternType::from_native($x) )* ).report()
})) {
Ok(Ok(returns)) => return returns.into_c_struct(),
Ok(Err(err)) => err,
Ok(Err(err)) => {
let b: Box<_> = err.into();
b as Box<dyn Any>
},
Err(err) => err,
};

Expand Down
Loading

0 comments on commit a42b7d3

Please sign in to comment.