Skip to content

Commit

Permalink
Update Wasmer error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Sep 8, 2020
1 parent 89e770e commit 3274902
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
5 changes: 2 additions & 3 deletions packages/vm/src/backends/singlepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use wasmer_runtime_core::{
vm::Ctx,
};

use crate::errors::VmResult;
use crate::errors::{VmError, VmResult};
// use crate::middleware::DeterministicMiddleware;

/// In Wasmer, the gas limit is set on modules during compilation and is included in the cached modules.
Expand All @@ -26,8 +26,7 @@ const MAX_GAS_LIMIT: u64 = u64::MAX / 2;
const FAKE_GAS_AVAILABLE: u64 = 1_000_000;

pub fn compile(code: &[u8]) -> VmResult<Module> {
let module = compile_with(code, Backend::Auto)?;
Ok(module)
compile_with(code, Backend::Auto).map_err(|err| VmError::compile_err(err.to_string()))
}

pub fn compiler() -> Box<dyn Compiler> {
Expand Down
33 changes: 10 additions & 23 deletions packages/vm/src/errors/vm_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,34 +199,15 @@ impl From<wasmer_runtime_core::error::CompileError> for VmError {
}
}

impl From<wasmer_runtime_core::error::ResolveError> for VmError {
fn from(original: wasmer_runtime_core::error::ResolveError) -> Self {
VmError::resolve_err(format!("Wasmer resolve error: {:?}", original))
impl From<wasmer_runtime_core::error::ExportError> for VmError {
fn from(original: wasmer_runtime_core::error::ExportError) -> Self {
VmError::resolve_err(format!("Wasmer export error: {:?}", original))
}
}

impl From<wasmer_runtime_core::error::RuntimeError> for VmError {
fn from(original: wasmer_runtime_core::error::RuntimeError) -> Self {
use wasmer_runtime_core::error::{InvokeError, RuntimeError};

fn runtime_error(err: RuntimeError) -> VmError {
VmError::runtime_err(format!("Wasmer runtime error: {:?}", err))
}

match original {
// TODO: fix the issue described below:
// `InvokeError::FailedWithNoError` happens when running out of gas in singlepass v0.17
// but it's supposed to indicate bugs in Wasmer...
// https://github.com/wasmerio/wasmer/issues/1452
// https://github.com/CosmWasm/cosmwasm/issues/375
RuntimeError::InvokeError(InvokeError::FailedWithNoError) => VmError::GasDepletion,
// This variant contains the error we return from imports.
RuntimeError::User(err) => match err.downcast::<VmError>() {
Ok(err) => *err,
Err(err) => runtime_error(RuntimeError::User(err)),
},
_ => runtime_error(original),
}
VmError::runtime_err(format!("Wasmer runtime error: {:?}", original))
}
}

Expand All @@ -236,6 +217,12 @@ impl From<InsufficientGasLeft> for VmError {
}
}

impl From<std::convert::Infallible> for VmError {
fn from(_original: std::convert::Infallible) -> Self {
unreachable!();
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 3274902

Please sign in to comment.