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

Add memory offset #1032

Merged
merged 46 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
81f6d57
mems offset
DoTheBestToGetTheBest Jan 30, 2024
3cfbdce
Update evm.rs
DoTheBestToGetTheBest Jan 30, 2024
ad8b6af
Update context.rs
DoTheBestToGetTheBest Jan 30, 2024
9923c28
Update host.rs
DoTheBestToGetTheBest Jan 30, 2024
1bce713
Update host.rs
DoTheBestToGetTheBest Jan 30, 2024
7d4542a
Update host.rs
DoTheBestToGetTheBest Jan 30, 2024
2db4e52
use core instead of std
DoTheBestToGetTheBest Jan 30, 2024
47ae246
Update host.rs
DoTheBestToGetTheBest Jan 31, 2024
857c9d9
Update evm.rs
DoTheBestToGetTheBest Jan 31, 2024
11cf24d
Update interpreter.rs
DoTheBestToGetTheBest Jan 31, 2024
d3526bc
clippy
DoTheBestToGetTheBest Jan 31, 2024
6c33f94
Update interpreter.rs
DoTheBestToGetTheBest Jan 31, 2024
04b314a
rename Ranger to Range
DoTheBestToGetTheBest Jan 31, 2024
5589bbc
Update inner_models.rs
DoTheBestToGetTheBest Jan 31, 2024
35c45b4
Update inner_models.rs
DoTheBestToGetTheBest Feb 1, 2024
d9ea8d0
Update evm.rs
DoTheBestToGetTheBest Feb 1, 2024
d720618
Update execution.rs
DoTheBestToGetTheBest Feb 2, 2024
8e1de41
Update evm.rs
DoTheBestToGetTheBest Feb 2, 2024
a63bb1e
Update evm.rs
DoTheBestToGetTheBest Feb 2, 2024
6450b24
Update evm.rs
DoTheBestToGetTheBest Feb 2, 2024
baee6c0
Update host.rs
DoTheBestToGetTheBest Feb 2, 2024
bf5cb66
Update crates/interpreter/src/interpreter.rs
DoTheBestToGetTheBest Feb 2, 2024
b4a601f
Update execution.rs
DoTheBestToGetTheBest Feb 7, 2024
91fc750
Update execution.rs
DoTheBestToGetTheBest Feb 7, 2024
25d0b77
Update handler_register.rs
DoTheBestToGetTheBest Feb 7, 2024
59a7d68
Update context.rs
DoTheBestToGetTheBest Feb 7, 2024
f505139
Update frame.rs
DoTheBestToGetTheBest Feb 7, 2024
bcbcbb1
Update execution.rs
DoTheBestToGetTheBest Feb 7, 2024
e2dfde8
Update execution.rs
DoTheBestToGetTheBest Feb 7, 2024
5f6a870
Update evm.rs
DoTheBestToGetTheBest Feb 7, 2024
1f0111c
Update handler_register.rs
DoTheBestToGetTheBest Feb 7, 2024
b6da825
Update eip3155.rs
DoTheBestToGetTheBest Feb 7, 2024
6a5c2a2
Update eip3155.rs
DoTheBestToGetTheBest Feb 7, 2024
0716693
Update customprinter.rs
DoTheBestToGetTheBest Feb 7, 2024
eb0b8ee
Update inspector.rs
DoTheBestToGetTheBest Feb 7, 2024
6218664
Update gas.rs
DoTheBestToGetTheBest Feb 7, 2024
709c831
Update handler_register.rs
DoTheBestToGetTheBest Feb 7, 2024
80dcd5d
Update context.rs
DoTheBestToGetTheBest Feb 7, 2024
22e7fd9
Update execution.rs
DoTheBestToGetTheBest Feb 7, 2024
cd79f7f
Update evm.rs
DoTheBestToGetTheBest Feb 7, 2024
5f84bcd
Update inner_models.rs
DoTheBestToGetTheBest Feb 7, 2024
0e0ae60
Update handler_register.rs
DoTheBestToGetTheBest Feb 7, 2024
59a5b5d
Update handler_register.rs
DoTheBestToGetTheBest Feb 8, 2024
cb2e066
Update inner_models.rs
DoTheBestToGetTheBest Feb 8, 2024
1ebe9eb
Update crates/revm/src/inspector.rs
rakita Feb 12, 2024
cedfd61
Merge branch 'main' into add-memory-offset
rakita Feb 12, 2024
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
9 changes: 6 additions & 3 deletions crates/interpreter/src/inner_models.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub use crate::primitives::CreateScheme;
use crate::primitives::{Address, Bytes, TransactTo, TxEnv, U256};
use alloc::boxed::Box;

pub use crate::primitives::CreateScheme;
use core::ops::Range;

/// Inputs for a call.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand All @@ -19,6 +19,8 @@ pub struct CallInputs {
pub context: CallContext,
/// Whether this is a static call.
pub is_static: bool,
/// The return memory offset where the output of the call is written.
pub return_memory_offset: Range<usize>,
}

/// Inputs for a create call.
Expand Down Expand Up @@ -61,6 +63,7 @@ impl CallInputs {
scheme: CallScheme::Call,
},
is_static: false,
return_memory_offset: 0..0,
})
}

Expand Down Expand Up @@ -106,7 +109,7 @@ impl CreateInputs {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CallScheme {
/// `CALL`
/// `CALL`.
Call,
/// `CALLCODE`
CallCode,
Expand Down
8 changes: 4 additions & 4 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ pub fn call<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
scheme: CallScheme::Call,
},
is_static: interpreter.is_static,
return_memory_offset,
}),
return_memory_offset,
};
interpreter.instruction_result = InstructionResult::CallOrCreate;
}
Expand Down Expand Up @@ -419,8 +419,8 @@ pub fn call_code<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut
scheme: CallScheme::CallCode,
},
is_static: interpreter.is_static,
return_memory_offset,
}),
return_memory_offset,
};
interpreter.instruction_result = InstructionResult::CallOrCreate;
}
Expand Down Expand Up @@ -465,8 +465,8 @@ pub fn delegate_call<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &
scheme: CallScheme::DelegateCall,
},
is_static: interpreter.is_static,
return_memory_offset,
}),
return_memory_offset,
};
interpreter.instruction_result = InstructionResult::CallOrCreate;
}
Expand Down Expand Up @@ -511,8 +511,8 @@ pub fn static_call<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mu
scheme: CallScheme::StaticCall,
},
is_static: true,
return_memory_offset,
}),
return_memory_offset,
};
interpreter.instruction_result = InstructionResult::CallOrCreate;
}
5 changes: 0 additions & 5 deletions crates/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::{
};
use alloc::boxed::Box;
use core::cmp::min;
use core::ops::Range;
use revm_primitives::U256;

pub use self::shared_memory::EMPTY_SHARED_MEMORY;
Expand Down Expand Up @@ -70,10 +69,6 @@ pub enum InterpreterAction {
Call {
/// Call inputs
inputs: Box<CallInputs>,
/// The offset into `self.memory` of the return data.
///
/// This value must be ignored if `self.return_len` is 0.
return_memory_offset: Range<usize>,
},
/// CREATE or CREATE2 instruction called.
Create { inputs: Box<CreateInputs> },
Expand Down
22 changes: 9 additions & 13 deletions crates/revm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::{
FrameOrResult, JournalCheckpoint, CALL_STACK_LIMIT,
};
use alloc::boxed::Box;
use core::ops::Range;

/// Main Context structure that contains both EvmContext and External context.
pub struct Context<EXT, DB: Database> {
Expand Down Expand Up @@ -358,11 +357,7 @@ impl<DB: Database> EvmContext<DB> {
}

/// Make call frame
pub fn make_call_frame(
&mut self,
inputs: &CallInputs,
return_memory_range: Range<usize>,
) -> FrameOrResult {
pub fn make_call_frame(&mut self, inputs: &CallInputs) -> FrameOrResult {
let gas = Gas::new(inputs.gas_limit);

let return_result = |instruction_result: InstructionResult| {
Expand All @@ -372,7 +367,7 @@ impl<DB: Database> EvmContext<DB> {
gas,
output: Bytes::new(),
},
return_memory_range.clone(),
inputs.return_memory_offset.clone(),
)
};

Expand Down Expand Up @@ -421,7 +416,7 @@ impl<DB: Database> EvmContext<DB> {
} else {
self.journaled_state.checkpoint_revert(checkpoint);
}
FrameOrResult::new_call_result(result, return_memory_range)
FrameOrResult::new_call_result(result, inputs.return_memory_offset.clone())
} else if !bytecode.is_empty() {
let contract = Box::new(Contract::new_with_context(
inputs.input.clone(),
Expand All @@ -431,7 +426,7 @@ impl<DB: Database> EvmContext<DB> {
));
// Create interpreter and executes call and push new CallStackFrame.
FrameOrResult::new_call_frame(
return_memory_range,
inputs.return_memory_offset.clone(),
checkpoint,
Interpreter::new(contract, gas.limit(), inputs.is_static),
)
Expand Down Expand Up @@ -601,6 +596,7 @@ pub(crate) mod test_utils {
scheme: revm_interpreter::CallScheme::Call,
},
is_static: false,
return_memory_offset: 0..0,
}
}

Expand Down Expand Up @@ -672,7 +668,7 @@ mod tests {
evm_context.journaled_state.depth = CALL_STACK_LIMIT as usize + 1;
let contract = address!("dead10000000000000000000000000000001dead");
let call_inputs = test_utils::create_mock_call_inputs(contract);
let res = evm_context.make_call_frame(&call_inputs, 0..0);
let res = evm_context.make_call_frame(&call_inputs);
let FrameOrResult::Result(err) = res else {
panic!("Expected FrameOrResult::Result");
};
Expand All @@ -693,7 +689,7 @@ mod tests {
let contract = address!("dead10000000000000000000000000000001dead");
let mut call_inputs = test_utils::create_mock_call_inputs(contract);
call_inputs.transfer.value = U256::from(1);
let res = evm_context.make_call_frame(&call_inputs, 0..0);
let res = evm_context.make_call_frame(&call_inputs);
let FrameOrResult::Result(result) = res else {
panic!("Expected FrameOrResult::Result");
};
Expand All @@ -714,7 +710,7 @@ mod tests {
let mut evm_context = create_cache_db_evm_context_with_balance(Box::new(env), cdb, bal);
let contract = address!("dead10000000000000000000000000000001dead");
let call_inputs = test_utils::create_mock_call_inputs(contract);
let res = evm_context.make_call_frame(&call_inputs, 0..0);
let res = evm_context.make_call_frame(&call_inputs);
let FrameOrResult::Result(result) = res else {
panic!("Expected FrameOrResult::Result");
};
Expand All @@ -739,7 +735,7 @@ mod tests {
);
let mut evm_context = create_cache_db_evm_context_with_balance(Box::new(env), cdb, bal);
let call_inputs = test_utils::create_mock_call_inputs(contract);
let res = evm_context.make_call_frame(&call_inputs, 0..0);
let res = evm_context.make_call_frame(&call_inputs);
let FrameOrResult::Frame(Frame::Call(call_frame)) = res else {
panic!("Expected FrameOrResult::Frame(Frame::Call(..))");
};
Expand Down
6 changes: 1 addition & 5 deletions crates/revm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,7 @@ impl<EXT, DB: Database> Evm<'_, EXT, DB> {

let exec = &mut self.handler.execution;
let frame_or_result = match next_action {
InterpreterAction::Call {
inputs,
return_memory_offset,
} => exec.call(&mut self.context, inputs, return_memory_offset),
InterpreterAction::Call { inputs } => exec.call(&mut self.context, inputs),
InterpreterAction::Create { inputs } => exec.create(&mut self.context, inputs),
InterpreterAction::Return { result } => {
// free memory context.
Expand Down Expand Up @@ -335,7 +332,6 @@ impl<EXT, DB: Database> Evm<'_, EXT, DB> {
TransactTo::Call(_) => exec.call(
ctx,
CallInputs::new_boxed(&ctx.evm.env.tx, gas_limit).unwrap(),
0..0,
),
TransactTo::Create(_) => exec.create(
ctx,
Expand Down
13 changes: 4 additions & 9 deletions crates/revm/src/handler/handle_types/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
CallFrame, Context, CreateFrame, Frame, FrameOrResult, FrameResult,
};
use alloc::{boxed::Box, sync::Arc};
use core::ops::Range;

use revm_interpreter::{CallOutcome, CreateOutcome, InterpreterResult};

/// Handles first frame return handle.
Expand All @@ -14,7 +14,7 @@ pub type LastFrameReturnHandle<'a, EXT, DB> =

/// Handle sub call.
pub type FrameCallHandle<'a, EXT, DB> =
Arc<dyn Fn(&mut Context<EXT, DB>, Box<CallInputs>, Range<usize>) -> FrameOrResult + 'a>;
Arc<dyn Fn(&mut Context<EXT, DB>, Box<CallInputs>) -> FrameOrResult + 'a>;

/// Handle call return
pub type FrameCallReturnHandle<'a, EXT, DB> =
Expand Down Expand Up @@ -83,13 +83,8 @@ impl<'a, EXT, DB: Database> ExecutionHandler<'a, EXT, DB> {

/// Call frame call handler.
#[inline]
pub fn call(
&self,
context: &mut Context<EXT, DB>,
inputs: Box<CallInputs>,
return_memory_offset: Range<usize>,
) -> FrameOrResult {
(self.call)(context, inputs, return_memory_offset)
pub fn call(&self, context: &mut Context<EXT, DB>, inputs: Box<CallInputs>) -> FrameOrResult {
(self.call)(context, inputs.clone())
}

/// Call registered handler for call return.
Expand Down
7 changes: 2 additions & 5 deletions crates/revm/src/handler/mainnet/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
CallFrame, Context, CreateFrame, Frame, FrameOrResult, FrameResult,
};
use alloc::boxed::Box;
use core::ops::Range;

use revm_interpreter::{CallOutcome, InterpreterResult};

/// Helper function called inside [`last_frame_return`]
Expand Down Expand Up @@ -61,11 +61,8 @@ pub fn last_frame_return<SPEC: Spec, EXT, DB: Database>(
pub fn call<SPEC: Spec, EXT, DB: Database>(
context: &mut Context<EXT, DB>,
inputs: Box<CallInputs>,
return_memory_offset: Range<usize>,
) -> FrameOrResult {
context
.evm
.make_call_frame(&inputs, return_memory_offset.clone())
context.evm.make_call_frame(&inputs)
}

#[inline]
Expand Down
4 changes: 0 additions & 4 deletions crates/revm/src/inspector.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::ops::Range;

use crate::{
interpreter::{CallInputs, CreateInputs, Interpreter},
primitives::{db::Database, Address, Log, U256},
Expand Down Expand Up @@ -82,11 +80,9 @@ pub trait Inspector<DB: Database> {
&mut self,
context: &mut EvmContext<DB>,
inputs: &mut CallInputs,
return_memory_offset: Range<usize>,
) -> Option<CallOutcome> {
let _ = context;
let _ = inputs;
let _ = return_memory_offset;
None
}

Expand Down
3 changes: 0 additions & 3 deletions crates/revm/src/inspector/customprinter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Custom print inspector, it has step level information of execution.
//! It is a great tool if some debugging is needed.

use core::ops::Range;

use revm_interpreter::CallOutcome;
use revm_interpreter::CreateOutcome;

Expand Down Expand Up @@ -79,7 +77,6 @@ impl<DB: Database> Inspector<DB> for CustomPrintTracer {
&mut self,
_context: &mut EvmContext<DB>,
inputs: &mut CallInputs,
_return_memory_offset: Range<usize>,
) -> Option<CallOutcome> {
println!(
"SM CALL: {:?}, context:{:?}, is_static:{:?}, transfer:{:?}, input_size:{:?}",
Expand Down
2 changes: 0 additions & 2 deletions crates/revm/src/inspector/eip3155.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
EvmContext, Inspector,
};

use core::ops::Range;
use revm_interpreter::CallOutcome;
use revm_interpreter::CreateOutcome;
use serde_json::json;
Expand Down Expand Up @@ -86,7 +85,6 @@ impl<DB: Database> Inspector<DB> for TracerEip3155 {
&mut self,
_context: &mut EvmContext<DB>,
_inputs: &mut CallInputs,
_return_memory_offset: Range<usize>,
) -> Option<CallOutcome> {
None
}
Expand Down
4 changes: 1 addition & 3 deletions crates/revm/src/inspector/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ impl<DB: Database> Inspector<DB> for GasInspector {

#[cfg(test)]
mod tests {
use core::ops::Range;

use revm_interpreter::CallOutcome;
use revm_interpreter::CreateOutcome;
Expand Down Expand Up @@ -115,9 +114,8 @@ mod tests {
&mut self,
context: &mut EvmContext<DB>,
call: &mut CallInputs,
return_memory_offset: Range<usize>,
) -> Option<CallOutcome> {
self.gas_inspector.call(context, call, return_memory_offset)
self.gas_inspector.call(context, call)
}

fn call_end(
Expand Down
10 changes: 5 additions & 5 deletions crates/revm/src/inspector/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
};
use alloc::{boxed::Box, rc::Rc, sync::Arc, vec::Vec};

/// Provides access to an `Inspector` instance.
pub trait GetInspector<DB: Database> {
fn get_inspector(&mut self) -> &mut dyn Inspector<DB>;
}
Expand Down Expand Up @@ -154,16 +155,17 @@ pub fn inspector_handle_register<'a, DB: Database, EXT: GetInspector<DB>>(
// Call handler
let call_input_stack_inner = call_input_stack.clone();
let old_handle = handler.execution.call.clone();
handler.execution.call = Arc::new(move |ctx, mut inputs, range| -> FrameOrResult {
handler.execution.call = Arc::new(move |ctx, mut inputs| -> FrameOrResult {
let inspector = ctx.external.get_inspector();
let _mems = inputs.return_memory_offset.clone();
// call inspector callto change input or return outcome.
if let Some(outcome) = inspector.call(&mut ctx.evm, &mut inputs, range.clone()) {
if let Some(outcome) = inspector.call(&mut ctx.evm, &mut inputs) {
call_input_stack_inner.borrow_mut().push(inputs.clone());
return FrameOrResult::Result(FrameResult::Call(outcome));
}
call_input_stack_inner.borrow_mut().push(inputs.clone());

let mut frame_or_result = old_handle(ctx, inputs, range);
let mut frame_or_result = old_handle(ctx, inputs);

let inspector = ctx.external.get_inspector();
if let FrameOrResult::Frame(frame) = &mut frame_or_result {
Expand Down Expand Up @@ -251,7 +253,6 @@ pub fn inspector_instruction<

#[cfg(test)]
mod tests {
use core::ops::Range;

use super::*;
use crate::{
Expand Down Expand Up @@ -305,7 +306,6 @@ mod tests {
&mut self,
context: &mut EvmContext<DB>,
_call: &mut CallInputs,
_return_memory_offset: Range<usize>,
) -> Option<CallOutcome> {
if self.call {
unreachable!("call should not be called twice")
Expand Down
Loading