Skip to content

Commit

Permalink
chore: drop shared prefix from memory and stack, add interpreter getters
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo Feroleto committed Nov 15, 2023
1 parent 64c1ffc commit 60640ad
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 257 deletions.
2 changes: 1 addition & 1 deletion crates/interpreter/src/instructions/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn return_inner(interpreter: &mut Interpreter, instruction_result: InstructionRe
let mut output = Bytes::default();
if len != 0 {
let offset = as_usize_or_fail!(interpreter, offset);
shared_memory_resize!(interpreter, offset, len);
memory_resize!(interpreter, offset, len);

output = interpreter
.shared_context
Expand Down
10 changes: 5 additions & 5 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn extcodecopy<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mu
}
let memory_offset = as_usize_or_fail!(interpreter, memory_offset);
let code_offset = min(as_usize_saturated!(code_offset), code.len());
shared_memory_resize!(interpreter, memory_offset, len);
memory_resize!(interpreter, memory_offset, len);

// Note: this can't panic because we resized memory to fit.
interpreter
Expand Down Expand Up @@ -199,7 +199,7 @@ pub fn log<const N: usize, H: Host>(interpreter: &mut Interpreter, host: &mut H)
Bytes::new()
} else {
let offset = as_usize_or_fail!(interpreter, offset);
shared_memory_resize!(interpreter, offset, len);
memory_resize!(interpreter, offset, len);
Bytes::copy_from_slice(interpreter.shared_context.memory.slice(offset, len))
};

Expand Down Expand Up @@ -270,7 +270,7 @@ pub fn create<const IS_CREATE2: bool, H: Host, SPEC: Spec>(
}

let code_offset = as_usize_or_fail!(interpreter, code_offset);
shared_memory_resize!(interpreter, code_offset, len);
memory_resize!(interpreter, code_offset, len);
code = Bytes::copy_from_slice(interpreter.shared_context.memory.slice(code_offset, len));
}

Expand Down Expand Up @@ -364,7 +364,7 @@ pub fn call_inner<SPEC: Spec, H: Host>(
let in_len = as_usize_or_fail!(interpreter, in_len);
let input = if in_len != 0 {
let in_offset = as_usize_or_fail!(interpreter, in_offset);
shared_memory_resize!(interpreter, in_offset, in_len);
memory_resize!(interpreter, in_offset, in_len);
Bytes::copy_from_slice(interpreter.shared_context.memory.slice(in_offset, in_len))
} else {
Bytes::new()
Expand All @@ -373,7 +373,7 @@ pub fn call_inner<SPEC: Spec, H: Host>(
let out_len = as_usize_or_fail!(interpreter, out_len);
let out_offset = if out_len != 0 {
let out_offset = as_usize_or_fail!(interpreter, out_offset);
shared_memory_resize!(interpreter, out_offset, out_len);
memory_resize!(interpreter, out_offset, out_len);
out_offset
} else {
usize::MAX //unrealistic value so we are sure it is not used
Expand Down
10 changes: 5 additions & 5 deletions crates/interpreter/src/instructions/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ macro_rules! gas_or_fail {
};
}

macro_rules! shared_memory_resize {
macro_rules! memory_resize {
($interp:expr, $offset:expr, $len:expr) => {
let size = $offset.saturating_add($len);
if size > $interp.shared_context.memory.len() {
Expand Down Expand Up @@ -159,8 +159,8 @@ macro_rules! pop_top {

#[macro_export]
macro_rules! push_b256 {
($interp:expr, $shared_stack: expr, $($x:expr),* $(,)?) => ($(
match $shared_stack.push_b256($x) {
($interp:expr, $stack: expr, $($x:expr),* $(,)?) => ($(
match $stack.push_b256($x) {
Ok(()) => {},
Err(e) => {
$interp.instruction_result = e;
Expand All @@ -175,8 +175,8 @@ macro_rules! push_b256 {

#[macro_export]
macro_rules! push {
($interp:expr, $shared_stack: expr, $($x:expr),* $(,)?) => ($(
match $shared_stack.push($x) {
($interp:expr, $stack: expr, $($x:expr),* $(,)?) => ($(
match $stack.push($x) {
Ok(()) => {},
Err(e) => {
$interp.instruction_result = e;
Expand Down
8 changes: 4 additions & 4 deletions crates/interpreter/src/instructions/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn mload<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop!(interpreter, index);
let index = as_usize_or_fail!(interpreter, index);
shared_memory_resize!(interpreter, index, 32);
memory_resize!(interpreter, index, 32);
push!(
interpreter,
interpreter.shared_context.memory.get_u256(index)
Expand All @@ -20,15 +20,15 @@ pub fn mstore<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop!(interpreter, index, value);
let index = as_usize_or_fail!(interpreter, index);
shared_memory_resize!(interpreter, index, 32);
memory_resize!(interpreter, index, 32);
interpreter.shared_context.memory.set_u256(index, value);
}

pub fn mstore8<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
gas!(interpreter, gas::VERYLOW);
pop!(interpreter, index, value);
let index = as_usize_or_fail!(interpreter, index);
shared_memory_resize!(interpreter, index, 1);
memory_resize!(interpreter, index, 1);
interpreter
.shared_context
.memory
Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn mcopy<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H)
let dst = as_usize_or_fail!(interpreter, dst);
let src = as_usize_or_fail!(interpreter, src);
// resize memory
shared_memory_resize!(interpreter, max(dst, src), len);
memory_resize!(interpreter, max(dst, src), len);
// copy memory in place
interpreter.shared_context.memory.copy(dst, src, len);
}
8 changes: 4 additions & 4 deletions crates/interpreter/src/instructions/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn keccak256<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
KECCAK_EMPTY
} else {
let from = as_usize_or_fail!(interpreter, from);
shared_memory_resize!(interpreter, from, len);
memory_resize!(interpreter, from, len);
crate::primitives::keccak256(interpreter.shared_context.memory.slice(from, len))
};

Expand Down Expand Up @@ -43,7 +43,7 @@ pub fn codecopy<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
}
let memory_offset = as_usize_or_fail!(interpreter, memory_offset);
let code_offset = as_usize_saturated!(code_offset);
shared_memory_resize!(interpreter, memory_offset, len);
memory_resize!(interpreter, memory_offset, len);

// Note: this can't panic because we resized memory to fit.
interpreter.shared_context.memory.set_data(
Expand Down Expand Up @@ -89,7 +89,7 @@ pub fn calldatacopy<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
}
let memory_offset = as_usize_or_fail!(interpreter, memory_offset);
let data_offset = as_usize_saturated!(data_offset);
shared_memory_resize!(interpreter, memory_offset, len);
memory_resize!(interpreter, memory_offset, len);

// Note: this can't panic because we resized memory to fit.
interpreter.shared_context.memory.set_data(
Expand Down Expand Up @@ -124,7 +124,7 @@ pub fn returndatacopy<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host:
}
if len != 0 {
let memory_offset = as_usize_or_fail!(interpreter, memory_offset);
shared_memory_resize!(interpreter, memory_offset, len);
memory_resize!(interpreter, memory_offset, len);
interpreter.shared_context.memory.set(
memory_offset,
&interpreter.return_data_buffer[data_offset..data_end],
Expand Down
38 changes: 31 additions & 7 deletions crates/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
pub mod analysis;
mod contract;
mod memory;
mod shared_context;
mod shared_memory;
mod shared_stack;
mod stack;

pub use analysis::BytecodeLocked;
pub use contract::Contract;
pub use memory::*;
pub use shared_context::*;
pub use shared_memory::*;
pub use shared_stack::*;
pub use stack::*;

use crate::primitives::Bytes;
use crate::{
Expand All @@ -20,8 +20,8 @@ use core::cmp::min;
use core::ops::Range;
use revm_primitives::{Address, U256};

pub use self::shared_memory::EMPTY_SHARED_MEMORY;
pub use self::shared_stack::EMPTY_SHARED_STACK;
pub use self::memory::EMPTY_MEMORY;
pub use self::stack::EMPTY_STACK;

/// EIP-170: Contract code size limit
///
Expand Down Expand Up @@ -143,7 +143,7 @@ impl Interpreter {
/// When sub call returns we can insert output of that call into this interpreter.
///
/// Note that shared memory is required as a input field.
/// As SharedMemory inside Interpreter is taken and replaced with empty (not valid) memory.
/// As Memory inside Interpreter is taken and replaced with empty (not valid) memory.
pub fn insert_call_output(
&mut self,
shared_context: &mut SharedContext,
Expand Down Expand Up @@ -212,6 +212,30 @@ impl Interpreter {
}
}

/// Return the stack of the current context
#[inline]
pub fn stack(&self) -> &Vec<U256> {
self.shared_context.stack.buffer()
}

/// Return the stack of the current context
#[inline]
pub fn stack_mut(&mut self) -> &Vec<U256> {
self.shared_context.stack.buffer_mut()
}

/// Returns a reference to the memory of the current context, the active memory.
#[inline]
pub fn memory(&self) -> &[u8] {
self.shared_context.memory.context_memory()
}

/// Returns a reference to the memory of the current context, the active memory.
#[inline]
pub fn memory_mut(&mut self) -> &mut [u8] {
self.shared_context.memory.context_memory_mut()
}

/// Executes the instruction at the current instruction pointer.
///
/// Internally it will increment instruction pointer by one.
Expand Down
Loading

0 comments on commit 60640ad

Please sign in to comment.