From 354539f5cb4c48d65991511d367669f57dac3cb9 Mon Sep 17 00:00:00 2001 From: Valery Antopol Date: Thu, 2 Nov 2023 19:05:57 +0400 Subject: [PATCH] feat!: propagate errors from module calls (#42) * compiling * prepare for merge --- Cargo.lock | 8 ++++++++ crates/it-lilo/Cargo.toml | 1 + crates/it-lilo/src/traits/allocatable.rs | 8 ++++++-- wasmer-it/Cargo.toml | 1 + wasmer-it/src/errors.rs | 6 +++++- wasmer-it/src/interpreter/instructions/call_core.rs | 3 ++- .../src/interpreter/instructions/lilo/lo_helper.rs | 2 +- wasmer-it/src/interpreter/wasm/structures.rs | 12 ++++++------ 8 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 50c73ad..6f9309a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + [[package]] name = "cfg-if" version = "1.0.0" @@ -29,6 +35,7 @@ dependencies = [ name = "it-lilo" version = "0.5.1" dependencies = [ + "anyhow", "fluence-it-types", "it-memory-traits", "log", @@ -223,6 +230,7 @@ dependencies = [ name = "wasmer-interface-types-fl" version = "0.26.1" dependencies = [ + "anyhow", "fluence-it-types", "it-lilo", "it-memory-traits", diff --git a/crates/it-lilo/Cargo.toml b/crates/it-lilo/Cargo.toml index ad12e74..f9cbe51 100644 --- a/crates/it-lilo/Cargo.toml +++ b/crates/it-lilo/Cargo.toml @@ -14,6 +14,7 @@ path = "src/lib.rs" fluence-it-types = { path = "../it-types/", version = "0.4.1" } it-memory-traits = { path = "../it-memory-traits", version = "0.4.0" } +anyhow = "1.0.75" paste = "1.0.11" thiserror = "1.0.38" log = "0.4.17" diff --git a/crates/it-lilo/src/traits/allocatable.rs b/crates/it-lilo/src/traits/allocatable.rs index 6e94578..3855f09 100644 --- a/crates/it-lilo/src/traits/allocatable.rs +++ b/crates/it-lilo/src/traits/allocatable.rs @@ -45,8 +45,12 @@ pub enum AllocatableError { }, /// Failed to call a allocate function. - #[error("call to allocated was failed")] - AllocateCallFailed, + #[error(r#"call to allocate function was failed: {reason}"#)] + AllocateCallFailed { + /// error returned by the allocate function + #[source] + reason: anyhow::Error, + }, /// Allocate input types doesn't match with needed. #[error( diff --git a/wasmer-it/Cargo.toml b/wasmer-it/Cargo.toml index e5eee4d..f6b2d43 100644 --- a/wasmer-it/Cargo.toml +++ b/wasmer-it/Cargo.toml @@ -13,6 +13,7 @@ it-to-bytes = { path = "../crates/to-bytes", version = "0.1.0" } it-lilo = { path = "../crates/it-lilo", version = "0.5.1" } it-memory-traits = { path = "../crates/it-memory-traits", version = "0.4.0" } +anyhow = "1.0.75" nom = "7.1" # do not update wast, new versions expect different wit syntax wast = "8.0" diff --git a/wasmer-it/src/errors.rs b/wasmer-it/src/errors.rs index 9714d75..4638b2b 100644 --- a/wasmer-it/src/errors.rs +++ b/wasmer-it/src/errors.rs @@ -172,10 +172,14 @@ pub enum InstructionErrorKind { }, /// Failed to call a local or import function. - #[error("failed while calling the local or import function `{function_name}`")] + #[error("failed while calling the local or import function `{function_name}`: {reason}")] LocalOrImportCall { /// The local or import function name that has been called. function_name: String, + + /// Error returned by the local or import call + #[source] + reason: anyhow::Error, }, /// The memory doesn't exist. diff --git a/wasmer-it/src/interpreter/instructions/call_core.rs b/wasmer-it/src/interpreter/instructions/call_core.rs index 729d8ca..5c8045a 100644 --- a/wasmer-it/src/interpreter/instructions/call_core.rs +++ b/wasmer-it/src/interpreter/instructions/call_core.rs @@ -34,11 +34,12 @@ executable_instruction!( log::debug!("call-core: calling {} with arguments: {:?}", local_or_import.name(), inputs); - let outputs = local_or_import.call(runtime.store, &inputs).map_err(|_| { + let outputs = local_or_import.call(runtime.store, &inputs).map_err(|e| { InstructionError::from_error_kind( instruction.clone(), InstructionErrorKind::LocalOrImportCall { function_name: local_or_import.name().to_string(), + reason: e }, ) })?; diff --git a/wasmer-it/src/interpreter/instructions/lilo/lo_helper.rs b/wasmer-it/src/interpreter/instructions/lilo/lo_helper.rs index c1556df..97d3085 100644 --- a/wasmer-it/src/interpreter/instructions/lilo/lo_helper.rs +++ b/wasmer-it/src/interpreter/instructions/lilo/lo_helper.rs @@ -87,7 +87,7 @@ where let outcome = local_or_import .call(store, &inputs) - .map_err(|_| AllocateCallFailed)?; + .map_err(|e| AllocateCallFailed { reason: e })?; if outcome.len() != 1 { return Err(AllocateFuncIncompatibleOutput); diff --git a/wasmer-it/src/interpreter/wasm/structures.rs b/wasmer-it/src/interpreter/wasm/structures.rs index d0f8ad3..e4c59f4 100644 --- a/wasmer-it/src/interpreter/wasm/structures.rs +++ b/wasmer-it/src/interpreter/wasm/structures.rs @@ -51,7 +51,7 @@ pub trait Export { fn outputs_cardinality(&self) -> usize; fn arguments(&self) -> &[FunctionArg]; fn outputs(&self) -> &[IType]; - fn call(&self, arguments: &[IValue]) -> Result, ()>; + fn call(&self, arguments: &[IValue]) -> Result, anyhow::Error>; } pub trait LocalImport { @@ -64,7 +64,7 @@ pub trait LocalImport { &self, store: &mut ::ActualStore<'_>, arguments: &[IValue], - ) -> Result, ()>; + ) -> Result, anyhow::Error>; } pub use it_memory_traits::Store; @@ -105,8 +105,8 @@ impl Export for () { &[] } - fn call(&self, _arguments: &[IValue]) -> Result, ()> { - Err(()) + fn call(&self, _arguments: &[IValue]) -> Result, anyhow::Error> { + Err(anyhow::anyhow!("some error")) } } @@ -135,8 +135,8 @@ impl LocalImport for () { &self, _store: &mut ::ActualStore<'_>, _arguments: &[IValue], - ) -> Result, ()> { - Err(()) + ) -> Result, anyhow::Error> { + Err(anyhow::anyhow!("some error")) } }