From 9ead93684e1539a1d2bac0a8edc07c3e288360d1 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 7 Jan 2020 11:45:13 -0600 Subject: [PATCH] Ensure `Trap` is returned for start function traps (#768) * Ensure `Trap` is returned for start function traps Handle another case of errors coming out of instantiation, resolve a FIXME, and remove an unneeded dependency from the wast testsuite crate. * Run rustfmt --- Cargo.lock | 1 - crates/api/src/instance.rs | 6 ++++-- crates/api/src/trap.rs | 2 +- crates/wast/Cargo.toml | 1 - crates/wast/src/wast.rs | 11 +++-------- 5 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea00c661a5..5a94f65114 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2216,7 +2216,6 @@ version = "0.7.0" dependencies = [ "anyhow", "wasmtime", - "wasmtime-runtime", "wast 4.0.0", ] diff --git a/crates/api/src/instance.rs b/crates/api/src/instance.rs index f7fc2f599a..f85b243447 100644 --- a/crates/api/src/instance.rs +++ b/crates/api/src/instance.rs @@ -10,8 +10,8 @@ use anyhow::{Error, Result}; use std::cell::RefCell; use std::collections::{HashMap, HashSet}; use std::rc::Rc; -use wasmtime_jit::{instantiate, Resolver}; -use wasmtime_runtime::{Export, InstanceHandle}; +use wasmtime_jit::{instantiate, Resolver, SetupError}; +use wasmtime_runtime::{Export, InstanceHandle, InstantiationError}; struct SimpleResolver { imports: Vec<(String, String, Extern)>, @@ -46,6 +46,8 @@ pub fn instantiate_in_context( .map_err(|e| -> Error { if let Some(trap) = take_api_trap() { Trap::from(trap).into() + } else if let SetupError::Instantiate(InstantiationError::StartTrap(msg)) = e { + Trap::new(msg).into() } else { e.into() } diff --git a/crates/api/src/trap.rs b/crates/api/src/trap.rs index 60c2d1f858..abad7fabd0 100644 --- a/crates/api/src/trap.rs +++ b/crates/api/src/trap.rs @@ -74,7 +74,7 @@ impl fmt::Debug for UnsafeTrapInfo { /// A struct representing an aborted instruction execution, with a message /// indicating the cause. -#[derive(Error, Debug)] +#[derive(Error, Debug, Clone)] #[error("Wasm trap: {message}")] pub struct Trap { message: String, diff --git a/crates/wast/Cargo.toml b/crates/wast/Cargo.toml index 4a6b9e0876..d20170711b 100644 --- a/crates/wast/Cargo.toml +++ b/crates/wast/Cargo.toml @@ -13,7 +13,6 @@ edition = "2018" [dependencies] anyhow = "1.0.19" wasmtime = { path = "../api" } -wasmtime-runtime = { path = "../runtime" } wast = "4.0.0" [badges] diff --git a/crates/wast/src/wast.rs b/crates/wast/src/wast.rs index 8fd11f8995..f392756bd8 100644 --- a/crates/wast/src/wast.rs +++ b/crates/wast/src/wast.rs @@ -93,15 +93,10 @@ impl WastContext { } let instance = match Instance::new(&self.store, &module, &imports) { Ok(i) => i, - // FIXME(#683) shouldn't have to reach into runtime crate Err(e) => { - use wasmtime_runtime::InstantiationError; - let err = e - .chain() - .filter_map(|e| e.downcast_ref::()) - .next(); - if let Some(InstantiationError::StartTrap(msg)) = err { - return Ok(Outcome::Trap(Trap::new(msg.clone()))); + let err = e.chain().filter_map(|e| e.downcast_ref::()).next(); + if let Some(trap) = err { + return Ok(Outcome::Trap(trap.clone())); } return Err(e); }