From f4c0337af7b70e6fec25dff7451b8aebcbcdbd9d Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Mon, 10 Oct 2022 23:46:45 -0700 Subject: [PATCH] Interpreter feature to configure debug offsets --- evm/src/cpu/kernel/interpreter.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/evm/src/cpu/kernel/interpreter.rs b/evm/src/cpu/kernel/interpreter.rs index 2eb9dcb9..a85f3db9 100644 --- a/evm/src/cpu/kernel/interpreter.rs +++ b/evm/src/cpu/kernel/interpreter.rs @@ -75,6 +75,7 @@ pub struct Interpreter<'a> { pub(crate) generation_state: GenerationState, prover_inputs_map: &'a HashMap, pub(crate) halt_offsets: Vec, + pub(crate) debug_offsets: Vec, running: bool, } @@ -128,6 +129,7 @@ impl<'a> Interpreter<'a> { prover_inputs_map: prover_inputs, context: 0, halt_offsets: vec![DEFAULT_HALT_OFFSET], + debug_offsets: vec![], running: false, } } @@ -283,7 +285,7 @@ impl<'a> Interpreter<'a> { 0x55 => todo!(), // "SSTORE", 0x56 => self.run_jump(), // "JUMP", 0x57 => self.run_jumpi(), // "JUMPI", - 0x58 => todo!(), // "GETPC", + 0x58 => self.run_pc(), // "PC", 0x59 => self.run_msize(), // "MSIZE", 0x5a => todo!(), // "GAS", 0x5b => self.run_jumpdest(), // "JUMPDEST", @@ -318,9 +320,24 @@ impl<'a> Interpreter<'a> { 0xff => todo!(), // "SELFDESTRUCT", _ => bail!("Unrecognized opcode {}.", opcode), }; + + if self.debug_offsets.contains(&self.offset) { + println!("At {}, stack={:?}", self.offset_name(), self.stack()); + } + Ok(()) } + /// Get a string representation of the current offset for debugging purposes. + fn offset_name(&self) -> String { + // TODO: Not sure we should use KERNEL? Interpreter is more general in other places. + let label = KERNEL + .global_labels + .iter() + .find_map(|(k, v)| (*v == self.offset).then(|| k.clone())); + label.unwrap_or_else(|| self.offset.to_string()) + } + fn run_stop(&mut self) { self.running = false; } @@ -476,6 +493,7 @@ impl<'a> Interpreter<'a> { let bytes = (offset..offset + size) .map(|i| self.memory.mload_general(context, segment, i).byte(0)) .collect::>(); + println!("Hashing {:?}", &bytes); let hash = keccak(bytes); self.push(U256::from_big_endian(hash.as_bytes())); } @@ -544,6 +562,10 @@ impl<'a> Interpreter<'a> { } } + fn run_pc(&mut self) { + self.push((self.offset - 1).into()); + } + fn run_msize(&mut self) { let num_bytes = self.memory.context_memory[self.context].segments [Segment::MainMemory as usize]