From 7b8f539cf06201af69e234e9155a983fda7408a3 Mon Sep 17 00:00:00 2001 From: Jon-Becker Date: Tue, 26 Dec 2023 15:23:30 -0600 Subject: [PATCH] chore(sym-exec): convert tuple to `JumpFrame` --- common/src/ether/evm/ext/exec/jump_frame.rs | 15 ++++++++++++ common/src/ether/evm/ext/exec/mod.rs | 27 ++++++++++----------- common/src/utils/range_map.rs | 6 +---- 3 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 common/src/ether/evm/ext/exec/jump_frame.rs diff --git a/common/src/ether/evm/ext/exec/jump_frame.rs b/common/src/ether/evm/ext/exec/jump_frame.rs new file mode 100644 index 00000000..f41e1781 --- /dev/null +++ b/common/src/ether/evm/ext/exec/jump_frame.rs @@ -0,0 +1,15 @@ +use ethers::types::U256; + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct JumpFrame { + pub pc: u128, + pub jumpdest: U256, + pub stack_depth: usize, + pub jump_taken: bool, +} + +impl JumpFrame { + pub fn new(pc: u128, jumpdest: U256, stack_depth: usize, jump_taken: bool) -> Self { + Self { pc, jumpdest, stack_depth, jump_taken } + } +} diff --git a/common/src/ether/evm/ext/exec/mod.rs b/common/src/ether/evm/ext/exec/mod.rs index c3607650..f676da51 100644 --- a/common/src/ether/evm/ext/exec/mod.rs +++ b/common/src/ether/evm/ext/exec/mod.rs @@ -1,10 +1,14 @@ +mod jump_frame; mod util; -use self::util::{ - jump_condition_appears_recursive, jump_condition_contains_mutated_memory_access, - jump_condition_contains_mutated_storage_access, - jump_condition_historical_diffs_approximately_equal, stack_contains_too_many_of_the_same_item, - stack_diff, stack_item_source_depth_too_deep, +use self::{ + jump_frame::JumpFrame, + util::{ + jump_condition_appears_recursive, jump_condition_contains_mutated_memory_access, + jump_condition_contains_mutated_storage_access, + jump_condition_historical_diffs_approximately_equal, + stack_contains_too_many_of_the_same_item, stack_diff, stack_item_source_depth_too_deep, + }, }; use crate::{ debug_max, @@ -14,7 +18,6 @@ use crate::{ }, utils::{io::logging::Logger, strings::decode_hex}, }; -use ethers::types::U256; use std::collections::HashMap; #[derive(Clone, Debug)] @@ -66,7 +69,7 @@ impl VM { fn recursive_map( &mut self, branch_count: &mut u32, - handled_jumps: &mut HashMap<(u128, U256, usize, bool), Vec>, + handled_jumps: &mut HashMap>, logger: &Logger, ) -> VMTrace { let mut vm = self.clone(); @@ -96,16 +99,12 @@ impl VM { state.last_instruction.instruction ); - // jump frame contains: - // 1. the instruction (PC) of the JUMPI - // 2. the jump destination - // 3. the stack size at the time of the JUMPI - // 4. whether the jump condition is zero - let jump_frame: (u128, U256, usize, bool) = ( + // build hashable jump frame + let jump_frame = JumpFrame::new( state.last_instruction.instruction, state.last_instruction.inputs[0], vm.stack.size(), - state.last_instruction.inputs[1].is_zero(), + !state.last_instruction.inputs[1].is_zero(), ); // if the stack has over 16 items of the same source, it's probably a loop diff --git a/common/src/utils/range_map.rs b/common/src/utils/range_map.rs index 2994dd5b..a30d4356 100644 --- a/common/src/utils/range_map.rs +++ b/common/src/utils/range_map.rs @@ -103,11 +103,7 @@ impl RangeMap { } fn affected_ranges(&self, range: Range) -> Vec> { - self.0 - .keys() - .filter(|incumbent| Self::range_collides(&range, *incumbent)) - .cloned() - .collect() + self.0.keys().filter(|incumbent| Self::range_collides(&range, incumbent)).cloned().collect() } fn range_collides(incoming: &Range, incumbent: &Range) -> bool {