use std::fs;
use std::io::{Read, Write};
use std::os::unix::process::CommandExt;
use std::path::Path;
use std::process::{Command, Stdio};
use std::time::Duration;
use anyhow::{bail, Context, Result};
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
use fnprint_core::{
both_named, dump_traces, eval, index_bytes, index_bytes_shard, match_aligned, match_by_name,
query_corpus, source_str, triage, warm_pool, EffectTrace, IndexedFunc, Verdict, MIN_COMPLEXITY,
};
use fnprint_db::{Db, FuncPrint, FuncRec, MemCorpus};
use fnprint_emu::Config;
use fnprint_loader::FuncSource;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug)]
struct StdoutClosed;
impl std::fmt::Display for StdoutClosed {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("stdout closed")
}
}
impl std::error::Error for StdoutClosed {}
fn stdout_result(r: std::io::Result<()>) -> Result<()> {
match r {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => Err(StdoutClosed.into()),
Err(e) => Err(e).context("writing to stdout"),
}
}
macro_rules! outln {
($($t:tt)*) => {
stdout_result(writeln!(std::io::stdout(), $($t)*))?
};
}
const MAX_INPUT: u64 = 512 << 20; const MAX_REPLY: u64 = 128 << 20;
const WORKER_TOKEN: &str = "__worker";
const DEADLINE_THREAD_CLAMP: u64 = 16;
fn worker_deadline() -> Duration {
let threads = std::thread::available_parallelism()
.map(|n| n.get() as u64)
.unwrap_or(8)
.min(DEADLINE_THREAD_CLAMP);
Duration::from_secs(120u64.saturating_mul(threads).saturating_add(300))
}
const POST_REPLY_GRACE: Duration = Duration::from_secs(30);
fn reap_bounded(
child: &mut std::process::Child,
pgid: Option<rustix::process::Pid>,
grace: Duration,
) -> std::io::Result<std::process::ExitStatus> {
let until = std::time::Instant::now() + grace;
loop {
if let Some(st) = child.try_wait()? {
return Ok(st);
}
if std::time::Instant::now() >= until {
if let Some(pgid) = pgid {
let _ = rustix::process::kill_process_group(pgid, rustix::process::Signal::Kill);
}
return child.wait();
}
std::thread::sleep(Duration::from_millis(20));
}
}
#[derive(Serialize, Deserialize)]
enum WorkerReply {
IndexOk(Vec<IndexedFunc>),
DumpOk(Vec<String>),
DbOk(Vec<FuncRec>),
Err(String),
}
#[derive(Parser)]
#[command(
name = "fnprint",
version,
about = "behavioral function fingerprinting",
after_help = "\
examples:
fnprint index libz.so -o corpus.db fingerprint a build you have symbols for
fnprint query mystery.so --corpus corpus.db name unknown functions from that corpus
fnprint match old.so new.so show which shared functions changed behavior
fnprint triage t.so --vuln v.db --patched p.db rank a build against vuln vs patched
fnprint query t.so --corpus c.db --format json machine-readable output for scripts
fnprint completions bash > ~/.local/share/bash-completion/completions/fnprint
install shell completion for your user
"
)]
struct Cli {
#[arg(long, global = true)]
no_sandbox: bool,
#[arg(long, value_enum, default_value_t = Format::Human, global = true)]
format: Format,
#[arg(long, default_value_t = fnprint_core::GRAPH_WEIGHT, global = true)]
graph_weight: f64,
#[command(subcommand)]
cmd: Cmd,
}
const OUTPUT_SCHEMA_VERSION: u32 = 1;
#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]
enum Format {
Human,
Json,
R2,
}
#[derive(Subcommand)]
enum Cmd {
Index {
binary: String,
#[arg(short, long)]
out: Option<String>,
},
Match {
a: String,
b: String,
#[arg(long, default_value_t = 40)]
limit: usize,
#[arg(long)]
align: bool,
},
Query {
target: String,
#[arg(long)]
corpus: String,
#[arg(long, default_value_t = 0.7)]
threshold: f64,
#[arg(long, default_value_t = 0)]
limit: usize,
},
Eval { a: String, b: String },
Triage {
target: String,
#[arg(long)]
vuln: String,
#[arg(long)]
patched: String,
#[arg(long, default_value_t = 0.6)]
min_sim: f64,
#[arg(long, default_value_t = 0.08)]
margin: f64,
#[arg(long, default_value_t = 0)]
limit: usize,
},
Dump { binary: String, func: String },
Completions {
#[arg(value_enum)]
shell: clap_complete::Shell,
},
}
fn main() -> Result<()> {
let mut raw = std::env::args();
let _bin = raw.next();
if raw.next().as_deref() == Some(WORKER_TOKEN) {
let op = raw.next().unwrap_or_default();
let func = raw.next();
return worker_main(&op, func.as_deref());
}
let cli = Cli::parse();
let ns = cli.no_sandbox;
let fmt = cli.format;
let gw = cli.graph_weight;
if !gw.is_finite() || gw < 0.0 {
bail!("--graph-weight must be a non-negative number");
}
let res = match cli.cmd {
Cmd::Index { binary, out } => cmd_index(&binary, out.as_deref(), ns, fmt),
Cmd::Match { a, b, limit, align } => cmd_match(&a, &b, limit, align, gw, ns, fmt),
Cmd::Query {
target,
corpus,
threshold,
limit,
} => cmd_query(&target, &corpus, threshold, limit, gw, ns, fmt),
Cmd::Eval { a, b } => cmd_eval(&a, &b, gw, ns, fmt),
Cmd::Triage {
target,
vuln,
patched,
min_sim,
margin,
limit,
} => cmd_triage(&target, &vuln, &patched, min_sim, margin, limit, ns, fmt),
Cmd::Dump { binary, func } => cmd_dump(&binary, &func, ns, fmt),
Cmd::Completions { shell } => cmd_completions(shell),
};
match res {
Err(e) if e.is::<StdoutClosed>() => Ok(()),
r => r,
}
}
fn cmd_completions(shell: clap_complete::Shell) -> Result<()> {
let mut cmd = Cli::command();
let mut buf = Vec::new();
clap_complete::generate(shell, &mut cmd, "fnprint", &mut buf);
stdout_result(std::io::stdout().write_all(&buf))
}
fn worker_main(op: &str, func: Option<&str>) -> Result<()> {
fnprint_sandbox::harden_worker_preinput().context("worker preinput hardening")?;
warm_thread_pool();
fnprint_sandbox::lock_down_worker().context("worker could not enter sandbox")?;
let mut input = Vec::new();
let n = std::io::stdin()
.lock()
.take(MAX_INPUT + 1)
.read_to_end(&mut input)
.context("worker reading stdin")?;
if n as u64 > MAX_INPUT {
return emit(&WorkerReply::Err("input over size cap".into()));
}
let reply = match op {
"index" => {
let (idx, cnt) = parse_shard(func);
match index_bytes_shard(&input, emu_config(), idx, cnt) {
Ok(funcs) => WorkerReply::IndexOk(funcs),
Err(e) => WorkerReply::Err(format!("{e:#}")),
}
}
"dump" => {
let name = func.unwrap_or_default();
match dump_traces(&input, name, emu_config()) {
Ok(traces) => WorkerReply::DumpOk(render_traces(&traces)),
Err(e) => WorkerReply::Err(format!("{e:#}")),
}
}
"dbload" => match Db::open_image(&input).and_then(|db| db.all()) {
Ok(recs) => WorkerReply::DbOk(recs),
Err(e) => WorkerReply::Err(format!("{e:#}")),
},
other => WorkerReply::Err(format!("unknown worker op: {other}")),
};
emit(&reply)
}
fn warm_thread_pool() {
warm_pool();
}
fn render_traces(traces: &[EffectTrace]) -> Vec<String> {
let mut out = Vec::new();
for (i, t) in traces.iter().enumerate() {
out.push(format!(
"-- path {} ({} effects, instret {}, capped {}, coverage {:.2}) --",
i,
t.effects.len(),
t.instret,
t.capped,
t.coverage
));
for e in &t.effects {
out.push(format!(" {e:?}"));
}
}
out
}
fn emit(reply: &WorkerReply) -> Result<()> {
let buf = postcard::to_allocvec(reply).context("serializing worker reply")?;
std::io::stdout()
.lock()
.write_all(&buf)
.context("worker writing reply")?;
Ok(())
}
fn run_worker(op: &str, func: Option<&str>, input: Arc<Vec<u8>>) -> Result<WorkerReply> {
let exe = std::env::current_exe().context("locating self for worker re-exec")?;
let mut cmd = Command::new(exe);
cmd.env_clear();
cmd.env("FNPRINT_BLANKET", emu_config().blanket_restarts.to_string());
cmd.arg(WORKER_TOKEN).arg(op);
if let Some(f) = func {
cmd.arg(f);
}
cmd.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
cmd.process_group(0);
let mut child = cmd.spawn().context("spawning jailed worker")?;
let wpgid = rustix::process::Pid::from_raw(child.id() as i32);
let mut stdin = child.stdin.take().context("worker stdin")?;
let payload = Arc::clone(&input);
let writer = std::thread::spawn(move || {
let _ = stdin.write_all(&payload);
drop(stdin);
});
let mut errpipe = child.stderr.take().context("worker stderr")?;
let errdrain = std::thread::spawn(move || {
let mut buf = Vec::new();
let _ = errpipe.by_ref().take(8 * 1024).read_to_end(&mut buf);
let _ = std::io::copy(&mut errpipe, &mut std::io::sink());
buf
});
let mut stdout = child.stdout.take().context("worker stdout")?;
let (reply_tx, reply_rx) = std::sync::mpsc::channel::<std::io::Result<Vec<u8>>>();
let reader = std::thread::spawn(move || {
let mut out = Vec::new();
let r = stdout.by_ref().take(MAX_REPLY + 1).read_to_end(&mut out);
let _ = reply_tx.send(r.map(|_| out));
});
let deadline = worker_deadline();
let out = match reply_rx.recv_timeout(deadline) {
Ok(Ok(out)) => out,
Ok(Err(e)) => {
let _ = child.wait();
let _ = writer.join();
let _ = reader.join();
return Err(anyhow::Error::new(e).context("reading worker reply"));
}
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
if let Some(pgid) = wpgid {
let _ = rustix::process::kill_process_group(pgid, rustix::process::Signal::Kill);
}
let _ = child.wait();
bail!(
"jailed worker exceeded the {}s time budget and was killed",
deadline.as_secs()
);
}
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
let _ = child.wait();
bail!("jailed worker reply reader failed");
}
};
let status = reap_bounded(&mut child, wpgid, POST_REPLY_GRACE).context("waiting on worker")?;
let _ = writer.join();
let _ = reader.join();
let errtail = errdrain.join().unwrap_or_default();
if !status.success() {
let tail = String::from_utf8_lossy(&errtail);
let tail = tail.trim();
if tail.is_empty() {
bail!("jailed worker exited abnormally ({status}) - likely killed by the sandbox or a crash in the emulator");
}
bail!(
"jailed worker exited abnormally ({status}) - likely killed by the sandbox or a crash in the emulator; stderr: {}",
esc(tail)
);
}
if out.len() as u64 > MAX_REPLY {
bail!("jailed worker reply over size cap");
}
let reply: WorkerReply = postcard::from_bytes(&out).context("decoding worker reply")?;
Ok(reply)
}
fn parse_shard(spec: Option<&str>) -> (usize, usize) {
let s = match spec {
Some(s) => s,
None => return (0, 1),
};
let mut it = s.split('/');
let idx = it.next().and_then(|v| v.parse::<usize>().ok());
let cnt = it.next().and_then(|v| v.parse::<usize>().ok());
match (idx, cnt) {
(Some(i), Some(c)) if c >= 1 && i < c => (i, c),
_ => (0, 1),
}
}
fn emu_config() -> Config {
let mut cfg = Config::default();
if let Ok(v) = std::env::var("FNPRINT_BLANKET") {
if let Ok(n) = v.parse::<usize>() {
cfg.blanket_restarts = n.min(16);
}
}
cfg
}
fn shard_count() -> usize {
if let Ok(v) = std::env::var("FNPRINT_SHARDS") {
if let Ok(n) = v.parse::<usize>() {
return n.clamp(1, 64);
}
}
1
}
fn sanitize_coverage(funcs: &mut [IndexedFunc]) {
for f in funcs {
f.coverage = if f.coverage.is_finite() {
f.coverage.clamp(0.0, 1.0)
} else {
1.0
};
}
}
const MAX_AGGREGATE_INPUT: u64 = 2 << 30;
fn clamp_shards_for_input(requested: usize, input_len: u64) -> usize {
if input_len == 0 {
return requested;
}
let max_by_mem = (MAX_AGGREGATE_INPUT / input_len).max(1);
(requested as u64).min(max_by_mem) as usize
}
fn run_index(bytes: &[u8], no_sandbox: bool) -> Result<Vec<IndexedFunc>> {
if no_sandbox {
eprintln!("warning: --no-sandbox, running the emulator without the jail");
return index_bytes(bytes, emu_config());
}
let requested = shard_count();
let n = clamp_shards_for_input(requested, bytes.len() as u64);
if n < requested {
eprintln!(
"note: reduced shards {requested} -> {n} to bound memory for a {}-byte input",
bytes.len()
);
}
if n <= 1 {
return match run_worker("index", None, Arc::new(bytes.to_vec()))? {
WorkerReply::IndexOk(mut funcs) => {
validate_reply_fps(&funcs)?;
sanitize_coverage(&mut funcs);
Ok(funcs)
}
WorkerReply::Err(e) => bail!("{}", esc(&e)),
_ => bail!("worker returned the wrong reply kind for index"),
};
}
run_workers_sharded(bytes, n)
}
fn run_workers_sharded(bytes: &[u8], n: usize) -> Result<Vec<IndexedFunc>> {
let shared = Arc::new(bytes.to_vec());
let handles: Vec<_> = (0..n)
.map(|i| {
let shared = Arc::clone(&shared);
std::thread::spawn(move || {
let spec = format!("{i}/{n}");
run_worker("index", Some(&spec), shared)
})
})
.collect();
let mut all: Vec<IndexedFunc> = Vec::new();
let mut first_err: Option<anyhow::Error> = None;
for h in handles {
let res = match h.join() {
Ok(r) => r,
Err(_) => {
if first_err.is_none() {
first_err = Some(anyhow::anyhow!("a shard worker thread panicked"));
}
continue;
}
};
match res {
Ok(WorkerReply::IndexOk(mut funcs)) => {
if let Err(e) = validate_reply_fps(&funcs) {
if first_err.is_none() {
first_err = Some(e);
}
continue;
}
sanitize_coverage(&mut funcs);
all.append(&mut funcs);
}
Ok(WorkerReply::Err(e)) => {
if first_err.is_none() {
first_err = Some(anyhow::anyhow!("{}", esc(&e)));
}
}
Ok(_) => {
if first_err.is_none() {
first_err = Some(anyhow::anyhow!("worker returned the wrong reply kind"));
}
}
Err(e) => {
if first_err.is_none() {
first_err = Some(e);
}
}
}
}
if let Some(e) = first_err {
return Err(e);
}
all.sort_by(|a, b| {
a.entry
.cmp(&b.entry)
.then_with(|| a.name.cmp(&b.name))
.then_with(|| source_str(a.source).cmp(source_str(b.source)))
});
Ok(all)
}
fn validate_reply_fps(funcs: &[IndexedFunc]) -> Result<()> {
for f in funcs {
if f.fp.sig.len() != fnprint_core::SIG_LEN {
bail!("jailed worker returned a fingerprint with an invalid signature length");
}
}
Ok(())
}
fn run_dump(bytes: &[u8], func: &str, no_sandbox: bool) -> Result<Vec<String>> {
if no_sandbox {
eprintln!("warning: --no-sandbox, running the emulator without the jail");
return Ok(render_traces(&dump_traces(bytes, func, emu_config())?));
}
match run_worker("dump", Some(func), Arc::new(bytes.to_vec()))? {
WorkerReply::DumpOk(lines) => Ok(lines),
WorkerReply::Err(e) => bail!("{}", esc(&e)),
_ => bail!("worker returned the wrong reply kind for dump"),
}
}
fn is_db(p: &str) -> bool {
Path::new(p).extension().map(|e| e == "db").unwrap_or(false)
}
fn row_cap(limit: usize) -> usize {
if limit == 0 {
usize::MAX
} else {
limit
}
}
fn esc(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for b in s.bytes() {
if b == b' ' || b.is_ascii_graphic() {
out.push(b as char);
} else {
out.push_str(&format!("\\x{b:02x}"));
}
}
out
}
fn r2_ident(s: &str) -> String {
let mut out = String::with_capacity(s.len() + 4);
out.push_str("fnp.");
for c in s.chars() {
if c.is_ascii_alphanumeric() || c == '_' || c == '.' {
out.push(c);
} else {
out.push('_');
}
}
out
}
fn emit_json(v: &serde_json::Value) -> Result<()> {
outln!("{}", serde_json::to_string(v)?);
Ok(())
}
fn validate_corpus_records(recs: &[FuncRec]) -> Result<()> {
for r in recs {
if r.fp.sig.len() != fnprint_core::SIG_LEN {
bail!("jailed db worker returned a record with an invalid signature length");
}
}
Ok(())
}
fn load_corpus_records(path: &str, no_sandbox: bool) -> Result<Vec<FuncRec>> {
if no_sandbox {
eprintln!("warning: --no-sandbox, parsing corpus with in-process sqlite (unjailed)");
return Db::open(path)
.with_context(|| format!("opening corpus {}", esc(path)))?
.all();
}
let bytes = read_input(path)?;
match run_worker("dbload", None, Arc::new(bytes))? {
WorkerReply::DbOk(recs) => {
validate_corpus_records(&recs)?;
Ok(recs)
}
WorkerReply::Err(e) => bail!("{}", esc(&e)),
_ => bail!("worker returned the wrong reply kind for dbload"),
}
}
fn load_corpus(path: &str, no_sandbox: bool) -> Result<MemCorpus> {
Ok(MemCorpus::from_records(load_corpus_records(
path, no_sandbox,
)?))
}
fn read_input(path: &str) -> Result<Vec<u8>> {
let f = fs::File::open(path).with_context(|| format!("opening {}", esc(path)))?;
let md = f
.metadata()
.with_context(|| format!("stat {}", esc(path)))?;
if !md.is_file() {
bail!("{} is not a regular file", esc(path));
}
if md.len() > MAX_INPUT {
bail!(
"{} is {} bytes, over the {MAX_INPUT} byte cap",
esc(path),
md.len()
);
}
let mut bytes = Vec::new();
let n = f
.take(MAX_INPUT + 1)
.read_to_end(&mut bytes)
.with_context(|| format!("reading {}", esc(path)))?;
if n as u64 > MAX_INPUT {
bail!("{} over the {MAX_INPUT} byte cap", esc(path));
}
Ok(bytes)
}
fn load_index(path: &str, no_sandbox: bool) -> Result<Vec<IndexedFunc>> {
if is_db(path) {
Ok(load_corpus_records(path, no_sandbox)?
.into_iter()
.map(|r| IndexedFunc {
name: r.name,
entry: r.entry,
source: FuncSource::Symtab,
fp: r.fp,
coverage: 1.0,
callees: r.callees,
})
.collect())
} else {
let bytes = read_input(path)?;
run_index(&bytes, no_sandbox)
}
}
fn cmd_index(binary: &str, out: Option<&str>, no_sandbox: bool, fmt: Format) -> Result<()> {
if fmt == Format::R2 {
bail!("r2 output is only supported for query and triage");
}
let bytes = read_input(binary)?;
let funcs = run_index(&bytes, no_sandbox)?;
let label = Path::new(binary)
.file_name()
.and_then(|s| s.to_str())
.unwrap_or(binary);
let named = funcs.iter().filter(|f| f.name.is_some()).count();
let signal_cov: Vec<f32> = funcs
.iter()
.filter(|f| f.fp.complexity >= MIN_COMPLEXITY)
.map(|f| f.coverage)
.collect();
let usable = signal_cov.len();
let mean_cov = if signal_cov.is_empty() {
None
} else {
Some(signal_cov.iter().sum::<f32>() / signal_cov.len() as f32)
};
let mut wrote = None;
if let Some(path) = out {
let db = Db::open(path)?;
let prints: Vec<FuncPrint> = funcs
.iter()
.map(|f| FuncPrint {
name: f.name.as_deref(),
entry: f.entry,
source: source_str(f.source),
fp: &f.fp,
callees: &f.callees,
})
.collect();
db.insert_all(label, &prints)?;
wrote = Some(path);
}
if fmt == Format::Json {
let list: Vec<serde_json::Value> = funcs
.iter()
.map(|f| {
serde_json::json!({
"entry": format!("{:#x}", f.entry),
"name": f.name.as_deref().map(esc),
"source": source_str(f.source),
"complexity": f.fp.complexity,
"shingles": f.fp.shingles,
"capped": f.fp.capped,
"coverage": f.coverage,
})
})
.collect();
return emit_json(&serde_json::json!({
"schema_version": OUTPUT_SCHEMA_VERSION,
"binary": esc(label),
"functions": funcs.len(),
"named": named,
"with_signal": usable,
"wrote": wrote,
"funcs": list,
}));
}
match mean_cov {
Some(c) => outln!(
"{}: {} functions, {} named, {} with enough signal ({:.0}% mean coverage)",
esc(label),
funcs.len(),
named,
usable,
c * 100.0
),
None => outln!(
"{}: {} functions, {} named, {} with enough signal",
esc(label),
funcs.len(),
named,
usable
),
}
if let Some(path) = wrote {
outln!("wrote {} prints -> {}", funcs.len(), esc(path));
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
fn cmd_match(
a: &str,
b: &str,
limit: usize,
force_align: bool,
graph_weight: f64,
no_sandbox: bool,
fmt: Format,
) -> Result<()> {
if fmt == Format::R2 {
bail!("r2 output is only supported for query and triage");
}
let ia = load_index(a, no_sandbox)?;
let ib = load_index(b, no_sandbox)?;
let aligned = force_align || !both_named(&ia, &ib);
let rep = if aligned {
match_aligned(&ia, &ib, graph_weight)
} else {
match_by_name(&ia, &ib)
};
if fmt == Format::Json {
let changed: Vec<serde_json::Value> = rep
.changed
.iter()
.map(|c| {
serde_json::json!({
"name": esc(&c.name),
"similarity": c.similarity,
"entry_a": c.entry_a.map(|e| format!("{e:#x}")),
"entry_b": c.entry_b.map(|e| format!("{e:#x}")),
})
})
.collect();
return emit_json(&serde_json::json!({
"schema_version": OUTPUT_SCHEMA_VERSION,
"mode": if aligned { "aligned" } else { "name" },
"compared": rep.compared,
"unchanged": rep.same,
"changed": changed,
"low_signal": rep.low_signal,
"only_a": rep.only_a.iter().map(|s| esc(s)).collect::<Vec<_>>(),
"only_b": rep.only_b.iter().map(|s| esc(s)).collect::<Vec<_>>(),
}));
}
if aligned {
outln!(
"aligned {} function pairs by behavior + call graph (no symbol names needed)",
rep.compared
);
} else {
outln!("compared {} functions present in both", rep.compared);
}
outln!(" unchanged: {}", rep.same);
outln!(" changed: {}", rep.changed.len());
outln!(" low-signal: {} (too small to judge)", rep.low_signal);
outln!(" only in {}: {}", esc(a), rep.only_a.len());
outln!(" only in {}: {}", esc(b), rep.only_b.len());
if !rep.changed.is_empty() {
outln!("\nchanged behavior (lowest similarity first):");
for c in rep.changed.iter().take(row_cap(limit)) {
outln!(" {:>5.1}% {}", c.similarity * 100.0, esc(&c.name));
}
let shown = rep.changed.len().min(row_cap(limit));
if shown < rep.changed.len() {
outln!(
" ... {} more (raise --limit or --limit 0 for all)",
rep.changed.len() - shown
);
}
}
Ok(())
}
fn cmd_query(
target: &str,
corpus: &str,
threshold: f64,
limit: usize,
graph_weight: f64,
no_sandbox: bool,
fmt: Format,
) -> Result<()> {
let it = load_index(target, no_sandbox)?;
let db = load_corpus(corpus, no_sandbox)?;
let named = query_corpus(&it, &db, threshold, graph_weight)?;
if fmt == Format::Json {
let list: Vec<serde_json::Value> = named
.iter()
.map(|n| {
serde_json::json!({
"entry": format!("{:#x}", n.entry),
"guess": esc(&n.guess),
"from_binary": esc(&n.from_binary),
"similarity": n.similarity,
"graph": n.graph,
"score": n.score,
})
})
.collect();
return emit_json(&serde_json::json!({
"schema_version": OUTPUT_SCHEMA_VERSION,
"threshold": threshold,
"named": list,
}));
}
if fmt == Format::R2 {
outln!(
"# fnprint: {} function(s) named at >= {:.0}%",
named.len(),
threshold * 100.0
);
for n in &named {
outln!("afn {} {:#x}", r2_ident(&n.guess), n.entry);
}
return Ok(());
}
if named.is_empty() {
outln!(
"no matches above {:.0}% (try a lower --threshold)",
threshold * 100.0
);
return Ok(());
}
outln!("named {} function(s):", named.len());
outln!(" addr sim graph name");
for n in named.iter().take(row_cap(limit)) {
outln!(
" {:#010x} {:>5.1}% {:>4.0}% {} ({})",
n.entry,
n.similarity * 100.0,
n.graph * 100.0,
esc(&n.guess),
esc(&n.from_binary)
);
}
let shown = named.len().min(row_cap(limit));
if shown < named.len() {
outln!(
" ... {} more (raise --limit or --limit 0 for all)",
named.len() - shown
);
}
Ok(())
}
fn cmd_eval(a: &str, b: &str, graph_weight: f64, no_sandbox: bool, fmt: Format) -> Result<()> {
if fmt == Format::R2 {
bail!("r2 output is only supported for query and triage");
}
let ia = load_index(a, no_sandbox)?;
let ib = load_index(b, no_sandbox)?;
let r = eval(&ia, &ib, graph_weight);
if fmt == Format::Json {
return emit_json(&serde_json::json!({
"schema_version": OUTPUT_SCHEMA_VERSION,
"scored": r.scored,
"rank1_acc": r.rank1_acc(),
"recall_at_3": r.recall_at(3),
"recall_at_5": r.recall_at(5),
"mrr": r.mrr(),
"precision": r.precision(),
"recall": r.recall(),
"abstain_rate": r.abstain_rate(),
"tp": r.tp,
"fp": r.fp,
"abstained": r.abstained,
"align_acc": r.align_acc(),
"align_precision": r.align_precision(),
"align_recall": r.align_recall(),
"align_tp": r.align_tp,
"align_fp": r.align_fp,
"align_paired": r.align_paired,
}));
}
outln!("scored {} functions (had signal + a twin in B)", r.scored);
outln!(" rank-1 accuracy: {:.1}%", r.rank1_acc() * 100.0);
outln!(" recall@3: {:.1}%", r.recall_at(3) * 100.0);
outln!(" recall@5: {:.1}%", r.recall_at(5) * 100.0);
outln!(" MRR: {:.3}", r.mrr());
outln!(
" precision@same: {:.1}% ({} tp / {} fp)",
r.precision() * 100.0,
r.tp,
r.fp
);
outln!(" recall@same: {:.1}%", r.recall() * 100.0);
outln!(
" abstained: {:.1}% ({} of {} below same-threshold)",
r.abstain_rate() * 100.0,
r.abstained,
r.scored
);
outln!(
"aligned (1:1 + call graph, what query does; graph weight {:.2}):",
graph_weight
);
outln!(
" aligned-acc: {:.1}% ({} of {} paired right)",
r.align_acc() * 100.0,
r.align_correct,
r.scored
);
outln!(
" aligned-prec: {:.1}% ({} tp / {} fp at same-threshold)",
r.align_precision() * 100.0,
r.align_tp,
r.align_fp
);
outln!(" aligned-recall: {:.1}%", r.align_recall() * 100.0);
Ok(())
}
#[allow(clippy::too_many_arguments)]
fn cmd_triage(
target: &str,
vuln: &str,
patched: &str,
min_sim: f64,
margin: f64,
limit: usize,
no_sandbox: bool,
fmt: Format,
) -> Result<()> {
let it = load_index(target, no_sandbox)?;
let vdb = load_corpus(vuln, no_sandbox)?;
let pdb = load_corpus(patched, no_sandbox)?;
let hits = triage(&it, &vdb, &pdb, min_sim, margin)?;
let vulns: Vec<_> = hits
.iter()
.filter(|h| h.verdict == Verdict::Vulnerable)
.collect();
if fmt == Format::Json {
let verdict_str = |v: &Verdict| match v {
Verdict::Vulnerable => "vulnerable",
Verdict::Patched => "patched",
Verdict::Inconclusive => "inconclusive",
};
let list: Vec<serde_json::Value> = hits
.iter()
.map(|h| {
serde_json::json!({
"entry": format!("{:#x}", h.entry),
"verdict": verdict_str(&h.verdict),
"vuln_sim": h.vuln_sim,
"vuln_name": esc(&h.vuln_name),
"patched_sim": h.patched_sim,
"patched_name": esc(&h.patched_name),
"margin": h.margin(),
"coverage": h.coverage,
})
})
.collect();
return emit_json(&serde_json::json!({
"schema_version": OUTPUT_SCHEMA_VERSION,
"counts": {
"vulnerable": vulns.len(),
"patched": hits.iter().filter(|h| h.verdict == Verdict::Patched).count(),
"inconclusive": hits.iter().filter(|h| h.verdict == Verdict::Inconclusive).count(),
},
"hits": list,
}));
}
if fmt == Format::R2 {
outln!("# fnprint triage: {} vuln-leaning function(s)", vulns.len());
for h in &vulns {
outln!("afn {} {:#x}", r2_ident(&h.vuln_name), h.entry);
}
return Ok(());
}
outln!(
"{} functions triaged: {} look vulnerable, {} patched, {} inconclusive",
hits.len(),
vulns.len(),
hits.iter()
.filter(|h| h.verdict == Verdict::Patched)
.count(),
hits.iter()
.filter(|h| h.verdict == Verdict::Inconclusive)
.count(),
);
if vulns.is_empty() {
outln!("\nno function leans vulnerable above the margin. nothing to review.");
return Ok(());
}
outln!("\nreview queue (vuln-leaning, strongest first):");
outln!(" addr vuln% patched% margin cov matches");
let total_vulns = vulns.len();
for h in vulns.iter().take(row_cap(limit)) {
let cov_flag = if h.coverage <= fnprint_core::LOW_COVERAGE_ADVISORY {
"!"
} else {
" "
};
outln!(
" {:#010x} {:>5.1} {:>6.1} {:>+5.1} {:>3.0}%{} {} vs {}",
h.entry,
h.vuln_sim * 100.0,
h.patched_sim * 100.0,
h.margin() * 100.0,
h.coverage * 100.0,
cov_flag,
esc(&h.vuln_name),
esc(&h.patched_name),
);
}
let shown = total_vulns.min(row_cap(limit));
if shown < total_vulns {
outln!(
" ... {} more (raise --limit or --limit 0 for all)",
total_vulns - shown
);
}
outln!("\ncov = fraction of the target function body microexecution observed.");
outln!(
"a low cov (flagged !) means the verdict rests on little behavior; give it a second look."
);
Ok(())
}
fn cmd_dump(binary: &str, func: &str, no_sandbox: bool, fmt: Format) -> Result<()> {
if fmt == Format::R2 {
bail!("r2 output is only supported for query and triage");
}
let bytes = read_input(binary)?;
let lines = run_dump(&bytes, func, no_sandbox)?;
if fmt == Format::Json {
return emit_json(&serde_json::json!({
"schema_version": OUTPUT_SCHEMA_VERSION,
"func": esc(func),
"lines": lines.iter().map(|l| esc(l)).collect::<Vec<_>>(),
}));
}
for line in &lines {
outln!("{}", esc(line));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::{esc, r2_ident};
#[test]
fn r2_ident_strips_injection() {
assert_eq!(r2_ident("adler32_z"), "fnp.adler32_z");
assert_eq!(r2_ident("x; rm -rf /"), "fnp.x__rm__rf__");
let bad = r2_ident("a b;c`d`\ne");
assert!(!bad.contains(' '));
assert!(!bad.contains(';'));
assert!(!bad.contains('`'));
assert!(!bad.contains('\n'));
assert!(bad.starts_with("fnp."));
}
#[test]
fn shard_clamp_bounds_aggregate_memory() {
use super::{clamp_shards_for_input, MAX_AGGREGATE_INPUT};
assert_eq!(clamp_shards_for_input(8, 1024), 8);
assert_eq!(clamp_shards_for_input(8, 0), 8);
assert_eq!(clamp_shards_for_input(16, MAX_AGGREGATE_INPUT / 2), 2);
assert_eq!(clamp_shards_for_input(16, MAX_AGGREGATE_INPUT + 1), 1);
}
#[test]
fn read_input_rejects_non_regular_file() {
let e = super::read_input("/dev/zero")
.err()
.map(|e| e.to_string())
.unwrap_or_default();
assert!(e.contains("not a regular file"), "got: {e}");
}
#[test]
fn read_input_reads_a_regular_file() {
let mut p = std::env::temp_dir();
p.push(format!("fnprint_ri_{}.bin", std::process::id()));
std::fs::write(&p, b"\x7fELFhello").unwrap();
let got = super::read_input(p.to_str().unwrap());
let _ = std::fs::remove_file(&p);
assert_eq!(got.unwrap(), b"\x7fELFhello");
}
#[test]
fn stdout_result_maps_broken_pipe_to_quiet_marker() {
use std::io::{Error, ErrorKind};
assert!(super::stdout_result(Ok(())).is_ok());
let closed = super::stdout_result(Err(Error::from(ErrorKind::BrokenPipe))).unwrap_err();
assert!(closed.is::<super::StdoutClosed>());
let other =
super::stdout_result(Err(Error::from(ErrorKind::PermissionDenied))).unwrap_err();
assert!(!other.is::<super::StdoutClosed>());
assert!(other.to_string().contains("writing to stdout"));
}
#[test]
fn clap_command_is_valid() {
use clap::CommandFactory;
super::Cli::command().debug_assert();
}
#[test]
fn completions_generate_for_every_shell() {
use clap::CommandFactory;
use clap_complete::Shell;
for shell in [
Shell::Bash,
Shell::Zsh,
Shell::Fish,
Shell::PowerShell,
Shell::Elvish,
] {
let mut cmd = super::Cli::command();
let mut out = Vec::new();
clap_complete::generate(shell, &mut cmd, "fnprint", &mut out);
assert!(!out.is_empty(), "empty completion script for {shell:?}");
}
}
#[test]
fn esc_defangs_terminal_escapes() {
assert_eq!(esc("foo\x1b[31mbar"), "foo\\x1b[31mbar");
assert_eq!(esc("a\nb\r"), "a\\x0ab\\x0d");
assert_eq!(esc("adler32_z"), "adler32_z");
assert_eq!(esc("é"), "\\xc3\\xa9");
}
}