• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::common::CodegenCx;
2 use crate::coverageinfo;
3 use crate::coverageinfo::map_data::{Counter, CounterExpression};
4 use crate::llvm;
5 
6 use llvm::coverageinfo::CounterMappingRegion;
7 use rustc_codegen_ssa::traits::ConstMethods;
8 use rustc_data_structures::fx::FxIndexSet;
9 use rustc_hir::def::DefKind;
10 use rustc_hir::def_id::DefId;
11 use rustc_llvm::RustString;
12 use rustc_middle::bug;
13 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
14 use rustc_middle::mir::coverage::CodeRegion;
15 use rustc_middle::ty::TyCtxt;
16 
17 use std::ffi::CString;
18 
19 /// Generates and exports the Coverage Map.
20 ///
21 /// Rust Coverage Map generation supports LLVM Coverage Mapping Format version
22 /// 6 (zero-based encoded as 5), as defined at
23 /// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
24 /// These versions are supported by the LLVM coverage tools (`llvm-profdata` and `llvm-cov`)
25 /// bundled with Rust's fork of LLVM.
26 ///
27 /// Consequently, Rust's bundled version of Clang also generates Coverage Maps compliant with
28 /// the same version. Clang's implementation of Coverage Map generation was referenced when
29 /// implementing this Rust version, and though the format documentation is very explicit and
30 /// detailed, some undocumented details in Clang's implementation (that may or may not be important)
31 /// were also replicated for Rust's Coverage Map.
finalize(cx: &CodegenCx<'_, '_>)32 pub fn finalize(cx: &CodegenCx<'_, '_>) {
33     let tcx = cx.tcx;
34 
35     // Ensure the installed version of LLVM supports Coverage Map Version 6
36     // (encoded as a zero-based value: 5), which was introduced with LLVM 13.
37     let version = coverageinfo::mapping_version();
38     assert_eq!(version, 5, "The `CoverageMappingVersion` exposed by `llvm-wrapper` is out of sync");
39 
40     debug!("Generating coverage map for CodegenUnit: `{}`", cx.codegen_unit.name());
41 
42     // In order to show that unused functions have coverage counts of zero (0), LLVM requires the
43     // functions exist. Generate synthetic functions with a (required) single counter, and add the
44     // MIR `Coverage` code regions to the `function_coverage_map`, before calling
45     // `ctx.take_function_coverage_map()`.
46     if cx.codegen_unit.is_code_coverage_dead_code_cgu() {
47         add_unused_functions(cx);
48     }
49 
50     let function_coverage_map = match cx.coverage_context() {
51         Some(ctx) => ctx.take_function_coverage_map(),
52         None => return,
53     };
54 
55     if function_coverage_map.is_empty() {
56         // This module has no functions with coverage instrumentation
57         return;
58     }
59 
60     let mut mapgen = CoverageMapGenerator::new(tcx);
61 
62     // Encode coverage mappings and generate function records
63     let mut function_data = Vec::new();
64     for (instance, function_coverage) in function_coverage_map {
65         debug!("Generate function coverage for {}, {:?}", cx.codegen_unit.name(), instance);
66         let mangled_function_name = tcx.symbol_name(instance).to_string();
67         let source_hash = function_coverage.source_hash();
68         let is_used = function_coverage.is_used();
69         let (expressions, counter_regions) =
70             function_coverage.get_expressions_and_counter_regions();
71 
72         let coverage_mapping_buffer = llvm::build_byte_buffer(|coverage_mapping_buffer| {
73             mapgen.write_coverage_mapping(expressions, counter_regions, coverage_mapping_buffer);
74         });
75 
76         if coverage_mapping_buffer.is_empty() {
77             if function_coverage.is_used() {
78                 bug!(
79                     "A used function should have had coverage mapping data but did not: {}",
80                     mangled_function_name
81                 );
82             } else {
83                 debug!("unused function had no coverage mapping data: {}", mangled_function_name);
84                 continue;
85             }
86         }
87 
88         function_data.push((mangled_function_name, source_hash, is_used, coverage_mapping_buffer));
89     }
90 
91     // Encode all filenames referenced by counters/expressions in this module
92     let filenames_buffer = llvm::build_byte_buffer(|filenames_buffer| {
93         coverageinfo::write_filenames_section_to_buffer(&mapgen.filenames, filenames_buffer);
94     });
95 
96     let filenames_size = filenames_buffer.len();
97     let filenames_val = cx.const_bytes(&filenames_buffer);
98     let filenames_ref = coverageinfo::hash_bytes(filenames_buffer);
99 
100     // Generate the LLVM IR representation of the coverage map and store it in a well-known global
101     let cov_data_val = mapgen.generate_coverage_map(cx, version, filenames_size, filenames_val);
102 
103     for (mangled_function_name, source_hash, is_used, coverage_mapping_buffer) in function_data {
104         save_function_record(
105             cx,
106             mangled_function_name,
107             source_hash,
108             filenames_ref,
109             coverage_mapping_buffer,
110             is_used,
111         );
112     }
113 
114     // Save the coverage data value to LLVM IR
115     coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
116 }
117 
118 struct CoverageMapGenerator {
119     filenames: FxIndexSet<CString>,
120 }
121 
122 impl CoverageMapGenerator {
new(tcx: TyCtxt<'_>) -> Self123     fn new(tcx: TyCtxt<'_>) -> Self {
124         let mut filenames = FxIndexSet::default();
125         // LLVM Coverage Mapping Format version 6 (zero-based encoded as 5)
126         // requires setting the first filename to the compilation directory.
127         // Since rustc generates coverage maps with relative paths, the
128         // compilation directory can be combined with the relative paths
129         // to get absolute paths, if needed.
130         let working_dir =
131             tcx.sess.opts.working_dir.remapped_path_if_available().to_string_lossy().to_string();
132         let c_filename =
133             CString::new(working_dir).expect("null error converting filename to C string");
134         filenames.insert(c_filename);
135         Self { filenames }
136     }
137 
138     /// Using the `expressions` and `counter_regions` collected for the current function, generate
139     /// the `mapping_regions` and `virtual_file_mapping`, and capture any new filenames. Then use
140     /// LLVM APIs to encode the `virtual_file_mapping`, `expressions`, and `mapping_regions` into
141     /// the given `coverage_mapping` byte buffer, compliant with the LLVM Coverage Mapping format.
write_coverage_mapping<'a>( &mut self, expressions: Vec<CounterExpression>, counter_regions: impl Iterator<Item = (Counter, &'a CodeRegion)>, coverage_mapping_buffer: &RustString, )142     fn write_coverage_mapping<'a>(
143         &mut self,
144         expressions: Vec<CounterExpression>,
145         counter_regions: impl Iterator<Item = (Counter, &'a CodeRegion)>,
146         coverage_mapping_buffer: &RustString,
147     ) {
148         let mut counter_regions = counter_regions.collect::<Vec<_>>();
149         if counter_regions.is_empty() {
150             return;
151         }
152 
153         let mut virtual_file_mapping = Vec::new();
154         let mut mapping_regions = Vec::new();
155         let mut current_file_name = None;
156         let mut current_file_id = 0;
157 
158         // Convert the list of (Counter, CodeRegion) pairs to an array of `CounterMappingRegion`, sorted
159         // by filename and position. Capture any new files to compute the `CounterMappingRegion`s
160         // `file_id` (indexing files referenced by the current function), and construct the
161         // function-specific `virtual_file_mapping` from `file_id` to its index in the module's
162         // `filenames` array.
163         counter_regions.sort_unstable_by_key(|(_counter, region)| *region);
164         for (counter, region) in counter_regions {
165             let CodeRegion { file_name, start_line, start_col, end_line, end_col } = *region;
166             let same_file = current_file_name.is_some_and(|p| p == file_name);
167             if !same_file {
168                 if current_file_name.is_some() {
169                     current_file_id += 1;
170                 }
171                 current_file_name = Some(file_name);
172                 let c_filename = CString::new(file_name.to_string())
173                     .expect("null error converting filename to C string");
174                 debug!("  file_id: {} = '{:?}'", current_file_id, c_filename);
175                 let (filenames_index, _) = self.filenames.insert_full(c_filename);
176                 virtual_file_mapping.push(filenames_index as u32);
177             }
178             debug!("Adding counter {:?} to map for {:?}", counter, region);
179             mapping_regions.push(CounterMappingRegion::code_region(
180                 counter,
181                 current_file_id,
182                 start_line,
183                 start_col,
184                 end_line,
185                 end_col,
186             ));
187         }
188 
189         // Encode and append the current function's coverage mapping data
190         coverageinfo::write_mapping_to_buffer(
191             virtual_file_mapping,
192             expressions,
193             mapping_regions,
194             coverage_mapping_buffer,
195         );
196     }
197 
198     /// Construct coverage map header and the array of function records, and combine them into the
199     /// coverage map. Save the coverage map data into the LLVM IR as a static global using a
200     /// specific, well-known section and name.
generate_coverage_map<'ll>( self, cx: &CodegenCx<'ll, '_>, version: u32, filenames_size: usize, filenames_val: &'ll llvm::Value, ) -> &'ll llvm::Value201     fn generate_coverage_map<'ll>(
202         self,
203         cx: &CodegenCx<'ll, '_>,
204         version: u32,
205         filenames_size: usize,
206         filenames_val: &'ll llvm::Value,
207     ) -> &'ll llvm::Value {
208         debug!("cov map: filenames_size = {}, 0-based version = {}", filenames_size, version);
209 
210         // Create the coverage data header (Note, fields 0 and 2 are now always zero,
211         // as of `llvm::coverage::CovMapVersion::Version4`.)
212         let zero_was_n_records_val = cx.const_u32(0);
213         let filenames_size_val = cx.const_u32(filenames_size as u32);
214         let zero_was_coverage_size_val = cx.const_u32(0);
215         let version_val = cx.const_u32(version);
216         let cov_data_header_val = cx.const_struct(
217             &[zero_was_n_records_val, filenames_size_val, zero_was_coverage_size_val, version_val],
218             /*packed=*/ false,
219         );
220 
221         // Create the complete LLVM coverage data value to add to the LLVM IR
222         cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false)
223     }
224 }
225 
226 /// Construct a function record and combine it with the function's coverage mapping data.
227 /// Save the function record into the LLVM IR as a static global using a
228 /// specific, well-known section and name.
save_function_record( cx: &CodegenCx<'_, '_>, mangled_function_name: String, source_hash: u64, filenames_ref: u64, coverage_mapping_buffer: Vec<u8>, is_used: bool, )229 fn save_function_record(
230     cx: &CodegenCx<'_, '_>,
231     mangled_function_name: String,
232     source_hash: u64,
233     filenames_ref: u64,
234     coverage_mapping_buffer: Vec<u8>,
235     is_used: bool,
236 ) {
237     // Concatenate the encoded coverage mappings
238     let coverage_mapping_size = coverage_mapping_buffer.len();
239     let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer);
240 
241     let func_name_hash = coverageinfo::hash_str(&mangled_function_name);
242     let func_name_hash_val = cx.const_u64(func_name_hash);
243     let coverage_mapping_size_val = cx.const_u32(coverage_mapping_size as u32);
244     let source_hash_val = cx.const_u64(source_hash);
245     let filenames_ref_val = cx.const_u64(filenames_ref);
246     let func_record_val = cx.const_struct(
247         &[
248             func_name_hash_val,
249             coverage_mapping_size_val,
250             source_hash_val,
251             filenames_ref_val,
252             coverage_mapping_val,
253         ],
254         /*packed=*/ true,
255     );
256 
257     coverageinfo::save_func_record_to_mod(cx, func_name_hash, func_record_val, is_used);
258 }
259 
260 /// When finalizing the coverage map, `FunctionCoverage` only has the `CodeRegion`s and counters for
261 /// the functions that went through codegen; such as public functions and "used" functions
262 /// (functions referenced by other "used" or public items). Any other functions considered unused,
263 /// or "Unreachable", were still parsed and processed through the MIR stage, but were not
264 /// codegenned. (Note that `-Clink-dead-code` can force some unused code to be codegenned, but
265 /// that flag is known to cause other errors, when combined with `-C instrument-coverage`; and
266 /// `-Clink-dead-code` will not generate code for unused generic functions.)
267 ///
268 /// We can find the unused functions (including generic functions) by the set difference of all MIR
269 /// `DefId`s (`tcx` query `mir_keys`) minus the codegenned `DefId`s (`tcx` query
270 /// `codegened_and_inlined_items`).
271 ///
272 /// These unused functions are then codegen'd in one of the CGUs which is marked as the
273 /// "code coverage dead code cgu" during the partitioning process. This prevents us from generating
274 /// code regions for the same function more than once which can lead to linker errors regarding
275 /// duplicate symbols.
add_unused_functions(cx: &CodegenCx<'_, '_>)276 fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
277     assert!(cx.codegen_unit.is_code_coverage_dead_code_cgu());
278 
279     let tcx = cx.tcx;
280 
281     let ignore_unused_generics = tcx.sess.instrument_coverage_except_unused_generics();
282 
283     let eligible_def_ids: Vec<DefId> = tcx
284         .mir_keys(())
285         .iter()
286         .filter_map(|local_def_id| {
287             let def_id = local_def_id.to_def_id();
288             let kind = tcx.def_kind(def_id);
289             // `mir_keys` will give us `DefId`s for all kinds of things, not
290             // just "functions", like consts, statics, etc. Filter those out.
291             // If `ignore_unused_generics` was specified, filter out any
292             // generic functions from consideration as well.
293             if !matches!(
294                 kind,
295                 DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Generator
296             ) {
297                 return None;
298             }
299             if ignore_unused_generics && tcx.generics_of(def_id).requires_monomorphization(tcx) {
300                 return None;
301             }
302             Some(local_def_id.to_def_id())
303         })
304         .collect();
305 
306     let codegenned_def_ids = tcx.codegened_and_inlined_items(());
307 
308     for non_codegenned_def_id in
309         eligible_def_ids.into_iter().filter(|id| !codegenned_def_ids.contains(id))
310     {
311         let codegen_fn_attrs = tcx.codegen_fn_attrs(non_codegenned_def_id);
312 
313         // If a function is marked `#[no_coverage]`, then skip generating a
314         // dead code stub for it.
315         if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_COVERAGE) {
316             debug!("skipping unused fn marked #[no_coverage]: {:?}", non_codegenned_def_id);
317             continue;
318         }
319 
320         debug!("generating unused fn: {:?}", non_codegenned_def_id);
321         cx.define_unused_fn(non_codegenned_def_id);
322     }
323 }
324