@@ -6,12 +6,16 @@ use std::ptr::NonNull;
66/// Inspector implementation for collecting coverage information.
77#[ derive( Clone , Debug ) ]
88pub struct CoverageCollector {
9+ // NOTE: `current_map` is always a valid reference into `maps`.
10+ // It is accessed only through `get_or_insert_map` which guarantees that it's valid.
11+ // Both of these fields are unsafe to access directly outside of `*insert_map`.
912 current_map : NonNull < HitMap > ,
1013 current_hash : B256 ,
14+
1115 maps : HitMaps ,
1216}
1317
14- // SAFETY: `current_map` is always valid and points into an allocation managed by self .
18+ // SAFETY: See comments on `current_map` .
1519unsafe impl Send for CoverageCollector { }
1620unsafe impl Sync for CoverageCollector { }
1721
@@ -44,12 +48,17 @@ impl CoverageCollector {
4448 self . maps
4549 }
4650
51+ /// Gets the hit map for the current contract, or inserts a new one if it doesn't exist.
52+ ///
53+ /// The map is stored in `current_map` and returned as a mutable reference.
54+ /// See comments on `current_map` for more details.
4755 #[ inline]
4856 fn get_or_insert_map ( & mut self , interpreter : & mut Interpreter ) -> & mut HitMap {
4957 let hash = get_or_insert_contract_hash ( interpreter) ;
5058 if self . current_hash != * hash {
5159 self . insert_map ( interpreter) ;
5260 }
61+ // SAFETY: See comments on `current_map`.
5362 unsafe { self . current_map . as_mut ( ) }
5463 }
5564
@@ -58,6 +67,7 @@ impl CoverageCollector {
5867 fn insert_map ( & mut self , interpreter : & Interpreter ) {
5968 let Some ( hash) = interpreter. contract . hash else { eof_panic ( ) } ;
6069 self . current_hash = hash;
70+ // Converts the mutable reference to a `NonNull` pointer.
6171 self . current_map = self
6272 . maps
6373 . entry ( hash)
0 commit comments