revm_interpreter/
interpreter.rs

1//! Core interpreter implementation and components.
2
3/// Extended bytecode functionality.
4pub mod ext_bytecode;
5mod input;
6mod loop_control;
7mod return_data;
8mod runtime_flags;
9mod shared_memory;
10mod stack;
11
12// re-exports
13pub use ext_bytecode::ExtBytecode;
14pub use input::InputsImpl;
15pub use return_data::ReturnDataImpl;
16pub use runtime_flags::RuntimeFlags;
17pub use shared_memory::{num_words, resize_memory, SharedMemory};
18pub use stack::{Stack, STACK_LIMIT};
19
20// imports
21use crate::{
22    host::DummyHost, instruction_context::InstructionContext, interpreter_types::*, Gas, Host,
23    InstructionResult, InstructionTable, InterpreterAction,
24};
25use bytecode::Bytecode;
26use primitives::{hardfork::SpecId, Bytes};
27
28/// Main interpreter structure that contains all components defined in [`InterpreterTypes`].
29#[derive(Debug, Clone)]
30#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
31pub struct Interpreter<WIRE: InterpreterTypes = EthInterpreter> {
32    /// Bytecode being executed.
33    pub bytecode: WIRE::Bytecode,
34    /// Gas tracking for execution costs.
35    pub gas: Gas,
36    /// EVM stack for computation.
37    pub stack: WIRE::Stack,
38    /// Buffer for return data from calls.
39    pub return_data: WIRE::ReturnData,
40    /// EVM memory for data storage.
41    pub memory: WIRE::Memory,
42    /// Input data for current execution context.
43    pub input: WIRE::Input,
44    /// Runtime flags controlling execution behavior.
45    pub runtime_flag: WIRE::RuntimeFlag,
46    /// Extended functionality and customizations.
47    pub extend: WIRE::Extend,
48}
49
50impl<EXT: Default> Interpreter<EthInterpreter<EXT>> {
51    /// Create new interpreter
52    pub fn new(
53        memory: SharedMemory,
54        bytecode: ExtBytecode,
55        input: InputsImpl,
56        is_static: bool,
57        spec_id: SpecId,
58        gas_limit: u64,
59    ) -> Self {
60        Self::new_inner(
61            Stack::new(),
62            memory,
63            bytecode,
64            input,
65            is_static,
66            spec_id,
67            gas_limit,
68        )
69    }
70
71    /// Create a new interpreter with default extended functionality.
72    pub fn default_ext() -> Self {
73        Self::do_default(Stack::new(), SharedMemory::new())
74    }
75
76    /// Create a new invalid interpreter.
77    pub fn invalid() -> Self {
78        Self::do_default(Stack::invalid(), SharedMemory::invalid())
79    }
80
81    fn do_default(stack: Stack, memory: SharedMemory) -> Self {
82        Self::new_inner(
83            stack,
84            memory,
85            ExtBytecode::default(),
86            InputsImpl::default(),
87            false,
88            SpecId::default(),
89            u64::MAX,
90        )
91    }
92
93    #[allow(clippy::too_many_arguments)]
94    fn new_inner(
95        stack: Stack,
96        memory: SharedMemory,
97        bytecode: ExtBytecode,
98        input: InputsImpl,
99        is_static: bool,
100        spec_id: SpecId,
101        gas_limit: u64,
102    ) -> Self {
103        Self {
104            bytecode,
105            gas: Gas::new(gas_limit),
106            stack,
107            return_data: Default::default(),
108            memory,
109            input,
110            runtime_flag: RuntimeFlags { is_static, spec_id },
111            extend: Default::default(),
112        }
113    }
114
115    /// Clears and reinitializes the interpreter with new parameters.
116    #[allow(clippy::too_many_arguments)]
117    pub fn clear(
118        &mut self,
119        memory: SharedMemory,
120        bytecode: ExtBytecode,
121        input: InputsImpl,
122        is_static: bool,
123        spec_id: SpecId,
124        gas_limit: u64,
125    ) {
126        let Self {
127            bytecode: bytecode_ref,
128            gas,
129            stack,
130            return_data,
131            memory: memory_ref,
132            input: input_ref,
133            runtime_flag,
134            extend,
135        } = self;
136        *bytecode_ref = bytecode;
137        *gas = Gas::new(gas_limit);
138        if stack.data().capacity() == 0 {
139            *stack = Stack::new();
140        } else {
141            stack.clear();
142        }
143        return_data.0.clear();
144        *memory_ref = memory;
145        *input_ref = input;
146        *runtime_flag = RuntimeFlags { spec_id, is_static };
147        *extend = EXT::default();
148    }
149
150    /// Sets the bytecode that is going to be executed
151    pub fn with_bytecode(mut self, bytecode: Bytecode) -> Self {
152        self.bytecode = ExtBytecode::new(bytecode);
153        self
154    }
155
156    /// Sets the specid for the interpreter.
157    pub fn set_spec_id(&mut self, spec_id: SpecId) {
158        self.runtime_flag.spec_id = spec_id;
159    }
160}
161
162impl Default for Interpreter<EthInterpreter> {
163    fn default() -> Self {
164        Self::default_ext()
165    }
166}
167
168/// Default types for Ethereum interpreter.
169#[derive(Debug)]
170pub struct EthInterpreter<EXT = (), MG = SharedMemory> {
171    _phantom: core::marker::PhantomData<fn() -> (EXT, MG)>,
172}
173
174impl<EXT> InterpreterTypes for EthInterpreter<EXT> {
175    type Stack = Stack;
176    type Memory = SharedMemory;
177    type Bytecode = ExtBytecode;
178    type ReturnData = ReturnDataImpl;
179    type Input = InputsImpl;
180    type RuntimeFlag = RuntimeFlags;
181    type Extend = EXT;
182    type Output = InterpreterAction;
183}
184
185impl<IW: InterpreterTypes> Interpreter<IW> {
186    /// Performs EVM memory resize.
187    #[inline]
188    #[must_use]
189    pub fn resize_memory(&mut self, offset: usize, len: usize) -> bool {
190        resize_memory(&mut self.gas, &mut self.memory, offset, len)
191    }
192
193    /// Takes the next action from the control and returns it.
194    #[inline]
195    pub fn take_next_action(&mut self) -> InterpreterAction {
196        // Return next action if it is some.
197        core::mem::take(self.bytecode.action()).expect("Interpreter to set action")
198    }
199
200    /// Halt the interpreter with the given result.
201    ///
202    /// This will set the action to [`InterpreterAction::Return`] and set the gas to the current gas.
203    #[cold]
204    #[inline(never)]
205    pub fn halt(&mut self, result: InstructionResult) {
206        self.bytecode
207            .set_action(InterpreterAction::new_halt(result, self.gas));
208    }
209
210    /// Return with the given output.
211    ///
212    /// This will set the action to [`InterpreterAction::Return`] and set the gas to the current gas.
213    pub fn return_with_output(&mut self, output: Bytes) {
214        self.bytecode.set_action(InterpreterAction::new_return(
215            InstructionResult::Return,
216            output,
217            self.gas,
218        ));
219    }
220
221    /// Executes the instruction at the current instruction pointer.
222    ///
223    /// Internally it will increment instruction pointer by one.
224    #[inline]
225    pub fn step<H: ?Sized>(&mut self, instruction_table: &InstructionTable<IW, H>, host: &mut H) {
226        let context = InstructionContext {
227            interpreter: self,
228            host,
229        };
230        context.step(instruction_table);
231    }
232
233    /// Executes the instruction at the current instruction pointer.
234    ///
235    /// Internally it will increment instruction pointer by one.
236    ///
237    /// This uses dummy Host.
238    #[inline]
239    pub fn step_dummy(&mut self, instruction_table: &InstructionTable<IW, DummyHost>) {
240        let context = InstructionContext {
241            interpreter: self,
242            host: &mut DummyHost,
243        };
244        context.step(instruction_table);
245    }
246
247    /// Executes the interpreter until it returns or stops.
248    #[inline]
249    pub fn run_plain<H: ?Sized>(
250        &mut self,
251        instruction_table: &InstructionTable<IW, H>,
252        host: &mut H,
253    ) -> InterpreterAction {
254        while self.bytecode.is_not_end() {
255            // Get current opcode.
256            let opcode = self.bytecode.opcode();
257
258            // SAFETY: In analysis we are doing padding of bytecode so that we are sure that last
259            // byte instruction is STOP so we are safe to just increment program_counter bcs on last instruction
260            // it will do noop and just stop execution of this contract
261            self.bytecode.relative_jump(1);
262            let context = InstructionContext {
263                interpreter: self,
264                host,
265            };
266            // Execute instruction.
267            instruction_table[opcode as usize](context);
268        }
269        self.bytecode.revert_to_previous_pointer();
270
271        self.take_next_action()
272    }
273}
274
275/// The result of an interpreter operation.
276#[derive(Clone, Debug, PartialEq, Eq)]
277#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
278pub struct InterpreterResult {
279    /// The result of the instruction execution.
280    pub result: InstructionResult,
281    /// The output of the instruction execution.
282    pub output: Bytes,
283    /// The gas usage information.
284    pub gas: Gas,
285}
286
287impl InterpreterResult {
288    /// Returns a new `InterpreterResult` with the given values.
289    pub fn new(result: InstructionResult, output: Bytes, gas: Gas) -> Self {
290        Self {
291            result,
292            output,
293            gas,
294        }
295    }
296
297    /// Returns whether the instruction result is a success.
298    #[inline]
299    pub const fn is_ok(&self) -> bool {
300        self.result.is_ok()
301    }
302
303    /// Returns whether the instruction result is a revert.
304    #[inline]
305    pub const fn is_revert(&self) -> bool {
306        self.result.is_revert()
307    }
308
309    /// Returns whether the instruction result is an error.
310    #[inline]
311    pub const fn is_error(&self) -> bool {
312        self.result.is_error()
313    }
314}
315
316// Special implementation for types where Output can be created from InterpreterAction
317impl<IW: InterpreterTypes> Interpreter<IW>
318where
319    IW::Output: From<InterpreterAction>,
320{
321    /// Takes the next action from the control and returns it as the specific Output type.
322    #[inline]
323    pub fn take_next_action_as_output(&mut self) -> IW::Output {
324        From::from(self.take_next_action())
325    }
326
327    /// Executes the interpreter until it returns or stops, returning the specific Output type.
328    #[inline]
329    pub fn run_plain_as_output<H: Host + ?Sized>(
330        &mut self,
331        instruction_table: &InstructionTable<IW, H>,
332        host: &mut H,
333    ) -> IW::Output {
334        From::from(self.run_plain(instruction_table, host))
335    }
336}
337
338#[cfg(test)]
339mod tests {
340    #[test]
341    #[cfg(feature = "serde")]
342    fn test_interpreter_serde() {
343        use super::*;
344        use bytecode::Bytecode;
345        use primitives::Bytes;
346
347        let bytecode = Bytecode::new_raw(Bytes::from(&[0x60, 0x00, 0x60, 0x00, 0x01][..]));
348        let interpreter = Interpreter::<EthInterpreter>::new(
349            SharedMemory::new(),
350            ExtBytecode::new(bytecode),
351            InputsImpl::default(),
352            false,
353            SpecId::default(),
354            u64::MAX,
355        );
356
357        let serialized =
358            bincode::serde::encode_to_vec(&interpreter, bincode::config::legacy()).unwrap();
359
360        let deserialized: Interpreter<EthInterpreter> =
361            bincode::serde::decode_from_slice(&serialized, bincode::config::legacy())
362                .unwrap()
363                .0;
364
365        assert_eq!(
366            interpreter.bytecode.pc(),
367            deserialized.bytecode.pc(),
368            "Program counter should be preserved"
369        );
370    }
371}