examples/union-cc/src/main.rs
// SPDX-License-Identifier: AGPL-3.0-only
//! union-cc: Heimdall's worked example and end-to-end test (see Cargo.toml).
//!
//! - `union-cc c`: the corpus run. Rust prints the answer of a C function (c/parent.c).
//! - `union-cc rare <dir>`: never in the corpus. It makes the directory and removes it again,
//! calls the corpus never makes, so only Mímir's graph can put them in the filter.
//! - `union-cc rare-c`: never in the corpus either. C asks its nice value (getpriority, in
//! c/parent.c): a call the Rust code never makes, so only the graph's C half (or its binary
//! half) can put it in the filter; the Rust graph alone cannot.
use std::ffi::c_long;
use std::process::ExitCode;
unsafe extern "C" {
/// c/parent.c: the parent's process id, asked in C.
fn union_cc_parent() -> c_long;
/// c/parent.c: the process's nice value, asked in C.
fn union_cc_priority() -> c_long;
}
fn main() -> ExitCode {
let args: Vec<String> = std::env::args().skip(1).collect();
match args
.iter()
.map(String::as_str)
.collect::<Vec<_>>()
.as_slice()
{
["c"] => {
// SAFETY: a C function with no arguments that only asks the kernel.
let parent = unsafe { union_cc_parent() };
println!("union-cc: parent {parent}");
ExitCode::SUCCESS
}
["rare-c"] => {
// SAFETY: a C function with no arguments that only asks the kernel.
let nice = unsafe { union_cc_priority() };
println!("union-cc: priority {nice}");
ExitCode::SUCCESS
}
["rare", dir] => match rare(dir) {
Ok(()) => {
println!("union-cc: made and removed {dir}");
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("union-cc: {dir}: {e}");
ExitCode::from(1)
}
},
_ => {
eprintln!("usage: union-cc c | union-cc rare <dir> | union-cc rare-c");
ExitCode::from(2)
}
}
}
/// The path only the graph knows: a directory made and removed.
fn rare(dir: &str) -> std::io::Result<()> {
std::fs::create_dir(dir)?;
std::fs::remove_dir(dir)
}