Skip to content

Commit 5b32bef

Browse files
committed
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.
1 parent 9045930 commit 5b32bef

File tree

8 files changed

+169
-12
lines changed

8 files changed

+169
-12
lines changed

.github/workflows/ci.yml

+10-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ jobs:
6262
- name: rustdoc
6363
run: RUSTDOCFLAGS="-Dwarnings" ./miri doc --document-private-items
6464

65-
# These jobs doesn't actually test anything, but they're only used to tell
65+
coverage:
66+
name: Coverage report
67+
runs-on: ubuntu-latest
68+
steps:
69+
- uses: actions/checkout@v4
70+
- uses: ./.github/workflows/setup
71+
- name: coverage
72+
run: ./miri test --coverage
73+
# These jobs don't actually test anything, but they're only used to tell
6674
# bors the build completed, as there is no practical way to detect when a
6775
# workflow is successful listening to webhooks only.
6876
#
@@ -92,7 +100,7 @@ jobs:
92100
contents: write
93101
# ... and create a PR.
94102
pull-requests: write
95-
needs: [build, style]
103+
needs: [build, style, coverage]
96104
if: github.event_name == 'schedule' && failure()
97105
steps:
98106
# Send a Zulip notification

ci/ci.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,4 @@ case $HOST_TARGET in
175175
echo "FATAL: unknown host target: $HOST_TARGET"
176176
exit 1
177177
;;
178-
esac
178+
esac

miri-script/Cargo.lock

+39-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

miri-script/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ rustc_version = "0.4"
2424
dunce = "1.0.4"
2525
directories = "5"
2626
serde_json = "1"
27+
tempfile = "3.13.0"

miri-script/src/commands.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,13 @@ impl Command {
172172
Command::Install { flags } => Self::install(flags),
173173
Command::Build { flags } => Self::build(flags),
174174
Command::Check { flags } => Self::check(flags),
175-
Command::Test { bless, flags, target } => Self::test(bless, flags, target),
175+
Command::Test { bless, flags, target, coverage } =>
176+
Self::test(
177+
bless,
178+
flags,
179+
target,
180+
coverage.then_some(crate::coverage::CoverageReport::new()?),
181+
),
176182
Command::Run { dep, verbose, many_seeds, target, edition, flags } =>
177183
Self::run(dep, verbose, many_seeds, target, edition, flags),
178184
Command::Doc { flags } => Self::doc(flags),
@@ -458,9 +464,18 @@ impl Command {
458464
Ok(())
459465
}
460466

461-
fn test(bless: bool, mut flags: Vec<String>, target: Option<String>) -> Result<()> {
467+
fn test(
468+
bless: bool,
469+
mut flags: Vec<String>,
470+
target: Option<String>,
471+
coverage: Option<crate::coverage::CoverageReport>,
472+
) -> Result<()> {
462473
let mut e = MiriEnv::new()?;
463474

475+
if let Some(report) = &coverage {
476+
report.add_env_vars(&mut e)?;
477+
}
478+
464479
// Prepare a sysroot. (Also builds cargo-miri, which we need.)
465480
e.build_miri_sysroot(/* quiet */ false, target.as_deref())?;
466481

@@ -479,6 +494,11 @@ impl Command {
479494
// Then test, and let caller control flags.
480495
// Only in root project as `cargo-miri` has no tests.
481496
e.test(".", &flags)?;
497+
498+
if let Some(coverage) = &coverage {
499+
coverage.show_coverage_report(&e)?;
500+
}
501+
482502
Ok(())
483503
}
484504

miri-script/src/coverage.rs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use std::ffi::OsString;
2+
3+
use anyhow::{Context, Result};
4+
use xshell::cmd;
5+
6+
use crate::util::MiriEnv;
7+
8+
/// CoverageReport can generate code coverage reports for miri.
9+
pub struct CoverageReport {
10+
11+
/// path is a temporary directory where coverage artifacts will be stored.
12+
path: tempfile::TempDir,
13+
}
14+
15+
impl CoverageReport {
16+
17+
/// new creates a new CoverageReport
18+
///
19+
/// # Errors
20+
///
21+
/// An error will be returned if a temporary directory could not be created.
22+
pub fn new() -> Result<Self> {
23+
Ok(Self { path: tempfile::TempDir::new()? })
24+
}
25+
26+
/// add_env_vars will add the required environment variables to MiriEnv `e`.
27+
pub fn add_env_vars(&self, e: &mut MiriEnv) -> Result<()> {
28+
let rustflags = e.sh.var("RUSTFLAGS")?;
29+
let rustflags = format!("{rustflags} -C instrument-coverage");
30+
e.sh.set_var("RUSTFLAGS", rustflags);
31+
32+
let file_template = self.path.path().join("default_%m_%p.profraw");
33+
e.sh.set_var("LLVM_PROFILE_FILE", file_template);
34+
Ok(())
35+
}
36+
37+
/// show_coverage_report will print coverage information using the artifact
38+
/// files in `self.path`.
39+
pub fn show_coverage_report(&self, e: &MiriEnv) -> Result<()> {
40+
let profraw_files: Vec<_> = self.profraw_files()?;
41+
42+
let mut profdata_bin = e.libdir.clone();
43+
profdata_bin.push("..");
44+
profdata_bin.push("bin");
45+
profdata_bin.push("llvm-profdata");
46+
47+
let merged_file = self.path.path().join("merged.profdata");
48+
49+
// Merge the profraw files
50+
let profraw_files_cloned = profraw_files.iter();
51+
cmd!(e.sh, "{profdata_bin} merge -sparse {profraw_files_cloned...} -o {merged_file}")
52+
.quiet()
53+
.run()?;
54+
55+
// Create the coverage report.
56+
let mut cov_bin = e.libdir.clone();
57+
cov_bin.push("..");
58+
cov_bin.push("bin");
59+
cov_bin.push("llvm-cov");
60+
let miri_bin =
61+
e.build_get_binary(".").context("failed to get filename of miri executable")?;
62+
cmd!(
63+
e.sh,
64+
"{cov_bin} report --instr-profile={merged_file} --object {miri_bin} --sources src/"
65+
)
66+
.run()?;
67+
68+
Ok(())
69+
}
70+
71+
/// profraw_files returns the profraw files in `self.path`.
72+
///
73+
/// # Errors
74+
///
75+
/// An error will be returned if `self.path` can't be read.
76+
fn profraw_files(&self) -> Result<Vec<OsString>> {
77+
Ok(std::fs::read_dir(&self.path)?
78+
.filter_map(|r| r.ok())
79+
.map(|e| e.path())
80+
.filter(|p| p.extension().map(|e| e == "profraw").unwrap_or(false))
81+
.map(|p| p.as_os_str().to_os_string())
82+
.collect())
83+
}
84+
}

miri-script/src/main.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
mod args;
44
mod commands;
5+
mod coverage;
56
mod util;
67

78
use std::ops::Range;
@@ -34,6 +35,8 @@ pub enum Command {
3435
/// The cross-interpretation target.
3536
/// If none then the host is the target.
3637
target: Option<String>,
38+
/// Produce coverage report if set.
39+
coverage: bool,
3740
/// Flags that are passed through to the test harness.
3841
flags: Vec<String>,
3942
},
@@ -158,9 +161,12 @@ fn main() -> Result<()> {
158161
let mut target = None;
159162
let mut bless = false;
160163
let mut flags = Vec::new();
164+
let mut coverage = false;
161165
loop {
162166
if args.get_long_flag("bless")? {
163167
bless = true;
168+
} else if args.get_long_flag("coverage")? {
169+
coverage = true;
164170
} else if let Some(val) = args.get_long_opt("target")? {
165171
target = Some(val);
166172
} else if let Some(flag) = args.get_other() {
@@ -169,7 +175,7 @@ fn main() -> Result<()> {
169175
break;
170176
}
171177
}
172-
Command::Test { bless, flags, target }
178+
Command::Test { bless, flags, target, coverage }
173179
}
174180
Some("run") => {
175181
let mut dep = false;

miri-script/src/util.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub struct MiriEnv {
4141
pub sysroot: PathBuf,
4242
/// The shell we use.
4343
pub sh: Shell,
44+
/// The library dir in the sysroot.
45+
pub libdir: PathBuf,
4446
}
4547

4648
impl MiriEnv {
@@ -96,7 +98,8 @@ impl MiriEnv {
9698
// so that Windows can find the DLLs.
9799
if cfg!(windows) {
98100
let old_path = sh.var("PATH")?;
99-
let new_path = env::join_paths(iter::once(libdir).chain(env::split_paths(&old_path)))?;
101+
let new_path =
102+
env::join_paths(iter::once(libdir.clone()).chain(env::split_paths(&old_path)))?;
100103
sh.set_var("PATH", new_path);
101104
}
102105

@@ -111,7 +114,7 @@ impl MiriEnv {
111114
std::process::exit(1);
112115
}
113116

114-
Ok(MiriEnv { miri_dir, toolchain, sh, sysroot, cargo_extra_flags })
117+
Ok(MiriEnv { miri_dir, toolchain, sh, sysroot, cargo_extra_flags, libdir })
115118
}
116119

117120
pub fn cargo_cmd(&self, crate_dir: impl AsRef<OsStr>, cmd: &str) -> Cmd<'_> {

0 commit comments

Comments
 (0)