cverif 0.1.1

Verifies that compiled machine code faithfully matches its C source
Documentation
//! Machine-code mutation campaign against the verifier.
//!
//!     cargo run --release --example mutate -- <object.o> <source.c> [options]
//!
//!     --jobs N      worker threads (default and maximum: 8; memory use grows with it)
//!     --stride N    keep one mutant in N (default 1: all of them)
//!     --samples N   concrete inputs tried on each survivor (default 400)
//!     -I DIR        include directory for the C source
//!     -v            list every survivor, not only those seen to differ
//!
//! Every instruction of every proved function is mutated (single-bit flips, NOP, exchange
//! with its successor).  A mutant the verifier still proves, with the changed bytes counted as
//! covered, is a survivor; an independent interpreter then compares the original and the
//! mutated code on concrete inputs.  The exit status is 1 if any survivor was seen to differ.

#[path = "../tests/support/mod.rs"]
mod support;

use std::path::PathBuf;
use std::process::ExitCode;

fn main() -> ExitCode {
    let mut pos = Vec::new();
    let mut include = Vec::new();
    let (mut jobs, mut stride, mut samples, mut verbose) = (
        std::thread::available_parallelism()
            .map_or(4, |n| n.get())
            .min(support::mutate::MAX_JOBS),
        1usize,
        400u64,
        false,
    );
    let mut it = std::env::args().skip(1);
    while let Some(a) = it.next() {
        let mut num = |what: &str| -> usize {
            it.next()
                .and_then(|v| v.parse().ok())
                .unwrap_or_else(|| panic!("{what} needs a number"))
        };
        match a.as_str() {
            "--jobs" => jobs = num("--jobs"),
            "--stride" => stride = num("--stride"),
            "--samples" => samples = num("--samples") as u64,
            "-I" => include.push(PathBuf::from(it.next().expect("-I needs a directory"))),
            "-v" => verbose = true,
            _ => pos.push(PathBuf::from(a)),
        }
    }
    if pos.len() != 2 {
        eprintln!("usage: mutate <object.o> <source.c> [--jobs N] [--stride N] [--samples N] [-I DIR] [-v]");
        return ExitCode::from(2);
    }
    let elf = std::fs::read(&pos[0]).expect("read object");
    // The verifier's panics are caught and reported per mutant.
    if std::env::var_os("CVERIF_MUTATE_TRACE").is_none() {
        std::panic::set_hook(Box::new(|_| {}));
    }
    let s = support::mutate::campaign(&elf, &pos[1], &include, stride, jobs, samples);
    let suspects = s.suspects();
    println!(
        "{}: {} mutants, {} killed ({:.1}%), {} not vouched for, {} survived ({} seen to differ)",
        pos[0].display(),
        s.mutants,
        s.killed,
        100.0 * s.killed as f64 / s.mutants.max(1) as f64,
        s.lost(),
        s.survivors.len(),
        suspects.len()
    );
    println!("  of which {} retargeted relocations", s.relocations);
    for (why, n) in &s.not_vouched {
        println!("  not vouched {n:6}  {why}");
    }
    for p in &s.panics {
        println!("  PANIC    {p}");
    }
    for v in &s.survivors {
        match &v.difference {
            Some(d) if v.bounded => println!("  beyond-bound {} {}\n           {d}", v.func, v.what),
            Some(d) => println!("  DIFFERS  {} {}\n           {d}", v.func, v.what),
            None if verbose => println!("  neutral? {} {}", v.func, v.what),
            None => {}
        }
    }
    if suspects.is_empty() {
        ExitCode::SUCCESS
    } else {
        ExitCode::from(1)
    }
}