Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forge): add calldata and returndata views to debugger #7110

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 55 additions & 11 deletions crates/debugger/src/tui/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,38 @@ pub(crate) struct DrawMemory {
// TODO
pub(crate) current_startline: RefCell<usize>,
pub(crate) inner_call_index: usize,
pub(crate) current_mem_startline: usize,
pub(crate) current_buf_startline: usize,
pub(crate) current_stack_startline: usize,
}

/// Used to keep track of which buffer is currently active to be drawn by the debugger.
#[derive(Debug, PartialEq)]
pub(crate) enum BufferKind {
Memory,
Calldata,
Returndata,
}

impl BufferKind {
/// Helper to cycle through the active buffers.
pub(crate) fn next(&self) -> Self {
match self {
BufferKind::Memory => BufferKind::Calldata,
BufferKind::Calldata => BufferKind::Returndata,
BufferKind::Returndata => BufferKind::Memory,
}
}

/// Helper to format the title of the active buffer pane
pub(crate) fn title(&self, size: usize) -> String {
match self {
BufferKind::Memory => format!("Memory (max expansion: {} bytes)", size),
BufferKind::Calldata => format!("Calldata (size: {} bytes)", size),
BufferKind::Returndata => format!("Returndata (size: {} bytes)", size),
}
}
}

pub(crate) struct DebuggerContext<'a> {
pub(crate) debugger: &'a mut Debugger,

Expand All @@ -29,8 +57,11 @@ pub(crate) struct DebuggerContext<'a> {
pub(crate) last_index: usize,

pub(crate) stack_labels: bool,
pub(crate) mem_utf: bool,
/// Whether to decode active buffer as utf8 or not.
pub(crate) buf_utf: bool,
pub(crate) show_shortcuts: bool,
/// The currently active buffer (memory, calldata, returndata) to be drawn.
pub(crate) active_buffer: BufferKind,
}

impl<'a> DebuggerContext<'a> {
Expand All @@ -45,8 +76,9 @@ impl<'a> DebuggerContext<'a> {
last_index: 0,

stack_labels: false,
mem_utf: false,
buf_utf: false,
show_shortcuts: true,
active_buffer: BufferKind::Memory,
}
}

Expand Down Expand Up @@ -89,6 +121,14 @@ impl<'a> DebuggerContext<'a> {
fn opcode_list(&self) -> Vec<String> {
self.debug_steps().iter().map(DebugStep::pretty_opcode).collect()
}

fn active_buffer(&self) -> &[u8] {
match self.active_buffer {
BufferKind::Memory => &self.current_step().memory,
BufferKind::Calldata => &self.current_step().calldata,
BufferKind::Returndata => &self.current_step().returndata,
}
}
}

impl DebuggerContext<'_> {
Expand Down Expand Up @@ -121,9 +161,9 @@ impl DebuggerContext<'_> {
// Grab number of times to do it
for _ in 0..buffer_as_number(&self.key_buffer, 1) {
if event.modifiers.contains(KeyModifiers::CONTROL) {
let max_mem = (self.current_step().memory.len() / 32).saturating_sub(1);
if self.draw_memory.current_mem_startline < max_mem {
self.draw_memory.current_mem_startline += 1;
let max_buf = (self.active_buffer().len() / 32).saturating_sub(1);
if self.draw_memory.current_buf_startline < max_buf {
self.draw_memory.current_buf_startline += 1;
}
} else if self.current_step < self.opcode_list.len() - 1 {
self.current_step += 1;
Expand All @@ -147,8 +187,8 @@ impl DebuggerContext<'_> {
KeyCode::Char('k') | KeyCode::Up => {
for _ in 0..buffer_as_number(&self.key_buffer, 1) {
if event.modifiers.contains(KeyModifiers::CONTROL) {
self.draw_memory.current_mem_startline =
self.draw_memory.current_mem_startline.saturating_sub(1);
self.draw_memory.current_buf_startline =
self.draw_memory.current_buf_startline.saturating_sub(1);
} else if self.current_step > 0 {
self.current_step -= 1;
} else if self.draw_memory.inner_call_index > 0 {
Expand All @@ -165,6 +205,10 @@ impl DebuggerContext<'_> {
}
self.key_buffer.clear();
}
KeyCode::Char('b') => {
self.active_buffer = self.active_buffer.next();
self.draw_memory.current_buf_startline = 0;
}
// Go to top of file
KeyCode::Char('g') => {
self.draw_memory.inner_call_index = 0;
Expand Down Expand Up @@ -248,7 +292,7 @@ impl DebuggerContext<'_> {
// toggle stack labels
KeyCode::Char('t') => self.stack_labels = !self.stack_labels,
// toggle memory utf8 decoding
KeyCode::Char('m') => self.mem_utf = !self.mem_utf,
KeyCode::Char('m') => self.buf_utf = !self.buf_utf,
// toggle help notice
KeyCode::Char('h') => self.show_shortcuts = !self.show_shortcuts,
KeyCode::Char(
Expand Down Expand Up @@ -284,7 +328,7 @@ impl DebuggerContext<'_> {
self.current_step -= 1;
} else if self.draw_memory.inner_call_index > 0 {
self.draw_memory.inner_call_index -= 1;
self.draw_memory.current_mem_startline = 0;
self.draw_memory.current_buf_startline = 0;
self.draw_memory.current_stack_startline = 0;
self.current_step = self.debug_steps().len() - 1;
}
Expand All @@ -294,7 +338,7 @@ impl DebuggerContext<'_> {
self.current_step += 1;
} else if self.draw_memory.inner_call_index < self.debug_arena().len() - 1 {
self.draw_memory.inner_call_index += 1;
self.draw_memory.current_mem_startline = 0;
self.draw_memory.current_buf_startline = 0;
self.draw_memory.current_stack_startline = 0;
self.current_step = 0;
}
Expand Down
113 changes: 65 additions & 48 deletions crates/debugger/src/tui/draw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! TUI draw implementation.

use super::context::DebuggerContext;
use super::context::{BufferKind, DebuggerContext};
use crate::op::OpcodeParam;
use alloy_primitives::U256;
use foundry_compilers::sourcemap::SourceElement;
Expand Down Expand Up @@ -77,7 +77,7 @@ impl DebuggerContext<'_> {
/// |-----------------------------|
/// | stack |
/// |-----------------------------|
/// | mem |
/// | buf |
/// |-----------------------------|
/// | |
/// | src |
Expand Down Expand Up @@ -120,7 +120,7 @@ impl DebuggerContext<'_> {
self.draw_src(f, src_pane);
self.draw_op_list(f, op_pane);
self.draw_stack(f, stack_pane);
self.draw_memory(f, memory_pane);
self.draw_buffer(f, memory_pane);
}

/// Draws the layout in horizontal mode.
Expand All @@ -130,7 +130,7 @@ impl DebuggerContext<'_> {
/// | op | stack |
/// |-----------------|-----------|
/// | | |
/// | src | mem |
/// | src | buf |
/// | | |
/// |-----------------|-----------|
/// ```
Expand Down Expand Up @@ -180,12 +180,12 @@ impl DebuggerContext<'_> {
self.draw_src(f, src_pane);
self.draw_op_list(f, op_pane);
self.draw_stack(f, stack_pane);
self.draw_memory(f, memory_pane);
self.draw_buffer(f, memory_pane);
}

fn draw_footer(&self, f: &mut Frame<'_>, area: Rect) {
let l1 = "[q]: quit | [k/j]: prev/next op | [a/s]: prev/next jump | [c/C]: prev/next call | [g/G]: start/end";
let l2 = "[t]: stack labels | [m]: memory decoding | [shift + j/k]: scroll stack | [ctrl + j/k]: scroll memory | ['<char>]: goto breakpoint | [h] toggle help";
let l1 = "[q]: quit | [k/j]: prev/next op | [a/s]: prev/next jump | [c/C]: prev/next call | [g/G]: start/end | [b]: cycle memory/calldata/returndata buffers";
let l2 = "[t]: stack labels | [m]: buffer decoding | [shift + j/k]: scroll stack | [ctrl + j/k]: scroll buffer | ['<char>]: goto breakpoint | [h] toggle help";
let dimmed = Style::new().add_modifier(Modifier::DIM);
let lines =
vec![Line::from(Span::styled(l1, dimmed)), Line::from(Span::styled(l2, dimmed))];
Expand Down Expand Up @@ -500,11 +500,15 @@ impl DebuggerContext<'_> {
f.render_widget(paragraph, area);
}

fn draw_memory(&self, f: &mut Frame<'_>, area: Rect) {
fn draw_buffer(&self, f: &mut Frame<'_>, area: Rect) {
let step = self.current_step();
let memory = &step.memory;
let buf = match self.active_buffer {
BufferKind::Memory => &step.memory,
BufferKind::Calldata => &step.calldata,
BufferKind::Returndata => &step.returndata,
};

let min_len = hex_digits(memory.len());
let min_len = hex_digits(buf.len());

// Color memory region based on read/write.
let mut offset = None;
Expand All @@ -513,13 +517,17 @@ impl DebuggerContext<'_> {
if let Instruction::OpCode(op) = step.instruction {
let stack_len = step.stack.len();
if stack_len > 0 {
let (read_offset, read_size, write_offset, write_size) =
get_memory_access(op, &step.stack);
if read_offset.is_some() {
let (read_buffer_accessed, read_offset, read_size, write_offset, write_size) =
get_buffer_access(op, &step.stack);

if read_offset.is_some() &&
read_buffer_accessed.is_some_and(|b| b == self.active_buffer)
{
offset = read_offset;
size = read_size;
color = Some(Color::Cyan);
} else if write_offset.is_some() {
} else if write_offset.is_some() && self.active_buffer == BufferKind::Memory {
// memory is the only volatile buffer, so hardcode memory check
offset = write_offset;
size = write_size;
color = Some(Color::Red);
Expand All @@ -532,8 +540,9 @@ impl DebuggerContext<'_> {
let prev_step = self.current_step - 1;
let prev_step = &self.debug_steps()[prev_step];
if let Instruction::OpCode(op) = prev_step.instruction {
let (_, _, write_offset, write_size) = get_memory_access(op, &prev_step.stack);
if write_offset.is_some() {
let (_, _, _, write_offset, write_size) = get_buffer_access(op, &prev_step.stack);
if write_offset.is_some() && self.active_buffer == BufferKind::Memory {
// memory is the only volatile buffer, so hardcode memory check
offset = write_offset;
size = write_size;
color = Some(Color::Green);
Expand All @@ -542,24 +551,24 @@ impl DebuggerContext<'_> {
}

let height = area.height as usize;
let end_line = self.draw_memory.current_mem_startline + height;
let end_line = self.draw_memory.current_buf_startline + height;

let text: Vec<Line> = memory
let text: Vec<Line> = buf
.chunks(32)
.enumerate()
.skip(self.draw_memory.current_mem_startline)
.skip(self.draw_memory.current_buf_startline)
.take_while(|(i, _)| *i < end_line)
.map(|(i, mem_word)| {
.map(|(i, buf_word)| {
let mut spans = Vec::with_capacity(1 + 32 * 2 + 1 + 32 / 4 + 1);

// Memory index.
// Buffer index.
spans.push(Span::styled(
format!("{:0min_len$x}| ", i * 32),
Style::new().fg(Color::White),
));

// Word hex bytes.
hex_bytes_spans(mem_word, &mut spans, |j, _| {
hex_bytes_spans(buf_word, &mut spans, |j, _| {
let mut byte_color = Color::White;
if let (Some(offset), Some(size), Some(color)) = (offset, size, color) {
let idx = i * 32 + j;
Expand All @@ -573,9 +582,9 @@ impl DebuggerContext<'_> {
Style::new().fg(byte_color)
});

if self.mem_utf {
if self.buf_utf {
spans.push(Span::raw("|"));
for utf in mem_word.chunks(4) {
for utf in buf_word.chunks(4) {
if let Ok(utf_str) = std::str::from_utf8(utf) {
spans.push(Span::raw(utf_str.replace('\0', ".")));
} else {
Expand All @@ -590,7 +599,7 @@ impl DebuggerContext<'_> {
})
.collect();

let title = format!("Memory (max expansion: {} bytes)", memory.len());
let title = self.active_buffer.title(buf.len());
let block = Block::default().title(title).borders(Borders::ALL);
let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: true });
f.render_widget(paragraph, area);
Expand Down Expand Up @@ -628,30 +637,37 @@ impl<'a> SourceLines<'a> {
}
}

/// The memory_access variable stores the index on the stack that indicates the memory
/// The memory_access variable stores the index on the stack that indicates the buffer
/// offset/size accessed by the given opcode:
/// (read memory offset, read memory size, write memory offset, write memory size)
/// (read buffer, buffer read offset, buffer read size, write memory offset, write memory size)
/// >= 1: the stack index
/// 0: no memory access
/// -1: a fixed size of 32 bytes
/// -2: a fixed size of 1 byte
/// The return value is a tuple about accessed memory region by the given opcode:
/// (read memory offset, read memory size, write memory offset, write memory size)
fn get_memory_access(
/// The return value is a tuple about accessed buffer region by the given opcode:
/// (read buffer, buffer read offset, buffer read size, write memory offset, write memory size)
fn get_buffer_access(
op: u8,
stack: &[U256],
) -> (Option<usize>, Option<usize>, Option<usize>, Option<usize>) {
let memory_access = match op {
opcode::KECCAK256 | opcode::RETURN | opcode::REVERT => (1, 2, 0, 0),
opcode::CALLDATACOPY | opcode::CODECOPY | opcode::RETURNDATACOPY => (0, 0, 1, 3),
opcode::EXTCODECOPY => (0, 0, 2, 4),
opcode::MLOAD => (1, -1, 0, 0),
opcode::MSTORE => (0, 0, 1, -1),
opcode::MSTORE8 => (0, 0, 1, -2),
opcode::LOG0 | opcode::LOG1 | opcode::LOG2 | opcode::LOG3 | opcode::LOG4 => (1, 2, 0, 0),
opcode::CREATE | opcode::CREATE2 => (2, 3, 0, 0),
opcode::CALL | opcode::CALLCODE => (4, 5, 0, 0),
opcode::DELEGATECALL | opcode::STATICCALL => (3, 4, 0, 0),
) -> (Option<BufferKind>, Option<usize>, Option<usize>, Option<usize>, Option<usize>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clippy is complaining about this, which is fair. You can extract it into a struct and return one Option<_>

let buffer_access = match op {
opcode::KECCAK256 | opcode::RETURN | opcode::REVERT => {
(Some(BufferKind::Memory), 1, 2, 0, 0)
}
opcode::CALLDATACOPY => (Some(BufferKind::Calldata), 2, 3, 1, 3),
opcode::RETURNDATACOPY => (Some(BufferKind::Returndata), 2, 3, 1, 3),
opcode::CALLDATALOAD => (Some(BufferKind::Calldata), 1, -1, 0, 0),
opcode::CODECOPY => (Some(BufferKind::Memory), 0, 0, 1, 3),
opcode::EXTCODECOPY => (Some(BufferKind::Memory), 0, 0, 2, 4),
opcode::MLOAD => (Some(BufferKind::Memory), 1, -1, 0, 0),
opcode::MSTORE => (Some(BufferKind::Memory), 0, 0, 1, -1),
opcode::MSTORE8 => (Some(BufferKind::Memory), 0, 0, 1, -2),
opcode::LOG0 | opcode::LOG1 | opcode::LOG2 | opcode::LOG3 | opcode::LOG4 => {
(Some(BufferKind::Memory), 1, 2, 0, 0)
}
opcode::CREATE | opcode::CREATE2 => (Some(BufferKind::Memory), 2, 3, 0, 0),
opcode::CALL | opcode::CALLCODE => (Some(BufferKind::Memory), 4, 5, 0, 0),
opcode::DELEGATECALL | opcode::STATICCALL => (Some(BufferKind::Memory), 3, 4, 0, 0),
_ => Default::default(),
};

Expand All @@ -670,13 +686,14 @@ fn get_memory_access(
_ => panic!("invalid stack index"),
};

let (read_offset, read_size, write_offset, write_size) = (
get_size(memory_access.0),
get_size(memory_access.1),
get_size(memory_access.2),
get_size(memory_access.3),
let (read_buffer_accessed, read_offset, read_size, write_offset, write_size) = (
buffer_access.0,
get_size(buffer_access.1),
get_size(buffer_access.2),
get_size(buffer_access.3),
get_size(buffer_access.4),
);
(read_offset, read_size, write_offset, write_size)
(read_buffer_accessed, read_offset, read_size, write_offset, write_size)
}

fn hex_bytes_spans(bytes: &[u8], spans: &mut Vec<Span<'_>>, f: impl Fn(usize, u8) -> Style) {
Expand Down
6 changes: 6 additions & 0 deletions crates/evm/core/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ pub struct DebugStep {
pub stack: Vec<U256>,
/// Memory *prior* to running the associated opcode
pub memory: Vec<u8>,
/// Calldata *prior* to running the associated opcode
pub calldata: Vec<u8>,
/// Returndata *prior* to running the associated opcode
pub returndata: Vec<u8>,
/// Opcode to be executed
pub instruction: Instruction,
/// Optional bytes that are being pushed onto the stack
Expand All @@ -187,6 +191,8 @@ impl Default for DebugStep {
Self {
stack: vec![],
memory: Default::default(),
calldata: Default::default(),
returndata: Default::default(),
instruction: Instruction::OpCode(revm::interpreter::opcode::INVALID),
push_bytes: None,
pc: 0,
Expand Down
Loading
Loading