Skip to content

docs: CoverageCollector comments #9474

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

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Changes from all commits
Commits
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
12 changes: 11 additions & 1 deletion crates/evm/coverage/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ use std::ptr::NonNull;
/// Inspector implementation for collecting coverage information.
#[derive(Clone, Debug)]
pub struct CoverageCollector {
// NOTE: `current_map` is always a valid reference into `maps`.
// It is accessed only through `get_or_insert_map` which guarantees that it's valid.
// Both of these fields are unsafe to access directly outside of `*insert_map`.
current_map: NonNull<HitMap>,
current_hash: B256,

maps: HitMaps,
}

// SAFETY: `current_map` is always valid and points into an allocation managed by self.
// SAFETY: See comments on `current_map`.
unsafe impl Send for CoverageCollector {}
unsafe impl Sync for CoverageCollector {}

Expand Down Expand Up @@ -44,12 +48,17 @@ impl CoverageCollector {
self.maps
}

/// Gets the hit map for the current contract, or inserts a new one if it doesn't exist.
///
/// The map is stored in `current_map` and returned as a mutable reference.
/// See comments on `current_map` for more details.
#[inline]
fn get_or_insert_map(&mut self, interpreter: &mut Interpreter) -> &mut HitMap {
let hash = get_or_insert_contract_hash(interpreter);
if self.current_hash != *hash {
self.insert_map(interpreter);
}
// SAFETY: See comments on `current_map`.
unsafe { self.current_map.as_mut() }
}

Expand All @@ -58,6 +67,7 @@ impl CoverageCollector {
fn insert_map(&mut self, interpreter: &Interpreter) {
let Some(hash) = interpreter.contract.hash else { eof_panic() };
self.current_hash = hash;
// Converts the mutable reference to a `NonNull` pointer.
self.current_map = self
.maps
.entry(hash)
Expand Down
Loading