Skip to content

Commit

Permalink
change start of PC to start of bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Jan 27, 2025
1 parent 4fde015 commit 691ce24
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
24 changes: 18 additions & 6 deletions crates/bytecode/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,34 @@ impl Bytecode {
}
}

/// Pointer to the executable bytecode.
///
/// Note: EOF will return the pointer to the start of the code section.
/// while legacy bytecode will point to the start of the bytes.
pub fn bytecode_ptr(&self) -> *const u8 {
self.bytecode().as_ptr()
}

/// Returns bytes.
#[inline]
pub fn bytes(&self) -> Bytes {
self.bytes_ref().clone()
}

/// Returns bytes.
#[inline]
pub fn bytes_ref(&self) -> &Bytes {
match self {
Self::LegacyAnalyzed(analyzed) => analyzed.bytecode().clone(),
_ => self.original_bytes(),
Self::LegacyAnalyzed(analyzed) => analyzed.bytecode(),
Self::Eof(eof) => &eof.raw,
Self::Eip7702(code) => code.raw(),
}
}

/// Returns bytes slice.
#[inline]
pub fn bytes_slice(&self) -> &[u8] {
match self {
Self::LegacyAnalyzed(analyzed) => analyzed.bytecode(),
_ => self.original_byte_slice(),
}
self.bytes_ref()
}

/// Returns a reference to the original bytecode.
Expand Down
1 change: 1 addition & 0 deletions crates/bytecode/src/eof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl Default for Eof {
code_section: vec![1],
// One code section with a STOP byte.
code: Bytes::from_static(&[0x00]),
code_offset: 0,
container_section: vec![],
data_section: Bytes::new(),
is_data_filled: true,
Expand Down
14 changes: 6 additions & 8 deletions crates/bytecode/src/eof/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct EofBody {
/// Index of the last byte of each code section
pub code_section: Vec<usize>,
pub code: Bytes,
pub code_offset: usize,
pub container_section: Vec<Bytes>,
pub data_section: Bytes,
pub is_data_filled: bool,
Expand All @@ -34,7 +35,6 @@ impl EofBody {

/// Creates an EOF container from this body.
pub fn into_eof(self) -> Eof {
// TODO : Add bounds checks.
let mut prev_value = 0;
let header = EofHeader {
types_size: self.types_section.len() as u16 * 4,
Expand All @@ -59,22 +59,19 @@ impl EofBody {
let mut buffer = Vec::new();
header.encode(&mut buffer);
self.encode(&mut buffer);
Eof {
header,
body: self,
raw: buffer.into(),
}
Eof::decode(buffer.into()).expect("Failed to encode EOF")
}

/// Returns offset of the start of indexed code section.
///
/// First code section starts at 0.
pub fn eof_code_section_start(&self, idx: usize) -> Option<usize> {
// Starting code section start with 0.
let code_offset = self.code_offset;
if idx == 0 {
return Some(0);
return Some(code_offset);
}
self.code_section.get(idx - 1).cloned()
self.code_section.get(idx - 1).map(|i| i + code_offset)
}

/// Encodes this body into the given buffer.
Expand Down Expand Up @@ -118,6 +115,7 @@ impl EofBody {

// Extract code section
let start = header_len + header.types_size as usize;
body.code_offset = start;
let mut code_end = 0;
for size in header.code_sizes.iter().map(|x| *x as usize) {
code_end += size;
Expand Down
6 changes: 3 additions & 3 deletions crates/inspector/src/eip3155.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct TracerEip3155<CTX, INTR> {
/// Print summary of the execution.
print_summary: bool,
stack: Vec<U256>,
pc: usize,
pc: u64,
section: Option<u64>,
function_depth: Option<u64>,
opcode: u8,
Expand Down Expand Up @@ -224,7 +224,7 @@ where
} else {
None
};
self.pc = interp.bytecode.trace_pc();
self.pc = interp.bytecode.pc() as u64;
self.section = if interp.runtime_flag.is_eof() {
Some(interp.sub_routine.routine_idx() as u64)
} else {
Expand All @@ -249,7 +249,7 @@ where
}

let value = Output {
pc: self.pc as u64,
pc: self.pc,
section: self.section,
op: self.opcode,
gas: hex_number(self.gas),
Expand Down
8 changes: 5 additions & 3 deletions crates/interpreter/src/interpreter/ext_bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Deref for ExtBytecode {
impl ExtBytecode {
/// Create new extended bytecode and set the instruction pointer to the start of the bytecode.
pub fn new(base: Bytecode) -> Self {
let instruction_pointer = base.bytecode().as_ptr();
let instruction_pointer = base.bytecode_ptr();
Self {
base,
instruction_pointer,
Expand All @@ -40,7 +40,7 @@ impl ExtBytecode {

/// Creates new `ExtBytecode` with the given hash.
pub fn new_with_hash(base: Bytecode, hash: B256) -> Self {
let instruction_pointer = base.bytecode().as_ptr();
let instruction_pointer = base.bytecode_ptr();
Self {
base,
instruction_pointer,
Expand All @@ -66,10 +66,12 @@ impl Jumps for ExtBytecode {
fn relative_jump(&mut self, offset: isize) {
self.instruction_pointer = unsafe { self.instruction_pointer.offset(offset) };
}

#[inline]
fn absolute_jump(&mut self, offset: usize) {
self.instruction_pointer = unsafe { self.base.bytecode().as_ptr().add(offset) };
self.instruction_pointer = unsafe { self.base.bytes_ref().as_ptr().add(offset) };
}

#[inline]
fn is_valid_legacy_jump(&mut self, offset: usize) -> bool {
self.base
Expand Down

0 comments on commit 691ce24

Please sign in to comment.