Skip to content

Commit 61b566a

Browse files
0xRVEpgherveougithub-actions[bot]Robert van Eerdewijkxermicus
authored
added trace logging in EVM interpreter loop (#9561)
Added trace logging for each instruction to evm::run function. solves #9575 --------- Signed-off-by: xermicus <cyrill@parity.io> Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com> Co-authored-by: pgherveou <pgherveou@gmail.com> Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Robert van Eerdewijk <robert@Roberts-MacBook-Pro.local> Co-authored-by: xermicus <cyrill@parity.io> Co-authored-by: Alexander Theißen <alex.theissen@me.com> Co-authored-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
1 parent 1cbf4ee commit 61b566a

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

prdoc/pr_9561.prdoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title: added trace logging in EVM interpreter loop
2+
doc:
3+
- audience: Runtime Dev
4+
description: |-
5+
Added trace logging for each instruction to evm::run function.
6+
solves https://github.com/paritytech/polkadot-sdk/issues/9575
7+
crates:
8+
- name: pallet-revive
9+
bump: patch

substrate/frame/revive/src/vm/evm.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ fn run<'a, E: Ext>(
156156
) -> InterpreterResult {
157157
let host = &mut DummyHost {};
158158
loop {
159+
#[cfg(not(feature = "std"))]
159160
let action = interpreter.run_plain(table, host);
161+
#[cfg(feature = "std")]
162+
let action = run_plain(interpreter, table, host);
160163
match action {
161164
InterpreterAction::Return(result) => {
162165
log::trace!(target: LOG_TARGET, "Evm return {:?}", result);
@@ -242,6 +245,52 @@ fn run_call<'a, E: Ext>(
242245
}
243246
}
244247

248+
/// Re-implementation of REVM run_plain function to add trace logging to our EVM interpreter loop.
249+
/// NB: copied directly from revm tag v82
250+
#[cfg(feature = "std")]
251+
fn run_plain<WIRE: InterpreterTypes>(
252+
interpreter: &mut Interpreter<WIRE>,
253+
instruction_table: &revm::interpreter::InstructionTable<WIRE, DummyHost>,
254+
host: &mut DummyHost,
255+
) -> InterpreterAction {
256+
use crate::{alloc::string::ToString, format};
257+
use revm::{
258+
bytecode::OpCode,
259+
interpreter::{
260+
instruction_context::InstructionContext,
261+
interpreter_types::{Jumps, LoopControl, MemoryTr, StackTr},
262+
},
263+
};
264+
while interpreter.bytecode.is_not_end() {
265+
log::trace!(target: LOG_TARGET,
266+
"[{pc}]: {opcode}, stacktop: {stacktop}, memory size: {memsize} {memory:?}",
267+
pc = interpreter.bytecode.pc(),
268+
opcode = OpCode::new(interpreter.bytecode.opcode())
269+
.map_or("INVALID".to_string(), |x| format!("{:?}", x.info())),
270+
stacktop = interpreter.stack.top().map_or("None".to_string(), |x| format!("{:#x}", x)),
271+
memsize = interpreter.memory.size(),
272+
// printing at most the first 32 bytes of memory
273+
memory = interpreter
274+
.memory
275+
.slice_len(0, core::cmp::min(32, interpreter.memory.size()))
276+
.to_vec(),
277+
);
278+
// Get current opcode.
279+
let opcode = interpreter.bytecode.opcode();
280+
281+
// SAFETY: In analysis we are doing padding of bytecode so that we are sure that last
282+
// byte instruction is STOP so we are safe to just increment program_counter bcs on last
283+
// instruction it will do noop and just stop execution of this contract
284+
interpreter.bytecode.relative_jump(1);
285+
let context = InstructionContext { interpreter, host };
286+
// Execute instruction.
287+
instruction_table[opcode as usize](context);
288+
}
289+
interpreter.bytecode.revert_to_previous_pointer();
290+
291+
interpreter.take_next_action()
292+
}
293+
245294
fn run_create<'a, E: Ext>(
246295
interpreter: &mut Interpreter<EVMInterpreter<'a, E>>,
247296
create_input: Box<CreateInputs>,

0 commit comments

Comments
 (0)