propogate changes to use anyhow::Error instead of Box<dyn Error...>

This commit is contained in:
Pat Hickey
2021-10-21 11:53:55 -07:00
parent a5007f318f
commit abbe28d833
5 changed files with 30 additions and 42 deletions

View File

@@ -551,7 +551,7 @@ impl Table {
let init = init.into_table_element(store, ty)?; let init = init.into_table_element(store, ty)?;
let table = self.wasmtime_table(store); let table = self.wasmtime_table(store);
unsafe { unsafe {
match (*table).grow(delta, init, store) { match (*table).grow(delta, init, store)? {
Some(size) => { Some(size) => {
let vm = (*table).vmtable(); let vm = (*table).vmtable();
*store[self.0].definition = vm; *store[self.0].definition = vm;

View File

@@ -4,8 +4,6 @@ use crate::{
StoreContext, StoreContextMut, Trap, Val, ValRaw, ValType, StoreContext, StoreContextMut, Trap, Val, ValRaw, ValType,
}; };
use anyhow::{bail, Context as _, Result}; use anyhow::{bail, Context as _, Result};
use std::error::Error;
use std::fmt;
use std::future::Future; use std::future::Future;
use std::mem; use std::mem;
use std::panic::{self, AssertUnwindSafe}; use std::panic::{self, AssertUnwindSafe};
@@ -1784,25 +1782,6 @@ impl<T> AsContextMut for Caller<'_, T> {
} }
} }
fn cross_store_trap() -> Box<dyn Error + Send + Sync> {
#[derive(Debug)]
struct CrossStoreError;
impl Error for CrossStoreError {}
impl fmt::Display for CrossStoreError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"host function attempted to return cross-`Store` \
value to Wasm",
)
}
}
Box::new(CrossStoreError)
}
macro_rules! impl_into_func { macro_rules! impl_into_func {
($num:tt $($args:ident)*) => { ($num:tt $($args:ident)*) => {
// Implement for functions without a leading `&Caller` parameter, // Implement for functions without a leading `&Caller` parameter,
@@ -1852,7 +1831,7 @@ macro_rules! impl_into_func {
{ {
enum CallResult<U> { enum CallResult<U> {
Ok(U), Ok(U),
Trap(Box<dyn Error + Send + Sync>), Trap(anyhow::Error),
Panic(Box<dyn std::any::Any + Send>), Panic(Box<dyn std::any::Any + Send>),
} }
@@ -1901,7 +1880,9 @@ macro_rules! impl_into_func {
// can't assume it returned a value that is // can't assume it returned a value that is
// compatible with this store. // compatible with this store.
if !ret.compatible_with_store(caller.store.0) { if !ret.compatible_with_store(caller.store.0) {
CallResult::Trap(cross_store_trap()) CallResult::Trap(anyhow::anyhow!(
"host function attempted to return cross-`Store` value to Wasm"
))
} else { } else {
match ret.into_abi_for_ret(caller.store.0, retptr) { match ret.into_abi_for_ret(caller.store.0, retptr) {
Ok(val) => CallResult::Ok(val), Ok(val) => CallResult::Ok(val),

View File

@@ -479,7 +479,7 @@ impl Memory {
let store = store.as_context_mut().0; let store = store.as_context_mut().0;
let mem = self.wasmtime_memory(store); let mem = self.wasmtime_memory(store);
unsafe { unsafe {
match (*mem).grow(delta, store) { match (*mem).grow(delta, store)? {
Some(size) => { Some(size) => {
let vm = (*mem).vmmemory(); let vm = (*mem).vmmemory();
*store[self.0].definition = vm; *store[self.0].definition = vm;

View File

@@ -81,7 +81,6 @@ use anyhow::{bail, Result};
use std::cell::UnsafeCell; use std::cell::UnsafeCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::error::Error;
use std::fmt; use std::fmt;
use std::future::Future; use std::future::Future;
use std::marker; use std::marker;
@@ -1527,7 +1526,12 @@ unsafe impl<T> wasmtime_runtime::Store for StoreInner<T> {
(&mut inner.externref_activations_table, &inner.modules) (&mut inner.externref_activations_table, &inner.modules)
} }
fn memory_growing(&mut self, current: usize, desired: usize, maximum: Option<usize>) -> bool { fn memory_growing(
&mut self,
current: usize,
desired: usize,
maximum: Option<usize>,
) -> Result<bool, anyhow::Error> {
// Need to borrow async_cx before the mut borrow of the limiter. // Need to borrow async_cx before the mut borrow of the limiter.
// self.async_cx() panicks when used with a non-async store, so // self.async_cx() panicks when used with a non-async store, so
// wrap this in an option. // wrap this in an option.
@@ -1539,20 +1543,19 @@ unsafe impl<T> wasmtime_runtime::Store for StoreInner<T> {
}; };
match self.limiter { match self.limiter {
Some(ResourceLimiterInner::Sync(ref mut limiter)) => { Some(ResourceLimiterInner::Sync(ref mut limiter)) => {
limiter(&mut self.data).memory_growing(current, desired, maximum) Ok(limiter(&mut self.data).memory_growing(current, desired, maximum))
} }
#[cfg(feature = "async")] #[cfg(feature = "async")]
Some(ResourceLimiterInner::Async(ref mut limiter)) => unsafe { Some(ResourceLimiterInner::Async(ref mut limiter)) => unsafe {
async_cx Ok(async_cx
.expect("ResourceLimiterAsync requires async Store") .expect("ResourceLimiterAsync requires async Store")
.block_on( .block_on(
limiter(&mut self.data) limiter(&mut self.data)
.memory_growing(current, desired, maximum) .memory_growing(current, desired, maximum)
.as_mut(), .as_mut(),
) )?)
.expect("FIXME idk how to deal with a trap here!")
}, },
None => true, None => Ok(true),
} }
} }
@@ -1569,7 +1572,12 @@ unsafe impl<T> wasmtime_runtime::Store for StoreInner<T> {
} }
} }
fn table_growing(&mut self, current: u32, desired: u32, maximum: Option<u32>) -> bool { fn table_growing(
&mut self,
current: u32,
desired: u32,
maximum: Option<u32>,
) -> Result<bool, anyhow::Error> {
// Need to borrow async_cx before the mut borrow of the limiter. // Need to borrow async_cx before the mut borrow of the limiter.
// self.async_cx() panicks when used with a non-async store, so // self.async_cx() panicks when used with a non-async store, so
// wrap this in an option. // wrap this in an option.
@@ -1582,33 +1590,32 @@ unsafe impl<T> wasmtime_runtime::Store for StoreInner<T> {
match self.limiter { match self.limiter {
Some(ResourceLimiterInner::Sync(ref mut limiter)) => { Some(ResourceLimiterInner::Sync(ref mut limiter)) => {
limiter(&mut self.data).table_growing(current, desired, maximum) Ok(limiter(&mut self.data).table_growing(current, desired, maximum))
} }
#[cfg(feature = "async")] #[cfg(feature = "async")]
Some(ResourceLimiterInner::Async(ref mut limiter)) => unsafe { Some(ResourceLimiterInner::Async(ref mut limiter)) => unsafe {
async_cx Ok(async_cx
.expect("ResourceLimiterAsync requires async Store") .expect("ResourceLimiterAsync requires async Store")
.block_on( .block_on(
limiter(&mut self.data) limiter(&mut self.data)
.table_growing(current, desired, maximum) .table_growing(current, desired, maximum)
.as_mut(), .as_mut(),
) )?)
.expect("FIXME idk how to deal with a trap here!")
}, },
None => true, None => Ok(true),
} }
} }
fn out_of_gas(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> { fn out_of_gas(&mut self) -> Result<(), anyhow::Error> {
return match &mut self.out_of_gas_behavior { return match &mut self.out_of_gas_behavior {
OutOfGas::Trap => Err(Box::new(OutOfGasError)), OutOfGas::Trap => Err(anyhow::Error::new(OutOfGasError)),
#[cfg(feature = "async")] #[cfg(feature = "async")]
OutOfGas::InjectFuel { OutOfGas::InjectFuel {
injection_count, injection_count,
fuel_to_inject, fuel_to_inject,
} => { } => {
if *injection_count == 0 { if *injection_count == 0 {
return Err(Box::new(OutOfGasError)); return Err(anyhow::Error::new(OutOfGasError));
} }
*injection_count -= 1; *injection_count -= 1;
let fuel = *fuel_to_inject; let fuel = *fuel_to_inject;

View File

@@ -57,7 +57,7 @@ unsafe extern "C" fn stub_fn<F>(
// call-site, which gets unwrapped in `Trap::from_runtime` later on as we // call-site, which gets unwrapped in `Trap::from_runtime` later on as we
// convert from the internal `Trap` type to our own `Trap` type in this // convert from the internal `Trap` type to our own `Trap` type in this
// crate. // crate.
Ok(Err(trap)) => wasmtime_runtime::raise_user_trap(Box::new(trap)), Ok(Err(trap)) => wasmtime_runtime::raise_user_trap(trap.into()),
// And finally if the imported function panicked, then we trigger the // And finally if the imported function panicked, then we trigger the
// form of unwinding that's safe to jump over wasm code on all // form of unwinding that's safe to jump over wasm code on all