1053: For RuntimeError and breakpoints, use Box<Any + Send> instead of Box<Any>. r=syrusakbary a=nlewycky

# Description
For RuntimeError and breakpoints, use Box<Any + Send> instead of Box\<Any\>.

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file

Fixes #1049 

Co-authored-by: Nick Lewycky <nick@wasmer.io>
Co-authored-by: Syrus Akbary <me@syrusakbary.com>
This commit is contained in:
bors[bot] 2019-12-10 22:00:02 +00:00 committed by GitHub
commit 8a13917ec5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 37 additions and 34 deletions

View File

@ -2,6 +2,7 @@
## **[Unreleased]** ## **[Unreleased]**
- [#1053](https://github.com/wasmerio/wasmer/pull/1053) For RuntimeError and breakpoints, use Box<Any + Send> instead of Box<Any>.
- [#1052](https://github.com/wasmerio/wasmer/pull/1052) Fix minor panic and improve Error handling in singlepass backend. - [#1052](https://github.com/wasmerio/wasmer/pull/1052) Fix minor panic and improve Error handling in singlepass backend.
- [#1050](https://github.com/wasmerio/wasmer/pull/1050) Attach C & C++ headers to releases. - [#1050](https://github.com/wasmerio/wasmer/pull/1050) Attach C & C++ headers to releases.
- [#1033](https://github.com/wasmerio/wasmer/pull/1033) Set cranelift backend as default compiler backend again, require at least one backend to be enabled for Wasmer CLI - [#1033](https://github.com/wasmerio/wasmer/pull/1033) Set cranelift backend as default compiler backend again, require at least one backend to be enabled for Wasmer CLI

View File

@ -26,12 +26,12 @@ pub use self::unix::*;
pub use self::windows::*; pub use self::windows::*;
thread_local! { thread_local! {
pub static TRAP_EARLY_DATA: Cell<Option<Box<dyn Any>>> = Cell::new(None); pub static TRAP_EARLY_DATA: Cell<Option<Box<dyn Any + Send>>> = Cell::new(None);
} }
pub enum CallProtError { pub enum CallProtError {
Trap(WasmTrapInfo), Trap(WasmTrapInfo),
Error(Box<dyn Any>), Error(Box<dyn Any + Send>),
} }
pub struct Caller { pub struct Caller {
@ -67,7 +67,7 @@ impl RunnableModule for Caller {
args: *const u64, args: *const u64,
rets: *mut u64, rets: *mut u64,
trap_info: *mut WasmTrapInfo, trap_info: *mut WasmTrapInfo,
user_error: *mut Option<Box<dyn Any>>, user_error: *mut Option<Box<dyn Any + Send>>,
invoke_env: Option<NonNull<c_void>>, invoke_env: Option<NonNull<c_void>>,
) -> bool { ) -> bool {
let handler_data = &*invoke_env.unwrap().cast().as_ptr(); let handler_data = &*invoke_env.unwrap().cast().as_ptr();
@ -108,7 +108,7 @@ impl RunnableModule for Caller {
}) })
} }
unsafe fn do_early_trap(&self, data: Box<dyn Any>) -> ! { unsafe fn do_early_trap(&self, data: Box<dyn Any + Send>) -> ! {
TRAP_EARLY_DATA.with(|cell| cell.set(Some(data))); TRAP_EARLY_DATA.with(|cell| cell.set(Some(data)));
trigger_trap() trigger_trap()
} }

View File

@ -66,7 +66,7 @@ extern "C" {
params: *const u64, params: *const u64,
results: *mut u64, results: *mut u64,
trap_out: *mut WasmTrapInfo, trap_out: *mut WasmTrapInfo,
user_error: *mut Option<Box<dyn Any>>, user_error: *mut Option<Box<dyn Any + Send>>,
invoke_env: Option<NonNull<c_void>>, invoke_env: Option<NonNull<c_void>>,
) -> bool; ) -> bool;
} }
@ -427,7 +427,7 @@ impl RunnableModule for LLVMBackend {
self.msm.clone() self.msm.clone()
} }
unsafe fn do_early_trap(&self, data: Box<dyn Any>) -> ! { unsafe fn do_early_trap(&self, data: Box<dyn Any + Send>) -> ! {
throw_any(Box::leak(data)) throw_any(Box::leak(data))
} }
} }

View File

@ -856,7 +856,8 @@ pub unsafe extern "C" fn callback_trampoline(
callback: *mut BreakpointHandler, callback: *mut BreakpointHandler,
) { ) {
let callback = Box::from_raw(callback); let callback = Box::from_raw(callback);
let result: Result<(), Box<dyn std::any::Any>> = callback(BreakpointInfo { fault: None }); let result: Result<(), Box<dyn std::any::Any + Send>> =
callback(BreakpointInfo { fault: None });
match result { match result {
Ok(()) => *b = None, Ok(()) => *b = None,
Err(e) => *b = Some(e), Err(e) => *b = Some(e),

View File

@ -271,7 +271,7 @@ pub trait RunnableModule: Send + Sync {
/// signature and an invoke function that can call the trampoline. /// signature and an invoke function that can call the trampoline.
fn get_trampoline(&self, info: &ModuleInfo, sig_index: SigIndex) -> Option<Wasm>; fn get_trampoline(&self, info: &ModuleInfo, sig_index: SigIndex) -> Option<Wasm>;
unsafe fn do_early_trap(&self, data: Box<dyn Any>) -> !; unsafe fn do_early_trap(&self, data: Box<dyn Any + Send>) -> !;
/// Returns the machine code associated with this module. /// Returns the machine code associated with this module.
fn get_code(&self) -> Option<&[u8]> { fn get_code(&self) -> Option<&[u8]> {

View File

@ -23,7 +23,7 @@ use wasmparser::{Operator, Type as WpType};
/// A type that defines a function pointer, which is called when breakpoints occur. /// A type that defines a function pointer, which is called when breakpoints occur.
pub type BreakpointHandler = pub type BreakpointHandler =
Box<dyn Fn(BreakpointInfo) -> Result<(), Box<dyn Any>> + Send + Sync + 'static>; Box<dyn Fn(BreakpointInfo) -> Result<(), Box<dyn Any + Send>> + Send + Sync + 'static>;
/// Maps instruction pointers to their breakpoint handlers. /// Maps instruction pointers to their breakpoint handlers.
pub type BreakpointMap = Arc<HashMap<usize, BreakpointHandler>>; pub type BreakpointMap = Arc<HashMap<usize, BreakpointHandler>>;

View File

@ -187,7 +187,7 @@ pub enum RuntimeError {
/// Error. /// Error.
Error { Error {
/// Error data. /// Error data.
data: Box<dyn Any>, data: Box<dyn Any + Send>,
}, },
} }

View File

@ -61,7 +61,7 @@ type SetJmpBuffer = [i32; SETJMP_BUFFER_LEN];
struct UnwindInfo { struct UnwindInfo {
jmpbuf: SetJmpBuffer, // in jmpbuf: SetJmpBuffer, // in
breakpoints: Option<BreakpointMap>, breakpoints: Option<BreakpointMap>,
payload: Option<Box<dyn Any>>, // out payload: Option<Box<dyn Any + Send>>, // out
} }
/// A store for boundary register preservation. /// A store for boundary register preservation.
@ -182,7 +182,7 @@ pub unsafe fn clear_wasm_interrupt() {
pub unsafe fn catch_unsafe_unwind<R, F: FnOnce() -> R>( pub unsafe fn catch_unsafe_unwind<R, F: FnOnce() -> R>(
f: F, f: F,
breakpoints: Option<BreakpointMap>, breakpoints: Option<BreakpointMap>,
) -> Result<R, Box<dyn Any>> { ) -> Result<R, Box<dyn Any + Send>> {
let unwind = UNWIND.with(|x| x.get()); let unwind = UNWIND.with(|x| x.get());
let old = (*unwind).take(); let old = (*unwind).take();
*unwind = Some(UnwindInfo { *unwind = Some(UnwindInfo {
@ -205,7 +205,7 @@ pub unsafe fn catch_unsafe_unwind<R, F: FnOnce() -> R>(
} }
/// Begins an unsafe unwind. /// Begins an unsafe unwind.
pub unsafe fn begin_unsafe_unwind(e: Box<dyn Any>) -> ! { pub unsafe fn begin_unsafe_unwind(e: Box<dyn Any + Send>) -> ! {
let unwind = UNWIND.with(|x| x.get()); let unwind = UNWIND.with(|x| x.get());
let inner = (*unwind) let inner = (*unwind)
.as_mut() .as_mut()
@ -283,7 +283,7 @@ extern "C" fn signal_trap_handler(
static ARCH: Architecture = Architecture::Aarch64; static ARCH: Architecture = Architecture::Aarch64;
let mut should_unwind = false; let mut should_unwind = false;
let mut unwind_result: Box<dyn Any> = Box::new(()); let mut unwind_result: Box<dyn Any + Send> = Box::new(());
unsafe { unsafe {
let fault = get_fault_info(siginfo as _, ucontext); let fault = get_fault_info(siginfo as _, ucontext);
@ -307,7 +307,7 @@ extern "C" fn signal_trap_handler(
match ib.ty { match ib.ty {
InlineBreakpointType::Trace => {} InlineBreakpointType::Trace => {}
InlineBreakpointType::Middleware => { InlineBreakpointType::Middleware => {
let out: Option<Result<(), Box<dyn Any>>> = let out: Option<Result<(), Box<dyn Any + Send>>> =
with_breakpoint_map(|bkpt_map| { with_breakpoint_map(|bkpt_map| {
bkpt_map.and_then(|x| x.get(&ip)).map(|x| { bkpt_map.and_then(|x| x.get(&ip)).map(|x| {
x(BreakpointInfo { x(BreakpointInfo {
@ -348,13 +348,14 @@ extern "C" fn signal_trap_handler(
match Signal::from_c_int(signum) { match Signal::from_c_int(signum) {
Ok(SIGTRAP) => { Ok(SIGTRAP) => {
// breakpoint // breakpoint
let out: Option<Result<(), Box<dyn Any>>> = with_breakpoint_map(|bkpt_map| { let out: Option<Result<(), Box<dyn Any + Send>>> =
bkpt_map.and_then(|x| x.get(&(fault.ip.get()))).map(|x| { with_breakpoint_map(|bkpt_map| {
x(BreakpointInfo { bkpt_map.and_then(|x| x.get(&(fault.ip.get()))).map(|x| {
fault: Some(&fault), x(BreakpointInfo {
fault: Some(&fault),
})
}) })
}) });
});
match out { match out {
Some(Ok(())) => { Some(Ok(())) => {
return false; return false;

View File

@ -652,7 +652,7 @@ pub mod x64 {
image: InstanceImage, image: InstanceImage,
vmctx: &mut Ctx, vmctx: &mut Ctx,
breakpoints: Option<BreakpointMap>, breakpoints: Option<BreakpointMap>,
) -> Result<u64, Box<dyn Any>> { ) -> Result<u64, Box<dyn Any + Send>> {
let mut stack: Vec<u64> = vec![0; 1048576 * 8 / 8]; // 8MB stack let mut stack: Vec<u64> = vec![0; 1048576 * 8 / 8]; // 8MB stack
let mut stack_offset: usize = stack.len(); let mut stack_offset: usize = stack.len();

View File

@ -78,7 +78,7 @@ pub type Invoke = unsafe extern "C" fn(
args: *const u64, args: *const u64,
rets: *mut u64, rets: *mut u64,
trap_info: *mut WasmTrapInfo, trap_info: *mut WasmTrapInfo,
user_error: *mut Option<Box<dyn Any>>, user_error: *mut Option<Box<dyn Any + Send>>,
extra: Option<NonNull<c_void>>, extra: Option<NonNull<c_void>>,
) -> bool; ) -> bool;
@ -201,7 +201,7 @@ where
Rets: WasmTypeList, Rets: WasmTypeList,
{ {
/// The error type for this trait. /// The error type for this trait.
type Error: 'static; type Error: Send + 'static;
/// Get returns or error result. /// Get returns or error result.
fn report(self) -> Result<Rets, Self::Error>; fn report(self) -> Result<Rets, Self::Error>;
} }
@ -219,7 +219,7 @@ where
impl<Rets, E> TrapEarly<Rets> for Result<Rets, E> impl<Rets, E> TrapEarly<Rets> for Result<Rets, E>
where where
Rets: WasmTypeList, Rets: WasmTypeList,
E: 'static, E: Send + 'static,
{ {
type Error = E; type Error = E;
fn report(self) -> Result<Rets, E> { fn report(self) -> Result<Rets, E> {
@ -507,7 +507,7 @@ macro_rules! impl_traits {
Ok(Ok(returns)) => return returns.into_c_struct(), Ok(Ok(returns)) => return returns.into_c_struct(),
Ok(Err(err)) => { Ok(Err(err)) => {
let b: Box<_> = err.into(); let b: Box<_> = err.into();
b as Box<dyn Any> b as Box<dyn Any + Send>
}, },
Err(err) => err, Err(err) => err,
}; };
@ -619,7 +619,7 @@ macro_rules! impl_traits {
Ok(Ok(returns)) => return returns.into_c_struct(), Ok(Ok(returns)) => return returns.into_c_struct(),
Ok(Err(err)) => { Ok(Err(err)) => {
let b: Box<_> = err.into(); let b: Box<_> = err.into();
b as Box<dyn Any> b as Box<dyn Any + Send>
}, },
Err(err) => err, Err(err) => err,
}; };

View File

@ -1069,7 +1069,7 @@ mod vm_ctx_tests {
fn get_trampoline(&self, _module: &ModuleInfo, _sig_index: SigIndex) -> Option<Wasm> { fn get_trampoline(&self, _module: &ModuleInfo, _sig_index: SigIndex) -> Option<Wasm> {
unimplemented!("generate_module::get_trampoline") unimplemented!("generate_module::get_trampoline")
} }
unsafe fn do_early_trap(&self, _: Box<dyn Any>) -> ! { unsafe fn do_early_trap(&self, _: Box<dyn Any + Send>) -> ! {
unimplemented!("generate_module::do_early_trap") unimplemented!("generate_module::do_early_trap")
} }
} }

View File

@ -206,7 +206,7 @@ pub struct X64FunctionCode {
breakpoints: Option< breakpoints: Option<
HashMap< HashMap<
AssemblyOffset, AssemblyOffset,
Box<dyn Fn(BreakpointInfo) -> Result<(), Box<dyn Any>> + Send + Sync + 'static>, Box<dyn Fn(BreakpointInfo) -> Result<(), Box<dyn Any + Send>> + Send + Sync + 'static>,
>, >,
>, >,
returns: SmallVec<[WpType; 1]>, returns: SmallVec<[WpType; 1]>,
@ -359,7 +359,7 @@ impl RunnableModule for X64ExecutionContext {
args: *const u64, args: *const u64,
rets: *mut u64, rets: *mut u64,
trap_info: *mut WasmTrapInfo, trap_info: *mut WasmTrapInfo,
user_error: *mut Option<Box<dyn Any>>, user_error: *mut Option<Box<dyn Any + Send>>,
num_params_plus_one: Option<NonNull<c_void>>, num_params_plus_one: Option<NonNull<c_void>>,
) -> bool { ) -> bool {
let rm: &Box<dyn RunnableModule> = &(&*(*ctx).module).runnable_module; let rm: &Box<dyn RunnableModule> = &(&*(*ctx).module).runnable_module;
@ -533,7 +533,7 @@ impl RunnableModule for X64ExecutionContext {
}) })
} }
unsafe fn do_early_trap(&self, data: Box<dyn Any>) -> ! { unsafe fn do_early_trap(&self, data: Box<dyn Any + Send>) -> ! {
protect_unix::TRAP_EARLY_DATA.with(|x| x.set(Some(data))); protect_unix::TRAP_EARLY_DATA.with(|x| x.set(Some(data)));
protect_unix::trigger_trap(); protect_unix::trigger_trap();
} }

View File

@ -16,7 +16,7 @@ use wasmer_runtime_core::fault::{begin_unsafe_unwind, catch_unsafe_unwind, ensur
use wasmer_runtime_core::typed_func::WasmTrapInfo; use wasmer_runtime_core::typed_func::WasmTrapInfo;
thread_local! { thread_local! {
pub static TRAP_EARLY_DATA: Cell<Option<Box<dyn Any>>> = Cell::new(None); pub static TRAP_EARLY_DATA: Cell<Option<Box<dyn Any + Send>>> = Cell::new(None);
} }
pub unsafe fn trigger_trap() -> ! { pub unsafe fn trigger_trap() -> ! {
@ -25,7 +25,7 @@ pub unsafe fn trigger_trap() -> ! {
pub enum CallProtError { pub enum CallProtError {
Trap(WasmTrapInfo), Trap(WasmTrapInfo),
Error(Box<dyn Any>), Error(Box<dyn Any + Send>),
} }
pub fn call_protected<T>( pub fn call_protected<T>(
@ -48,6 +48,6 @@ pub fn call_protected<T>(
} }
} }
pub unsafe fn throw(payload: Box<dyn Any>) -> ! { pub unsafe fn throw(payload: Box<dyn Any + Send>) -> ! {
begin_unsafe_unwind(payload); begin_unsafe_unwind(payload);
} }