Merge branch 'master' into feature/better-tar-install

This commit is contained in:
Syrus Akbary 2019-04-22 17:27:25 -07:00 committed by GitHub
commit 15d7056972
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 604 additions and 425 deletions

View File

@ -5,6 +5,11 @@ All PRs to the Wasmer repository must add to this file.
Blocks of changes will separated by version increments.
## **[Unreleased]**
- [#383](https://github.com/wasmerio/wasmer/pull/383) Hook up wasi exit code to wasmer cli.
- [#382](https://github.com/wasmerio/wasmer/pull/382) Improve error message on `--backend` flag to only suggest currently enabled backends
- [#381](https://github.com/wasmerio/wasmer/pull/381) Allow retrieving propagated user errors.
- [#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
- [#367](https://github.com/wasmerio/wasmer/pull/367) Add caching support to the LLVM backend.

11
examples/exit.wat Normal file
View File

@ -0,0 +1,11 @@
(module
(import "wasi_unstable" "proc_exit" (func $proc_exit (param i32)))
(export "_start" (func $_start))
(memory 10)
(export "memory" (memory 0))
(func $_start
(call $proc_exit (i32.const 7))
)
)

View File

@ -27,6 +27,11 @@ thread_local! {
pub static TRAP_EARLY_DATA: Cell<Option<Box<dyn Any>>> = Cell::new(None);
}
pub enum CallProtError {
Trap(WasmTrapInfo),
Error(Box<dyn Any>),
}
pub struct Caller {
handler_data: HandlerData,
trampolines: Arc<Trampolines>,
@ -59,7 +64,8 @@ impl RunnableModule for Caller {
func: NonNull<vm::Func>,
args: *const u64,
rets: *mut u64,
_trap_info: *mut WasmTrapInfo,
trap_info: *mut WasmTrapInfo,
user_error: *mut Option<Box<dyn Any>>,
invoke_env: Option<NonNull<c_void>>,
) -> bool {
let handler_data = &*invoke_env.unwrap().cast().as_ptr();
@ -68,14 +74,22 @@ impl RunnableModule for Caller {
let res = call_protected(handler_data, || {
// Leap of faith.
trampoline(ctx, func, args, rets);
})
.is_ok();
});
// the trampoline is called from C on windows
#[cfg(target_os = "windows")]
let res = call_protected(handler_data, trampoline, ctx, func, args, rets).is_ok();
let res = call_protected(handler_data, trampoline, ctx, func, args, rets);
res
match res {
Err(err) => {
match err {
CallProtError::Trap(info) => *trap_info = info,
CallProtError::Error(data) => *user_error = Some(data),
}
false
}
Ok(()) => true,
}
}
let trampoline = self

View File

@ -10,7 +10,7 @@
//! unless you have memory unsafety elsewhere in your code.
//!
use crate::relocation::{TrapCode, TrapData};
use crate::signal::HandlerData;
use crate::signal::{CallProtError, HandlerData};
use libc::{c_int, c_void, siginfo_t};
use nix::sys::signal::{
sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal, SIGBUS, SIGFPE, SIGILL, SIGSEGV,
@ -18,7 +18,7 @@ use nix::sys::signal::{
use std::cell::{Cell, UnsafeCell};
use std::ptr;
use std::sync::Once;
use wasmer_runtime_core::error::{RuntimeError, RuntimeResult};
use wasmer_runtime_core::typed_func::WasmTrapInfo;
extern "C" fn signal_trap_handler(
signum: ::nix::libc::c_int,
@ -62,7 +62,10 @@ pub unsafe fn trigger_trap() -> ! {
longjmp(jmp_buf as *mut c_void, 0)
}
pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> RuntimeResult<T> {
pub fn call_protected<T>(
handler_data: &HandlerData,
f: impl FnOnce() -> T,
) -> Result<T, CallProtError> {
unsafe {
let jmp_buf = SETJMP_BUFFER.with(|buf| buf.get());
let prev_jmp_buf = *jmp_buf;
@ -76,7 +79,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(CallProtError::Error(data))
} else {
let (faulting_addr, inst_ptr) = CAUGHT_ADDRESSES.with(|cell| cell.get());
@ -85,33 +88,18 @@ pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> R
srcloc: _,
}) = handler_data.lookup(inst_ptr)
{
Err(match Signal::from_c_int(signum) {
Err(CallProtError::Trap(match Signal::from_c_int(signum) {
Ok(SIGILL) => match trapcode {
TrapCode::BadSignature => RuntimeError::Trap {
msg: "incorrect call_indirect signature".into(),
},
TrapCode::IndirectCallToNull => RuntimeError::Trap {
msg: "indirect call to null".into(),
},
TrapCode::HeapOutOfBounds => RuntimeError::Trap {
msg: "memory out-of-bounds access".into(),
},
TrapCode::TableOutOfBounds => RuntimeError::Trap {
msg: "table out-of-bounds access".into(),
},
_ => RuntimeError::Trap {
msg: "unknown trap".into(),
},
},
Ok(SIGSEGV) | Ok(SIGBUS) => RuntimeError::Trap {
msg: "memory out-of-bounds access".into(),
},
Ok(SIGFPE) => RuntimeError::Trap {
msg: "illegal arithmetic operation".into(),
TrapCode::BadSignature => WasmTrapInfo::IncorrectCallIndirectSignature,
TrapCode::IndirectCallToNull => WasmTrapInfo::CallIndirectOOB,
TrapCode::HeapOutOfBounds => WasmTrapInfo::MemoryOutOfBounds,
TrapCode::TableOutOfBounds => WasmTrapInfo::CallIndirectOOB,
_ => WasmTrapInfo::Unknown,
},
Ok(SIGSEGV) | Ok(SIGBUS) => WasmTrapInfo::MemoryOutOfBounds,
Ok(SIGFPE) => WasmTrapInfo::IllegalArithmetic,
_ => unimplemented!(),
}
.into())
}))
} else {
let signal = match Signal::from_c_int(signum) {
Ok(SIGFPE) => "floating-point exception",
@ -122,10 +110,8 @@ pub fn call_protected<T>(handler_data: &HandlerData, f: impl FnOnce() -> T) -> R
_ => "unkown trapped signal",
};
// When the trap-handler is fully implemented, this will return more information.
Err(RuntimeError::Trap {
msg: format!("unknown trap at {:p} - {}", faulting_addr, signal).into(),
}
.into())
let s = format!("unknown trap at {:p} - {}", faulting_addr, signal);
Err(CallProtError::Error(Box::new(s)))
}
}
} else {

View File

@ -1,10 +1,11 @@
use crate::relocation::{TrapCode, TrapData};
use crate::signal::HandlerData;
use crate::signal::{CallProtError, HandlerData};
use crate::trampoline::Trampoline;
use std::cell::Cell;
use std::ffi::c_void;
use std::ptr::{self, NonNull};
use wasmer_runtime_core::error::{RuntimeError, RuntimeResult};
use wasmer_runtime_core::typed_func::WasmTrapInfo;
use wasmer_runtime_core::vm::Ctx;
use wasmer_runtime_core::vm::Func;
use wasmer_win_exception_handler::CallProtectedData;
@ -28,7 +29,7 @@ pub fn call_protected(
func: NonNull<Func>,
param_vec: *const u64,
return_vec: *mut u64,
) -> RuntimeResult<()> {
) -> Result<(), CallProtError> {
// TODO: trap early
// user code error
// if let Some(msg) = super::TRAP_EARLY_DATA.with(|cell| cell.replace(None)) {
@ -52,38 +53,22 @@ pub fn call_protected(
srcloc: _,
}) = handler_data.lookup(instruction_pointer as _)
{
Err(match signum as DWORD {
EXCEPTION_ACCESS_VIOLATION => RuntimeError::Trap {
msg: "memory out-of-bounds access".into(),
},
Err(CallProtError::Trap(match signum as DWORD {
EXCEPTION_ACCESS_VIOLATION => WasmTrapInfo::MemoryOutOfBounds,
EXCEPTION_ILLEGAL_INSTRUCTION => match trapcode {
TrapCode::BadSignature => RuntimeError::Trap {
msg: "incorrect call_indirect signature".into(),
},
TrapCode::IndirectCallToNull => RuntimeError::Trap {
msg: "indirect call to null".into(),
},
TrapCode::HeapOutOfBounds => RuntimeError::Trap {
msg: "memory out-of-bounds access".into(),
},
TrapCode::TableOutOfBounds => RuntimeError::Trap {
msg: "table out-of-bounds access".into(),
},
_ => RuntimeError::Trap {
msg: "unknown trap".into(),
},
TrapCode::BadSignature => WasmTrapInfo::IncorrectCallIndirectSignature,
TrapCode::IndirectCallToNull => WasmTrapInfo::CallIndirectOOB,
TrapCode::HeapOutOfBounds => WasmTrapInfo::MemoryOutOfBounds,
TrapCode::TableOutOfBounds => WasmTrapInfo::CallIndirectOOB,
TrapCode::UnreachableCodeReached => WasmTrapInfo::Unreachable,
_ => WasmTrapInfo::Unknown,
},
EXCEPTION_STACK_OVERFLOW => RuntimeError::Trap {
msg: "stack overflow trap".into(),
},
EXCEPTION_INT_DIVIDE_BY_ZERO | EXCEPTION_INT_OVERFLOW => RuntimeError::Trap {
msg: "illegal arithmetic operation".into(),
},
_ => RuntimeError::Trap {
msg: "unknown trap".into(),
},
}
.into())
EXCEPTION_STACK_OVERFLOW => WasmTrapInfo::Unknown,
EXCEPTION_INT_DIVIDE_BY_ZERO | EXCEPTION_INT_OVERFLOW => {
WasmTrapInfo::IllegalArithmetic
}
_ => WasmTrapInfo::Unknown,
}))
} else {
let signal = match signum as DWORD {
EXCEPTION_FLT_DENORMAL_OPERAND
@ -98,10 +83,9 @@ pub fn call_protected(
_ => "unkown trapped signal",
};
Err(RuntimeError::Trap {
msg: format!("unknown trap at {} - {}", exception_address, signal).into(),
}
.into())
let s = format!("unknown trap at {} - {}", exception_address, signal);
Err(CallProtError::Error(Box::new(s)))
}
}

View File

@ -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)]
@ -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 }
}
}

View File

@ -213,6 +213,8 @@ fn main() {
println!("cargo:rustc-link-lib=static=llvm-backend");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=cpp/object_loader.cpp");
println!("cargo:rerun-if-changed=cpp/object_loader.hh");
// Enable "nightly" cfg if the current compiler is nightly.
if rustc_version::version_meta().unwrap().channel == rustc_version::Channel::Nightly {

View File

@ -43,6 +43,11 @@ typedef struct
visit_fde_t visit_fde;
} callbacks_t;
typedef struct
{
size_t data, vtable;
} box_any_t;
struct WasmException
{
public:
@ -61,7 +66,7 @@ struct UncatchableException : WasmException
struct UserException : UncatchableException
{
public:
UserException(size_t data, size_t vtable) : data(data), vtable(vtable) {}
UserException(size_t data, size_t vtable) : error_data({ data, vtable }) {}
virtual std::string description() const noexcept override
{
@ -69,7 +74,7 @@ struct UserException : UncatchableException
}
// The parts of a `Box<dyn Any>`.
size_t data, vtable;
box_any_t error_data;
};
struct WasmTrap : UncatchableException
@ -194,6 +199,7 @@ extern "C"
void *params,
void *results,
WasmTrap::Type *trap_out,
box_any_t *user_error,
void *invoke_env) throw()
{
try
@ -206,6 +212,11 @@ extern "C"
*trap_out = e.type;
return false;
}
catch (const UserException &e)
{
*user_error = e.error_data;
return false;
}
catch (const WasmException &e)
{
*trap_out = WasmTrap::Type::Unknown;

View File

@ -93,6 +93,7 @@ extern "C" {
params: *const u64,
results: *mut u64,
trap_out: *mut WasmTrapInfo,
user_error: *mut Option<Box<dyn Any>>,
invoke_env: Option<NonNull<c_void>>,
) -> bool;
}

View File

@ -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;
@ -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 {
@ -137,19 +136,14 @@ 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 } => {
let msg = if let Some(s) = data.downcast_ref::<String>() {
s
RuntimeError::Error { data } => {
if let Some(s) = data.downcast_ref::<String>() {
write!(f, "\"{}\"", s)
} else if let Some(s) = data.downcast_ref::<&str>() {
s
write!(f, "\"{}\"", s)
} else {
"user-defined, opaque"
};
write!(f, "{}", msg)
write!(f, "unknown error")
}
}
}
}

View File

@ -528,6 +528,7 @@ fn call_func_with_index(
let run_wasm = |result_space: *mut u64| unsafe {
let mut trap_info = WasmTrapInfo::Unknown;
let mut user_error = None;
let success = invoke(
trampoline,
@ -536,15 +537,20 @@ fn call_func_with_index(
raw_args.as_ptr(),
result_space,
&mut trap_info,
&mut user_error,
invoke_env,
);
if success {
Ok(())
} else {
Err(RuntimeError::Trap {
msg: trap_info.to_string().into(),
})
if let Some(data) = user_error {
Err(RuntimeError::Error { data })
} else {
Err(RuntimeError::Trap {
msg: trap_info.to_string().into(),
})
}
}
};

View File

@ -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,
@ -57,6 +58,7 @@ pub type Invoke = unsafe extern "C" fn(
*const u64,
*mut u64,
*mut WasmTrapInfo,
*mut Option<Box<dyn Any>>,
Option<NonNull<c_void>>,
) -> bool;
@ -103,7 +105,7 @@ pub trait WasmTypeList {
f: NonNull<vm::Func>,
wasm: Wasm,
ctx: *mut Ctx,
) -> Result<Rets, WasmTrapInfo>
) -> Result<Rets, RuntimeError>
where
Rets: WasmTypeList;
}
@ -120,14 +122,16 @@ 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)
}
}
@ -135,10 +139,11 @@ where
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
}
}
@ -209,26 +214,57 @@ where
}
}
impl WasmTypeList for Infallible {
type CStruct = Infallible;
type RetArray = [u64; 0];
fn from_ret_array(_: Self::RetArray) -> Self {
unreachable!()
}
fn empty_ret_array() -> Self::RetArray {
unreachable!()
}
fn from_c_struct(_: Self::CStruct) -> Self {
unreachable!()
}
fn into_c_struct(self) -> Self::CStruct {
unreachable!()
}
fn types() -> &'static [Type] {
&[]
}
#[allow(non_snake_case)]
unsafe fn call<Rets: WasmTypeList>(
self,
_: NonNull<vm::Func>,
_: Wasm,
_: *mut Ctx,
) -> Result<Rets, RuntimeError> {
unreachable!()
}
}
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>(
@ -236,14 +272,12 @@ impl<A: WasmExternType> WasmTypeList for (A,) {
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;
) -> Result<Rets, RuntimeError> {
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;
let mut user_error = None;
if (wasm.invoke)(
wasm.trampoline,
@ -252,11 +286,18 @@ impl<A: WasmExternType> WasmTypeList for (A,) {
args.as_ptr(),
rets.as_mut().as_mut_ptr(),
&mut trap,
&mut user_error,
wasm.invoke_env,
) {
Ok(Rets::from_ret_array(rets))
} else {
Err(trap)
if let Some(data) = user_error {
Err(RuntimeError::Error { data })
} else {
Err(RuntimeError::Trap {
msg: trap.to_string().into(),
})
}
}
}
}
@ -266,18 +307,14 @@ where
Rets: WasmTypeList,
{
pub fn call(&self, a: A) -> Result<Rets, RuntimeError> {
unsafe { <A as WasmTypeList>::call(a, self.f, self.inner, self.ctx) }.map_err(|e| {
RuntimeError::Trap {
msg: e.to_string().into(),
}
})
unsafe { <A as WasmTypeList>::call(a, self.f, self.inner, self.ctx) }
}
}
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 ),*>;
@ -285,7 +322,7 @@ macro_rules! impl_traits {
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 ),* )]
@ -293,38 +330,34 @@ macro_rules! impl_traits {
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;
unsafe fn call<Rets: WasmTypeList>(self, f: NonNull<vm::Func>, wasm: Wasm, ctx: *mut Ctx) -> Result<Rets, RuntimeError> {
#[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;
let mut user_error = None;
if (wasm.invoke)(wasm.trampoline, ctx, f, args.as_ptr(), rets.as_mut().as_mut_ptr(), &mut trap, wasm.invoke_env) {
if (wasm.invoke)(wasm.trampoline, ctx, f, args.as_ptr(), rets.as_mut().as_mut_ptr(), &mut trap, &mut user_error, wasm.invoke_env) {
Ok(Rets::from_ret_array(rets))
} else {
Err(trap)
if let Some(data) = user_error {
Err(RuntimeError::Error { data })
} else {
Err(RuntimeError::Trap { msg: trap.to_string().into() })
}
}
// 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)
}
}
@ -335,15 +368,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,
};
@ -363,11 +398,7 @@ macro_rules! impl_traits {
#[allow(non_snake_case)]
pub fn call(&self, $( $x: $x, )* ) -> Result<Rets, RuntimeError> {
#[allow(unused_parens)]
unsafe { <( $( $x ),* ) as WasmTypeList>::call(( $($x),* ), self.f, self.inner, self.ctx) }.map_err(|e| {
RuntimeError::Trap {
msg: e.to_string().into(),
}
})
unsafe { <( $( $x ),* ) as WasmTypeList>::call(( $($x),* ), self.f, self.inner, self.ctx) }
}
}
};

View File

@ -71,103 +71,149 @@ impl From<f64> for Value {
}
}
pub unsafe trait WasmExternType: Copy + Clone
pub unsafe trait NativeWasmType: Copy + Into<Value>
where
Self: Sized,
{
const TYPE: Type;
fn from_bits(bits: u64) -> Self;
fn to_bits(self) -> u64;
fn from_bits(n: u64) -> Self;
}
unsafe impl NativeWasmType for i32 {
const TYPE: Type = Type::I32;
fn from_bits(bits: u64) -> Self {
bits as _
}
fn to_bits(self) -> u64 {
self as _
}
}
unsafe impl NativeWasmType for i64 {
const TYPE: Type = Type::I64;
fn from_bits(bits: u64) -> Self {
bits as _
}
fn to_bits(self) -> u64 {
self as _
}
}
unsafe impl NativeWasmType for f32 {
const TYPE: Type = Type::F32;
fn from_bits(bits: u64) -> Self {
bits as _
}
fn to_bits(self) -> u64 {
self as _
}
}
unsafe impl NativeWasmType for f64 {
const TYPE: Type = Type::F64;
fn from_bits(bits: u64) -> Self {
bits as _
}
fn to_bits(self) -> u64 {
self as _
}
}
pub unsafe trait WasmExternType: Copy
where
Self: Sized,
{
type Native: NativeWasmType;
fn from_native(native: Self::Native) -> Self;
fn to_native(self) -> Self::Native;
}
unsafe impl WasmExternType for i8 {
const TYPE: Type = Type::I32;
fn to_bits(self) -> u64 {
self as u64
type Native = i32;
fn from_native(native: Self::Native) -> Self {
native as _
}
fn from_bits(n: u64) -> Self {
n as _
fn to_native(self) -> Self::Native {
self as _
}
}
unsafe impl WasmExternType for u8 {
const TYPE: Type = Type::I32;
fn to_bits(self) -> u64 {
self as u64
type Native = i32;
fn from_native(native: Self::Native) -> Self {
native as _
}
fn from_bits(n: u64) -> Self {
n as _
fn to_native(self) -> Self::Native {
self as _
}
}
unsafe impl WasmExternType for i16 {
const TYPE: Type = Type::I32;
fn to_bits(self) -> u64 {
self as u64
type Native = i32;
fn from_native(native: Self::Native) -> Self {
native as _
}
fn from_bits(n: u64) -> Self {
n as _
fn to_native(self) -> Self::Native {
self as _
}
}
unsafe impl WasmExternType for u16 {
const TYPE: Type = Type::I32;
fn to_bits(self) -> u64 {
self as u64
type Native = i32;
fn from_native(native: Self::Native) -> Self {
native as _
}
fn from_bits(n: u64) -> Self {
n as _
fn to_native(self) -> Self::Native {
self as _
}
}
unsafe impl WasmExternType for i32 {
const TYPE: Type = Type::I32;
fn to_bits(self) -> u64 {
self as u64
type Native = i32;
fn from_native(native: Self::Native) -> Self {
native
}
fn from_bits(n: u64) -> Self {
n as _
fn to_native(self) -> Self::Native {
self
}
}
unsafe impl WasmExternType for u32 {
const TYPE: Type = Type::I32;
fn to_bits(self) -> u64 {
self as u64
type Native = i32;
fn from_native(native: Self::Native) -> Self {
native as _
}
fn from_bits(n: u64) -> Self {
n as _
fn to_native(self) -> Self::Native {
self as _
}
}
unsafe impl WasmExternType for i64 {
const TYPE: Type = Type::I64;
fn to_bits(self) -> u64 {
self as u64
type Native = i64;
fn from_native(native: Self::Native) -> Self {
native
}
fn from_bits(n: u64) -> Self {
n as _
fn to_native(self) -> Self::Native {
self
}
}
unsafe impl WasmExternType for u64 {
const TYPE: Type = Type::I64;
fn to_bits(self) -> u64 {
self
type Native = i64;
fn from_native(native: Self::Native) -> Self {
native as _
}
fn from_bits(n: u64) -> Self {
n
fn to_native(self) -> Self::Native {
self as _
}
}
unsafe impl WasmExternType for f32 {
const TYPE: Type = Type::F32;
fn to_bits(self) -> u64 {
self.to_bits() as u64
type Native = f32;
fn from_native(native: Self::Native) -> Self {
native
}
fn from_bits(n: u64) -> Self {
f32::from_bits(n as u32)
fn to_native(self) -> Self::Native {
self
}
}
unsafe impl WasmExternType for f64 {
const TYPE: Type = Type::F64;
fn to_bits(self) -> u64 {
self.to_bits()
type Native = f64;
fn from_native(native: Self::Native) -> Self {
native
}
fn from_bits(n: u64) -> Self {
f64::from_bits(n)
fn to_native(self) -> Self::Native {
self
}
}

View File

@ -1,4 +1,4 @@
use wasmer_runtime::{compile, error, imports, Ctx, Func, Value};
use wasmer_runtime::{compile, error, error::RuntimeError, imports, Ctx, Func, Value};
use wabt::wat2wasm;
@ -7,6 +7,8 @@ static WAT: &'static str = r#"
(type (;0;) (func (result i32)))
(import "env" "do_panic" (func $do_panic (type 0)))
(func $dbz (result i32)
call $do_panic
drop
i32.const 42
i32.const 0
i32.div_u
@ -34,8 +36,13 @@ fn foobar(_ctx: &mut Ctx) -> i32 {
42
}
fn do_panic(_ctx: &mut Ctx) -> Result<i32, String> {
Err("error".to_string())
#[derive(Debug)]
struct ExitCode {
code: i32,
}
fn do_panic(_ctx: &mut Ctx) -> Result<i32, ExitCode> {
Err(ExitCode { code: 42 })
}
fn main() -> Result<(), error::Error> {
@ -63,5 +70,11 @@ fn main() -> Result<(), error::Error> {
println!("result: {:?}", result);
if let Err(RuntimeError::Error { data }) = result {
if let Ok(exit_code) = data.downcast::<ExitCode>() {
println!("exit code: {:?}", exit_code);
}
}
Ok(())
}

View File

@ -0,0 +1,49 @@
#[test]
fn error_propagation() {
use std::convert::Infallible;
use wabt::wat2wasm;
use wasmer_runtime::{compile, error::RuntimeError, imports, Ctx, Func};
static WAT: &'static str = r#"
(module
(type (;0;) (func))
(import "env" "ret_err" (func $ret_err (type 0)))
(func $call_panic
call $ret_err
)
(export "call_err" (func $call_panic))
)
"#;
#[derive(Debug)]
struct ExitCode {
code: i32,
}
fn ret_err(_ctx: &mut Ctx) -> Result<Infallible, ExitCode> {
Err(ExitCode { code: 42 })
}
let wasm = wat2wasm(WAT).unwrap();
let module = compile(&wasm).unwrap();
let instance = module
.instantiate(&imports! {
"env" => {
"ret_err" => Func::new(ret_err),
},
})
.unwrap();
let foo: Func<(), ()> = instance.func("call_err").unwrap();
let result = foo.call();
if let Err(RuntimeError::Error { data }) = result {
let exit_code = data.downcast::<ExitCode>().unwrap();
assert_eq!(exit_code.code, 42);
} else {
panic!("didn't return RuntimeError::Error")
}
}

View File

@ -205,7 +205,8 @@ impl RunnableModule for X64ExecutionContext {
func: NonNull<vm::Func>,
args: *const u64,
rets: *mut u64,
_trap_info: *mut WasmTrapInfo,
trap_info: *mut WasmTrapInfo,
user_error: *mut Option<Box<dyn Any>>,
num_params_plus_one: Option<NonNull<c_void>>,
) -> bool {
let args = ::std::slice::from_raw_parts(
@ -227,7 +228,13 @@ impl RunnableModule for X64ExecutionContext {
}
true
}
Err(_) => false,
Err(err) => {
match err {
protect_unix::CallProtError::Trap(info) => *trap_info = info,
protect_unix::CallProtError::Error(data) => *user_error = Some(data),
}
false
}
}
}

View File

@ -17,7 +17,7 @@ use std::any::Any;
use std::cell::{Cell, UnsafeCell};
use std::ptr;
use std::sync::Once;
use wasmer_runtime_core::error::{RuntimeError, RuntimeResult};
use wasmer_runtime_core::typed_func::WasmTrapInfo;
extern "C" fn signal_trap_handler(
signum: ::nix::libc::c_int,
@ -62,7 +62,12 @@ pub unsafe fn trigger_trap() -> ! {
longjmp(jmp_buf as *mut c_void, 0)
}
pub fn call_protected<T>(f: impl FnOnce() -> T) -> RuntimeResult<T> {
pub enum CallProtError {
Trap(WasmTrapInfo),
Error(Box<dyn Any>),
}
pub fn call_protected<T>(f: impl FnOnce() -> T) -> Result<T, CallProtError> {
unsafe {
let jmp_buf = SETJMP_BUFFER.with(|buf| buf.get());
let prev_jmp_buf = *jmp_buf;
@ -76,23 +81,24 @@ pub fn call_protected<T>(f: impl FnOnce() -> T) -> RuntimeResult<T> {
*jmp_buf = prev_jmp_buf;
if let Some(data) = TRAP_EARLY_DATA.with(|cell| cell.replace(None)) {
Err(RuntimeError::Panic { data })
Err(CallProtError::Error(data))
} else {
let (faulting_addr, _inst_ptr) = CAUGHT_ADDRESSES.with(|cell| cell.get());
// let (faulting_addr, _inst_ptr) = CAUGHT_ADDRESSES.with(|cell| cell.get());
let signal = match Signal::from_c_int(signum) {
Ok(SIGFPE) => "floating-point exception",
Ok(SIGILL) => "illegal instruction",
Ok(SIGSEGV) => "segmentation violation",
Ok(SIGBUS) => "bus error",
Err(_) => "error while getting the Signal",
_ => "unkown trapped signal",
};
// When the trap-handler is fully implemented, this will return more information.
Err(RuntimeError::Trap {
msg: format!("unknown trap at {:p} - {}", faulting_addr, signal).into(),
}
.into())
// let signal = match Signal::from_c_int(signum) {
// Ok(SIGFPE) => "floating-point exception",
// Ok(SIGILL) => "illegal instruction",
// Ok(SIGSEGV) => "segmentation violation",
// Ok(SIGBUS) => "bus error",
// Err(_) => "error while getting the Signal",
// _ => "unkown trapped signal",
// };
// // When the trap-handler is fully implemented, this will return more information.
// Err(RuntimeError::Trap {
// msg: format!("unknown trap at {:p} - {}", faulting_addr, signal).into(),
// }
// .into())
Err(CallProtError::Trap(WasmTrapInfo::Unknown))
}
} else {
let ret = f(); // TODO: Switch stack?

View File

@ -17,6 +17,12 @@ pub use self::utils::is_wasi_module;
use wasmer_runtime_core::{func, import::ImportObject, imports};
/// This is returned in the Box<dyn Any> RuntimeError::Error variant.
/// Use `downcast` or `downcast_ref` to retrieve the `ExitCode`.
pub struct ExitCode {
pub code: syscalls::types::__wasi_exitcode_t,
}
pub fn generate_import_object(
args: Vec<Vec<u8>>,
envs: Vec<Vec<u8>>,

View File

@ -2,8 +2,14 @@ macro_rules! wasi_try {
($expr:expr) => {{
let res: Result<_, crate::syscalls::types::__wasi_errno_t> = $expr;
match res {
Ok(val) => val,
Err(err) => return err,
Ok(val) => {
debug!("wasi::wasi_try::val: {:?}", val);
val
}
Err(err) => {
debug!("wasi::wasi_try::err: {:?}", err);
return err;
}
}
}};
($expr:expr; $e:expr) => {{

View File

@ -2,7 +2,7 @@ use crate::syscalls::types::{__wasi_errno_t, __WASI_EFAULT};
use std::{cell::Cell, fmt, marker::PhantomData, mem};
use wasmer_runtime_core::{
memory::Memory,
types::{Type, ValueType, WasmExternType},
types::{ValueType, WasmExternType},
};
pub struct Array;
@ -73,12 +73,12 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
}
unsafe impl<T: Copy, Ty> WasmExternType for WasmPtr<T, Ty> {
const TYPE: Type = Type::I32;
type Native = i32;
fn to_bits(self) -> u64 {
self.offset as u64
fn to_native(self) -> Self::Native {
self.offset as i32
}
fn from_bits(n: u64) -> Self {
fn from_native(n: Self::Native) -> Self {
Self {
offset: n as u32,
_phantom: PhantomData,

View File

@ -171,6 +171,7 @@ pub struct Fd {
pub inode: Inode,
}
#[derive(Debug)]
pub struct WasiFs {
//pub repo: Repo,
pub name_map: HashMap<String, Inode>,
@ -430,6 +431,7 @@ impl WasiFs {
}
}
#[derive(Debug)]
pub struct WasiState<'a> {
pub fs: WasiFs,
pub args: &'a [Vec<u8>],

View File

@ -9,9 +9,11 @@ use self::types::*;
use crate::{
ptr::{Array, WasmPtr},
state::{Fd, InodeVal, Kind, WasiFile, WasiState, MAX_SYMLINKS},
ExitCode,
};
use rand::{thread_rng, Rng};
use std::cell::Cell;
use std::convert::Infallible;
use std::io::{self, Read, Seek, Write};
use wasmer_runtime_core::{debug, memory::Memory, vm::Ctx};
@ -1430,9 +1432,9 @@ pub fn poll_oneoff(
debug!("wasi::poll_oneoff");
unimplemented!()
}
pub fn proc_exit(ctx: &mut Ctx, rval: __wasi_exitcode_t) -> Result<(), &'static str> {
debug!("wasi::proc_exit, {}", rval);
Err("Instance exited")
pub fn proc_exit(ctx: &mut Ctx, code: __wasi_exitcode_t) -> Result<Infallible, ExitCode> {
debug!("wasi::proc_exit, {}", code);
Err(ExitCode { code })
}
pub fn proc_raise(ctx: &mut Ctx, sig: __wasi_signal_t) -> __wasi_errno_t {
debug!("wasi::proc_raise");

View File

@ -2,6 +2,7 @@
use crate::ptr::{Array, WasmPtr};
use byteorder::{ReadBytesExt, WriteBytesExt, LE};
use std::fmt;
use std::mem;
use wasmer_runtime_core::types::ValueType;
@ -42,84 +43,84 @@ pub struct __wasi_dirent_t {
pub d_type: __wasi_filetype_t,
}
pub type __wasi_errno_t = u32;
pub const __WASI_ESUCCESS: u32 = 0;
pub const __WASI_E2BIG: u32 = 1;
pub const __WASI_EACCES: u32 = 2;
pub const __WASI_EADDRINUSE: u32 = 3;
pub const __WASI_EADDRNOTAVAIL: u32 = 4;
pub const __WASI_EAFNOSUPPORT: u32 = 5;
pub const __WASI_EAGAIN: u32 = 6;
pub const __WASI_EALREADY: u32 = 7;
pub const __WASI_EBADF: u32 = 8;
pub const __WASI_EBADMSG: u32 = 9;
pub const __WASI_EBUSY: u32 = 10;
pub const __WASI_ECANCELED: u32 = 11;
pub const __WASI_ECHILD: u32 = 12;
pub const __WASI_ECONNABORTED: u32 = 13;
pub const __WASI_ECONNREFUSED: u32 = 14;
pub const __WASI_ECONNRESET: u32 = 15;
pub const __WASI_EDEADLK: u32 = 16;
pub const __WASI_EDESTADDRREQ: u32 = 17;
pub const __WASI_EDOM: u32 = 18;
pub const __WASI_EDQUOT: u32 = 19;
pub const __WASI_EEXIST: u32 = 20;
pub const __WASI_EFAULT: u32 = 21;
pub const __WASI_EFBIG: u32 = 22;
pub const __WASI_EHOSTUNREACH: u32 = 23;
pub const __WASI_EIDRM: u32 = 24;
pub const __WASI_EILSEQ: u32 = 25;
pub const __WASI_EINPROGRESS: u32 = 26;
pub const __WASI_EINTR: u32 = 27;
pub const __WASI_EINVAL: u32 = 28;
pub const __WASI_EIO: u32 = 29;
pub const __WASI_EISCONN: u32 = 30;
pub const __WASI_EISDIR: u32 = 31;
pub const __WASI_ELOOP: u32 = 32;
pub const __WASI_EMFILE: u32 = 33;
pub const __WASI_EMLINK: u32 = 34;
pub const __WASI_EMSGSIZE: u32 = 35;
pub const __WASI_EMULTIHOP: u32 = 36;
pub const __WASI_ENAMETOOLONG: u32 = 37;
pub const __WASI_ENETDOWN: u32 = 38;
pub const __WASI_ENETRESET: u32 = 39;
pub const __WASI_ENETUNREACH: u32 = 40;
pub const __WASI_ENFILE: u32 = 41;
pub const __WASI_ENOBUFS: u32 = 42;
pub const __WASI_ENODEV: u32 = 43;
pub const __WASI_ENOENT: u32 = 44;
pub const __WASI_ENOEXEC: u32 = 45;
pub const __WASI_ENOLCK: u32 = 46;
pub const __WASI_ENOLINK: u32 = 47;
pub const __WASI_ENOMEM: u32 = 48;
pub const __WASI_ENOMSG: u32 = 49;
pub const __WASI_ENOPROTOOPT: u32 = 50;
pub const __WASI_ENOSPC: u32 = 51;
pub const __WASI_ENOSYS: u32 = 52;
pub const __WASI_ENOTCONN: u32 = 53;
pub const __WASI_ENOTDIR: u32 = 54;
pub const __WASI_ENOTEMPTY: u32 = 55;
pub const __WASI_ENOTRECOVERABLE: u32 = 56;
pub const __WASI_ENOTSOCK: u32 = 57;
pub const __WASI_ENOTSUP: u32 = 58;
pub const __WASI_ENOTTY: u32 = 59;
pub const __WASI_ENXIO: u32 = 60;
pub const __WASI_EOVERFLOW: u32 = 61;
pub const __WASI_EOWNERDEAD: u32 = 62;
pub const __WASI_EPERM: u32 = 63;
pub const __WASI_EPIPE: u32 = 64;
pub const __WASI_EPROTO: u32 = 65;
pub const __WASI_EPROTONOSUPPORT: u32 = 66;
pub const __WASI_EPROTOTYPE: u32 = 67;
pub const __WASI_ERANGE: u32 = 68;
pub const __WASI_EROFS: u32 = 69;
pub const __WASI_ESPIPE: u32 = 70;
pub const __WASI_ESRCH: u32 = 71;
pub const __WASI_ESTALE: u32 = 72;
pub const __WASI_ETIMEDOUT: u32 = 73;
pub const __WASI_ETXTBSY: u32 = 74;
pub const __WASI_EXDEV: u32 = 75;
pub const __WASI_ENOTCAPABLE: u32 = 76;
pub type __wasi_errno_t = u16;
pub const __WASI_ESUCCESS: u16 = 0;
pub const __WASI_E2BIG: u16 = 1;
pub const __WASI_EACCES: u16 = 2;
pub const __WASI_EADDRINUSE: u16 = 3;
pub const __WASI_EADDRNOTAVAIL: u16 = 4;
pub const __WASI_EAFNOSUPPORT: u16 = 5;
pub const __WASI_EAGAIN: u16 = 6;
pub const __WASI_EALREADY: u16 = 7;
pub const __WASI_EBADF: u16 = 8;
pub const __WASI_EBADMSG: u16 = 9;
pub const __WASI_EBUSY: u16 = 10;
pub const __WASI_ECANCELED: u16 = 11;
pub const __WASI_ECHILD: u16 = 12;
pub const __WASI_ECONNABORTED: u16 = 13;
pub const __WASI_ECONNREFUSED: u16 = 14;
pub const __WASI_ECONNRESET: u16 = 15;
pub const __WASI_EDEADLK: u16 = 16;
pub const __WASI_EDESTADDRREQ: u16 = 17;
pub const __WASI_EDOM: u16 = 18;
pub const __WASI_EDQUOT: u16 = 19;
pub const __WASI_EEXIST: u16 = 20;
pub const __WASI_EFAULT: u16 = 21;
pub const __WASI_EFBIG: u16 = 22;
pub const __WASI_EHOSTUNREACH: u16 = 23;
pub const __WASI_EIDRM: u16 = 24;
pub const __WASI_EILSEQ: u16 = 25;
pub const __WASI_EINPROGRESS: u16 = 26;
pub const __WASI_EINTR: u16 = 27;
pub const __WASI_EINVAL: u16 = 28;
pub const __WASI_EIO: u16 = 29;
pub const __WASI_EISCONN: u16 = 30;
pub const __WASI_EISDIR: u16 = 31;
pub const __WASI_ELOOP: u16 = 32;
pub const __WASI_EMFILE: u16 = 33;
pub const __WASI_EMLINK: u16 = 34;
pub const __WASI_EMSGSIZE: u16 = 35;
pub const __WASI_EMULTIHOP: u16 = 36;
pub const __WASI_ENAMETOOLONG: u16 = 37;
pub const __WASI_ENETDOWN: u16 = 38;
pub const __WASI_ENETRESET: u16 = 39;
pub const __WASI_ENETUNREACH: u16 = 40;
pub const __WASI_ENFILE: u16 = 41;
pub const __WASI_ENOBUFS: u16 = 42;
pub const __WASI_ENODEV: u16 = 43;
pub const __WASI_ENOENT: u16 = 44;
pub const __WASI_ENOEXEC: u16 = 45;
pub const __WASI_ENOLCK: u16 = 46;
pub const __WASI_ENOLINK: u16 = 47;
pub const __WASI_ENOMEM: u16 = 48;
pub const __WASI_ENOMSG: u16 = 49;
pub const __WASI_ENOPROTOOPT: u16 = 50;
pub const __WASI_ENOSPC: u16 = 51;
pub const __WASI_ENOSYS: u16 = 52;
pub const __WASI_ENOTCONN: u16 = 53;
pub const __WASI_ENOTDIR: u16 = 54;
pub const __WASI_ENOTEMPTY: u16 = 55;
pub const __WASI_ENOTRECOVERABLE: u16 = 56;
pub const __WASI_ENOTSOCK: u16 = 57;
pub const __WASI_ENOTSUP: u16 = 58;
pub const __WASI_ENOTTY: u16 = 59;
pub const __WASI_ENXIO: u16 = 60;
pub const __WASI_EOVERFLOW: u16 = 61;
pub const __WASI_EOWNERDEAD: u16 = 62;
pub const __WASI_EPERM: u16 = 63;
pub const __WASI_EPIPE: u16 = 64;
pub const __WASI_EPROTO: u16 = 65;
pub const __WASI_EPROTONOSUPPORT: u16 = 66;
pub const __WASI_EPROTOTYPE: u16 = 67;
pub const __WASI_ERANGE: u16 = 68;
pub const __WASI_EROFS: u16 = 69;
pub const __WASI_ESPIPE: u16 = 70;
pub const __WASI_ESRCH: u16 = 71;
pub const __WASI_ESTALE: u16 = 72;
pub const __WASI_ETIMEDOUT: u16 = 73;
pub const __WASI_ETXTBSY: u16 = 74;
pub const __WASI_EXDEV: u16 = 75;
pub const __WASI_ENOTCAPABLE: u16 = 76;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(C)]
@ -173,8 +174,8 @@ impl __wasi_event_t {
}
}
pub type __wasi_eventrwflags_t = u32;
pub const __WASI_EVENT_FD_READWRITE_HANGUP: u32 = 1 << 0;
pub type __wasi_eventrwflags_t = u16;
pub const __WASI_EVENT_FD_READWRITE_HANGUP: u16 = 1 << 0;
pub type __wasi_eventtype_t = u8;
pub const __WASI_EVENTTYPE_CLOCK: u8 = 0;
@ -188,12 +189,12 @@ pub const __WASI_STDIN_FILENO: u32 = 0;
pub const __WASI_STDOUT_FILENO: u32 = 1;
pub const __WASI_STDERR_FILENO: u32 = 2;
pub type __wasi_fdflags_t = u32;
pub const __WASI_FDFLAG_APPEND: u32 = 1 << 0;
pub const __WASI_FDFLAG_DSYNC: u32 = 1 << 1;
pub const __WASI_FDFLAG_NONBLOCK: u32 = 1 << 2;
pub const __WASI_FDFLAG_RSYNC: u32 = 1 << 3;
pub const __WASI_FDFLAG_SYNC: u32 = 1 << 4;
pub type __wasi_fdflags_t = u16;
pub const __WASI_FDFLAG_APPEND: u16 = 1 << 0;
pub const __WASI_FDFLAG_DSYNC: u16 = 1 << 1;
pub const __WASI_FDFLAG_NONBLOCK: u16 = 1 << 2;
pub const __WASI_FDFLAG_RSYNC: u16 = 1 << 3;
pub const __WASI_FDFLAG_SYNC: u16 = 1 << 4;
pub type __wasi_preopentype_t = u8;
pub const __WASI_PREOPENTYPE_DIR: u8 = 0;
@ -212,9 +213,15 @@ pub union __wasi_prestat_u {
dir: __wasi_prestat_u_dir_t,
}
impl fmt::Debug for __wasi_prestat_u {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "__wasi_prestat_u")
}
}
unsafe impl ValueType for __wasi_prestat_u {}
#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct __wasi_prestat_t {
pub pr_type: __wasi_preopentype_t,
@ -289,11 +296,11 @@ pub const __WASI_FILETYPE_SOCKET_DGRAM: u8 = 5;
pub const __WASI_FILETYPE_SOCKET_STREAM: u8 = 6;
pub const __WASI_FILETYPE_SYMBOLIC_LINK: u8 = 7;
pub type __wasi_fstflags_t = u32;
pub const __WASI_FILESTAT_SET_ATIM: u32 = 1 << 0;
pub const __WASI_FILESTAT_SET_ATIM_NOW: u32 = 1 << 1;
pub const __WASI_FILESTAT_SET_MTIM: u32 = 1 << 2;
pub const __WASI_FILESTAT_SET_MTIM_NOW: u32 = 1 << 3;
pub type __wasi_fstflags_t = u16;
pub const __WASI_FILESTAT_SET_ATIM: u16 = 1 << 0;
pub const __WASI_FILESTAT_SET_ATIM_NOW: u16 = 1 << 1;
pub const __WASI_FILESTAT_SET_MTIM: u16 = 1 << 2;
pub const __WASI_FILESTAT_SET_MTIM_NOW: u16 = 1 << 3;
pub type __wasi_inode_t = u64;
@ -311,15 +318,15 @@ pub type __wasi_linkcount_t = u32;
pub type __wasi_lookupflags_t = u32;
pub const __WASI_LOOKUP_SYMLINK_FOLLOW: u32 = 1 << 0;
pub type __wasi_oflags_t = u32;
pub const __WASI_O_CREAT: u32 = 1 << 0;
pub const __WASI_O_DIRECTORY: u32 = 1 << 1;
pub const __WASI_O_EXCL: u32 = 1 << 2;
pub const __WASI_O_TRUNC: u32 = 1 << 3;
pub type __wasi_oflags_t = u16;
pub const __WASI_O_CREAT: u16 = 1 << 0;
pub const __WASI_O_DIRECTORY: u16 = 1 << 1;
pub const __WASI_O_EXCL: u16 = 1 << 2;
pub const __WASI_O_TRUNC: u16 = 1 << 3;
pub type __wasi_riflags_t = u32;
pub const __WASI_SOCK_RECV_PEEK: u32 = 1 << 0;
pub const __WASI_SOCK_RECV_WAITALL: u32 = 1 << 1;
pub type __wasi_riflags_t = u16;
pub const __WASI_SOCK_RECV_PEEK: u16 = 1 << 0;
pub const __WASI_SOCK_RECV_WAITALL: u16 = 1 << 1;
pub type __wasi_rights_t = u64;
pub const __WASI_RIGHT_FD_DATASYNC: u64 = 1 << 0;
@ -352,14 +359,14 @@ pub const __WASI_RIGHT_PATH_REMOVE_DIRECTORY: u64 = 1 << 26;
pub const __WASI_RIGHT_POLL_FD_READWRITE: u64 = 1 << 27;
pub const __WASI_RIGHT_SOCK_SHUTDOWN: u64 = 1 << 28;
pub type __wasi_roflags_t = u32;
pub const __WASI_SOCK_RECV_DATA_TRUNCATED: u32 = 1 << 0;
pub type __wasi_roflags_t = u16;
pub const __WASI_SOCK_RECV_DATA_TRUNCATED: u16 = 1 << 0;
pub type __wasi_sdflags_t = u8;
pub const __WASI_SHUT_RD: u8 = 1 << 0;
pub const __WASI_SHUT_WR: u8 = 1 << 1;
pub type __wasi_siflags_t = u32;
pub type __wasi_siflags_t = u16;
pub type __wasi_signal_t = u8;
pub const __WASI_SIGABRT: u8 = 0;
@ -389,8 +396,8 @@ pub const __WASI_SIGVTALRM: u8 = 23;
pub const __WASI_SIGXCPU: u8 = 24;
pub const __WASI_SIGXFSZ: u8 = 25;
pub type __wasi_subclockflags_t = u32;
pub const __WASI_SUBSCRIPTION_CLOCK_ABSTIME: u32 = 1 << 0;
pub type __wasi_subclockflags_t = u16;
pub const __WASI_SUBSCRIPTION_CLOCK_ABSTIME: u16 = 1 << 0;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(C)]

View File

@ -11,12 +11,15 @@ use std::str::FromStr;
use hashbrown::HashMap;
use structopt::StructOpt;
use wasmer::webassembly::InstanceABI;
use wasmer::*;
use wasmer_clif_backend::CraneliftCompiler;
#[cfg(feature = "backend:llvm")]
use wasmer_llvm_backend::LLVMCompiler;
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH};
use wasmer_runtime::{
cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH},
error::RuntimeError,
Func, Value,
};
use wasmer_runtime_core::{
self,
backend::{Compiler, CompilerConfig},
@ -105,7 +108,13 @@ enum Backend {
impl Backend {
pub fn variants() -> &'static [&'static str] {
&["singlepass", "cranelift", "llvm"]
&[
"cranelift",
#[cfg(feature = "backend:singlepass")]
"singlepass",
#[cfg(feature = "backend:llvm")]
"llvm",
]
}
}
@ -295,60 +304,81 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
};
// TODO: refactor this
let (abi, import_object, _em_globals) = if wasmer_emscripten::is_emscripten_module(&module) {
if wasmer_emscripten::is_emscripten_module(&module) {
let mut emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(&module);
(
InstanceABI::Emscripten,
wasmer_emscripten::generate_emscripten_env(&mut emscripten_globals),
Some(emscripten_globals), // TODO Em Globals is here to extend, lifetime, find better solution
let import_object = wasmer_emscripten::generate_emscripten_env(&mut emscripten_globals);
let mut instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
wasmer_emscripten::run_emscripten_instance(
&module,
&mut instance,
if let Some(cn) = &options.command_name {
cn
} else {
options.path.to_str().unwrap()
},
options.args.iter().map(|arg| arg.as_str()).collect(),
)
.map_err(|e| format!("{:?}", e))?;
} else {
if cfg!(feature = "wasi") && wasmer_wasi::is_wasi_module(&module) {
(
InstanceABI::WASI,
wasmer_wasi::generate_import_object(
if let Some(cn) = &options.command_name {
[cn.clone()]
} else {
[options.path.to_str().unwrap().to_owned()]
}
.iter()
.chain(options.args.iter())
.cloned()
.map(|arg| arg.into_bytes())
let import_object = wasmer_wasi::generate_import_object(
if let Some(cn) = &options.command_name {
[cn.clone()]
} else {
[options.path.to_str().unwrap().to_owned()]
}
.iter()
.chain(options.args.iter())
.cloned()
.map(|arg| arg.into_bytes())
.collect(),
env::vars()
.map(|(k, v)| format!("{}={}", k, v).into_bytes())
.collect(),
env::vars()
.map(|(k, v)| format!("{}={}", k, v).into_bytes())
.collect(),
options.pre_opened_directories.clone(),
),
None,
)
options.pre_opened_directories.clone(),
);
let instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
let start: Func<(), ()> = instance.func("_start").map_err(|e| format!("{:?}", e))?;
let result = start.call();
if let Err(ref err) = result {
match err {
RuntimeError::Trap { msg } => panic!("wasm trap occured: {}", msg),
RuntimeError::Error { data } => {
if let Some(error_code) = data.downcast_ref::<wasmer_wasi::ExitCode>() {
std::process::exit(error_code.code as i32)
}
}
}
panic!("error: {:?}", err)
}
} else {
(
InstanceABI::None,
wasmer_runtime_core::import::ImportObject::new(),
None,
)
let import_object = wasmer_runtime_core::import::ImportObject::new();
let instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
let args: Vec<Value> = options
.args
.iter()
.map(|arg| arg.as_str())
.map(|x| Value::I32(x.parse().unwrap()))
.collect();
instance
.dyn_func("main")
.map_err(|e| format!("{:?}", e))?
.call(&args)
.map_err(|e| format!("{:?}", e))?;
}
};
let mut instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
webassembly::run_instance(
&module,
&mut instance,
abi,
if let Some(cn) = &options.command_name {
cn
} else {
options.path.to_str().unwrap()
},
options.args.iter().map(|arg| arg.as_str()).collect(),
)
.map_err(|e| format!("{:?}", e))?;
}
Ok(())
}

View File

@ -77,40 +77,3 @@ pub fn compile(buffer_source: &[u8]) -> Result<Module> {
let module = runtime::compile(buffer_source)?;
Ok(module)
}
// /// The same as `compile` but takes a `CompilerConfig` for the purpose of
// /// changing the compiler's behavior
// pub fn compile_with_config_with(
// buffer_source: &[u8],
// compiler_config: CompilerConfig,
// ) -> Result<Module> {
// let module = runtime::compile_with_config(buffer_source, compiler_config)?;
// Ok(module)
// }
/// Performs common instance operations needed when an instance is first run
/// including data setup, handling arguments and calling a main function
pub fn run_instance(
module: &Module,
instance: &mut Instance,
abi: InstanceABI,
path: &str,
args: Vec<&str>,
) -> CallResult<()> {
match abi {
InstanceABI::Emscripten => {
run_emscripten_instance(module, instance, path, args)?;
}
InstanceABI::WASI => {
instance.call("_start", &[])?;
}
InstanceABI::None => {
let args: Vec<Value> = args
.into_iter()
.map(|x| Value::I32(x.parse().unwrap()))
.collect();
instance.call("main", &args)?;
}
}
Ok(())
}