Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Update to new wasmi & error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
NikVolf committed Feb 21, 2018
1 parent bace1ff commit 2bf064d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions ethcore/vm/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ pub struct Schedule {

/// Wasm cost table
pub struct WasmCosts {
/// Arena allocator cost, per byte
pub alloc: u32,
/// Default opcode cost
pub regular: u32,
/// Div operations multiplier.
pub div: u32,
/// Div operations multiplier.
Expand All @@ -145,7 +145,7 @@ pub struct WasmCosts {
impl Default for WasmCosts {
fn default() -> Self {
WasmCosts {
alloc: 2,
regular: 1,
div: 16,
mul: 4,
mem: 2,
Expand Down
46 changes: 23 additions & 23 deletions ethcore/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ impl From<InterpreterError> for Error {
}
}

impl From<Trap> for Error {
fn from(e: Trap) -> Self {
Error::Trap(e)
}
}

impl From<Error> for vm::Error {
fn from(e: Error) -> Self {
match e {
Expand Down Expand Up @@ -122,31 +128,25 @@ impl vm::Vm for WasmInterpreter {

let module_instance = module_instance.run_start(&mut runtime).map_err(Error::Trap)?;

match module_instance.invoke_export("call", &[], &mut runtime) {
Ok(_) => { },
Err(InterpreterError::Host(boxed)) => {
match boxed.downcast_ref::<runtime::Error>() {
None => {
return Err(vm::Error::Wasm("Invalid user error used in interpreter".to_owned()));
}
Some(runtime_err) => {
match *runtime_err {
runtime::Error::Suicide => {
// Suicide uses trap to break execution
}
ref any_err => {
trace!(target: "wasm", "Error executing contract: {:?}", boxed);
return Err(vm::Error::from(Error::from(InterpreterError::Host(Box::new(any_err.clone())))));
}
}
}
}
},
Err(err) => {
trace!(target: "wasm", "Error executing contract: {:?}", err);
return Err(vm::Error::from(Error::from(err)))
let invoke_result = module_instance.invoke_export("call", &[], &mut runtime);

let mut suicide = false;
if let Err(InterpreterError::Trap(ref trap)) = invoke_result {
if let wasmi::TrapKind::Host(ref boxed) = *trap.kind() {
let ref runtime_err = boxed.downcast_ref::<runtime::Error>()
.expect("Host errors other than runtime::Error never produced; qed");

if let runtime::Error::Suicide = **runtime_err { suicide = true; }
}
}

if !suicide {
if let Err(e) = invoke_result {
trace!(target: "wasm", "Error executing contract: {:?}", e);
return Err(vm::Error::from(Error::from(e)));
}
}

(
runtime.gas_left().expect("Cannot fail since it was not updated since last charge"),
runtime.into_result(),
Expand Down
4 changes: 1 addition & 3 deletions ethcore/wasm/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use parity_wasm::peek_size;

fn gas_rules(wasm_costs: &vm::WasmCosts) -> rules::Set {
rules::Set::new(
1,
wasm_costs.regular,
{
let mut vals = ::std::collections::HashMap::with_capacity(8);
vals.insert(rules::InstructionType::Load, rules::Metering::Fixed(wasm_costs.mem as u32));
Expand Down Expand Up @@ -77,8 +77,6 @@ pub fn payload<'a>(params: &'a vm::ActionParams, wasm_costs: &vm::WasmCosts)
&gas_rules(wasm_costs),
).map_err(|_| vm::Error::Wasm(format!("Wasm contract error: bytecode invalid")))?;

::parity_wasm::elements::serialize_to_file("./debug.wasm", contract_module.clone());

let data = match params.params_type {
vm::ParamsType::Embedded => {
if data_position < code.len() { &code[data_position..] } else { &[] }
Expand Down
30 changes: 27 additions & 3 deletions ethcore/wasm/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ethereum_types::{U256, H256, Address};
use vm::{self, CallType};
use wasmi::{self, MemoryRef, RuntimeArgs, RuntimeValue, Error as InterpreterError, Trap};
use wasmi::{self, MemoryRef, RuntimeArgs, RuntimeValue, Error as InterpreterError, Trap, TrapKind};
use super::panic_payload;

pub struct RuntimeContext {
Expand Down Expand Up @@ -52,6 +52,16 @@ pub enum Error {
Other,
/// Syscall signature mismatch
InvalidSyscall,
/// Unreachable instruction encountered
Unreachable,
/// Invalid virtual call
InvalidVirtualCall,
/// Division by zero
DivisionByZero,
/// Invalid conversion to integer
InvalidConversionToInt,
/// Stack overflow
StackOverflow,
/// Panic with message
Panic(String),
}
Expand All @@ -60,7 +70,16 @@ impl wasmi::HostError for Error { }

impl From<Trap> for Error {
fn from(trap: Trap) -> Self {
Error::Other
match *trap.kind() {
TrapKind::Unreachable => Error::Unreachable,
TrapKind::MemoryAccessOutOfBounds => Error::MemoryAccessViolation,
TrapKind::TableAccessOutOfBounds | TrapKind::ElemUninitialized => Error::InvalidVirtualCall,
TrapKind::DivisionByZero => Error::DivisionByZero,
TrapKind::InvalidConversionToInt => Error::InvalidConversionToInt,
TrapKind::UnexpectedSignature => Error::InvalidVirtualCall,
TrapKind::StackOverflow => Error::StackOverflow,
TrapKind::Host(_) => Error::Other,
}
}
}

Expand Down Expand Up @@ -91,6 +110,11 @@ impl ::std::fmt::Display for Error {
Error::Log => write!(f, "Error occured while logging an event"),
Error::InvalidSyscall => write!(f, "Invalid syscall signature encountered at runtime"),
Error::Other => write!(f, "Other unspecified error"),
Error::Unreachable => write!(f, "Unreachable instruction encountered"),
Error::InvalidVirtualCall => write!(f, "Invalid virtual call"),
Error::DivisionByZero => write!(f, "Division by zero"),
Error::StackOverflow => write!(f, "Stack overflow"),
Error::InvalidConversionToInt => write!(f, "Invalid conversion to integer"),
Error::Panic(ref msg) => write!(f, "Panic: {}", msg),
}
}
Expand Down Expand Up @@ -649,7 +673,7 @@ impl<'a> Runtime<'a> {

mod ext_impl {

use wasmi::{Externals, RuntimeArgs, RuntimeValue, Error, Trap};
use wasmi::{Externals, RuntimeArgs, RuntimeValue, Trap};
use env::ids::*;

macro_rules! void {
Expand Down

0 comments on commit 2bf064d

Please sign in to comment.