Skip to content

Commit

Permalink
Allow external construction of the storage slot hashes (#99)
Browse files Browse the repository at this point in the history
# Summary

Adds support for external construction of the storage slot hashes

# Details 
- Adds a new method to `StorageSlotHashes` so it can be constructed with a predefined BitMap of the hashes. 
- Adds a new method to `LiftingPasses` so it can be constructed with a vector of passes 

# Checklist

- [x] Code is formatted by Rustfmt.
- [x] Documentation has been updated if necessary.

Fixes #97
  • Loading branch information
2xic authored Sep 28, 2023
1 parent 355558a commit 5db8153
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/inference/lift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ pub struct LiftingPasses {
}

impl LiftingPasses {
/// Creates a new instance of the lifting pass with the provided `passes`
#[must_use]
pub fn new(passes: impl Into<Vec<Box<dyn Lift>>>) -> Self {
Self {
passes: passes.into(),
}
}

/// Adds the `pass` to the end of the pass ordering.
///
/// If a pass of the given type already exists in the ordering, it will not
Expand Down
45 changes: 30 additions & 15 deletions src/inference/lift/recognise_hashed_slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//! [`SLOT_COUNT`] storage slot indices in order to make the recognition of
//! dynamic array accesses easier.

use std::sync::{Arc, RwLock};

use bimap::BiMap;
use ethnum::U256;
use sha3::{Digest, Keccak256};
Expand All @@ -28,17 +30,25 @@ pub const SLOT_COUNT: usize = 10000;
///
/// where:
/// - `C` is the sha3 hash of one of the first [`SLOT_COUNT`] integers.
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug)]
pub struct StorageSlotHashes {
hashes: BiMap<U256, usize>,
hashes: Arc<RwLock<BiMap<U256, usize>>>,
}

impl StorageSlotHashes {
/// Creates a new instance of the mapping access lifting pass.
#[must_use]
pub fn new() -> Box<Self> {
let hashes = Self::make_hashes(SLOT_COUNT);
Box::new(Self { hashes })
let hashes = Arc::new(RwLock::new(Self::make_hashes(SLOT_COUNT)));
Self::new_with_hashes(hashes)
}

// Creates a new instance with the provided hashes
#[must_use]
pub fn new_with_hashes(hashes: impl Into<Arc<RwLock<BiMap<U256, usize>>>>) -> Box<Self> {
Box::new(Self {
hashes: hashes.into(),
})
}

/// Generates the slot hashes for the first `count` slots, assuming
Expand Down Expand Up @@ -79,17 +89,22 @@ impl Lift for StorageSlotHashes {

// Now we can look up the hash we found, and convert it to the `Sha3` of a known
// value if it is one.
if let Some(slot_index) = hashes.get_by_left(&known_value.value_le()) {
let data = RSV::new_known_value(
value_clone.instruction_pointer(),
KnownWord::from(*slot_index),
value_clone.provenance(),
None,
);

Some(RSVD::Sha3 { data })
} else {
None
match hashes.read() {
Ok(hashes) => {
if let Some(slot_index) = hashes.get_by_left(&known_value.value_le()) {
let data = RSV::new_known_value(
value_clone.instruction_pointer(),
KnownWord::from(*slot_index),
value_clone.provenance(),
None,
);

Some(RSVD::Sha3 { data })
} else {
None
}
}
_ => None,
}
};

Expand Down

0 comments on commit 5db8153

Please sign in to comment.