rustc_codegen_llvm/coverageinfo/
mapgen.rs

1use std::assert_matches::assert_matches;
2use std::sync::Arc;
3
4use itertools::Itertools;
5use rustc_abi::Align;
6use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, ConstCodegenMethods};
7use rustc_data_structures::fx::FxIndexMap;
8use rustc_index::IndexVec;
9use rustc_middle::ty::TyCtxt;
10use rustc_span::{RemapPathScopeComponents, SourceFile, StableSourceFileId};
11use tracing::debug;
12
13use crate::common::CodegenCx;
14use crate::coverageinfo::llvm_cov;
15use crate::coverageinfo::mapgen::covfun::prepare_covfun_record;
16use crate::{TryFromU32, llvm};
17
18mod covfun;
19mod spans;
20mod unused;
21
22/// Version number that will be included the `__llvm_covmap` section header.
23/// Corresponds to LLVM's `llvm::coverage::CovMapVersion` (in `CoverageMapping.h`),
24/// or at least the subset that we know and care about.
25///
26/// Note that version `n` is encoded as `(n-1)`.
27#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, TryFromU32)]
28enum CovmapVersion {
29    /// Used by LLVM 18 onwards.
30    Version7 = 6,
31}
32
33impl CovmapVersion {
34    fn to_u32(self) -> u32 {
35        self as u32
36    }
37}
38
39/// Generates and exports the coverage map, which is embedded in special
40/// linker sections in the final binary.
41///
42/// Those sections are then read and understood by LLVM's `llvm-cov` tool,
43/// which is distributed in the `llvm-tools` rustup component.
44pub(crate) fn finalize(cx: &mut CodegenCx<'_, '_>) {
45    let tcx = cx.tcx;
46
47    // Ensure that LLVM is using a version of the coverage mapping format that
48    // agrees with our Rust-side code. Expected versions are:
49    // - `Version7` (6) used by LLVM 18 onwards.
50    let covmap_version =
51        CovmapVersion::try_from(llvm_cov::mapping_version()).unwrap_or_else(|raw_version: u32| {
52            panic!("unknown coverage mapping version reported by `llvm-wrapper`: {raw_version}")
53        });
54    assert_matches!(covmap_version, CovmapVersion::Version7);
55
56    debug!("Generating coverage map for CodegenUnit: `{}`", cx.codegen_unit.name());
57
58    // FIXME(#132395): Can this be none even when coverage is enabled?
59    let Some(ref coverage_cx) = cx.coverage_cx else { return };
60
61    let mut covfun_records = coverage_cx
62        .instances_used()
63        .into_iter()
64        // Sort by symbol name, so that the global file table is built in an
65        // order that doesn't depend on the stable-hash-based order in which
66        // instances were visited during codegen.
67        .sorted_by_cached_key(|&instance| tcx.symbol_name(instance).name)
68        .filter_map(|instance| prepare_covfun_record(tcx, instance, true))
69        .collect::<Vec<_>>();
70
71    // In a single designated CGU, also prepare covfun records for functions
72    // in this crate that were instrumented for coverage, but are unused.
73    if cx.codegen_unit.is_code_coverage_dead_code_cgu() {
74        unused::prepare_covfun_records_for_unused_functions(cx, &mut covfun_records);
75    }
76
77    // If there are no covfun records for this CGU, don't generate a covmap record.
78    // Emitting a covmap record without any covfun records causes `llvm-cov` to
79    // fail when generating coverage reports, and if there are no covfun records
80    // then the covmap record isn't useful anyway.
81    // This should prevent a repeat of <https://github.com/rust-lang/rust/issues/133606>.
82    if covfun_records.is_empty() {
83        return;
84    }
85
86    // Prepare the global file table for this CGU, containing all paths needed
87    // by one or more covfun records.
88    let global_file_table =
89        GlobalFileTable::build(tcx, covfun_records.iter().flat_map(|c| c.all_source_files()));
90
91    for covfun in &covfun_records {
92        covfun::generate_covfun_record(cx, &global_file_table, covfun)
93    }
94
95    // Generate the coverage map header, which contains the filenames used by
96    // this CGU's coverage mappings, and store it in a well-known global.
97    // (This is skipped if we returned early due to having no covfun records.)
98    generate_covmap_record(cx, covmap_version, &global_file_table.filenames_buffer);
99}
100
101/// Maps "global" (per-CGU) file ID numbers to their underlying source file paths.
102#[derive(Debug)]
103struct GlobalFileTable {
104    /// This "raw" table doesn't include the working dir, so a file's
105    /// global ID is its index in this set **plus one**.
106    raw_file_table: FxIndexMap<StableSourceFileId, String>,
107
108    /// The file table in encoded form (possibly compressed), which can be
109    /// included directly in this CGU's `__llvm_covmap` record.
110    filenames_buffer: Vec<u8>,
111
112    /// Truncated hash of the bytes in `filenames_buffer`.
113    ///
114    /// The `llvm-cov` tool uses this hash to associate each covfun record with
115    /// its corresponding filenames table, since the final binary will typically
116    /// contain multiple covmap records from different compilation units.
117    filenames_hash: u64,
118}
119
120impl GlobalFileTable {
121    /// Builds a "global file table" for this CGU, mapping numeric IDs to
122    /// path strings.
123    fn build<'a>(tcx: TyCtxt<'_>, all_files: impl Iterator<Item = &'a SourceFile>) -> Self {
124        let mut raw_file_table = FxIndexMap::default();
125
126        for file in all_files {
127            raw_file_table.entry(file.stable_id).or_insert_with(|| {
128                file.name.display(RemapPathScopeComponents::COVERAGE).to_string_lossy().into_owned()
129            });
130        }
131
132        // FIXME(Zalathar): Consider sorting the file table here, but maybe
133        // only after adding filename support to coverage-dump, so that the
134        // table order isn't directly visible in `.coverage-map` snapshots.
135
136        let mut table = Vec::with_capacity(raw_file_table.len() + 1);
137
138        // Since version 6 of the LLVM coverage mapping format, the first entry
139        // in the global file table is treated as a base directory, used to
140        // resolve any other entries that are stored as relative paths.
141        let base_dir = tcx
142            .sess
143            .psess
144            .source_map()
145            .working_dir()
146            .path(RemapPathScopeComponents::COVERAGE)
147            .to_string_lossy();
148        table.push(base_dir.as_ref());
149
150        // Add the regular entries after the base directory.
151        table.extend(raw_file_table.values().map(|name| name.as_str()));
152
153        // Encode the file table into a buffer, and get the hash of its encoded
154        // bytes, so that we can embed that hash in `__llvm_covfun` records.
155        let filenames_buffer = llvm_cov::write_filenames_to_buffer(&table);
156        let filenames_hash = llvm_cov::hash_bytes(&filenames_buffer);
157
158        Self { raw_file_table, filenames_buffer, filenames_hash }
159    }
160
161    fn get_existing_id(&self, file: &SourceFile) -> Option<GlobalFileId> {
162        let raw_id = self.raw_file_table.get_index_of(&file.stable_id)?;
163        // The raw file table doesn't include an entry for the base dir
164        // (which has ID 0), so add 1 to get the correct ID.
165        Some(GlobalFileId::from_usize(raw_id + 1))
166    }
167}
168
169rustc_index::newtype_index! {
170    /// An index into the CGU's overall list of file paths. The underlying paths
171    /// will be embedded in the `__llvm_covmap` linker section.
172    struct GlobalFileId {}
173}
174rustc_index::newtype_index! {
175    /// An index into a function's list of global file IDs. That underlying list
176    /// of local-to-global mappings will be embedded in the function's record in
177    /// the `__llvm_covfun` linker section.
178    struct LocalFileId {}
179}
180
181/// Holds a mapping from "local" (per-function) file IDs to their corresponding
182/// source files.
183#[derive(Debug, Default)]
184struct VirtualFileMapping {
185    local_file_table: IndexVec<LocalFileId, Arc<SourceFile>>,
186}
187
188impl VirtualFileMapping {
189    fn push_file(&mut self, source_file: &Arc<SourceFile>) -> LocalFileId {
190        self.local_file_table.push(Arc::clone(source_file))
191    }
192
193    /// Resolves all of the filenames in this local file mapping to a list of
194    /// global file IDs in its CGU, for inclusion in this function's
195    /// `__llvm_covfun` record.
196    ///
197    /// The global file IDs are returned as `u32` to make FFI easier.
198    fn resolve_all(&self, global_file_table: &GlobalFileTable) -> Option<Vec<u32>> {
199        self.local_file_table
200            .iter()
201            .map(|file| try {
202                let id = global_file_table.get_existing_id(file)?;
203                GlobalFileId::as_u32(id)
204            })
205            .collect::<Option<Vec<_>>>()
206    }
207}
208
209/// Generates the contents of the covmap record for this CGU, which mostly
210/// consists of a header and a list of filenames. The record is then stored
211/// as a global variable in the `__llvm_covmap` section.
212fn generate_covmap_record<'ll>(
213    cx: &mut CodegenCx<'ll, '_>,
214    version: CovmapVersion,
215    filenames_buffer: &[u8],
216) {
217    // A covmap record consists of four target-endian u32 values, followed by
218    // the encoded filenames table. Two of the header fields are unused in
219    // modern versions of the LLVM coverage mapping format, and are always 0.
220    // <https://llvm.org/docs/CoverageMappingFormat.html#llvm-ir-representation>
221    // See also `src/llvm-project/clang/lib/CodeGen/CoverageMappingGen.cpp`.
222    let covmap_header = cx.const_struct(
223        &[
224            cx.const_u32(0), // (unused)
225            cx.const_u32(filenames_buffer.len() as u32),
226            cx.const_u32(0), // (unused)
227            cx.const_u32(version.to_u32()),
228        ],
229        /* packed */ false,
230    );
231    let covmap_record = cx
232        .const_struct(&[covmap_header, cx.const_bytes(filenames_buffer)], /* packed */ false);
233
234    let covmap_global =
235        llvm::add_global(cx.llmod, cx.val_ty(covmap_record), &llvm_cov::covmap_var_name());
236    llvm::set_initializer(covmap_global, covmap_record);
237    llvm::set_global_constant(covmap_global, true);
238    llvm::set_linkage(covmap_global, llvm::Linkage::PrivateLinkage);
239    llvm::set_section(covmap_global, &llvm_cov::covmap_section_name(cx.llmod));
240    // LLVM's coverage mapping format specifies 8-byte alignment for items in this section.
241    // <https://llvm.org/docs/CoverageMappingFormat.html>
242    llvm::set_alignment(covmap_global, Align::EIGHT);
243
244    cx.add_used_global(covmap_global);
245}