rustc_codegen_llvm/coverageinfo/
llvm_cov.rs

1//! Safe wrappers for coverage-specific FFI functions.
2
3use std::ffi::CString;
4
5use crate::coverageinfo::ffi;
6use crate::llvm;
7
8pub(crate) fn covmap_var_name() -> CString {
9    CString::new(llvm::build_byte_buffer(|s| {
10        llvm::LLVMRustCoverageWriteCovmapVarNameToString(s);
11    }))
12    .expect("covmap variable name should not contain NUL")
13}
14
15pub(crate) fn covmap_section_name(llmod: &llvm::Module) -> CString {
16    CString::new(llvm::build_byte_buffer(|s| {
17        llvm::LLVMRustCoverageWriteCovmapSectionNameToString(llmod, s);
18    }))
19    .expect("covmap section name should not contain NUL")
20}
21
22pub(crate) fn covfun_section_name(llmod: &llvm::Module) -> CString {
23    CString::new(llvm::build_byte_buffer(|s| {
24        llvm::LLVMRustCoverageWriteCovfunSectionNameToString(llmod, s);
25    }))
26    .expect("covfun section name should not contain NUL")
27}
28
29pub(crate) fn create_pgo_func_name_var<'ll>(
30    llfn: &'ll llvm::Value,
31    mangled_fn_name: &str,
32) -> &'ll llvm::Value {
33    unsafe {
34        llvm::LLVMRustCoverageCreatePGOFuncNameVar(
35            llfn,
36            mangled_fn_name.as_ptr(),
37            mangled_fn_name.len(),
38        )
39    }
40}
41
42pub(crate) fn write_filenames_to_buffer(filenames: &[impl AsRef<str>]) -> Vec<u8> {
43    let (pointers, lengths) = filenames
44        .into_iter()
45        .map(AsRef::as_ref)
46        .map(|s: &str| (s.as_ptr(), s.len()))
47        .unzip::<_, _, Vec<_>, Vec<_>>();
48
49    llvm::build_byte_buffer(|buffer| unsafe {
50        llvm::LLVMRustCoverageWriteFilenamesToBuffer(
51            pointers.as_ptr(),
52            pointers.len(),
53            lengths.as_ptr(),
54            lengths.len(),
55            buffer,
56        );
57    })
58}
59
60/// Holds tables of the various region types in one struct.
61///
62/// Don't pass this struct across FFI; pass the individual region tables as
63/// pointer/length pairs instead.
64///
65/// Each field name has a `_regions` suffix for improved readability after
66/// exhaustive destructing, which ensures that all region types are handled.
67#[derive(Clone, Debug, Default)]
68pub(crate) struct Regions {
69    pub(crate) code_regions: Vec<ffi::CodeRegion>,
70    pub(crate) expansion_regions: Vec<ffi::ExpansionRegion>,
71    pub(crate) branch_regions: Vec<ffi::BranchRegion>,
72}
73
74impl Regions {
75    /// Returns true if none of this structure's tables contain any regions.
76    pub(crate) fn has_no_regions(&self) -> bool {
77        let Self { code_regions, expansion_regions, branch_regions } = self;
78
79        code_regions.is_empty() && expansion_regions.is_empty() && branch_regions.is_empty()
80    }
81}
82
83pub(crate) fn write_function_mappings_to_buffer(
84    virtual_file_mapping: &[u32],
85    expressions: &[ffi::CounterExpression],
86    regions: &Regions,
87) -> Vec<u8> {
88    let Regions { code_regions, expansion_regions, branch_regions } = regions;
89
90    // SAFETY:
91    // - All types are FFI-compatible and have matching representations in Rust/C++.
92    // - For pointer/length pairs, the pointer and length come from the same vector or slice.
93    // - C++ code does not retain any pointers after the call returns.
94    llvm::build_byte_buffer(|buffer| unsafe {
95        llvm::LLVMRustCoverageWriteFunctionMappingsToBuffer(
96            virtual_file_mapping.as_ptr(),
97            virtual_file_mapping.len(),
98            expressions.as_ptr(),
99            expressions.len(),
100            code_regions.as_ptr(),
101            code_regions.len(),
102            expansion_regions.as_ptr(),
103            expansion_regions.len(),
104            branch_regions.as_ptr(),
105            branch_regions.len(),
106            buffer,
107        )
108    })
109}
110
111/// Hashes some bytes into a 64-bit hash, via LLVM's `IndexedInstrProf::ComputeHash`,
112/// as required for parts of the LLVM coverage mapping format.
113pub(crate) fn hash_bytes(bytes: &[u8]) -> u64 {
114    unsafe { llvm::LLVMRustCoverageHashBytes(bytes.as_ptr(), bytes.len()) }
115}
116
117/// Returns LLVM's `coverage::CovMapVersion::CurrentVersion` (CoverageMapping.h)
118/// as a raw numeric value. For historical reasons, the numeric value is 1 less
119/// than the number in the version's name, so `Version7` is actually `6u32`.
120pub(crate) fn mapping_version() -> u32 {
121    llvm::LLVMRustCoverageMappingVersion()
122}