Skip to content

Make analysis/test miri-compatible and add a miri test for it #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions analysis/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ edition = "2021"
[dependencies]
libc = "0.2"
c2rust-analysis-rt = { path = "../runtime", optional = true, version = "0.1.0" }

[features]
miri = []
25 changes: 25 additions & 0 deletions analysis/test/src/pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,34 @@ extern "C" {
fn calloc(_: libc::c_ulong, _: libc::c_ulong) -> *mut libc::c_void;
fn realloc(_: *mut libc::c_void, _: libc::c_ulong) -> *mut libc::c_void;
fn free(__ptr: *mut libc::c_void);
}

#[cfg(not(feature = "miri"))]
extern "C" {
fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
}

/// `miri` does not support calling variadic functions like [`printf`],
/// but we want to test for UB, leaks, etc. using `cargo miri run`.
///
/// Luckily, all [`printf`] calls in this module are monomorphic,
/// in that they all have the same format string and same call signature,
/// so we can replace it with a [`printf`] shim that preserves the behavior
/// only for the exact monomorphic usages in this module.
///
/// Note that there is no way to detect `miri` is running,
/// so we have to put this under a separate `miri` feature
/// that should be enabled when testing under `miri` with
/// `cargo miri run --features miri`.
#[cfg(feature = "miri")]
fn printf(fmt: *const libc::c_char, i: i32) -> libc::c_int {
use std::ffi::CStr;
assert_eq!(unsafe { CStr::from_ptr(fmt) }, CStr::from_bytes_with_nul(b"%i\n\x00").unwrap());
let s = format!("{i}\n");
print!("{s}");
s.len() as libc::c_int
}

/// Hidden from instrumentation so that we can polyfill [`reallocarray`] with it.
const REALLOC: unsafe extern "C" fn(*mut libc::c_void, libc::c_ulong) -> *mut libc::c_void = realloc;

Expand Down
13 changes: 13 additions & 0 deletions pdg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,17 @@ mod tests {
insta::assert_display_snapshot!(pdg);
Ok(())
}

#[test]
#[ignore]
fn analysis_test_miri() -> eyre::Result<()> {
init();
let mut cmd = Command::new("cargo");
cmd.current_dir(repo_dir()?.join("analysis/test"))
.args(&["miri", "run", "--features", "miri"])
.env("MIRIFLAGS", "");
let status = cmd.status()?;
ensure!(status.success(), eyre!("{cmd:?} failed: {status}"));
Ok(())
}
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2022-08-08"
components = ["rustfmt-preview", "rustc-dev", "rust-src"]
components = ["rustfmt-preview", "rustc-dev", "rust-src", "miri"]