2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-07-10 17:42:10 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-07-10 17:42:10 +02:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-07-10 17:42:10 +02:00
|
|
|
|
|
|
|
//! Wasm Interpreter
|
|
|
|
|
2018-02-05 20:59:27 +01:00
|
|
|
extern crate byteorder;
|
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;
|
2018-01-15 15:24:24 +01:00
|
|
|
extern crate libc;
|
2018-02-05 20:59:27 +01:00
|
|
|
extern crate parity_wasm;
|
|
|
|
extern crate vm;
|
2018-03-26 10:55:48 +02:00
|
|
|
extern crate pwasm_utils as wasm_utils;
|
2018-02-05 20:59:27 +01:00
|
|
|
extern crate wasmi;
|
2017-08-01 12:37:57 +02:00
|
|
|
|
2017-07-10 17:42:10 +02:00
|
|
|
#[cfg(test)]
|
2019-01-08 15:07:20 +01:00
|
|
|
extern crate env_logger;
|
|
|
|
|
2017-07-10 17:42:10 +02:00
|
|
|
mod env;
|
2017-11-15 10:49:07 +01:00
|
|
|
mod panic_payload;
|
2018-02-05 20:59:27 +01:00
|
|
|
mod parser;
|
2019-01-08 15:07:20 +01:00
|
|
|
mod runtime;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2017-08-01 12:37:57 +02:00
|
|
|
use vm::{GasLeft, ReturnData, ActionParams};
|
2018-03-12 12:37:32 +01:00
|
|
|
use wasmi::{Error as InterpreterError, Trap};
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2018-02-05 20:59:27 +01:00
|
|
|
use runtime::{Runtime, RuntimeContext};
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2018-02-05 20:59:27 +01:00
|
|
|
use ethereum_types::U256;
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2017-09-10 18:02:31 +02:00
|
|
|
/// Wrapped interpreter error
|
|
|
|
#[derive(Debug)]
|
2018-03-12 12:37:32 +01:00
|
|
|
pub enum Error {
|
|
|
|
Interpreter(InterpreterError),
|
|
|
|
Trap(Trap),
|
|
|
|
}
|
2017-09-10 18:02:31 +02:00
|
|
|
|
|
|
|
impl From<InterpreterError> for Error {
|
|
|
|
fn from(e: InterpreterError) -> Self {
|
2018-03-12 12:37:32 +01:00
|
|
|
Error::Interpreter(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Trap> for Error {
|
|
|
|
fn from(e: Trap) -> Self {
|
|
|
|
Error::Trap(e)
|
2017-09-10 18:02:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Error> for vm::Error {
|
|
|
|
fn from(e: Error) -> Self {
|
2018-03-12 12:37:32 +01:00
|
|
|
match e {
|
|
|
|
Error::Interpreter(e) => vm::Error::Wasm(format!("Wasm runtime error: {:?}", e)),
|
|
|
|
Error::Trap(e) => vm::Error::Wasm(format!("Wasm contract trap: {:?}", e)),
|
|
|
|
}
|
2017-09-10 18:02:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 17:42:10 +02:00
|
|
|
/// Wasm interpreter instance
|
2018-08-13 22:06:15 +02:00
|
|
|
pub struct WasmInterpreter {
|
|
|
|
params: ActionParams,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WasmInterpreter {
|
|
|
|
pub fn new(params: ActionParams) -> Self {
|
|
|
|
WasmInterpreter { params }
|
|
|
|
}
|
|
|
|
}
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2018-02-05 20:59:27 +01:00
|
|
|
impl From<runtime::Error> for vm::Error {
|
|
|
|
fn from(e: runtime::Error) -> Self {
|
|
|
|
vm::Error::Wasm(format!("Wasm runtime error: {:?}", e))
|
2017-07-10 17:42:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 13:27:56 +01:00
|
|
|
enum ExecutionOutcome {
|
|
|
|
Suicide,
|
|
|
|
Return,
|
|
|
|
NotSpecial,
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:33:19 +02:00
|
|
|
impl WasmInterpreter {
|
|
|
|
pub fn run(self: Box<WasmInterpreter>, ext: &mut vm::Ext) -> vm::Result<GasLeft> {
|
2018-08-13 22:06:15 +02:00
|
|
|
let (module, data) = parser::payload(&self.params, ext.schedule().wasm())?;
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2018-03-12 12:37:32 +01:00
|
|
|
let loaded_module = wasmi::Module::from_parity_wasm_module(module).map_err(Error::Interpreter)?;
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2018-08-06 17:15:52 +02:00
|
|
|
let instantiation_resolver = env::ImportResolver::with_limit(16, ext.schedule().wasm());
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2018-02-05 20:59:27 +01:00
|
|
|
let module_instance = wasmi::ModuleInstance::new(
|
|
|
|
&loaded_module,
|
2018-03-12 12:37:32 +01:00
|
|
|
&wasmi::ImportsBuilder::new().with_resolver("env", &instantiation_resolver)
|
|
|
|
).map_err(Error::Interpreter)?;
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2018-08-13 22:06:15 +02:00
|
|
|
let adjusted_gas = self.params.gas * U256::from(ext.schedule().wasm().opcodes_div) /
|
2018-02-19 12:27:42 +01:00
|
|
|
U256::from(ext.schedule().wasm().opcodes_mul);
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2018-02-05 20:59:27 +01:00
|
|
|
if adjusted_gas > ::std::u64::MAX.into()
|
|
|
|
{
|
|
|
|
return Err(vm::Error::Wasm("Wasm interpreter cannot run contracts with gas (wasm adjusted) >= 2^64".to_owned()));
|
2017-07-10 17:42:10 +02:00
|
|
|
}
|
2017-07-12 13:09:17 +02:00
|
|
|
|
2018-03-12 12:37:32 +01:00
|
|
|
let initial_memory = instantiation_resolver.memory_size().map_err(Error::Interpreter)?;
|
2018-02-05 20:59:27 +01:00
|
|
|
trace!(target: "wasm", "Contract requested {:?} pages of initial memory", initial_memory);
|
|
|
|
|
|
|
|
let (gas_left, result) = {
|
|
|
|
let mut runtime = Runtime::with_params(
|
|
|
|
ext,
|
2018-03-12 12:37:32 +01:00
|
|
|
instantiation_resolver.memory_ref(),
|
2018-02-05 20:59:27 +01:00
|
|
|
// cannot overflow, checked above
|
|
|
|
adjusted_gas.low_u64(),
|
|
|
|
data.to_vec(),
|
|
|
|
RuntimeContext {
|
2018-08-13 22:06:15 +02:00
|
|
|
address: self.params.address,
|
|
|
|
sender: self.params.sender,
|
|
|
|
origin: self.params.origin,
|
|
|
|
code_address: self.params.code_address,
|
|
|
|
value: self.params.value.value(),
|
2017-11-02 12:49:57 +01:00
|
|
|
},
|
2018-02-05 20:59:27 +01:00
|
|
|
);
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2018-02-05 20:59:27 +01:00
|
|
|
// cannot overflow if static_region < 2^16,
|
|
|
|
// initial_memory ∈ [0..2^32)
|
|
|
|
// total_charge <- static_region * 2^32 * 2^16
|
|
|
|
// total_charge ∈ [0..2^64) if static_region ∈ [0..2^16)
|
|
|
|
// qed
|
2018-02-19 12:27:42 +01:00
|
|
|
assert!(runtime.schedule().wasm().initial_mem < 1 << 16);
|
|
|
|
runtime.charge(|s| initial_memory as u64 * s.wasm().initial_mem as u64)?;
|
2017-07-12 13:09:17 +02:00
|
|
|
|
2018-03-12 12:37:32 +01:00
|
|
|
let module_instance = module_instance.run_start(&mut runtime).map_err(Error::Trap)?;
|
|
|
|
|
|
|
|
let invoke_result = module_instance.invoke_export("call", &[], &mut runtime);
|
|
|
|
|
2018-03-14 13:27:56 +01:00
|
|
|
let mut execution_outcome = ExecutionOutcome::NotSpecial;
|
2018-03-12 12:37:32 +01:00
|
|
|
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");
|
|
|
|
|
2018-03-14 13:27:56 +01:00
|
|
|
match **runtime_err {
|
|
|
|
runtime::Error::Suicide => { execution_outcome = ExecutionOutcome::Suicide; },
|
|
|
|
runtime::Error::Return => { execution_outcome = ExecutionOutcome::Return; },
|
|
|
|
_ => {}
|
|
|
|
}
|
2018-03-12 12:37:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 13:27:56 +01:00
|
|
|
if let (ExecutionOutcome::NotSpecial, Err(e)) = (execution_outcome, invoke_result) {
|
|
|
|
trace!(target: "wasm", "Error executing contract: {:?}", e);
|
|
|
|
return Err(vm::Error::from(Error::from(e)));
|
2017-09-10 18:02:31 +02:00
|
|
|
}
|
2018-03-12 12:37:32 +01:00
|
|
|
|
2018-02-05 20:59:27 +01:00
|
|
|
(
|
|
|
|
runtime.gas_left().expect("Cannot fail since it was not updated since last charge"),
|
|
|
|
runtime.into_result(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
let gas_left =
|
2018-02-19 12:27:42 +01:00
|
|
|
U256::from(gas_left) * U256::from(ext.schedule().wasm().opcodes_mul)
|
|
|
|
/ U256::from(ext.schedule().wasm().opcodes_div);
|
2017-07-10 17:42:10 +02:00
|
|
|
|
2018-02-05 20:59:27 +01:00
|
|
|
if result.is_empty() {
|
2017-07-10 17:42:10 +02:00
|
|
|
trace!(target: "wasm", "Contract execution result is empty.");
|
2018-02-05 20:59:27 +01:00
|
|
|
Ok(GasLeft::Known(gas_left))
|
2017-07-10 17:42:10 +02:00
|
|
|
} else {
|
2018-02-05 20:59:27 +01:00
|
|
|
let len = result.len();
|
2017-07-12 13:09:17 +02:00
|
|
|
Ok(GasLeft::NeedsReturn {
|
2018-02-05 20:59:27 +01:00
|
|
|
gas_left: gas_left,
|
2017-07-10 17:42:10 +02:00
|
|
|
data: ReturnData::new(
|
2018-02-05 20:59:27 +01:00
|
|
|
result,
|
2017-07-12 13:09:17 +02:00
|
|
|
0,
|
2017-07-10 17:42:10 +02:00
|
|
|
len,
|
|
|
|
),
|
|
|
|
apply_state: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-02 16:33:19 +02:00
|
|
|
|
|
|
|
impl vm::Exec for WasmInterpreter {
|
|
|
|
fn exec(self: Box<WasmInterpreter>, ext: &mut vm::Ext) -> vm::ExecTrapResult<GasLeft> {
|
|
|
|
Ok(self.run(ext))
|
|
|
|
}
|
|
|
|
}
|