foundry_evm_coverage/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! # foundry-evm-coverage
//!
//! EVM bytecode coverage analysis.

#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

#[macro_use]
extern crate tracing;

use alloy_primitives::{
    map::{B256HashMap, HashMap},
    Bytes,
};
use eyre::Result;
use foundry_compilers::artifacts::sourcemap::SourceMap;
use semver::Version;
use std::{
    collections::BTreeMap,
    fmt::Display,
    num::NonZeroU32,
    ops::{Deref, DerefMut, Range},
    path::{Path, PathBuf},
    sync::Arc,
};

pub mod analysis;
pub mod anchors;

mod inspector;
pub use inspector::CoverageCollector;

/// A coverage report.
///
/// A coverage report contains coverage items and opcodes corresponding to those items (called
/// "anchors"). A single coverage item may be referred to by multiple anchors.
#[derive(Clone, Debug, Default)]
pub struct CoverageReport {
    /// A map of source IDs to the source path.
    pub source_paths: HashMap<(Version, usize), PathBuf>,
    /// A map of source paths to source IDs.
    pub source_paths_to_ids: HashMap<(Version, PathBuf), usize>,
    /// All coverage items for the codebase, keyed by the compiler version.
    pub items: HashMap<Version, Vec<CoverageItem>>,
    /// All item anchors for the codebase, keyed by their contract ID.
    pub anchors: HashMap<ContractId, (Vec<ItemAnchor>, Vec<ItemAnchor>)>,
    /// All the bytecode hits for the codebase.
    pub bytecode_hits: HashMap<ContractId, HitMap>,
    /// The bytecode -> source mappings.
    pub source_maps: HashMap<ContractId, (SourceMap, SourceMap)>,
}

impl CoverageReport {
    /// Add a source file path.
    pub fn add_source(&mut self, version: Version, source_id: usize, path: PathBuf) {
        self.source_paths.insert((version.clone(), source_id), path.clone());
        self.source_paths_to_ids.insert((version, path), source_id);
    }

    /// Get the source ID for a specific source file path.
    pub fn get_source_id(&self, version: Version, path: PathBuf) -> Option<usize> {
        self.source_paths_to_ids.get(&(version, path)).copied()
    }

    /// Add the source maps.
    pub fn add_source_maps(
        &mut self,
        source_maps: impl IntoIterator<Item = (ContractId, (SourceMap, SourceMap))>,
    ) {
        self.source_maps.extend(source_maps);
    }

    /// Add coverage items to this report.
    pub fn add_items(&mut self, version: Version, items: impl IntoIterator<Item = CoverageItem>) {
        self.items.entry(version).or_default().extend(items);
    }

    /// Add anchors to this report.
    pub fn add_anchors(
        &mut self,
        anchors: impl IntoIterator<Item = (ContractId, (Vec<ItemAnchor>, Vec<ItemAnchor>))>,
    ) {
        self.anchors.extend(anchors);
    }

    /// Returns an iterator over coverage summaries by source file path.
    pub fn summary_by_file(&self) -> impl Iterator<Item = (&Path, CoverageSummary)> {
        self.by_file(|summary: &mut CoverageSummary, item| summary.add_item(item))
    }

    /// Returns an iterator over coverage items by source file path.
    pub fn items_by_file(&self) -> impl Iterator<Item = (&Path, Vec<&CoverageItem>)> {
        self.by_file(|list: &mut Vec<_>, item| list.push(item))
    }

    fn by_file<'a, T: Default>(
        &'a self,
        mut f: impl FnMut(&mut T, &'a CoverageItem),
    ) -> impl Iterator<Item = (&'a Path, T)> {
        let mut by_file: BTreeMap<&Path, T> = BTreeMap::new();
        for (version, items) in &self.items {
            for item in items {
                let key = (version.clone(), item.loc.source_id);
                let Some(path) = self.source_paths.get(&key) else { continue };
                f(by_file.entry(path).or_default(), item);
            }
        }
        by_file.into_iter()
    }

    /// Processes data from a [`HitMap`] and sets hit counts for coverage items in this coverage
    /// map.
    ///
    /// This function should only be called *after* all the relevant sources have been processed and
    /// added to the map (see [`add_source`](Self::add_source)).
    pub fn add_hit_map(
        &mut self,
        contract_id: &ContractId,
        hit_map: &HitMap,
        is_deployed_code: bool,
    ) -> Result<()> {
        // Add bytecode level hits
        self.bytecode_hits
            .entry(contract_id.clone())
            .and_modify(|m| m.merge(hit_map))
            .or_insert_with(|| hit_map.clone());

        // Add source level hits
        if let Some(anchors) = self.anchors.get(contract_id) {
            let anchors = if is_deployed_code { &anchors.1 } else { &anchors.0 };
            for anchor in anchors {
                if let Some(hits) = hit_map.get(anchor.instruction) {
                    self.items
                        .get_mut(&contract_id.version)
                        .and_then(|items| items.get_mut(anchor.item_id))
                        .expect("Anchor refers to non-existent coverage item")
                        .hits += hits.get();
                }
            }
        }
        Ok(())
    }

    /// Removes all the coverage items that should be ignored by the filter.
    ///
    /// This function should only be called after all the sources were used, otherwise, the output
    /// will be missing the ones that are dependent on them.
    pub fn filter_out_ignored_sources(&mut self, filter: impl Fn(&Path) -> bool) {
        self.items.retain(|version, items| {
            items.retain(|item| {
                self.source_paths
                    .get(&(version.clone(), item.loc.source_id))
                    .map(|path| filter(path))
                    .unwrap_or(false)
            });
            !items.is_empty()
        });
    }
}

/// A collection of [`HitMap`]s.
#[derive(Clone, Debug, Default)]
pub struct HitMaps(pub B256HashMap<HitMap>);

impl HitMaps {
    /// Merges two `Option<HitMaps>`.
    pub fn merge_opt(a: &mut Option<Self>, b: Option<Self>) {
        match (a, b) {
            (_, None) => {}
            (a @ None, Some(b)) => *a = Some(b),
            (Some(a), Some(b)) => a.merge(b),
        }
    }

    /// Merges two `HitMaps`.
    pub fn merge(&mut self, other: Self) {
        self.reserve(other.len());
        for (code_hash, other) in other.0 {
            self.entry(code_hash).and_modify(|e| e.merge(&other)).or_insert(other);
        }
    }

    /// Merges two `HitMaps`.
    pub fn merged(mut self, other: Self) -> Self {
        self.merge(other);
        self
    }
}

impl Deref for HitMaps {
    type Target = B256HashMap<HitMap>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for HitMaps {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

/// Hit data for an address.
///
/// Contains low-level data about hit counters for the instructions in the bytecode of a contract.
#[derive(Clone, Debug)]
pub struct HitMap {
    bytecode: Bytes,
    hits: HashMap<u32, u32>,
}

impl HitMap {
    /// Create a new hitmap with the given bytecode.
    #[inline]
    pub fn new(bytecode: Bytes) -> Self {
        Self { bytecode, hits: HashMap::with_capacity_and_hasher(1024, Default::default()) }
    }

    /// Returns the bytecode.
    #[inline]
    pub fn bytecode(&self) -> &Bytes {
        &self.bytecode
    }

    /// Returns the number of hits for the given program counter.
    #[inline]
    pub fn get(&self, pc: usize) -> Option<NonZeroU32> {
        NonZeroU32::new(self.hits.get(&Self::cvt_pc(pc)).copied().unwrap_or(0))
    }

    /// Increase the hit counter by 1 for the given program counter.
    #[inline]
    pub fn hit(&mut self, pc: usize) {
        self.hits(pc, 1)
    }

    /// Increase the hit counter by `hits` for the given program counter.
    #[inline]
    pub fn hits(&mut self, pc: usize, hits: u32) {
        *self.hits.entry(Self::cvt_pc(pc)).or_default() += hits;
    }

    /// Merge another hitmap into this, assuming the bytecode is consistent
    pub fn merge(&mut self, other: &Self) {
        self.hits.reserve(other.len());
        for (pc, hits) in other.iter() {
            self.hits(pc, hits);
        }
    }

    /// Returns an iterator over all the program counters and their hit counts.
    #[inline]
    pub fn iter(&self) -> impl Iterator<Item = (usize, u32)> + '_ {
        self.hits.iter().map(|(&pc, &hits)| (pc as usize, hits))
    }

    /// Returns the number of program counters hit in the hitmap.
    #[inline]
    pub fn len(&self) -> usize {
        self.hits.len()
    }

    /// Returns `true` if the hitmap is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.hits.is_empty()
    }

    #[inline]
    fn cvt_pc(pc: usize) -> u32 {
        pc.try_into().expect("4GiB bytecode")
    }
}

/// A unique identifier for a contract
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ContractId {
    pub version: Version,
    pub source_id: usize,
    pub contract_name: Arc<str>,
}

impl Display for ContractId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Contract \"{}\" (solc {}, source ID {})",
            self.contract_name, self.version, self.source_id
        )
    }
}

/// An item anchor describes what instruction marks a [CoverageItem] as covered.
#[derive(Clone, Debug)]
pub struct ItemAnchor {
    /// The program counter for the opcode of this anchor
    pub instruction: usize,
    /// The item ID this anchor points to
    pub item_id: usize,
}

impl Display for ItemAnchor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "IC {} -> Item {}", self.instruction, self.item_id)
    }
}

#[derive(Clone, Debug)]
pub enum CoverageItemKind {
    /// An executable line in the code.
    Line,
    /// A statement in the code.
    Statement,
    /// A branch in the code.
    Branch {
        /// The ID that identifies the branch.
        ///
        /// There may be multiple items with the same branch ID - they belong to the same branch,
        /// but represent different paths.
        branch_id: usize,
        /// The path ID for this branch.
        ///
        /// The first path has ID 0, the next ID 1, and so on.
        path_id: usize,
        /// If true, then the branch anchor is the first opcode within the branch source range.
        is_first_opcode: bool,
    },
    /// A function in the code.
    Function {
        /// The name of the function.
        name: String,
    },
}

#[derive(Clone, Debug)]
pub struct CoverageItem {
    /// The coverage item kind.
    pub kind: CoverageItemKind,
    /// The location of the item in the source code.
    pub loc: SourceLocation,
    /// The number of times this item was hit.
    pub hits: u32,
}

impl Display for CoverageItem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            CoverageItemKind::Line => {
                write!(f, "Line")?;
            }
            CoverageItemKind::Statement => {
                write!(f, "Statement")?;
            }
            CoverageItemKind::Branch { branch_id, path_id, .. } => {
                write!(f, "Branch (branch: {branch_id}, path: {path_id})")?;
            }
            CoverageItemKind::Function { name } => {
                write!(f, r#"Function "{name}""#)?;
            }
        }
        write!(f, " (location: {}, hits: {})", self.loc, self.hits)
    }
}

/// A source location.
#[derive(Clone, Debug)]
pub struct SourceLocation {
    /// The source ID.
    pub source_id: usize,
    /// The contract this source range is in.
    pub contract_name: Arc<str>,
    /// Byte range.
    pub bytes: Range<u32>,
    /// Line range. Indices are 1-based.
    pub lines: Range<u32>,
}

impl Display for SourceLocation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "source ID {}, lines {:?}, bytes {:?}", self.source_id, self.lines, self.bytes)
    }
}

impl SourceLocation {
    /// Returns the length of the byte range.
    pub fn len(&self) -> u32 {
        self.bytes.len() as u32
    }

    /// Returns true if the byte range is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Coverage summary for a source file.
#[derive(Clone, Debug, Default)]
pub struct CoverageSummary {
    /// The number of executable lines in the source file.
    pub line_count: usize,
    /// The number of lines that were hit.
    pub line_hits: usize,
    /// The number of statements in the source file.
    pub statement_count: usize,
    /// The number of statements that were hit.
    pub statement_hits: usize,
    /// The number of branches in the source file.
    pub branch_count: usize,
    /// The number of branches that were hit.
    pub branch_hits: usize,
    /// The number of functions in the source file.
    pub function_count: usize,
    /// The number of functions hit.
    pub function_hits: usize,
}

impl CoverageSummary {
    /// Creates a new, empty coverage summary.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a coverage summary from a collection of coverage items.
    pub fn from_items<'a>(items: impl IntoIterator<Item = &'a CoverageItem>) -> Self {
        let mut summary = Self::default();
        summary.add_items(items);
        summary
    }

    /// Adds another coverage summary to this one.
    pub fn merge(&mut self, other: &Self) {
        let Self {
            line_count,
            line_hits,
            statement_count,
            statement_hits,
            branch_count,
            branch_hits,
            function_count,
            function_hits,
        } = self;
        *line_count += other.line_count;
        *line_hits += other.line_hits;
        *statement_count += other.statement_count;
        *statement_hits += other.statement_hits;
        *branch_count += other.branch_count;
        *branch_hits += other.branch_hits;
        *function_count += other.function_count;
        *function_hits += other.function_hits;
    }

    /// Adds a coverage item to this summary.
    pub fn add_item(&mut self, item: &CoverageItem) {
        match item.kind {
            CoverageItemKind::Line => {
                self.line_count += 1;
                if item.hits > 0 {
                    self.line_hits += 1;
                }
            }
            CoverageItemKind::Statement => {
                self.statement_count += 1;
                if item.hits > 0 {
                    self.statement_hits += 1;
                }
            }
            CoverageItemKind::Branch { .. } => {
                self.branch_count += 1;
                if item.hits > 0 {
                    self.branch_hits += 1;
                }
            }
            CoverageItemKind::Function { .. } => {
                self.function_count += 1;
                if item.hits > 0 {
                    self.function_hits += 1;
                }
            }
        }
    }

    /// Adds multiple coverage items to this summary.
    pub fn add_items<'a>(&mut self, items: impl IntoIterator<Item = &'a CoverageItem>) {
        for item in items {
            self.add_item(item);
        }
    }
}