coverage: Extract safe FFI wrapper functions to llvm_cov
This commit is contained in:
parent
fbab78289d
commit
b790e4473c
102
compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs
Normal file
102
compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
//! Safe wrappers for coverage-specific FFI functions.
|
||||||
|
|
||||||
|
use std::ffi::CString;
|
||||||
|
|
||||||
|
use libc::c_uint;
|
||||||
|
|
||||||
|
use crate::common::AsCCharPtr;
|
||||||
|
use crate::coverageinfo::ffi;
|
||||||
|
use crate::llvm;
|
||||||
|
|
||||||
|
pub(crate) fn covmap_var_name() -> CString {
|
||||||
|
CString::new(llvm::build_byte_buffer(|s| unsafe {
|
||||||
|
llvm::LLVMRustCoverageWriteMappingVarNameToString(s);
|
||||||
|
}))
|
||||||
|
.expect("covmap variable name should not contain NUL")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn covmap_section_name(llmod: &llvm::Module) -> CString {
|
||||||
|
CString::new(llvm::build_byte_buffer(|s| unsafe {
|
||||||
|
llvm::LLVMRustCoverageWriteMapSectionNameToString(llmod, s);
|
||||||
|
}))
|
||||||
|
.expect("covmap section name should not contain NUL")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn covfun_section_name(llmod: &llvm::Module) -> CString {
|
||||||
|
CString::new(llvm::build_byte_buffer(|s| unsafe {
|
||||||
|
llvm::LLVMRustCoverageWriteFuncSectionNameToString(llmod, s);
|
||||||
|
}))
|
||||||
|
.expect("covfun section name should not contain NUL")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn create_pgo_func_name_var<'ll>(
|
||||||
|
llfn: &'ll llvm::Value,
|
||||||
|
mangled_fn_name: &str,
|
||||||
|
) -> &'ll llvm::Value {
|
||||||
|
unsafe {
|
||||||
|
llvm::LLVMRustCoverageCreatePGOFuncNameVar(
|
||||||
|
llfn,
|
||||||
|
mangled_fn_name.as_c_char_ptr(),
|
||||||
|
mangled_fn_name.len(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn write_filenames_to_buffer<'a>(
|
||||||
|
filenames: impl IntoIterator<Item = &'a str>,
|
||||||
|
) -> Vec<u8> {
|
||||||
|
let (pointers, lengths) = filenames
|
||||||
|
.into_iter()
|
||||||
|
.map(|s: &str| (s.as_c_char_ptr(), s.len()))
|
||||||
|
.unzip::<_, _, Vec<_>, Vec<_>>();
|
||||||
|
|
||||||
|
llvm::build_byte_buffer(|buffer| unsafe {
|
||||||
|
llvm::LLVMRustCoverageWriteFilenamesSectionToBuffer(
|
||||||
|
pointers.as_ptr(),
|
||||||
|
pointers.len(),
|
||||||
|
lengths.as_ptr(),
|
||||||
|
lengths.len(),
|
||||||
|
buffer,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn write_function_mappings_to_buffer(
|
||||||
|
virtual_file_mapping: &[u32],
|
||||||
|
expressions: &[ffi::CounterExpression],
|
||||||
|
code_regions: &[ffi::CodeRegion],
|
||||||
|
branch_regions: &[ffi::BranchRegion],
|
||||||
|
mcdc_branch_regions: &[ffi::MCDCBranchRegion],
|
||||||
|
mcdc_decision_regions: &[ffi::MCDCDecisionRegion],
|
||||||
|
) -> Vec<u8> {
|
||||||
|
llvm::build_byte_buffer(|buffer| unsafe {
|
||||||
|
llvm::LLVMRustCoverageWriteMappingToBuffer(
|
||||||
|
virtual_file_mapping.as_ptr(),
|
||||||
|
virtual_file_mapping.len() as c_uint,
|
||||||
|
expressions.as_ptr(),
|
||||||
|
expressions.len() as c_uint,
|
||||||
|
code_regions.as_ptr(),
|
||||||
|
code_regions.len() as c_uint,
|
||||||
|
branch_regions.as_ptr(),
|
||||||
|
branch_regions.len() as c_uint,
|
||||||
|
mcdc_branch_regions.as_ptr(),
|
||||||
|
mcdc_branch_regions.len() as c_uint,
|
||||||
|
mcdc_decision_regions.as_ptr(),
|
||||||
|
mcdc_decision_regions.len() as c_uint,
|
||||||
|
buffer,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Hashes some bytes into a 64-bit hash, via LLVM's `IndexedInstrProf::ComputeHash`,
|
||||||
|
/// as required for parts of the LLVM coverage mapping format.
|
||||||
|
pub(crate) fn hash_bytes(bytes: &[u8]) -> u64 {
|
||||||
|
unsafe { llvm::LLVMRustCoverageHashByteArray(bytes.as_c_char_ptr(), bytes.len()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns LLVM's `coverage::CovMapVersion::CurrentVersion` (CoverageMapping.h)
|
||||||
|
/// as a raw numeric value. For historical reasons, the numeric value is 1 less
|
||||||
|
/// than the number in the version's name, so `Version7` is actually `6u32`.
|
||||||
|
pub(crate) fn mapping_version() -> u32 {
|
||||||
|
unsafe { llvm::LLVMRustCoverageMappingVersion() }
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
|
use std::iter;
|
||||||
|
|
||||||
use itertools::Itertools as _;
|
use itertools::Itertools as _;
|
||||||
use rustc_abi::Align;
|
use rustc_abi::Align;
|
||||||
@ -17,9 +18,9 @@
|
|||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
use crate::common::CodegenCx;
|
use crate::common::CodegenCx;
|
||||||
use crate::coverageinfo::ffi;
|
|
||||||
use crate::coverageinfo::map_data::{FunctionCoverage, FunctionCoverageCollector};
|
use crate::coverageinfo::map_data::{FunctionCoverage, FunctionCoverageCollector};
|
||||||
use crate::{coverageinfo, llvm};
|
use crate::coverageinfo::{ffi, llvm_cov};
|
||||||
|
use crate::llvm;
|
||||||
|
|
||||||
/// Generates and exports the coverage map, which is embedded in special
|
/// Generates and exports the coverage map, which is embedded in special
|
||||||
/// linker sections in the final binary.
|
/// linker sections in the final binary.
|
||||||
@ -33,7 +34,7 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
|
|||||||
// agrees with our Rust-side code. Expected versions (encoded as n-1) are:
|
// agrees with our Rust-side code. Expected versions (encoded as n-1) are:
|
||||||
// - `CovMapVersion::Version7` (6) used by LLVM 18-19
|
// - `CovMapVersion::Version7` (6) used by LLVM 18-19
|
||||||
let covmap_version = {
|
let covmap_version = {
|
||||||
let llvm_covmap_version = coverageinfo::mapping_version();
|
let llvm_covmap_version = llvm_cov::mapping_version();
|
||||||
let expected_versions = 6..=6;
|
let expected_versions = 6..=6;
|
||||||
assert!(
|
assert!(
|
||||||
expected_versions.contains(&llvm_covmap_version),
|
expected_versions.contains(&llvm_covmap_version),
|
||||||
@ -78,7 +79,7 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
|
|||||||
|
|
||||||
let filenames_size = filenames_buffer.len();
|
let filenames_size = filenames_buffer.len();
|
||||||
let filenames_val = cx.const_bytes(&filenames_buffer);
|
let filenames_val = cx.const_bytes(&filenames_buffer);
|
||||||
let filenames_ref = coverageinfo::hash_bytes(&filenames_buffer);
|
let filenames_ref = llvm_cov::hash_bytes(&filenames_buffer);
|
||||||
|
|
||||||
// Generate the coverage map header, which contains the filenames used by
|
// Generate the coverage map header, which contains the filenames used by
|
||||||
// this CGU's coverage mappings, and store it in a well-known global.
|
// this CGU's coverage mappings, and store it in a well-known global.
|
||||||
@ -187,13 +188,10 @@ fn make_filenames_buffer(&self, tcx: TyCtxt<'_>) -> Vec<u8> {
|
|||||||
.for_scope(tcx.sess, RemapPathScopeComponents::MACRO)
|
.for_scope(tcx.sess, RemapPathScopeComponents::MACRO)
|
||||||
.to_string_lossy();
|
.to_string_lossy();
|
||||||
|
|
||||||
llvm::build_byte_buffer(|buffer| {
|
// Insert the working dir at index 0, before the other filenames.
|
||||||
coverageinfo::write_filenames_section_to_buffer(
|
let filenames =
|
||||||
// Insert the working dir at index 0, before the other filenames.
|
iter::once(working_dir).chain(self.raw_file_table.iter().map(Symbol::as_str));
|
||||||
std::iter::once(working_dir).chain(self.raw_file_table.iter().map(Symbol::as_str)),
|
llvm_cov::write_filenames_to_buffer(filenames)
|
||||||
buffer,
|
|
||||||
);
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,17 +294,14 @@ fn encode_mappings_for_function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Encode the function's coverage mappings into a buffer.
|
// Encode the function's coverage mappings into a buffer.
|
||||||
llvm::build_byte_buffer(|buffer| {
|
llvm_cov::write_function_mappings_to_buffer(
|
||||||
coverageinfo::write_mapping_to_buffer(
|
&virtual_file_mapping.into_vec(),
|
||||||
virtual_file_mapping.into_vec(),
|
&expressions,
|
||||||
expressions,
|
&code_regions,
|
||||||
&code_regions,
|
&branch_regions,
|
||||||
&branch_regions,
|
&mcdc_branch_regions,
|
||||||
&mcdc_branch_regions,
|
&mcdc_decision_regions,
|
||||||
&mcdc_decision_regions,
|
)
|
||||||
buffer,
|
|
||||||
);
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates the contents of the covmap record for this CGU, which mostly
|
/// Generates the contents of the covmap record for this CGU, which mostly
|
||||||
@ -335,23 +330,11 @@ fn generate_covmap_record<'ll>(
|
|||||||
let covmap_data =
|
let covmap_data =
|
||||||
cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false);
|
cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false);
|
||||||
|
|
||||||
let covmap_var_name = CString::new(llvm::build_byte_buffer(|s| unsafe {
|
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(covmap_data), &llvm_cov::covmap_var_name());
|
||||||
llvm::LLVMRustCoverageWriteMappingVarNameToString(s);
|
|
||||||
}))
|
|
||||||
.unwrap();
|
|
||||||
debug!("covmap var name: {:?}", covmap_var_name);
|
|
||||||
|
|
||||||
let covmap_section_name = CString::new(llvm::build_byte_buffer(|s| unsafe {
|
|
||||||
llvm::LLVMRustCoverageWriteMapSectionNameToString(cx.llmod, s);
|
|
||||||
}))
|
|
||||||
.expect("covmap section name should not contain NUL");
|
|
||||||
debug!("covmap section name: {:?}", covmap_section_name);
|
|
||||||
|
|
||||||
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(covmap_data), &covmap_var_name);
|
|
||||||
llvm::set_initializer(llglobal, covmap_data);
|
llvm::set_initializer(llglobal, covmap_data);
|
||||||
llvm::set_global_constant(llglobal, true);
|
llvm::set_global_constant(llglobal, true);
|
||||||
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
|
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
|
||||||
llvm::set_section(llglobal, &covmap_section_name);
|
llvm::set_section(llglobal, &llvm_cov::covmap_section_name(cx.llmod));
|
||||||
// LLVM's coverage mapping format specifies 8-byte alignment for items in this section.
|
// LLVM's coverage mapping format specifies 8-byte alignment for items in this section.
|
||||||
// <https://llvm.org/docs/CoverageMappingFormat.html>
|
// <https://llvm.org/docs/CoverageMappingFormat.html>
|
||||||
llvm::set_alignment(llglobal, Align::EIGHT);
|
llvm::set_alignment(llglobal, Align::EIGHT);
|
||||||
@ -373,7 +356,7 @@ fn generate_covfun_record(
|
|||||||
let coverage_mapping_size = coverage_mapping_buffer.len();
|
let coverage_mapping_size = coverage_mapping_buffer.len();
|
||||||
let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer);
|
let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer);
|
||||||
|
|
||||||
let func_name_hash = coverageinfo::hash_bytes(mangled_function_name.as_bytes());
|
let func_name_hash = llvm_cov::hash_bytes(mangled_function_name.as_bytes());
|
||||||
let func_name_hash_val = cx.const_u64(func_name_hash);
|
let func_name_hash_val = cx.const_u64(func_name_hash);
|
||||||
let coverage_mapping_size_val = cx.const_u32(coverage_mapping_size as u32);
|
let coverage_mapping_size_val = cx.const_u32(coverage_mapping_size as u32);
|
||||||
let source_hash_val = cx.const_u64(source_hash);
|
let source_hash_val = cx.const_u64(source_hash);
|
||||||
|
@ -1,24 +1,23 @@
|
|||||||
use std::cell::{OnceCell, RefCell};
|
use std::cell::{OnceCell, RefCell};
|
||||||
use std::ffi::{CStr, CString};
|
use std::ffi::{CStr, CString};
|
||||||
|
|
||||||
use libc::c_uint;
|
|
||||||
use rustc_abi::Size;
|
use rustc_abi::Size;
|
||||||
use rustc_codegen_ssa::traits::{
|
use rustc_codegen_ssa::traits::{
|
||||||
BuilderMethods, ConstCodegenMethods, CoverageInfoBuilderMethods, MiscCodegenMethods,
|
BuilderMethods, ConstCodegenMethods, CoverageInfoBuilderMethods, MiscCodegenMethods,
|
||||||
};
|
};
|
||||||
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
|
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
|
||||||
use rustc_llvm::RustString;
|
|
||||||
use rustc_middle::mir::coverage::CoverageKind;
|
use rustc_middle::mir::coverage::CoverageKind;
|
||||||
use rustc_middle::ty::Instance;
|
use rustc_middle::ty::Instance;
|
||||||
use rustc_middle::ty::layout::HasTyCtxt;
|
use rustc_middle::ty::layout::HasTyCtxt;
|
||||||
use tracing::{debug, instrument};
|
use tracing::{debug, instrument};
|
||||||
|
|
||||||
use crate::builder::Builder;
|
use crate::builder::Builder;
|
||||||
use crate::common::{AsCCharPtr, CodegenCx};
|
use crate::common::CodegenCx;
|
||||||
use crate::coverageinfo::map_data::FunctionCoverageCollector;
|
use crate::coverageinfo::map_data::FunctionCoverageCollector;
|
||||||
use crate::llvm;
|
use crate::llvm;
|
||||||
|
|
||||||
pub(crate) mod ffi;
|
pub(crate) mod ffi;
|
||||||
|
mod llvm_cov;
|
||||||
pub(crate) mod map_data;
|
pub(crate) mod map_data;
|
||||||
mod mapgen;
|
mod mapgen;
|
||||||
|
|
||||||
@ -80,12 +79,9 @@ pub(crate) fn coverageinfo_finalize(&self) {
|
|||||||
/// - `__LLVM_COV,__llvm_covfun` on macOS (includes `__LLVM_COV,` segment prefix)
|
/// - `__LLVM_COV,__llvm_covfun` on macOS (includes `__LLVM_COV,` segment prefix)
|
||||||
/// - `.lcovfun$M` on Windows (includes `$M` sorting suffix)
|
/// - `.lcovfun$M` on Windows (includes `$M` sorting suffix)
|
||||||
fn covfun_section_name(&self) -> &CStr {
|
fn covfun_section_name(&self) -> &CStr {
|
||||||
self.coverage_cx().covfun_section_name.get_or_init(|| {
|
self.coverage_cx()
|
||||||
CString::new(llvm::build_byte_buffer(|s| unsafe {
|
.covfun_section_name
|
||||||
llvm::LLVMRustCoverageWriteFuncSectionNameToString(self.llmod, s);
|
.get_or_init(|| llvm_cov::covfun_section_name(self.llmod))
|
||||||
}))
|
|
||||||
.expect("covfun section name should not contain NUL")
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// For LLVM codegen, returns a function-specific `Value` for a global
|
/// For LLVM codegen, returns a function-specific `Value` for a global
|
||||||
@ -95,9 +91,11 @@ fn covfun_section_name(&self) -> &CStr {
|
|||||||
fn get_pgo_func_name_var(&self, instance: Instance<'tcx>) -> &'ll llvm::Value {
|
fn get_pgo_func_name_var(&self, instance: Instance<'tcx>) -> &'ll llvm::Value {
|
||||||
debug!("getting pgo_func_name_var for instance={:?}", instance);
|
debug!("getting pgo_func_name_var for instance={:?}", instance);
|
||||||
let mut pgo_func_name_var_map = self.coverage_cx().pgo_func_name_var_map.borrow_mut();
|
let mut pgo_func_name_var_map = self.coverage_cx().pgo_func_name_var_map.borrow_mut();
|
||||||
pgo_func_name_var_map
|
pgo_func_name_var_map.entry(instance).or_insert_with(|| {
|
||||||
.entry(instance)
|
let llfn = self.get_fn(instance);
|
||||||
.or_insert_with(|| create_pgo_func_name_var(self, instance))
|
let mangled_fn_name: &str = self.tcx.symbol_name(instance).name;
|
||||||
|
llvm_cov::create_pgo_func_name_var(llfn, mangled_fn_name)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,80 +223,3 @@ fn add_coverage(&mut self, instance: Instance<'tcx>, kind: &CoverageKind) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calls llvm::createPGOFuncNameVar() with the given function instance's
|
|
||||||
/// mangled function name. The LLVM API returns an llvm::GlobalVariable
|
|
||||||
/// containing the function name, with the specific variable name and linkage
|
|
||||||
/// required by LLVM InstrProf source-based coverage instrumentation. Use
|
|
||||||
/// `bx.get_pgo_func_name_var()` to ensure the variable is only created once per
|
|
||||||
/// `Instance`.
|
|
||||||
fn create_pgo_func_name_var<'ll, 'tcx>(
|
|
||||||
cx: &CodegenCx<'ll, 'tcx>,
|
|
||||||
instance: Instance<'tcx>,
|
|
||||||
) -> &'ll llvm::Value {
|
|
||||||
let mangled_fn_name: &str = cx.tcx.symbol_name(instance).name;
|
|
||||||
let llfn = cx.get_fn(instance);
|
|
||||||
unsafe {
|
|
||||||
llvm::LLVMRustCoverageCreatePGOFuncNameVar(
|
|
||||||
llfn,
|
|
||||||
mangled_fn_name.as_c_char_ptr(),
|
|
||||||
mangled_fn_name.len(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn write_filenames_section_to_buffer<'a>(
|
|
||||||
filenames: impl IntoIterator<Item = &'a str>,
|
|
||||||
buffer: &RustString,
|
|
||||||
) {
|
|
||||||
let (pointers, lengths) = filenames
|
|
||||||
.into_iter()
|
|
||||||
.map(|s: &str| (s.as_c_char_ptr(), s.len()))
|
|
||||||
.unzip::<_, _, Vec<_>, Vec<_>>();
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
llvm::LLVMRustCoverageWriteFilenamesSectionToBuffer(
|
|
||||||
pointers.as_ptr(),
|
|
||||||
pointers.len(),
|
|
||||||
lengths.as_ptr(),
|
|
||||||
lengths.len(),
|
|
||||||
buffer,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn write_mapping_to_buffer(
|
|
||||||
virtual_file_mapping: Vec<u32>,
|
|
||||||
expressions: Vec<ffi::CounterExpression>,
|
|
||||||
code_regions: &[ffi::CodeRegion],
|
|
||||||
branch_regions: &[ffi::BranchRegion],
|
|
||||||
mcdc_branch_regions: &[ffi::MCDCBranchRegion],
|
|
||||||
mcdc_decision_regions: &[ffi::MCDCDecisionRegion],
|
|
||||||
buffer: &RustString,
|
|
||||||
) {
|
|
||||||
unsafe {
|
|
||||||
llvm::LLVMRustCoverageWriteMappingToBuffer(
|
|
||||||
virtual_file_mapping.as_ptr(),
|
|
||||||
virtual_file_mapping.len() as c_uint,
|
|
||||||
expressions.as_ptr(),
|
|
||||||
expressions.len() as c_uint,
|
|
||||||
code_regions.as_ptr(),
|
|
||||||
code_regions.len() as c_uint,
|
|
||||||
branch_regions.as_ptr(),
|
|
||||||
branch_regions.len() as c_uint,
|
|
||||||
mcdc_branch_regions.as_ptr(),
|
|
||||||
mcdc_branch_regions.len() as c_uint,
|
|
||||||
mcdc_decision_regions.as_ptr(),
|
|
||||||
mcdc_decision_regions.len() as c_uint,
|
|
||||||
buffer,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn hash_bytes(bytes: &[u8]) -> u64 {
|
|
||||||
unsafe { llvm::LLVMRustCoverageHashByteArray(bytes.as_c_char_ptr(), bytes.len()) }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn mapping_version() -> u32 {
|
|
||||||
unsafe { llvm::LLVMRustCoverageMappingVersion() }
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user