use std::path::Path;
use std::process::Command;
#[test]
fn panic_in_one_function_is_an_inconclusive_result() {
let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/corpus/arith.c");
let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("internal_error");
std::fs::create_dir_all(&dir).unwrap();
let obj = dir.join(format!("arith-{}.o", std::process::id()));
let built = Command::new("arm-none-eabi-gcc")
.args(["-mcpu=cortex-m0plus", "-mthumb", "-Os", "-ffreestanding", "-c"])
.arg(&src)
.arg("-o")
.arg(&obj)
.output()
.is_ok_and(|o| o.status.success());
if !built {
eprintln!("skipping: no arm-none-eabi-gcc");
return;
}
let run = |victim: Option<&str>| {
let mut c = Command::new(env!("CARGO_BIN_EXE_cverif"));
c.arg(&obj).arg(&src).args(["--work-limit", "30000", "-j", "2"]);
c.env_remove("CVERIF_DEBUG");
match victim {
Some(v) => c.env("CVERIF_TEST_PANIC", v),
None => c.env_remove("CVERIF_TEST_PANIC"),
};
let o = c.output().expect("run cverif");
(
o.status.code(),
String::from_utf8_lossy(&o.stdout).into_owned(),
String::from_utf8_lossy(&o.stderr).into_owned(),
)
};
let (_, clean, _) = run(None);
let proved = |out: &str| out.lines().filter(|l| l.starts_with("PROVED")).count();
let victim = clean
.lines()
.find(|l| l.starts_with("PROVED"))
.and_then(|l| l.split_whitespace().nth(1))
.expect("a proved function")
.to_string();
let (code, out, err) = run(Some(&victim));
assert!(code.is_some(), "cverif was killed: {err}");
let line = out
.lines()
.find(|l| l.split_whitespace().nth(1) == Some(victim.as_str()))
.unwrap_or_else(|| panic!("{victim} missing from the report:\n{out}"));
assert!(line.starts_with("INCONCLUSIVE"), "{line}");
assert!(
line.contains("internal error") && line.contains("CVERIF_TEST_PANIC"),
"{line}"
);
assert!(line.contains("src/verify.rs:"), "where it happened: {line}");
assert_eq!(
proved(&out),
proved(&clean) - 1,
"the other functions are verified as usual"
);
assert!(!err.contains("panicked at"), "no raw panic report: {err}");
assert!(
!out.contains("verdict: MATCH"),
"an internal error is not a match:\n{out}"
);
}
#[test]
fn panic_in_one_comparison_is_an_inconclusive_result() {
let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/corpus/arith.c");
let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("internal_error");
std::fs::create_dir_all(&dir).unwrap();
let mut objs = Vec::new();
for (cpu, level) in [("cortex-m0plus", "-Os"), ("cortex-m3", "-O1")] {
let obj = dir.join(format!("arith-cmp-{}{level}.o", std::process::id()));
let built = Command::new("arm-none-eabi-gcc")
.args([&format!("-mcpu={cpu}"), "-mthumb", level, "-ffreestanding", "-c"])
.arg(&src)
.arg("-o")
.arg(&obj)
.output()
.is_ok_and(|o| o.status.success());
if !built {
eprintln!("skipping: no arm-none-eabi-gcc");
return;
}
objs.push(obj);
}
let run = |victim: Option<&str>| {
let mut c = Command::new(env!("CARGO_BIN_EXE_cverif"));
c.arg("--compare")
.args(&objs)
.args(["--work-limit", "30000", "-j", "2"]);
c.env_remove("CVERIF_DEBUG");
match victim {
Some(v) => c.env("CVERIF_TEST_PANIC", v),
None => c.env_remove("CVERIF_TEST_PANIC"),
};
let o = c.output().expect("run cverif");
(
o.status.code(),
String::from_utf8_lossy(&o.stdout).into_owned(),
String::from_utf8_lossy(&o.stderr).into_owned(),
)
};
let (_, clean, _) = run(None);
let equivalent = |out: &str| out.lines().filter(|l| l.starts_with("EQUIVALENT")).count();
let victim = clean
.lines()
.find(|l| l.starts_with("EQUIVALENT"))
.and_then(|l| l.split_whitespace().nth(1))
.expect("a pair proved equivalent")
.to_string();
let (code, out, err) = run(Some(&victim));
assert!(code.is_some(), "cverif was killed: {err}");
let line = out
.lines()
.find(|l| l.split_whitespace().nth(1) == Some(victim.as_str()))
.unwrap_or_else(|| panic!("{victim} missing from the report:\n{out}"));
assert!(line.starts_with("INCONCLUSIVE"), "{line}");
assert!(
line.contains("internal error") && line.contains("CVERIF_TEST_PANIC"),
"{line}"
);
assert_eq!(
equivalent(&out),
equivalent(&clean) - 1,
"the other pairs are compared as usual"
);
assert!(!err.contains("panicked at"), "no raw panic report: {err}");
assert!(
!out.contains("verdict: EQUIVALENT"),
"an internal error is not a result:\n{out}"
);
for o in objs {
let _ = std::fs::remove_file(o);
}
}
#[test]
fn ill_formed_terms_are_refused_where_they_are_built() {
let mut tm = cverif::term::Terms::new();
let (a, b) = (tm.sym("a", 32), tm.sym("b", 8));
for (name, build) in [
(
"and",
(|tm: &mut cverif::term::Terms, a, b| tm.and(a, b)) as fn(&mut cverif::term::Terms, u32, u32) -> u32,
),
("add", |tm, a, b| tm.add(a, b)),
("xor", |tm, a, b| tm.xor(a, b)),
("eq", |tm, a, b| tm.eq(a, b)),
("ult", |tm, a, b| tm.ult(a, b)),
("udiv", |tm, a, b| tm.udiv(a, b)),
] {
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| build(&mut tm, a, b)));
let msg = r.expect_err(name);
let msg = msg.downcast_ref::<String>().cloned().unwrap_or_default();
assert!(
msg.contains("ill-formed term") && msg.contains("32-bit") && msg.contains("8-bit"),
"{name}: {msg}"
);
}
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| tm.extract(b, 0, 32)));
assert!(r.is_err(), "32 bits of an 8-bit value");
}
#[test]
#[ignore]
fn helper_verify_then_fail() {
let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/corpus/arith.c");
let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("internal_error");
std::fs::create_dir_all(&dir).unwrap();
let obj = dir.join(format!("arith-hook-{}.o", std::process::id()));
let built = Command::new("arm-none-eabi-gcc")
.args(["-mcpu=cortex-m0plus", "-mthumb", "-Os", "-ffreestanding", "-c"])
.arg(&src)
.arg("-o")
.arg(&obj)
.output()
.is_ok_and(|o| o.status.success());
if built {
let image = cverif::inputs::load(&std::fs::read(&obj).unwrap(), 0, &[], &[], None).unwrap();
let prog = cverif::c::parse_files(&[src], &cverif::c::Options::for_image(&image)).unwrap();
let opts = cverif::verify::Options {
jobs: 1,
work_limit: 2_000,
..Default::default()
};
let r = cverif::verify::verify(&prog, &image, &opts);
assert!(!r.funcs.is_empty());
}
panic!("MARKER a message of the test's own (verified: {built})");
}
#[test]
fn other_panics_are_reported_as_usual() {
let o = Command::new(std::env::current_exe().unwrap())
.args(["--ignored", "--exact", "helper_verify_then_fail", "--test-threads=1"])
.env_remove("CVERIF_DEBUG")
.output()
.expect("run the helper test");
let out = format!(
"{}{}",
String::from_utf8_lossy(&o.stdout),
String::from_utf8_lossy(&o.stderr)
);
assert!(!o.status.success(), "the helper fails on purpose:\n{out}");
assert!(
out.contains("MARKER a message of the test's own"),
"the message was swallowed:\n{out}"
);
if out.contains("verified: false") {
eprintln!("skipping: no arm-none-eabi-gcc (the hook was not installed)");
}
}