Skip to content

Commit

Permalink
make CallDataImpl non clonable
Browse files Browse the repository at this point in the history
  • Loading branch information
cdump committed Oct 28, 2024
1 parent 3123e4f commit 593bd8f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 26 deletions.
1 change: 0 additions & 1 deletion src/arguments/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::Label;
use crate::evm::{calldata::CallData, element::Element, U256, VAL_4, VAL_131072};
use std::error;

#[derive(Clone)]
pub(super) struct CallDataImpl {
pub selector: [u8; 4],
}
Expand Down
8 changes: 2 additions & 6 deletions src/arguments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,12 +561,8 @@ pub fn function_arguments_alloy(
selector[0], selector[1], selector[2], selector[3]
);
}
let mut vm = Vm::new(
code,
CallDataImpl {
selector: *selector,
},
);
let calldata = CallDataImpl { selector: *selector };
let mut vm = Vm::new(code, &calldata);
let mut args = ArgsResult::new();
let mut gas_used = 0;
let real_gas_limit = if gas_limit == 0 {
Expand Down
25 changes: 18 additions & 7 deletions src/evm/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,23 @@ impl<T> StepResult<T> {
}
}

#[derive(Clone)]
pub struct Vm<'a, T, U>
where
T: Clone + std::fmt::Debug,
U: Clone + CallData<T>,
U: CallData<T>,
{
pub code: &'a [u8],
pub pc: usize,
pub stack: Stack<T>,
pub memory: Memory<T>,
pub stopped: bool,
pub calldata: U,
pub calldata: &'a U,
}

impl<'a, T, U> fmt::Debug for Vm<'a, T, U>
where
T: Clone + std::fmt::Debug,
U: Clone + CallData<T>,
U: CallData<T>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
Expand All @@ -72,9 +71,9 @@ where
impl<'a, T, U> Vm<'a, T, U>
where
T: std::fmt::Debug + Clone + Eq,
U: Clone + CallData<T>,
U: CallData<T>,
{
pub fn new(code: &'a [u8], calldata: U) -> Self {
pub fn new(code: &'a [u8], calldata: &'a U) -> Self {
Self {
code,
pc: 0,
Expand All @@ -85,6 +84,18 @@ where
}
}

// not Clone trait because Cow experiments
pub fn clone(&'a self) -> Self {
Vm {
code: self.code,
pc: self.pc,
stack: self.stack.clone(),
memory: self.memory.clone(),
stopped: self.stopped,
calldata: self.calldata,
}
}

pub fn step(&mut self) -> Result<StepResult<T>, Box<dyn error::Error>> {
let op = self.code[self.pc];
let ret = self.exec_opcode(op)?;
Expand Down Expand Up @@ -647,7 +658,7 @@ mod tests {

#[test]
fn test_arithmetic() {
let mut vm = Vm::new(&[], DummyCallData {});
let mut vm = Vm::new(&[], &DummyCallData {});
let cases = [
(
I256::unchecked_from(-1).into_raw(),
Expand Down
1 change: 0 additions & 1 deletion src/selectors/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::Label;
use crate::evm::{calldata::CallData, element::Element, U256, VAL_4};
use std::error;

#[derive(Clone)]
pub(super) struct CallDataImpl {}

impl CallData<Label> for CallDataImpl {
Expand Down
2 changes: 1 addition & 1 deletion src/selectors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn process(
/// assert_eq!(selectors, vec![[0x21, 0x25, 0xb6, 0x5b], [0xb6, 0x9e, 0xf8, 0xa8]])
/// ```
pub fn function_selectors(code: &[u8], gas_limit: u32) -> Vec<Selector> {
let vm = Vm::new(code, CallDataImpl {});
let vm = Vm::new(code, &CallDataImpl {});
let mut selectors = BTreeSet::new();
process(
vm,
Expand Down
1 change: 0 additions & 1 deletion src/state_mutability/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::Label;
use crate::evm::{calldata::CallData, element::Element, U256, VAL_4, VAL_131072};
use std::error;

#[derive(Clone)]
pub(super) struct CallDataImpl {
pub selector: [u8; 4],
}
Expand Down
12 changes: 4 additions & 8 deletions src/state_mutability/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,10 @@ pub fn function_state_mutability(
selector: &Selector,
gas_limit: u32,
) -> StateMutability {
let mut cd: [u8; 32] = [0; 32];
cd[0..4].copy_from_slice(selector);
let vm = Vm::new(
code,
CallDataImpl {
selector: *selector,
},
);
let calldata = CallDataImpl {
selector: *selector,
};
let vm = Vm::new(code, &calldata);

let real_gas_limit = if gas_limit == 0 {
5e5 as u32
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloy_dyn_abi::DynSolType;
pub fn execute_until_function_start<T, U>(vm: &mut Vm<T, U>, gas_limit: u32) -> Option<u32>
where
T: Clone + std::fmt::Debug + std::cmp::Eq,
U: Clone + CallData<T>,
U: CallData<T>,
{
let mut gas_used = 0;
let mut found = false;
Expand Down

0 comments on commit 593bd8f

Please sign in to comment.