foundry_evm_coverage/
anchors.rs1use super::{CoverageItemKind, ItemAnchor, SourceLocation};
2use crate::analysis::SourceAnalysis;
3use alloy_primitives::map::rustc_hash::FxHashSet;
4use eyre::ensure;
5use foundry_compilers::artifacts::sourcemap::{SourceElement, SourceMap};
6use foundry_evm_core::{bytecode::InstIter, ic::IcPcMap};
7use revm::bytecode::opcode;
8
9pub fn find_anchors(
11 bytecode: &[u8],
12 source_map: &SourceMap,
13 ic_pc_map: &IcPcMap,
14 analysis: &SourceAnalysis,
15) -> Vec<ItemAnchor> {
16 let mut seen_sources = FxHashSet::default();
17 source_map
18 .iter()
19 .filter_map(|element| element.index())
20 .filter(|&source| seen_sources.insert(source))
21 .flat_map(|source| analysis.items_for_source_enumerated(source))
22 .filter_map(|(item_id, item)| {
23 match item.kind {
24 CoverageItemKind::Branch { path_id, is_first_opcode: false, .. } => {
25 find_anchor_branch(bytecode, source_map, item_id, &item.loc).map(|anchors| {
26 match path_id {
27 0 => anchors.0,
28 1 => anchors.1,
29 _ => panic!("too many path IDs for branch"),
30 }
31 })
32 }
33 _ => find_anchor_simple(source_map, ic_pc_map, item_id, &item.loc),
34 }
35 .inspect_err(|err| warn!(%item, %err, "could not find anchor"))
36 .ok()
37 })
38 .collect()
39}
40
41pub fn find_anchor_simple(
43 source_map: &SourceMap,
44 ic_pc_map: &IcPcMap,
45 item_id: u32,
46 loc: &SourceLocation,
47) -> eyre::Result<ItemAnchor> {
48 let instruction =
49 source_map.iter().position(|element| is_in_source_range(element, loc)).ok_or_else(
50 || eyre::eyre!("Could not find anchor: No matching instruction in range {loc}"),
51 )?;
52
53 Ok(ItemAnchor {
54 instruction: ic_pc_map.get(instruction as u32).ok_or_else(|| {
55 eyre::eyre!("We found an anchor, but we can't translate it to a program counter")
56 })?,
57 item_id,
58 })
59}
60
61pub fn find_anchor_branch(
90 bytecode: &[u8],
91 source_map: &SourceMap,
92 item_id: u32,
93 loc: &SourceLocation,
94) -> eyre::Result<(ItemAnchor, ItemAnchor)> {
95 let mut anchors: Option<(ItemAnchor, ItemAnchor)> = None;
96 for (ic, (pc, inst)) in InstIter::new(bytecode).with_pc().enumerate() {
97 if (opcode::PUSH1..=opcode::PUSH32).contains(&inst.opcode.get()) {
102 let Some(element) = source_map.get(ic) else {
103 break;
106 };
107
108 let next_pc = pc + inst.immediate.len() + 1;
111 let push_size = inst.immediate.len();
112 if bytecode.get(next_pc).copied() == Some(opcode::JUMPI)
113 && is_in_source_range(element, loc)
114 {
115 ensure!(push_size <= 4, "jump destination overflow");
117
118 let mut pc_bytes = [0u8; 4];
120 pc_bytes[4 - push_size..].copy_from_slice(inst.immediate);
121 let pc_jump = u32::from_be_bytes(pc_bytes);
122 anchors = Some((
123 ItemAnchor {
124 item_id,
125 instruction: (next_pc + 1) as u32,
127 },
128 ItemAnchor { item_id, instruction: pc_jump },
129 ));
130 }
131 }
132 }
133
134 anchors.ok_or_else(|| eyre::eyre!("Could not detect branches in source: {}", loc))
135}
136
137fn is_in_source_range(element: &SourceElement, location: &SourceLocation) -> bool {
139 let source_ids_match = element.index_i32() == location.source_id as i32;
141 if !source_ids_match {
142 return false;
143 }
144
145 let is_within_start = element.offset() >= location.bytes.start;
147 if !is_within_start {
148 return false;
149 }
150
151 let start_of_ranges = location.bytes.start.max(element.offset());
152 let end_of_ranges =
153 (location.bytes.start + location.len()).min(element.offset() + element.length());
154 let within_ranges = start_of_ranges <= end_of_ranges;
155 if !within_ranges {
156 return false;
157 }
158
159 true
160}