From abbe28d833d1a869384ec00b74af35cafc4766dc Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Thu, 21 Oct 2021 11:53:55 -0700 Subject: [PATCH] propogate changes to use anyhow::Error instead of Box --- crates/wasmtime/src/externals.rs | 2 +- crates/wasmtime/src/func.rs | 27 +++--------------- crates/wasmtime/src/memory.rs | 2 +- crates/wasmtime/src/store.rs | 39 +++++++++++++++----------- crates/wasmtime/src/trampoline/func.rs | 2 +- 5 files changed, 30 insertions(+), 42 deletions(-) diff --git a/crates/wasmtime/src/externals.rs b/crates/wasmtime/src/externals.rs index 8f3d7e1645..d267bf14a7 100644 --- a/crates/wasmtime/src/externals.rs +++ b/crates/wasmtime/src/externals.rs @@ -551,7 +551,7 @@ impl Table { let init = init.into_table_element(store, ty)?; let table = self.wasmtime_table(store); unsafe { - match (*table).grow(delta, init, store) { + match (*table).grow(delta, init, store)? { Some(size) => { let vm = (*table).vmtable(); *store[self.0].definition = vm; diff --git a/crates/wasmtime/src/func.rs b/crates/wasmtime/src/func.rs index dd8877e8f9..068d0b4aaa 100644 --- a/crates/wasmtime/src/func.rs +++ b/crates/wasmtime/src/func.rs @@ -4,8 +4,6 @@ use crate::{ StoreContext, StoreContextMut, Trap, Val, ValRaw, ValType, }; use anyhow::{bail, Context as _, Result}; -use std::error::Error; -use std::fmt; use std::future::Future; use std::mem; use std::panic::{self, AssertUnwindSafe}; @@ -1784,25 +1782,6 @@ impl AsContextMut for Caller<'_, T> { } } -fn cross_store_trap() -> Box { - #[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 { ($num:tt $($args:ident)*) => { // Implement for functions without a leading `&Caller` parameter, @@ -1852,7 +1831,7 @@ macro_rules! impl_into_func { { enum CallResult { Ok(U), - Trap(Box), + Trap(anyhow::Error), Panic(Box), } @@ -1901,7 +1880,9 @@ macro_rules! impl_into_func { // can't assume it returned a value that is // compatible with this store. 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 { match ret.into_abi_for_ret(caller.store.0, retptr) { Ok(val) => CallResult::Ok(val), diff --git a/crates/wasmtime/src/memory.rs b/crates/wasmtime/src/memory.rs index de15665afd..f541c2f81b 100644 --- a/crates/wasmtime/src/memory.rs +++ b/crates/wasmtime/src/memory.rs @@ -479,7 +479,7 @@ impl Memory { let store = store.as_context_mut().0; let mem = self.wasmtime_memory(store); unsafe { - match (*mem).grow(delta, store) { + match (*mem).grow(delta, store)? { Some(size) => { let vm = (*mem).vmmemory(); *store[self.0].definition = vm; diff --git a/crates/wasmtime/src/store.rs b/crates/wasmtime/src/store.rs index 8c3a63df48..b861befd53 100644 --- a/crates/wasmtime/src/store.rs +++ b/crates/wasmtime/src/store.rs @@ -81,7 +81,6 @@ use anyhow::{bail, Result}; use std::cell::UnsafeCell; use std::collections::HashMap; use std::convert::TryFrom; -use std::error::Error; use std::fmt; use std::future::Future; use std::marker; @@ -1527,7 +1526,12 @@ unsafe impl wasmtime_runtime::Store for StoreInner { (&mut inner.externref_activations_table, &inner.modules) } - fn memory_growing(&mut self, current: usize, desired: usize, maximum: Option) -> bool { + fn memory_growing( + &mut self, + current: usize, + desired: usize, + maximum: Option, + ) -> Result { // Need to borrow async_cx before the mut borrow of the limiter. // self.async_cx() panicks when used with a non-async store, so // wrap this in an option. @@ -1539,20 +1543,19 @@ unsafe impl wasmtime_runtime::Store for StoreInner { }; match self.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")] Some(ResourceLimiterInner::Async(ref mut limiter)) => unsafe { - async_cx + Ok(async_cx .expect("ResourceLimiterAsync requires async Store") .block_on( limiter(&mut self.data) .memory_growing(current, desired, maximum) .as_mut(), - ) - .expect("FIXME idk how to deal with a trap here!") + )?) }, - None => true, + None => Ok(true), } } @@ -1569,7 +1572,12 @@ unsafe impl wasmtime_runtime::Store for StoreInner { } } - fn table_growing(&mut self, current: u32, desired: u32, maximum: Option) -> bool { + fn table_growing( + &mut self, + current: u32, + desired: u32, + maximum: Option, + ) -> Result { // Need to borrow async_cx before the mut borrow of the limiter. // self.async_cx() panicks when used with a non-async store, so // wrap this in an option. @@ -1582,33 +1590,32 @@ unsafe impl wasmtime_runtime::Store for StoreInner { match self.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")] Some(ResourceLimiterInner::Async(ref mut limiter)) => unsafe { - async_cx + Ok(async_cx .expect("ResourceLimiterAsync requires async Store") .block_on( limiter(&mut self.data) .table_growing(current, desired, maximum) .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> { + fn out_of_gas(&mut self) -> Result<(), anyhow::Error> { return match &mut self.out_of_gas_behavior { - OutOfGas::Trap => Err(Box::new(OutOfGasError)), + OutOfGas::Trap => Err(anyhow::Error::new(OutOfGasError)), #[cfg(feature = "async")] OutOfGas::InjectFuel { injection_count, fuel_to_inject, } => { if *injection_count == 0 { - return Err(Box::new(OutOfGasError)); + return Err(anyhow::Error::new(OutOfGasError)); } *injection_count -= 1; let fuel = *fuel_to_inject; diff --git a/crates/wasmtime/src/trampoline/func.rs b/crates/wasmtime/src/trampoline/func.rs index e37803763f..fc217377be 100644 --- a/crates/wasmtime/src/trampoline/func.rs +++ b/crates/wasmtime/src/trampoline/func.rs @@ -57,7 +57,7 @@ unsafe extern "C" fn stub_fn( // 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 // 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 // form of unwinding that's safe to jump over wasm code on all