2020-07-02 18:27:15 +00:00
|
|
|
use crate::llvm;
|
|
|
|
|
2020-06-22 06:29:08 +00:00
|
|
|
use crate::builder::Builder;
|
|
|
|
use crate::common::CodegenCx;
|
2020-07-02 18:27:15 +00:00
|
|
|
|
|
|
|
use libc::c_uint;
|
2020-07-29 06:09:16 +00:00
|
|
|
use llvm::coverageinfo::CounterMappingRegion;
|
2020-06-22 06:29:08 +00:00
|
|
|
use log::debug;
|
2020-08-02 03:03:59 +00:00
|
|
|
use rustc_codegen_ssa::coverageinfo::map::{CounterExpression, ExprKind, FunctionCoverage, Region};
|
2020-07-02 18:27:15 +00:00
|
|
|
use rustc_codegen_ssa::traits::{
|
|
|
|
BaseTypeMethods, CoverageInfoBuilderMethods, CoverageInfoMethods, StaticMethods,
|
|
|
|
};
|
2020-06-22 06:29:08 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-07-02 18:27:15 +00:00
|
|
|
use rustc_llvm::RustString;
|
2020-06-22 06:29:08 +00:00
|
|
|
use rustc_middle::ty::Instance;
|
|
|
|
|
|
|
|
use std::cell::RefCell;
|
2020-07-02 18:27:15 +00:00
|
|
|
use std::ffi::CString;
|
|
|
|
|
|
|
|
pub mod mapgen;
|
|
|
|
|
|
|
|
const COVMAP_VAR_ALIGN_BYTES: usize = 8;
|
2020-06-22 06:29:08 +00:00
|
|
|
|
|
|
|
/// A context object for maintaining all state needed by the coverageinfo module.
|
|
|
|
pub struct CrateCoverageContext<'tcx> {
|
|
|
|
// Coverage region data for each instrumented function identified by DefId.
|
2020-07-25 04:14:28 +00:00
|
|
|
pub(crate) function_coverage_map: RefCell<FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>>>,
|
2020-06-22 06:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> CrateCoverageContext<'tcx> {
|
|
|
|
pub fn new() -> Self {
|
2020-07-02 18:27:15 +00:00
|
|
|
Self { function_coverage_map: Default::default() }
|
2020-06-22 06:29:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 04:14:28 +00:00
|
|
|
pub fn take_function_coverage_map(&self) -> FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>> {
|
2020-07-02 18:27:15 +00:00
|
|
|
self.function_coverage_map.replace(FxHashMap::default())
|
2020-06-22 06:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CoverageInfoMethods for CodegenCx<'ll, 'tcx> {
|
|
|
|
fn coverageinfo_finalize(&self) {
|
2020-07-02 18:27:15 +00:00
|
|
|
mapgen::finalize(self)
|
2020-06-22 06:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
|
|
|
|
fn add_counter_region(
|
|
|
|
&mut self,
|
|
|
|
instance: Instance<'tcx>,
|
2020-07-02 18:27:15 +00:00
|
|
|
function_source_hash: u64,
|
2020-07-25 04:14:28 +00:00
|
|
|
id: u32,
|
2020-08-02 03:03:59 +00:00
|
|
|
region: Region<'tcx>,
|
2020-06-22 06:29:08 +00:00
|
|
|
) {
|
|
|
|
debug!(
|
2020-07-25 04:14:28 +00:00
|
|
|
"adding counter to coverage_regions: instance={:?}, function_source_hash={}, id={}, \
|
2020-08-02 03:03:59 +00:00
|
|
|
at {:?}",
|
|
|
|
instance, function_source_hash, id, region,
|
2020-06-22 06:29:08 +00:00
|
|
|
);
|
2020-07-02 18:27:15 +00:00
|
|
|
let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
|
|
|
|
coverage_regions
|
|
|
|
.entry(instance)
|
2020-07-25 04:14:28 +00:00
|
|
|
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
|
2020-08-02 03:03:59 +00:00
|
|
|
.add_counter(function_source_hash, id, region);
|
2020-06-22 06:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_counter_expression_region(
|
|
|
|
&mut self,
|
|
|
|
instance: Instance<'tcx>,
|
2020-07-25 04:14:28 +00:00
|
|
|
id_descending_from_max: u32,
|
2020-06-22 06:29:08 +00:00
|
|
|
lhs: u32,
|
2020-07-25 04:14:28 +00:00
|
|
|
op: ExprKind,
|
2020-06-22 06:29:08 +00:00
|
|
|
rhs: u32,
|
2020-08-02 03:03:59 +00:00
|
|
|
region: Region<'tcx>,
|
2020-06-22 06:29:08 +00:00
|
|
|
) {
|
|
|
|
debug!(
|
2020-07-25 04:14:28 +00:00
|
|
|
"adding counter expression to coverage_regions: instance={:?}, id={}, {} {:?} {}, \
|
2020-08-02 03:03:59 +00:00
|
|
|
at {:?}",
|
|
|
|
instance, id_descending_from_max, lhs, op, rhs, region,
|
2020-06-22 06:29:08 +00:00
|
|
|
);
|
2020-07-02 18:27:15 +00:00
|
|
|
let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
|
|
|
|
coverage_regions
|
|
|
|
.entry(instance)
|
2020-07-25 04:14:28 +00:00
|
|
|
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
|
2020-08-02 03:03:59 +00:00
|
|
|
.add_counter_expression(id_descending_from_max, lhs, op, rhs, region);
|
2020-06-22 06:29:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 03:03:59 +00:00
|
|
|
fn add_unreachable_region(&mut self, instance: Instance<'tcx>, region: Region<'tcx>) {
|
2020-06-22 06:29:08 +00:00
|
|
|
debug!(
|
2020-08-02 03:03:59 +00:00
|
|
|
"adding unreachable code to coverage_regions: instance={:?}, at {:?}",
|
|
|
|
instance, region,
|
2020-06-22 06:29:08 +00:00
|
|
|
);
|
2020-07-02 18:27:15 +00:00
|
|
|
let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
|
|
|
|
coverage_regions
|
|
|
|
.entry(instance)
|
2020-07-25 04:14:28 +00:00
|
|
|
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
|
2020-08-02 03:03:59 +00:00
|
|
|
.add_unreachable_region(region);
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn write_filenames_section_to_buffer(filenames: &Vec<CString>, buffer: &RustString) {
|
|
|
|
let c_str_vec = filenames.iter().map(|cstring| cstring.as_ptr()).collect::<Vec<_>>();
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustCoverageWriteFilenamesSectionToBuffer(
|
|
|
|
c_str_vec.as_ptr(),
|
|
|
|
c_str_vec.len(),
|
|
|
|
buffer,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn write_mapping_to_buffer(
|
|
|
|
virtual_file_mapping: Vec<u32>,
|
2020-07-25 04:14:28 +00:00
|
|
|
expressions: Vec<CounterExpression>,
|
|
|
|
mut mapping_regions: Vec<CounterMappingRegion>,
|
2020-07-02 18:27:15 +00:00
|
|
|
buffer: &RustString,
|
|
|
|
) {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustCoverageWriteMappingToBuffer(
|
|
|
|
virtual_file_mapping.as_ptr(),
|
|
|
|
virtual_file_mapping.len() as c_uint,
|
|
|
|
expressions.as_ptr(),
|
2020-07-25 04:14:28 +00:00
|
|
|
expressions.len() as c_uint,
|
|
|
|
mapping_regions.as_mut_ptr(),
|
|
|
|
mapping_regions.len() as c_uint,
|
2020-07-02 18:27:15 +00:00
|
|
|
buffer,
|
|
|
|
);
|
2020-06-22 06:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-02 18:27:15 +00:00
|
|
|
|
|
|
|
pub(crate) fn compute_hash(name: &str) -> u64 {
|
|
|
|
let name = CString::new(name).expect("null error converting hashable name to C string");
|
|
|
|
unsafe { llvm::LLVMRustCoverageComputeHash(name.as_ptr()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn mapping_version() -> u32 {
|
|
|
|
unsafe { llvm::LLVMRustCoverageMappingVersion() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn save_map_to_mod<'ll, 'tcx>(
|
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
|
|
|
cov_data_val: &'ll llvm::Value,
|
|
|
|
) {
|
|
|
|
let covmap_var_name = llvm::build_string(|s| unsafe {
|
|
|
|
llvm::LLVMRustCoverageWriteMappingVarNameToString(s);
|
|
|
|
})
|
|
|
|
.expect("Rust Coverage Mapping var name failed UTF-8 conversion");
|
|
|
|
debug!("covmap var name: {:?}", covmap_var_name);
|
|
|
|
|
|
|
|
let covmap_section_name = llvm::build_string(|s| unsafe {
|
|
|
|
llvm::LLVMRustCoverageWriteSectionNameToString(cx.llmod, s);
|
|
|
|
})
|
|
|
|
.expect("Rust Coverage section name failed UTF-8 conversion");
|
|
|
|
debug!("covmap section name: {:?}", covmap_section_name);
|
|
|
|
|
|
|
|
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(cov_data_val), &covmap_var_name);
|
|
|
|
llvm::set_initializer(llglobal, cov_data_val);
|
|
|
|
llvm::set_global_constant(llglobal, true);
|
|
|
|
llvm::set_linkage(llglobal, llvm::Linkage::InternalLinkage);
|
|
|
|
llvm::set_section(llglobal, &covmap_section_name);
|
|
|
|
llvm::set_alignment(llglobal, COVMAP_VAR_ALIGN_BYTES);
|
|
|
|
cx.add_used_global(llglobal);
|
|
|
|
}
|