diff --git a/runtime/src/eval/mod.rs b/runtime/src/eval/mod.rs index 855ff9a9a..c65370fca 100644 --- a/runtime/src/eval/mod.rs +++ b/runtime/src/eval/mod.rs @@ -14,14 +14,14 @@ pub enum Control { Exit(ExitReason), } -fn handle_other(state: &mut Runtime<'_>, opcode: Opcode, handler: &mut H) -> Control { +fn handle_other(state: &mut Runtime, opcode: Opcode, handler: &mut H) -> Control { match handler.other(opcode, &mut state.machine) { Ok(()) => Control::Continue, Err(e) => Control::Exit(e.into()), } } -pub fn eval(state: &mut Runtime<'_>, opcode: Opcode, handler: &mut H) -> Control { +pub fn eval(state: &mut Runtime, opcode: Opcode, handler: &mut H) -> Control { match opcode { Opcode::SHA3 => system::sha3(state), Opcode::ADDRESS => system::address(state), @@ -64,7 +64,7 @@ pub fn eval(state: &mut Runtime<'_>, opcode: Opcode, handler: &mut H } pub fn finish_create( - runtime: &mut Runtime<'_>, + runtime: &mut Runtime, reason: ExitReason, address: Option, return_data: Vec, @@ -93,7 +93,7 @@ pub fn finish_create( } pub fn finish_call( - runtime: &mut Runtime<'_>, + runtime: &mut Runtime, out_len: U256, out_offset: U256, reason: ExitReason, diff --git a/runtime/src/eval/system.rs b/runtime/src/eval/system.rs index bf6736481..574355437 100644 --- a/runtime/src/eval/system.rs +++ b/runtime/src/eval/system.rs @@ -7,7 +7,7 @@ use alloc::vec::Vec; use primitive_types::{H256, U256}; use sha3::{Digest, Keccak256}; -pub fn sha3(runtime: &mut Runtime<'_>) -> Control { +pub fn sha3(runtime: &mut Runtime) -> Control { pop_u256!(runtime, from, len); try_or_fail!(runtime.machine.memory_mut().resize_offset(from, len)); @@ -26,47 +26,47 @@ pub fn sha3(runtime: &mut Runtime<'_>) -> Control { Control::Continue } -pub fn chainid(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn chainid(runtime: &mut Runtime, handler: &H) -> Control { push_u256!(runtime, handler.chain_id()); Control::Continue } -pub fn address(runtime: &mut Runtime<'_>) -> Control { +pub fn address(runtime: &mut Runtime) -> Control { let ret = H256::from(runtime.context.address); push!(runtime, ret); Control::Continue } -pub fn balance(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn balance(runtime: &mut Runtime, handler: &H) -> Control { pop!(runtime, address); push_u256!(runtime, handler.balance(address.into())); Control::Continue } -pub fn selfbalance(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn selfbalance(runtime: &mut Runtime, handler: &H) -> Control { push_u256!(runtime, handler.balance(runtime.context.address)); Control::Continue } -pub fn origin(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn origin(runtime: &mut Runtime, handler: &H) -> Control { let ret = H256::from(handler.origin()); push!(runtime, ret); Control::Continue } -pub fn caller(runtime: &mut Runtime<'_>) -> Control { +pub fn caller(runtime: &mut Runtime) -> Control { let ret = H256::from(runtime.context.caller); push!(runtime, ret); Control::Continue } -pub fn callvalue(runtime: &mut Runtime<'_>) -> Control { +pub fn callvalue(runtime: &mut Runtime) -> Control { let mut ret = H256::default(); runtime.context.apparent_value.to_big_endian(&mut ret[..]); push!(runtime, ret); @@ -74,7 +74,7 @@ pub fn callvalue(runtime: &mut Runtime<'_>) -> Control { Control::Continue } -pub fn gasprice(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn gasprice(runtime: &mut Runtime, handler: &H) -> Control { let mut ret = H256::default(); handler.gas_price().to_big_endian(&mut ret[..]); push!(runtime, ret); @@ -82,7 +82,7 @@ pub fn gasprice(runtime: &mut Runtime<'_>, handler: &H) -> Control(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn base_fee(runtime: &mut Runtime, handler: &H) -> Control { let mut ret = H256::default(); handler.block_base_fee_per_gas().to_big_endian(&mut ret[..]); push!(runtime, ret); @@ -90,21 +90,21 @@ pub fn base_fee(runtime: &mut Runtime<'_>, handler: &H) -> Control(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn extcodesize(runtime: &mut Runtime, handler: &H) -> Control { pop!(runtime, address); push_u256!(runtime, handler.code_size(address.into())); Control::Continue } -pub fn extcodehash(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn extcodehash(runtime: &mut Runtime, handler: &H) -> Control { pop!(runtime, address); push!(runtime, handler.code_hash(address.into())); Control::Continue } -pub fn extcodecopy(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn extcodecopy(runtime: &mut Runtime, handler: &H) -> Control { pop!(runtime, address); pop_u256!(runtime, memory_offset, code_offset, len); @@ -125,14 +125,14 @@ pub fn extcodecopy(runtime: &mut Runtime<'_>, handler: &H) -> Contro Control::Continue } -pub fn returndatasize(runtime: &mut Runtime<'_>) -> Control { +pub fn returndatasize(runtime: &mut Runtime) -> Control { let size = U256::from(runtime.return_data_buffer.len()); push_u256!(runtime, size); Control::Continue } -pub fn returndatacopy(runtime: &mut Runtime<'_>) -> Control { +pub fn returndatacopy(runtime: &mut Runtime) -> Control { pop_u256!(runtime, memory_offset, data_offset, len); try_or_fail!(runtime @@ -158,39 +158,39 @@ pub fn returndatacopy(runtime: &mut Runtime<'_>) -> Control { } } -pub fn blockhash(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn blockhash(runtime: &mut Runtime, handler: &H) -> Control { pop_u256!(runtime, number); push!(runtime, handler.block_hash(number)); Control::Continue } -pub fn coinbase(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn coinbase(runtime: &mut Runtime, handler: &H) -> Control { push!(runtime, handler.block_coinbase().into()); Control::Continue } -pub fn timestamp(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn timestamp(runtime: &mut Runtime, handler: &H) -> Control { push_u256!(runtime, handler.block_timestamp()); Control::Continue } -pub fn number(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn number(runtime: &mut Runtime, handler: &H) -> Control { push_u256!(runtime, handler.block_number()); Control::Continue } -pub fn difficulty(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn difficulty(runtime: &mut Runtime, handler: &H) -> Control { push_u256!(runtime, handler.block_difficulty()); Control::Continue } -pub fn gaslimit(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn gaslimit(runtime: &mut Runtime, handler: &H) -> Control { push_u256!(runtime, handler.block_gas_limit()); Control::Continue } -pub fn sload(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn sload(runtime: &mut Runtime, handler: &H) -> Control { pop!(runtime, index); let value = handler.storage(runtime.context.address, index); push!(runtime, value); @@ -204,7 +204,7 @@ pub fn sload(runtime: &mut Runtime<'_>, handler: &H) -> Control { Control::Continue } -pub fn sstore(runtime: &mut Runtime<'_>, handler: &mut H) -> Control { +pub fn sstore(runtime: &mut Runtime, handler: &mut H) -> Control { pop!(runtime, index, value); event!(SStore { @@ -219,13 +219,13 @@ pub fn sstore(runtime: &mut Runtime<'_>, handler: &mut H) -> Control } } -pub fn gas(runtime: &mut Runtime<'_>, handler: &H) -> Control { +pub fn gas(runtime: &mut Runtime, handler: &H) -> Control { push_u256!(runtime, handler.gas_left()); Control::Continue } -pub fn log(runtime: &mut Runtime<'_>, n: u8, handler: &mut H) -> Control { +pub fn log(runtime: &mut Runtime, n: u8, handler: &mut H) -> Control { pop_u256!(runtime, offset, len); try_or_fail!(runtime.machine.memory_mut().resize_offset(offset, len)); @@ -254,7 +254,7 @@ pub fn log(runtime: &mut Runtime<'_>, n: u8, handler: &mut H) -> Con } } -pub fn suicide(runtime: &mut Runtime<'_>, handler: &mut H) -> Control { +pub fn suicide(runtime: &mut Runtime, handler: &mut H) -> Control { pop!(runtime, target); match handler.mark_delete(runtime.context.address, target.into()) { @@ -265,7 +265,7 @@ pub fn suicide(runtime: &mut Runtime<'_>, handler: &mut H) -> Contro Control::Exit(ExitSucceed::Suicided.into()) } -pub fn create(runtime: &mut Runtime<'_>, is_create2: bool, handler: &mut H) -> Control { +pub fn create(runtime: &mut Runtime, is_create2: bool, handler: &mut H) -> Control { runtime.return_data_buffer = Vec::new(); pop_u256!(runtime, value, code_offset, len); @@ -305,7 +305,7 @@ pub fn create(runtime: &mut Runtime<'_>, is_create2: bool, handler: } } -pub fn call(runtime: &mut Runtime<'_>, scheme: CallScheme, handler: &mut H) -> Control { +pub fn call(runtime: &mut Runtime, scheme: CallScheme, handler: &mut H) -> Control { runtime.return_data_buffer = Vec::new(); pop_u256!(runtime, gas); diff --git a/runtime/src/interrupt.rs b/runtime/src/interrupt.rs index 136cf9a6a..188fc5127 100644 --- a/runtime/src/interrupt.rs +++ b/runtime/src/interrupt.rs @@ -1,31 +1,31 @@ use crate::{Handler, Runtime}; /// Interrupt resolution. -pub enum Resolve<'a, 'config, H: Handler> { +pub enum Resolve<'a, H: Handler> { /// Create interrupt resolution. - Create(H::CreateInterrupt, ResolveCreate<'a, 'config>), + Create(H::CreateInterrupt, ResolveCreate<'a>), /// Call interrupt resolution. - Call(H::CallInterrupt, ResolveCall<'a, 'config>), + Call(H::CallInterrupt, ResolveCall<'a>), } /// Create interrupt resolution. -pub struct ResolveCreate<'a, 'config> { - _runtime: &'a mut Runtime<'config>, +pub struct ResolveCreate<'a> { + _runtime: &'a mut Runtime, } -impl<'a, 'config> ResolveCreate<'a, 'config> { - pub(crate) fn new(runtime: &'a mut Runtime<'config>) -> Self { +impl<'a> ResolveCreate<'a> { + pub(crate) fn new(runtime: &'a mut Runtime) -> Self { Self { _runtime: runtime } } } /// Call interrupt resolution. -pub struct ResolveCall<'a, 'config> { - _runtime: &'a mut Runtime<'config>, +pub struct ResolveCall<'a> { + _runtime: &'a mut Runtime, } -impl<'a, 'config> ResolveCall<'a, 'config> { - pub(crate) fn new(runtime: &'a mut Runtime<'config>) -> Self { +impl<'a> ResolveCall<'a> { + pub(crate) fn new(runtime: &'a mut Runtime) -> Self { Self { _runtime: runtime } } } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 7d8bff7c5..a5d3a54b5 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -107,32 +107,31 @@ macro_rules! step { /// EVM runtime. /// /// The runtime wraps an EVM `Machine` with support of return data and context. -pub struct Runtime<'config> { +pub struct Runtime { machine: Machine, status: Result<(), ExitReason>, return_data_buffer: Vec, return_data_len: U256, return_data_offset: U256, context: Context, - _config: &'config Config, } -impl<'config> Runtime<'config> { +impl Runtime { /// Create a new runtime with given code and data. pub fn new( code: Rc>, data: Rc>, context: Context, - config: &'config Config, + stack_limit: usize, + memory_limit: usize, ) -> Self { Self { - machine: Machine::new(code, data, config.stack_limit, config.memory_limit), + machine: Machine::new(code, data, stack_limit, memory_limit), status: Ok(()), return_data_buffer: Vec::new(), return_data_len: U256::zero(), return_data_offset: U256::zero(), context, - _config: config, } } @@ -150,7 +149,7 @@ impl<'config> Runtime<'config> { pub fn step<'a, H: Handler>( &'a mut self, handler: &mut H, - ) -> Result<(), Capture>> { + ) -> Result<(), Capture>> { step!(self, handler, return Err; Ok) } @@ -158,7 +157,7 @@ impl<'config> Runtime<'config> { pub fn run<'a, H: Handler>( &'a mut self, handler: &mut H, - ) -> Capture> { + ) -> Capture> { loop { step!(self, handler, return;) } diff --git a/src/executor/stack/executor.rs b/src/executor/stack/executor.rs index ad27688e3..b50e68af2 100644 --- a/src/executor/stack/executor.rs +++ b/src/executor/stack/executor.rs @@ -291,7 +291,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> } /// Execute the runtime until it returns. - pub fn execute(&mut self, runtime: &mut Runtime<'config>) -> ExitReason { + pub fn execute(&mut self, runtime: &mut Runtime) -> ExitReason { let mut call_stack = Vec::with_capacity(DEFAULT_CALL_STACK_CAPACITY); call_stack.push(TaggedRuntime { kind: RuntimeKind::Execute, @@ -302,9 +302,9 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> } /// Execute using Runtimes on the call_stack until it returns. - fn execute_with_call_stack<'borrow>( + fn execute_with_call_stack( &mut self, - call_stack: &mut Vec>, + call_stack: &mut Vec, ) -> (ExitReason, Option, Vec) { // This `interrupt_runtime` is used to pass the runtime obtained from the // `Capture::Trap` branch in the match below back to the top of the call stack. @@ -664,7 +664,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> init_code: Vec, target_gas: Option, take_l64: bool, - ) -> Capture<(ExitReason, Option, Vec), StackExecutorCreateInterrupt<'config>> { + ) -> Capture<(ExitReason, Option, Vec), StackExecutorCreateInterrupt<'static>> { macro_rules! try_or_fail { ( $e:expr ) => { match $e { @@ -764,7 +764,8 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Rc::new(init_code), Rc::new(Vec::new()), context, - self.config, + self.config.stack_limit, + self.config.memory_limit, ); Capture::Trap(StackExecutorCreateInterrupt(TaggedRuntime { @@ -784,7 +785,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> take_l64: bool, take_stipend: bool, context: Context, - ) -> Capture<(ExitReason, Vec), StackExecutorCallInterrupt<'config>> { + ) -> Capture<(ExitReason, Vec), StackExecutorCallInterrupt<'static>> { macro_rules! try_or_fail { ( $e:expr ) => { match $e { @@ -892,7 +893,13 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> }; } - let runtime = Runtime::new(Rc::new(code), Rc::new(input), context, self.config); + let runtime = Runtime::new( + Rc::new(code), + Rc::new(input), + context, + self.config.stack_limit, + self.config.memory_limit, + ); Capture::Trap(StackExecutorCallInterrupt(TaggedRuntime { kind: RuntimeKind::Call(code_address), @@ -1001,15 +1008,15 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> } } -pub struct StackExecutorCallInterrupt<'config>(TaggedRuntime<'config, 'config>); -pub struct StackExecutorCreateInterrupt<'config>(TaggedRuntime<'config, 'config>); +pub struct StackExecutorCallInterrupt<'borrow>(TaggedRuntime<'borrow>); +pub struct StackExecutorCreateInterrupt<'borrow>(TaggedRuntime<'borrow>); impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler for StackExecutor<'config, 'precompiles, S, P> { - type CreateInterrupt = StackExecutorCreateInterrupt<'config>; + type CreateInterrupt = StackExecutorCreateInterrupt<'static>; type CreateFeedback = Infallible; - type CallInterrupt = StackExecutorCallInterrupt<'config>; + type CallInterrupt = StackExecutorCallInterrupt<'static>; type CallFeedback = Infallible; fn balance(&self, address: H160) -> U256 { diff --git a/src/executor/stack/tagged_runtime.rs b/src/executor/stack/tagged_runtime.rs index 9af9fa827..c0399f8e4 100644 --- a/src/executor/stack/tagged_runtime.rs +++ b/src/executor/stack/tagged_runtime.rs @@ -5,9 +5,9 @@ use crate::maybe_borrowed::MaybeBorrowed; use crate::Runtime; use primitive_types::H160; -pub struct TaggedRuntime<'config, 'borrow> { +pub struct TaggedRuntime<'borrow> { pub kind: RuntimeKind, - pub inner: MaybeBorrowed<'borrow, Runtime<'config>>, + pub inner: MaybeBorrowed<'borrow, Runtime>, } #[derive(Debug, Clone, Copy, PartialEq, Eq)]