2017-07-10 17:42:10 +02:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Wasm Interpreter
|
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
extern crate vm;
|
2018-01-10 13:35:18 +01:00
|
|
|
extern crate ethereum_types;
|
2017-08-01 12:37:57 +02:00
|
|
|
#[macro_use] extern crate log;
|
2017-08-01 13:33:49 +02:00
|
|
|
extern crate ethcore_logger;
|
2017-08-01 12:37:57 +02:00
|
|
|
extern crate byteorder;
|
|
|
|
extern crate parity_wasm;
|
2018-01-15 15:24:24 +01:00
|
|
|
extern crate libc;
|
2017-08-01 12:37:57 +02:00
|
|
|
extern crate wasm_utils;
|
|
|
|
|
2017-07-10 17:42:10 +02:00
|
|
|
mod runtime;
|
|
|
|
mod ptr;
|
|
|
|
mod result;
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
mod env;
|
2017-11-15 10:49:07 +01:00
|
|
|
mod panic_payload;
|
2017-07-10 17:42:10 +02:00
|
|
|
|
|
|
|
const DEFAULT_STACK_SPACE: u32 = 5 * 1024 * 1024;
|
|
|
|
|
|
|
|
use parity_wasm::{interpreter, elements};
|
|
|
|
use parity_wasm::interpreter::ModuleInstanceInterface;
|
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
use vm::{GasLeft, ReturnData, ActionParams};
|
2017-09-10 18:02:31 +02:00
|
|
|
use self::runtime::{Runtime, RuntimeContext, UserTrap};
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2017-09-10 18:02:31 +02:00
|
|
|
pub use self::runtime::InterpreterError;
|
2017-07-10 17:42:10 +02:00
|
|
|
|
|
|
|
const DEFAULT_RESULT_BUFFER: usize = 1024;
|
|
|
|
|
2017-09-10 18:02:31 +02:00
|
|
|
/// Wrapped interpreter error
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Error(InterpreterError);
|
|
|
|
|
|
|
|
impl From<InterpreterError> for Error {
|
|
|
|
fn from(e: InterpreterError) -> Self {
|
|
|
|
Error(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Error> for vm::Error {
|
|
|
|
fn from(e: Error) -> Self {
|
|
|
|
vm::Error::Wasm(format!("Wasm runtime error: {:?}", e.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<UserTrap> for vm::Error {
|
|
|
|
fn from(e: UserTrap) -> Self { e.into() }
|
|
|
|
}
|
|
|
|
|
2017-07-10 17:42:10 +02:00
|
|
|
/// Wasm interpreter instance
|
|
|
|
pub struct WasmInterpreter {
|
2017-09-10 18:02:31 +02:00
|
|
|
program: runtime::InterpreterProgramInstance,
|
2017-07-10 17:42:10 +02:00
|
|
|
result: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WasmInterpreter {
|
|
|
|
/// New wasm interpreter instance
|
2017-09-10 18:02:31 +02:00
|
|
|
pub fn new() -> Result<WasmInterpreter, Error> {
|
2017-07-10 17:42:10 +02:00
|
|
|
Ok(WasmInterpreter {
|
|
|
|
program: interpreter::ProgramInstance::new()?,
|
|
|
|
result: Vec::with_capacity(DEFAULT_RESULT_BUFFER),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
impl vm::Vm for WasmInterpreter {
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
fn exec(&mut self, params: ActionParams, ext: &mut vm::Ext) -> vm::Result<GasLeft> {
|
2017-07-10 17:42:10 +02:00
|
|
|
use parity_wasm::elements::Deserialize;
|
|
|
|
|
|
|
|
let code = params.code.expect("exec is only called on contract with code; qed");
|
|
|
|
|
|
|
|
trace!(target: "wasm", "Started wasm interpreter with code.len={:?}", code.len());
|
|
|
|
|
|
|
|
let env_instance = self.program.module("env")
|
|
|
|
// prefer explicit panic here
|
|
|
|
.expect("Wasm program to contain env module");
|
|
|
|
|
|
|
|
let env_memory = env_instance.memory(interpreter::ItemIndex::Internal(0))
|
|
|
|
// prefer explicit panic here
|
|
|
|
.expect("Linear memory to exist in wasm runtime");
|
|
|
|
|
|
|
|
if params.gas > ::std::u64::MAX.into() {
|
2017-08-01 12:37:57 +02:00
|
|
|
return Err(vm::Error::Wasm("Wasm interpreter cannot run contracts with gas >= 2^64".to_owned()));
|
2017-07-10 17:42:10 +02:00
|
|
|
}
|
2017-07-12 13:09:17 +02:00
|
|
|
|
2017-07-10 17:42:10 +02:00
|
|
|
let mut runtime = Runtime::with_params(
|
|
|
|
ext,
|
|
|
|
env_memory,
|
|
|
|
DEFAULT_STACK_SPACE,
|
|
|
|
params.gas.low_u64(),
|
2017-10-04 13:15:59 +02:00
|
|
|
RuntimeContext {
|
|
|
|
address: params.address,
|
|
|
|
sender: params.sender,
|
|
|
|
origin: params.origin,
|
2017-12-22 19:54:35 +01:00
|
|
|
code_address: params.code_address,
|
2017-10-04 13:15:59 +02:00
|
|
|
value: params.value.value(),
|
|
|
|
},
|
2017-08-20 06:02:59 +02:00
|
|
|
&self.program,
|
2017-07-10 17:42:10 +02:00
|
|
|
);
|
|
|
|
|
2017-11-02 12:49:57 +01:00
|
|
|
let (mut cursor, data_position) = match params.params_type {
|
|
|
|
vm::ParamsType::Embedded => {
|
|
|
|
let module_size = parity_wasm::peek_size(&*code);
|
|
|
|
(
|
|
|
|
::std::io::Cursor::new(&code[..module_size]),
|
|
|
|
module_size
|
|
|
|
)
|
|
|
|
},
|
|
|
|
vm::ParamsType::Separate => {
|
|
|
|
(::std::io::Cursor::new(&code[..]), 0)
|
|
|
|
},
|
|
|
|
};
|
2017-07-10 17:42:10 +02:00
|
|
|
|
|
|
|
let contract_module = wasm_utils::inject_gas_counter(
|
|
|
|
elements::Module::deserialize(
|
|
|
|
&mut cursor
|
|
|
|
).map_err(|err| {
|
2017-08-01 12:37:57 +02:00
|
|
|
vm::Error::Wasm(format!("Error deserializing contract code ({:?})", err))
|
2017-10-09 13:12:58 +02:00
|
|
|
})?,
|
|
|
|
runtime.gas_rules(),
|
2017-07-10 17:42:10 +02:00
|
|
|
);
|
|
|
|
|
2017-10-09 13:12:58 +02:00
|
|
|
let data_section_length = contract_module.data_section()
|
|
|
|
.map(|section| section.entries().iter().fold(0, |sum, entry| sum + entry.value().len()))
|
|
|
|
.unwrap_or(0)
|
|
|
|
as u64;
|
|
|
|
|
|
|
|
let static_segment_cost = data_section_length * runtime.ext().schedule().wasm.static_region as u64;
|
|
|
|
runtime.charge(|_| static_segment_cost).map_err(Error)?;
|
|
|
|
|
2017-11-02 12:49:57 +01:00
|
|
|
let d_ptr = {
|
|
|
|
match params.params_type {
|
|
|
|
vm::ParamsType::Embedded => {
|
|
|
|
runtime.write_descriptor(
|
|
|
|
if data_position < code.len() { &code[data_position..] } else { &[] }
|
|
|
|
).map_err(Error)?
|
|
|
|
},
|
|
|
|
vm::ParamsType::Separate => {
|
|
|
|
runtime.write_descriptor(¶ms.data.unwrap_or_default())
|
|
|
|
.map_err(Error)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-07-10 17:42:10 +02:00
|
|
|
|
|
|
|
{
|
2017-08-20 06:02:59 +02:00
|
|
|
let execution_params = runtime.execution_params()
|
|
|
|
.add_argument(interpreter::RuntimeValue::I32(d_ptr.as_raw() as i32));
|
2017-07-12 13:09:17 +02:00
|
|
|
|
2017-07-10 17:42:10 +02:00
|
|
|
let module_instance = self.program.add_module("contract", contract_module, Some(&execution_params.externals))
|
|
|
|
.map_err(|err| {
|
|
|
|
trace!(target: "wasm", "Error adding contract module: {:?}", err);
|
2017-09-10 18:02:31 +02:00
|
|
|
vm::Error::from(Error(err))
|
2017-07-10 17:42:10 +02:00
|
|
|
})?;
|
|
|
|
|
2017-09-10 18:02:31 +02:00
|
|
|
match module_instance.execute_export("_call", execution_params) {
|
|
|
|
Ok(_) => { },
|
|
|
|
Err(interpreter::Error::User(UserTrap::Suicide)) => { },
|
|
|
|
Err(err) => {
|
2017-07-10 17:42:10 +02:00
|
|
|
trace!(target: "wasm", "Error executing contract: {:?}", err);
|
2017-09-10 18:02:31 +02:00
|
|
|
return Err(vm::Error::from(Error(err)))
|
|
|
|
}
|
|
|
|
}
|
2017-07-10 17:42:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let result = result::WasmResult::new(d_ptr);
|
2017-09-10 18:02:31 +02:00
|
|
|
if result.peek_empty(&*runtime.memory()).map_err(|e| Error(e))? {
|
2017-07-10 17:42:10 +02:00
|
|
|
trace!(target: "wasm", "Contract execution result is empty.");
|
|
|
|
Ok(GasLeft::Known(runtime.gas_left()?.into()))
|
|
|
|
} else {
|
|
|
|
self.result.clear();
|
|
|
|
// todo: use memory views to avoid copy
|
2017-09-10 18:02:31 +02:00
|
|
|
self.result.extend(result.pop(&*runtime.memory()).map_err(|e| Error(e.into()))?);
|
2017-07-10 17:42:10 +02:00
|
|
|
let len = self.result.len();
|
2017-07-12 13:09:17 +02:00
|
|
|
Ok(GasLeft::NeedsReturn {
|
2017-09-10 18:02:31 +02:00
|
|
|
gas_left: runtime.gas_left().map_err(|e| Error(e.into()))?.into(),
|
2017-07-10 17:42:10 +02:00
|
|
|
data: ReturnData::new(
|
2017-07-12 13:09:17 +02:00
|
|
|
::std::mem::replace(&mut self.result, Vec::with_capacity(DEFAULT_RESULT_BUFFER)),
|
|
|
|
0,
|
2017-07-10 17:42:10 +02:00
|
|
|
len,
|
|
|
|
),
|
|
|
|
apply_state: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|