-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option for generating coverage reports
Add a `--coverage` option in the `test` subcommand of the miri script. This option, when set, will generate a coverage report after running the tests. `cargo-binutils` is needed as a dependency to generate the reports.
- Loading branch information
1 parent
9045930
commit 4aa0f4d
Showing
8 changed files
with
174 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,4 +175,4 @@ case $HOST_TARGET in | |
echo "FATAL: unknown host target: $HOST_TARGET" | ||
exit 1 | ||
;; | ||
esac | ||
esac |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ rustc_version = "0.4" | |
dunce = "1.0.4" | ||
directories = "5" | ||
serde_json = "1" | ||
tempfile = "3.13.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use std::ffi::OsString; | ||
|
||
use anyhow::{Context, Result}; | ||
use path_macro::path; | ||
use xshell::cmd; | ||
|
||
use crate::util::MiriEnv; | ||
|
||
/// CoverageReport can generate code coverage reports for miri. | ||
pub struct CoverageReport { | ||
/// path is a temporary directory where coverage artifacts will be stored. | ||
path: tempfile::TempDir, | ||
} | ||
|
||
impl CoverageReport { | ||
/// new creates a new CoverageReport | ||
/// | ||
/// # Errors | ||
/// | ||
/// An error will be returned if a temporary directory could not be created. | ||
pub fn new() -> Result<Self> { | ||
Ok(Self { path: tempfile::TempDir::new()? }) | ||
} | ||
|
||
/// add_env_vars will add the required environment variables to MiriEnv `e`. | ||
pub fn add_env_vars(&self, e: &mut MiriEnv) -> Result<()> { | ||
let mut rustflags = e.sh.var("RUSTFLAGS")?; | ||
rustflags.push_str(" -C instrument-coverage"); | ||
e.sh.set_var("RUSTFLAGS", rustflags); | ||
|
||
// Copy-pasting from: https://doc.rust-lang.org/rustc/instrument-coverage.html#instrumentation-based-code-coverage | ||
// The format symbols below have the following meaning: | ||
// - %p - The process ID. | ||
// - %Nm - the instrumented binary’s signature: | ||
// The runtime creates a pool of N raw profiles, used for on-line | ||
// profile merging. The runtime takes care of selecting a raw profile | ||
// from the pool, locking it, and updating it before the program | ||
// exits. N must be between 1 and 9, and defaults to 1 if omitted | ||
// (with simply %m). | ||
// | ||
// Additionally the default for LLVM_PROFILE_FILE is default_%m_%p.profraw. | ||
// So we just use the same value. | ||
let file_template = self.path.path().join("default_%m_%p.profraw"); | ||
e.sh.set_var("LLVM_PROFILE_FILE", file_template); | ||
Ok(()) | ||
} | ||
|
||
/// show_coverage_report will print coverage information using the artifact | ||
/// files in `self.path`. | ||
pub fn show_coverage_report(&self, e: &MiriEnv) -> Result<()> { | ||
let profraw_files: Vec<_> = self.profraw_files()?; | ||
|
||
let profdata_bin = path!(e.libdir / ".." / "bin" / "llvm-profdata"); | ||
|
||
let merged_file = self.path.path().join("merged.profdata"); | ||
|
||
// Merge the profraw files | ||
let profraw_files_cloned = profraw_files.iter(); | ||
cmd!(e.sh, "{profdata_bin} merge -sparse {profraw_files_cloned...} -o {merged_file}") | ||
.quiet() | ||
.run()?; | ||
|
||
// Create the coverage report. | ||
let cov_bin = path!(e.libdir / ".." / "bin" / "llvm-cov"); | ||
let miri_bin = | ||
e.build_get_binary(".").context("failed to get filename of miri executable")?; | ||
cmd!( | ||
e.sh, | ||
"{cov_bin} report --instr-profile={merged_file} --object {miri_bin} --sources src/" | ||
) | ||
.run()?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// profraw_files returns the profraw files in `self.path`. | ||
/// | ||
/// # Errors | ||
/// | ||
/// An error will be returned if `self.path` can't be read. | ||
fn profraw_files(&self) -> Result<Vec<OsString>> { | ||
Ok(std::fs::read_dir(&self.path)? | ||
.filter_map(|r| r.ok()) | ||
.map(|e| e.path()) | ||
.filter(|p| p.extension().map(|e| e == "profraw").unwrap_or(false)) | ||
.map(|p| p.as_os_str().to_os_string()) | ||
.collect()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters