Skip to content

Commit

Permalink
Allow for binary parse failures in report gen (#39)
Browse files Browse the repository at this point in the history
* Allow for binary parse failures in report gen

* Update some changelog stuff
  • Loading branch information
xd009642 authored Apr 28, 2024
1 parent fa32165 commit 4c55110
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.0] - 2024-04-28
### Changed
- Now allow binaries to fail parsing for the mapping information (depdendent on function argument)

## [0.4.0] - 2024-04-12
### Added
- LLVM 17 and 18 support (test fails still: `check_mapping_consistency` but tarpaulin tests all pass so nearly all working)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "llvm_profparser"
version = "0.4.0"
version = "0.5.0"
authors = ["xd009642 <[email protected]>"]
description = "Parsing and interpretation of llvm coverage profiles and generated data"
repository = "https://github.com/xd009642/llvm-profparser"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl ShowCommand {
} else {
panic!("Must provide an instrumentation profile");
};
let mapping = CoverageMapping::new(&self.objects, &instr_prof)?;
let mapping = CoverageMapping::new(&self.objects, &instr_prof, false)?;
let mut report = mapping.generate_report();
if let Some(remapping) = self.path_remapping.as_ref() {
report.apply_remapping(remapping);
Expand Down
32 changes: 26 additions & 6 deletions src/coverage/coverage_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::error::Error;
use std::fmt;
use std::fs;
use std::path::Path;
use tracing::{debug, warn};
use tracing::{debug, error, trace, warn};

/// Stores the instrumentation profile and information from the coverage mapping sections in the
/// object files in order to construct a coverage report. Inspired, from the LLVM implementation
Expand Down Expand Up @@ -105,14 +105,26 @@ pub fn read_object_file(object: &Path, version: u64) -> Result<CoverageMappingIn
}

impl<'a> CoverageMapping<'a> {
pub fn new(object_files: &[PathBuf], profile: &'a InstrumentationProfile) -> Result<Self> {
pub fn new(
object_files: &[PathBuf],
profile: &'a InstrumentationProfile,
allow_parsing_failures: bool,
) -> Result<Self> {
let mut mapping_info = vec![];
let version = match profile.version() {
Some(v) => v,
None => bail!("Invalid profile instrumentation, no version number provided"),
};
for file in object_files {
mapping_info.push(read_object_file(file.as_path(), version)?);
match read_object_file(file.as_path(), version) {
Ok(info) => mapping_info.push(info),
Err(e) => {
error!("{} couldn't be interpretted: {}", file.display(), e);
if !allow_parsing_failures {
return Err(e);
}
}
};
}
Ok(Self {
profile,
Expand Down Expand Up @@ -166,9 +178,15 @@ impl<'a> CoverageMapping<'a> {
let rhs = region_ids.get(&expr.rhs);
match (lhs, rhs) {
(Some(lhs), Some(rhs)) => {
let count = match expr.kind {
ExprKind::Subtract => lhs - rhs,
ExprKind::Add => lhs + rhs,
let count: i64 = match expr.kind {
ExprKind::Subtract => {
trace!("Subtracting counts: {} - {}", lhs, rhs);
lhs - rhs
}
ExprKind::Add => {
trace!("Adding counts: {} + {}", lhs, rhs);
lhs + rhs
}
};

let counter = Counter {
Expand Down Expand Up @@ -295,6 +313,7 @@ fn parse_coverage_functions(
endian: Endianness,
section: &Section<'_, '_>,
) -> Result<Vec<FunctionRecordV3>, SectionReadError> {
debug!("Parsing coverage functions");
if let Ok(original_data) = section.data() {
let mut bytes = original_data;
let mut res = vec![];
Expand Down Expand Up @@ -368,6 +387,7 @@ fn parse_coverage_functions(
}
Ok(res)
} else {
error!("Can't read data for coverage function section");
Err(SectionReadError::EmptySection(
LlvmSection::CoverageFunctions,
))
Expand Down
2 changes: 1 addition & 1 deletion tests/cov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ fn check_mapping_consistency() {

let instr = parse(prof).unwrap();

let mapping = CoverageMapping::new(&[obj], &instr).unwrap();
let mapping = CoverageMapping::new(&[obj], &instr, false).unwrap();
let info = &mapping.mapping_info[0];
for record in &instr.records {
let fun = info
Expand Down

0 comments on commit 4c55110

Please sign in to comment.