Merge remote-tracking branch 'origin/master' into feature/make-instance-send

This commit is contained in:
Mark McCaskey
2019-09-23 11:05:00 -07:00
31 changed files with 364 additions and 174 deletions

View File

@ -18,7 +18,6 @@ libc = "0.2.60"
hex = "0.3.2"
smallvec = "0.6.10"
bincode = "1.1"
colored = "1.8"
[dependencies.indexmap]
version = "1.2.0"

View File

@ -283,13 +283,10 @@ extern "C" fn signal_trap_handler(
let image = build_instance_image(ctx, es_image);
unwind_result = Box::new(image);
} else {
use colored::*;
if es_image.frames.len() > 0 {
eprintln!(
"\n{}",
"Wasmer encountered an error while running your WebAssembly program."
.bold()
.red()
);
es_image.print_backtrace_if_needed();
}

View File

@ -530,29 +530,12 @@ fn call_func_with_index(
args: &[Value],
rets: &mut Vec<Value>,
) -> CallResult<()> {
rets.clear();
let sig_index = *info
.func_assoc
.get(func_index)
.expect("broken invariant, incorrect func index");
let signature = &info.signatures[sig_index];
let num_results = signature.returns().len();
let num_results = num_results
+ signature
.returns()
.iter()
.filter(|&&ty| ty == Type::V128)
.count();
rets.reserve(num_results);
if !signature.check_param_value_types(args) {
Err(ResolveError::Signature {
expected: signature.clone(),
found: args.iter().map(|val| val.ty()).collect(),
})?
}
let func_ptr = match func_index.local_or_import(info) {
LocalOrImport::Local(local_func_index) => {
@ -570,6 +553,39 @@ fn call_func_with_index(
}
};
let wasm = runnable
.get_trampoline(info, sig_index)
.expect("wasm trampoline");
call_func_with_index_inner(ctx_ptr, func_ptr, signature, wasm, args, rets)
}
pub(crate) fn call_func_with_index_inner(
ctx_ptr: *mut vm::Ctx,
func_ptr: NonNull<vm::Func>,
signature: &FuncSig,
wasm: Wasm,
args: &[Value],
rets: &mut Vec<Value>,
) -> CallResult<()> {
rets.clear();
let num_results = signature.returns().len();
let num_results = num_results
+ signature
.returns()
.iter()
.filter(|&&ty| ty == Type::V128)
.count();
rets.reserve(num_results);
if !signature.check_param_value_types(args) {
Err(ResolveError::Signature {
expected: signature.clone(),
found: args.iter().map(|val| val.ty()).collect(),
})?
}
let mut raw_args: SmallVec<[u64; 8]> = SmallVec::new();
for v in args {
match v {
@ -601,9 +617,7 @@ fn call_func_with_index(
trampoline,
invoke,
invoke_env,
} = runnable
.get_trampoline(info, sig_index)
.expect("wasm trampoline");
} = wasm;
let run_wasm = |result_space: *mut u64| unsafe {
let mut trap_info = WasmTrapInfo::Unknown;

View File

@ -46,9 +46,7 @@ fn align_pointer(ptr: usize, align: usize) -> usize {
impl<T: Copy + ValueType> WasmPtr<T, Item> {
#[inline]
pub fn deref<'a>(self, memory: &'a Memory) -> Option<&'a Cell<T>> {
if self.offset == 0
|| (self.offset as usize) + mem::size_of::<T>() >= memory.size().bytes().0
{
if (self.offset as usize) + mem::size_of::<T>() >= memory.size().bytes().0 {
return None;
}
unsafe {
@ -62,9 +60,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Item> {
#[inline]
pub unsafe fn deref_mut<'a>(self, memory: &'a Memory) -> Option<&'a mut Cell<T>> {
if self.offset == 0
|| (self.offset as usize) + mem::size_of::<T>() >= memory.size().bytes().0
{
if (self.offset as usize) + mem::size_of::<T>() >= memory.size().bytes().0 {
return None;
}
let cell_ptr = align_pointer(
@ -83,9 +79,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
let item_size = mem::size_of::<T>() + (mem::size_of::<T>() % mem::align_of::<T>());
let slice_full_len = index as usize + length as usize;
if self.offset == 0
|| (self.offset as usize) + (item_size * slice_full_len) >= memory.size().bytes().0
{
if (self.offset as usize) + (item_size * slice_full_len) >= memory.size().bytes().0 {
return None;
}
@ -112,9 +106,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
let item_size = mem::size_of::<T>() + (mem::size_of::<T>() % mem::align_of::<T>());
let slice_full_len = index as usize + length as usize;
if self.offset == 0
|| (self.offset as usize) + (item_size * slice_full_len) >= memory.size().bytes().0
{
if (self.offset as usize) + (item_size * slice_full_len) >= memory.size().bytes().0 {
return None;
}

View File

@ -392,14 +392,19 @@ pub fn read_module<
}
pub fn wp_type_to_type(ty: WpType) -> Result<Type, BinaryReaderError> {
Ok(match ty {
WpType::I32 => Type::I32,
WpType::I64 => Type::I64,
WpType::F32 => Type::F32,
WpType::F64 => Type::F64,
WpType::V128 => Type::V128,
_ => panic!("broken invariant, invalid type"),
})
match ty {
WpType::I32 => Ok(Type::I32),
WpType::I64 => Ok(Type::I64),
WpType::F32 => Ok(Type::F32),
WpType::F64 => Ok(Type::F64),
WpType::V128 => Ok(Type::V128),
_ => {
return Err(BinaryReaderError {
message: "broken invariant, invalid type",
offset: -1isize as usize,
});
}
}
}
pub fn type_to_wp_type(ty: Type) -> WpType {

View File

@ -303,7 +303,7 @@ impl ExecutionStateImage {
if let Ok(x) = env::var("WASMER_BACKTRACE") {
if x == "1" {
eprintln!("{}", self.colored_output());
eprintln!("{}", self.output());
return;
}
}
@ -311,9 +311,7 @@ impl ExecutionStateImage {
eprintln!("Run with `WASMER_BACKTRACE=1` environment variable to display a backtrace.");
}
pub fn colored_output(&self) -> String {
use colored::*;
pub fn output(&self) -> String {
fn join_strings(x: impl Iterator<Item = String>, sep: &str) -> String {
let mut ret = String::new();
let mut first = true;
@ -341,8 +339,6 @@ impl ExecutionStateImage {
i,
x.map(|x| format!("{}", x))
.unwrap_or_else(|| "?".to_string())
.bold()
.cyan()
)
}),
", ",
@ -353,27 +349,23 @@ impl ExecutionStateImage {
let mut ret = String::new();
if self.frames.len() == 0 {
ret += &"Unknown fault address, cannot read stack.".yellow();
ret += &"Unknown fault address, cannot read stack.";
ret += "\n";
} else {
ret += &"Backtrace:".bold();
ret += &"Backtrace:";
ret += "\n";
for (i, f) in self.frames.iter().enumerate() {
ret += &format!("* Frame {} @ Local function {}", i, f.local_function_id).bold();
ret += &format!("* Frame {} @ Local function {}", i, f.local_function_id);
ret += "\n";
ret += &format!(" {} {}\n", "Offset:", format!("{}", f.wasm_inst_offset),);
ret += &format!(
" {} {}\n",
"Offset:".bold().yellow(),
format!("{}", f.wasm_inst_offset).bold().cyan(),
);
ret += &format!(
" {} {}\n",
"Locals:".bold().yellow(),
"Locals:",
format_optional_u64_sequence(&f.locals)
);
ret += &format!(
" {} {}\n\n",
"Stack:".bold().yellow(),
"Stack:",
format_optional_u64_sequence(&f.stack)
);
}

View File

@ -44,7 +44,7 @@ impl<'a> From<DynFunc<'a>> for Anyfunc<'a> {
}
pub struct AnyfuncTable {
backing: Vec<vm::Anyfunc>,
pub(crate) backing: Vec<vm::Anyfunc>,
max: Option<u32>,
}

View File

@ -13,7 +13,7 @@ use std::{
mod anyfunc;
pub use self::anyfunc::Anyfunc;
use self::anyfunc::AnyfuncTable;
pub(crate) use self::anyfunc::AnyfuncTable;
use crate::error::GrowError;
pub enum Element<'a> {

View File

@ -1,15 +1,19 @@
pub use crate::backing::{ImportBacking, LocalBacking, INTERNALS_SIZE};
use crate::{
error::CallResult,
instance::call_func_with_index_inner,
memory::{Memory, MemoryType},
module::{ModuleInfo, ModuleInner},
sig_registry::SigRegistry,
structures::TypedIndex,
types::{LocalOrImport, MemoryIndex},
types::{LocalOrImport, MemoryIndex, TableIndex, Value},
vmcalls,
};
use std::{
cell::UnsafeCell,
ffi::c_void,
mem, ptr,
mem,
ptr::{self, NonNull},
sync::atomic::{AtomicUsize, Ordering},
sync::Once,
};
@ -393,6 +397,41 @@ impl Ctx {
(*self.internal.internals)[field.index()] = value;
}
}
/// Calls a host or Wasm function at the given table index
pub fn call_with_table_index(
&mut self,
index: TableIndex,
args: &[Value],
) -> CallResult<Vec<Value>> {
let anyfunc_table =
unsafe { &*((**self.internal.tables).table as *mut crate::table::AnyfuncTable) };
let Anyfunc { func, ctx, sig_id } = anyfunc_table.backing[index.index()];
let signature = SigRegistry.lookup_signature(unsafe { std::mem::transmute(sig_id.0) });
let mut rets = vec![];
let wasm = {
let module = unsafe { &*self.module };
let runnable = &module.runnable_module;
let sig_index = SigRegistry.lookup_sig_index(signature.clone());
runnable
.get_trampoline(&module.info, sig_index)
.expect("wasm trampoline")
};
call_func_with_index_inner(
ctx,
NonNull::new(func as *mut _).unwrap(),
&signature,
wasm,
args,
&mut rets,
)?;
Ok(rets)
}
}
#[doc(hidden)]