Skip to main content

foundry_evm_traces/debug/
sources.rs

1use eyre::{Context, Result};
2use foundry_common::{
3    compact_to_contract, external_compiler::is_external_artifact, is_deploy_helper_path,
4    strip_bytecode_placeholders,
5};
6use foundry_compilers::{
7    Artifact, ProjectCompileOutput,
8    artifacts::{
9        Bytecode, ContractBytecodeSome, Libraries, Source, Sources,
10        sourcemap::{SourceElement, SourceMap},
11    },
12    multi::MultiCompilerLanguage,
13};
14use foundry_config::read_build_info_input;
15use foundry_evm_core::ic::PcIcMap;
16use foundry_linking::Linker;
17use rayon::prelude::*;
18use serde::Deserialize;
19use std::{
20    collections::{BTreeMap, HashMap, HashSet},
21    fmt::Write,
22    ops::Range,
23    path::{Path, PathBuf},
24    sync::Arc,
25};
26
27#[derive(Clone, Debug)]
28pub struct SourceData {
29    pub source: Arc<String>,
30    pub language: MultiCompilerLanguage,
31    pub path: PathBuf,
32    /// Maps contract name to (start, end) of the contract definition in the source code.
33    /// This is useful for determining which contract contains given function definition.
34    pub contract_definitions: Vec<(String, Range<usize>)>,
35}
36
37impl SourceData {
38    pub fn new(
39        output: &ProjectCompileOutput,
40        source: Arc<String>,
41        language: MultiCompilerLanguage,
42        path: PathBuf,
43        root: &Path,
44    ) -> Self {
45        let mut contract_definitions = Vec::new();
46
47        match language {
48            MultiCompilerLanguage::Vyper(_) => {
49                // Vyper contracts have the same name as the file name.
50                if let Some(name) = path.file_stem().map(|s| s.to_string_lossy().to_string()) {
51                    contract_definitions.push((name, 0..source.len()));
52                }
53            }
54            MultiCompilerLanguage::Solc(_) => {
55                let r = output.parser().solc().compiler().enter(|compiler| -> Option<()> {
56                    let (_, source) = compiler.gcx().get_ast_source(root.join(&path))?;
57                    let source_map = compiler.sess().source_map();
58                    for item in source.ast.as_ref()?.items.iter() {
59                        if let solar::ast::ItemKind::Contract(contract) = &item.kind {
60                            let Some(contract_range) = span_to_range(source_map, item.span) else {
61                                continue;
62                            };
63                            contract_definitions.push((contract.name.to_string(), contract_range));
64                        }
65                    }
66                    Some(())
67                });
68                if r.is_none() {
69                    warn!("failed to parse contract definitions for {}", path.display());
70                }
71            }
72        }
73
74        Self { source, language, path, contract_definitions }
75    }
76
77    /// Finds name of contract that contains given loc.
78    pub fn find_contract_name(&self, start: usize, end: usize) -> Option<&str> {
79        self.contract_definitions
80            .iter()
81            .find(|(_, r)| start >= r.start && end <= r.end)
82            .map(|(name, _)| name.as_str())
83    }
84}
85
86fn span_to_range(
87    source_map: &solar::interface::source_map::SourceMap,
88    span: solar::interface::Span,
89) -> Option<Range<usize>> {
90    source_map.span_to_range(span).ok()
91}
92
93#[derive(Clone, Debug)]
94pub struct ArtifactData {
95    pub source_map: Option<SourceMap>,
96    pub source_map_runtime: Option<SourceMap>,
97    pub pc_ic_map: Option<PcIcMap>,
98    pub pc_ic_map_runtime: Option<PcIcMap>,
99    pub build_id: String,
100    pub file_id: u32,
101    /// Whether the contract was compiled through the via-IR pipeline, if the metadata says.
102    pub via_ir: Option<bool>,
103}
104
105impl ArtifactData {
106    fn new(
107        bytecode: ContractBytecodeSome,
108        build_id: String,
109        file_id: u32,
110        via_ir: Option<bool>,
111    ) -> Result<Self> {
112        let parse = |b: &Bytecode, name: &str| {
113            // Only parse source map if it's not empty.
114            let source_map = if b.source_map.as_ref().is_none_or(|s| s.is_empty()) {
115                Ok(None)
116            } else {
117                b.source_map().transpose().wrap_err_with(|| {
118                    format!("failed to parse {name} source map of file {file_id} in {build_id}")
119                })
120            };
121
122            // Only parse bytecode if it's not empty, stripping placeholders if necessary.
123            let pc_ic_map = strip_bytecode_placeholders(&b.object)
124                .and_then(|bytes| (!bytes.is_empty()).then(|| PcIcMap::new(bytes.as_ref())));
125
126            source_map.map(|source_map| (source_map, pc_ic_map))
127        };
128        let (source_map, pc_ic_map) = parse(&bytecode.bytecode, "creation")?;
129        let (source_map_runtime, pc_ic_map_runtime) = bytecode
130            .deployed_bytecode
131            .bytecode
132            .map(|b| parse(&b, "runtime"))
133            .unwrap_or_else(|| Ok((None, None)))?;
134
135        Ok(Self {
136            source_map,
137            source_map_runtime,
138            pc_ic_map,
139            pc_ic_map_runtime,
140            build_id,
141            file_id,
142            via_ir,
143        })
144    }
145}
146
147/// Container with artifacts data useful for identifying individual execution steps.
148#[derive(Clone, Debug, Default)]
149pub struct ContractSources {
150    /// Map over build_id -> file_id -> (source code, language)
151    pub sources_by_id: HashMap<String, HashMap<u32, Arc<SourceData>>>,
152    /// Map over contract name -> Vec<(bytecode, build_id, file_id)>
153    pub artifacts_by_name: HashMap<String, Vec<ArtifactData>>,
154}
155
156impl ContractSources {
157    /// Collects the contract sources and artifacts from the project compile output.
158    pub fn from_project_output(
159        output: &ProjectCompileOutput,
160        root: &Path,
161        libraries: Option<&Libraries>,
162    ) -> Result<Self> {
163        let mut sources = Self::default();
164        sources.insert(output, root, libraries)?;
165        Ok(sources)
166    }
167
168    pub fn insert(
169        &mut self,
170        output: &ProjectCompileOutput,
171        root: &Path,
172        libraries: Option<&Libraries>,
173    ) -> Result<()> {
174        let link_data = libraries.map(|libraries| {
175            let linker = Linker::new(root, output.artifact_ids().collect());
176            (linker, libraries)
177        });
178
179        let artifacts: Vec<_> = output
180            .artifact_ids()
181            .filter(|(id, _)| !is_external_artifact(&id.build_id))
182            .collect::<Vec<_>>()
183            .par_iter()
184            .map(|(id, artifact)| {
185                let mut new_artifact = None;
186                if let Some(file_id) = artifact.id {
187                    // Solc records `viaIR` in the metadata settings only when it is enabled.
188                    let via_ir = artifact
189                        .metadata
190                        .as_ref()
191                        .map(|metadata| metadata.settings.via_ir.unwrap_or_default());
192                    let artifact = if let Some((linker, libraries)) = link_data.as_ref() {
193                        linker.link(id, libraries)?
194                    } else {
195                        artifact.get_contract_bytecode()
196                    };
197                    let bytecode = compact_to_contract(artifact.into_contract_bytecode())?;
198
199                    new_artifact = Some((
200                        id.name.clone(),
201                        ArtifactData::new(bytecode, id.build_id.clone(), file_id, via_ir)?,
202                    ));
203                } else {
204                    warn!(id = id.identifier(), "source not found");
205                };
206
207                Ok(new_artifact)
208            })
209            .collect::<Result<Vec<_>>>()?;
210
211        for (name, artifact) in artifacts.into_iter().flatten() {
212            self.artifacts_by_name.entry(name).or_default().push(artifact);
213        }
214
215        // Not all source files produce artifacts, so we are populating sources by using build
216        // infos.
217        let mut files: BTreeMap<PathBuf, Arc<SourceData>> = BTreeMap::new();
218        let mut removed_files = HashSet::new();
219        for (build_id, build) in output.builds() {
220            for (source_id, path) in &build.source_id_to_path {
221                if !path.exists() {
222                    // Preprocessor deploy helpers are compiled from memory and never exist on disk.
223                    if !is_deploy_helper_path(path) {
224                        removed_files.insert(path);
225                    }
226                    continue;
227                }
228
229                let source_data = match files.entry(path.clone()) {
230                    std::collections::btree_map::Entry::Vacant(entry) => {
231                        let source = Source::read(path).wrap_err_with(|| {
232                            format!("failed to read artifact source file for `{}`", path.display())
233                        })?;
234                        let stripped = path.strip_prefix(root).unwrap_or(path).to_path_buf();
235                        let source_data = Arc::new(SourceData::new(
236                            output,
237                            source.content,
238                            build.language,
239                            stripped,
240                            root,
241                        ));
242                        entry.insert(source_data.clone());
243                        source_data
244                    }
245                    std::collections::btree_map::Entry::Occupied(entry) => entry.get().clone(),
246                };
247                self.sources_by_id
248                    .entry(build_id.clone())
249                    .or_default()
250                    .insert(*source_id, source_data);
251            }
252        }
253
254        if !removed_files.is_empty() {
255            let mut warning = "Detected artifacts built from source files that no longer exist. \
256                Run `forge clean` to make sure builds are in sync with project files."
257                .to_string();
258            for file in removed_files {
259                write!(warning, "\n - {}", file.display())?;
260            }
261            let _ = sh_warn!("{}", warning);
262        }
263
264        Ok(())
265    }
266
267    /// Replaces each source that the compiler saw with different text than the file on disk, such
268    /// as a test rewritten by dynamic test linking, with that text. Source maps point into it.
269    ///
270    /// The text comes from the full build infos (`build_info = true`) in `build_info_dir`. A build
271    /// without one keeps the files on disk.
272    pub fn insert_compiled_sources(&mut self, build_info_dir: &Path) {
273        for (build_id, sources) in &mut self.sources_by_id {
274            let path = build_info_dir.join(build_id).with_extension("json");
275            if !path.exists() {
276                continue;
277            }
278            let mut input = match read_build_info_input::<BuildInfoInput>(&path) {
279                Ok(Some(input)) => input,
280                Ok(None) => continue,
281                Err(error) => {
282                    let _ = sh_warn!("skipping build info {}: {error}", path.display());
283                    continue;
284                }
285            };
286            for source in sources.values_mut() {
287                if let Some(compiled) = input.sources.remove(&source.path)
288                    && compiled.content != source.source
289                {
290                    *source =
291                        Arc::new(SourceData { source: compiled.content, ..(**source).clone() });
292                }
293            }
294        }
295    }
296
297    /// Merges given contract sources.
298    pub fn merge(&mut self, sources: Self) {
299        self.sources_by_id.extend(sources.sources_by_id);
300        for (name, artifacts) in sources.artifacts_by_name {
301            self.artifacts_by_name.entry(name).or_default().extend(artifacts);
302        }
303    }
304
305    /// Returns all sources for a contract by name.
306    pub fn get_sources(
307        &self,
308        name: &str,
309    ) -> Option<impl Iterator<Item = (&ArtifactData, &SourceData)>> {
310        self.artifacts_by_name.get(name).map(|artifacts| {
311            artifacts.iter().filter_map(|artifact| {
312                let source =
313                    self.sources_by_id.get(artifact.build_id.as_str())?.get(&artifact.file_id)?;
314                Some((artifact, source.as_ref()))
315            })
316        })
317    }
318
319    /// Returns all (name, bytecode, source) sets.
320    pub fn entries(&self) -> impl Iterator<Item = (&str, &ArtifactData, &SourceData)> {
321        self.artifacts_by_name.iter().flat_map(|(name, artifacts)| {
322            artifacts.iter().filter_map(|artifact| {
323                let source =
324                    self.sources_by_id.get(artifact.build_id.as_str())?.get(&artifact.file_id)?;
325                Some((name.as_str(), artifact, source.as_ref()))
326            })
327        })
328    }
329
330    pub fn find_source_mapping(
331        &self,
332        contract_name: &str,
333        pc: u32,
334        init_code: bool,
335    ) -> Option<(SourceElement, &SourceData)> {
336        self.get_sources(contract_name)?.find_map(|(artifact, source)| {
337            let source_map = if init_code {
338                artifact.source_map.as_ref()
339            } else {
340                artifact.source_map_runtime.as_ref()
341            }?;
342
343            // Solc indexes source maps by instruction counter, but Vyper indexes by program
344            // counter.
345            let source_element = if matches!(source.language, MultiCompilerLanguage::Solc(_)) {
346                let pc_ic_map = if init_code {
347                    artifact.pc_ic_map.as_ref()
348                } else {
349                    artifact.pc_ic_map_runtime.as_ref()
350                }?;
351                let ic = pc_ic_map.get(pc)?;
352
353                source_map.get(ic as usize)
354            } else {
355                source_map.get(pc as usize)
356            }?;
357            // if the source element has an index, find the sourcemap for that index
358            source_element
359                .index()
360                // if index matches current file_id, return current source code
361                .and_then(|index| {
362                    (index == artifact.file_id).then(|| (source_element.clone(), source))
363                })
364                .or_else(|| {
365                    // otherwise find the source code for the element's index
366                    self.sources_by_id
367                        .get(&artifact.build_id)?
368                        .get(&source_element.index()?)
369                        .map(|source| (source_element.clone(), source.as_ref()))
370                })
371        })
372    }
373}
374
375/// The compiler input of a build info, which only full build infos have.
376#[derive(Deserialize)]
377struct BuildInfoInput {
378    sources: Sources,
379}
380
381#[cfg(test)]
382mod tests {
383    use super::*;
384
385    #[test]
386    fn compiled_sources_replace_rewritten_files() {
387        let source = |path: &str, text: &str| {
388            Arc::new(SourceData {
389                source: Arc::new(text.to_string()),
390                language: Default::default(),
391                path: PathBuf::from(path),
392                contract_definitions: Vec::new(),
393            })
394        };
395        let mut sources = ContractSources::default();
396        sources.sources_by_id.insert(
397            "build".to_string(),
398            HashMap::from([
399                (0, source("src/A.sol", "contract A {}")),
400                (1, source("test/A.t.sol", "contract T { A a = new A(); }")),
401            ]),
402        );
403        // Only `build` has a full build info, and it records a rewritten test file.
404        sources.sources_by_id.insert(
405            "other".to_string(),
406            HashMap::from([(0, source("src/A.sol", "contract A {}"))]),
407        );
408        let dir = tempfile::tempdir().unwrap();
409        let build_info = serde_json::json!({
410            "id": "build",
411            "input": { "sources": {
412                "src/A.sol": { "content": "contract A {}" },
413                "test/A.t.sol": { "content": "contract T { A a = A(deploy()); }" },
414            }},
415        });
416        // The read stops before the output, so a truncated output does not matter.
417        let build_info = build_info.to_string();
418        let build_info =
419            format!(r#"{},"output":{{"contracts":"#, &build_info[..build_info.len() - 1]);
420        std::fs::write(dir.path().join("build.json"), build_info).unwrap();
421        // A malformed build info is skipped.
422        std::fs::write(dir.path().join("other.json"), "{").unwrap();
423
424        sources.insert_compiled_sources(dir.path());
425
426        let text =
427            |build: &str, id: u32| sources.sources_by_id[build][&id].source.as_str().to_owned();
428        assert_eq!(text("build", 0), "contract A {}");
429        assert_eq!(text("build", 1), "contract T { A a = A(deploy()); }");
430        assert_eq!(sources.sources_by_id["build"][&1].path, PathBuf::from("test/A.t.sol"));
431        assert_eq!(text("other", 0), "contract A {}");
432    }
433}