synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-wire / src / bin / synapse-wire-fuzz.rs
| 1 | //! Run the in-repository coverage-guided fuzzer on the wire frame decoder. |
| 2 | //! |
| 3 | //! ```text |
| 4 | //! synapse-wire-fuzz --out DIR [--seconds N | --execs N] [--seed S] [--cap BYTES] |
| 5 | //! ``` |
| 6 | //! |
| 7 | //! Writes `DIR/spec.json` (budget, seed, cap, revision) before the first execution, then |
| 8 | //! `DIR/result.json` and `DIR/corpus.bin` (every input that reached new coverage, replayed |
| 9 | //! afterwards without instrumentation: each must be a canonical frame or a typed refusal). |
| 10 | //! Exits non-zero on any violation. |
| 11 | |
| 12 | use std::path::PathBuf; |
| 13 | use std::process::ExitCode; |
| 14 | use synapse_wire::frame::WireLimits; |
| 15 | use synapse_wire::fuzz::{ |
| 16 | corpus_to_bytes, malformed_seed_corpus, replay_corpus, run, seed_corpus, Budget, CountingAlloc, |
| 17 | FuzzConfig, |
| 18 | }; |
| 19 | |
| 20 | #[global_allocator] |
| 21 | static ALLOC: CountingAlloc = CountingAlloc; |
| 22 | |
| 23 | fn usage() -> ExitCode { |
| 24 | eprintln!( |
| 25 | "usage: synapse-wire-fuzz --out DIR [--seconds N | --execs N] [--seed S] [--cap BYTES]" |
| 26 | ); |
| 27 | ExitCode::from(2) |
| 28 | } |
| 29 | |
| 30 | fn git_rev() -> String { |
| 31 | std::process::Command::new("git") |
| 32 | .args(["rev-parse", "HEAD"]) |
| 33 | .output() |
| 34 | .ok() |
| 35 | .filter(|o| o.status.success()) |
| 36 | .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string()) |
| 37 | .unwrap_or_else(|| "unknown".into()) |
| 38 | } |
| 39 | |
| 40 | fn main() -> ExitCode { |
| 41 | let args: Vec<String> = std::env::args().skip(1).collect(); |
| 42 | let mut out: Option<PathBuf> = None; |
| 43 | let mut budget = Budget::Seconds(60.0); |
| 44 | let mut seed = 0x5EED_6100_u64; |
| 45 | let mut cap = 8192u32; |
| 46 | let mut i = 0; |
| 47 | while i < args.len() { |
| 48 | let val = args.get(i + 1); |
| 49 | match (args[i].as_str(), val) { |
| 50 | ("--out", Some(v)) => out = Some(PathBuf::from(v)), |
| 51 | ("--seconds", Some(v)) => match v.parse() { |
| 52 | Ok(s) => budget = Budget::Seconds(s), |
| 53 | Err(_) => return usage(), |
| 54 | }, |
| 55 | ("--execs", Some(v)) => match v.parse() { |
| 56 | Ok(n) => budget = Budget::Execs(n), |
| 57 | Err(_) => return usage(), |
| 58 | }, |
| 59 | ("--seed", Some(v)) => match v.parse() { |
| 60 | Ok(s) => seed = s, |
| 61 | Err(_) => return usage(), |
| 62 | }, |
| 63 | ("--cap", Some(v)) => match v.parse() { |
| 64 | Ok(c) => cap = c, |
| 65 | Err(_) => return usage(), |
| 66 | }, |
| 67 | _ => return usage(), |
| 68 | } |
| 69 | i += 2; |
| 70 | } |
| 71 | let Some(out) = out else { return usage() }; |
| 72 | if let Err(e) = std::fs::create_dir_all(&out) { |
| 73 | eprintln!("cannot create {}: {e}", out.display()); |
| 74 | return ExitCode::from(2); |
| 75 | } |
| 76 | let limits = WireLimits { |
| 77 | max_frame_body: cap, |
| 78 | }; |
| 79 | let mut seeds = seed_corpus(&limits); |
| 80 | let valid_seeds = seeds.len(); |
| 81 | seeds.extend(malformed_seed_corpus(&limits)); |
| 82 | let (budget_kind, budget_value) = match budget { |
| 83 | Budget::Execs(n) => ("execs", n as f64), |
| 84 | Budget::Seconds(s) => ("seconds", s), |
| 85 | }; |
| 86 | let spec = serde_json::json!({ |
| 87 | "tool": "synapse-wire-fuzz", |
| 88 | "kind": "in-repository coverage-guided mutation fuzzer (edge feedback from instrumented frame-decoder states)", |
| 89 | "target": "synapse_wire::frame::decode_frame, then synapse_store::codec::decode_envelope on Submit payloads", |
| 90 | "budget_kind": budget_kind, |
| 91 | "budget_value": budget_value, |
| 92 | "seed": seed, |
| 93 | "max_frame_body": cap, |
| 94 | "seed_inputs": seeds.len(), |
| 95 | "valid_seed_inputs": valid_seeds, |
| 96 | "git_rev": git_rev(), |
| 97 | "declared_before_run": true, |
| 98 | }); |
| 99 | let write = |name: &str, bytes: &[u8]| -> Result<(), ExitCode> { |
| 100 | std::fs::write(out.join(name), bytes).map_err(|e| { |
| 101 | eprintln!("cannot write {name}: {e}"); |
| 102 | ExitCode::from(2) |
| 103 | }) |
| 104 | }; |
| 105 | let spec_text = serde_json::to_string_pretty(&spec).expect("json") + "\n"; |
| 106 | if let Err(c) = write("spec.json", spec_text.as_bytes()) { |
| 107 | return c; |
| 108 | } |
| 109 | eprintln!("spec written; fuzzing ({budget_kind} {budget_value}, seed {seed}, cap {cap})"); |
| 110 | |
| 111 | let report = run( |
| 112 | FuzzConfig { |
| 113 | seed, |
| 114 | budget, |
| 115 | limits, |
| 116 | max_corpus: 50_000, |
| 117 | }, |
| 118 | &seeds, |
| 119 | ); |
| 120 | let replay = replay_corpus(&report.corpus, &limits); |
| 121 | let result = serde_json::json!({ |
| 122 | "execs": report.execs, |
| 123 | "elapsed_s": report.elapsed_s, |
| 124 | "execs_per_s": report.execs as f64 / report.elapsed_s.max(1e-9), |
| 125 | "edges": report.edges, |
| 126 | "instrumented_sites": report.sites_total, |
| 127 | "sites_unreached_lines": report.sites_unreached, |
| 128 | "corpus_entries": report.corpus.len(), |
| 129 | "corpus_found_at_execs": report.corpus_found_at, |
| 130 | "largest_input_bytes": report.largest_input, |
| 131 | "alloc_tracking_installed": report.alloc_tracking, |
| 132 | "peak_single_alloc_bytes": report.peak_alloc, |
| 133 | "peak_total_alloc_per_decode_bytes": report.peak_alloc_total, |
| 134 | "outcomes": report.outcomes, |
| 135 | "violation_total": report.violation_total(), |
| 136 | "violation_counts": report.violation_counts, |
| 137 | "violations": report.violations.iter().map(|v| serde_json::json!({ |
| 138 | "kind": v.kind, "detail": v.detail, "input_hex": hex(&v.input), |
| 139 | })).collect::<Vec<_>>(), |
| 140 | "corpus_replay": { |
| 141 | "entries": replay.entries, |
| 142 | "accepted_canonical": replay.accepted_canonical, |
| 143 | "refused_by_kind": replay.refused, |
| 144 | "violations": replay.violations, |
| 145 | "peak_single_alloc_bytes": replay.peak_alloc, |
| 146 | }, |
| 147 | }); |
| 148 | let result_text = serde_json::to_string_pretty(&result).expect("json") + "\n"; |
| 149 | if let Err(c) = write("result.json", result_text.as_bytes()) { |
| 150 | return c; |
| 151 | } |
| 152 | if let Err(c) = write("corpus.bin", &corpus_to_bytes(&report.corpus)) { |
| 153 | return c; |
| 154 | } |
| 155 | eprintln!( |
| 156 | "execs {} in {:.1} s, edges {}, sites {}/{}, corpus {}, violations {}, replay violations {}", |
| 157 | report.execs, |
| 158 | report.elapsed_s, |
| 159 | report.edges, |
| 160 | report.sites_total - report.sites_unreached.len(), |
| 161 | report.sites_total, |
| 162 | report.corpus.len(), |
| 163 | report.violation_total(), |
| 164 | replay.violations.len() |
| 165 | ); |
| 166 | if report.violation_total() == 0 && replay.violations.is_empty() && report.alloc_tracking { |
| 167 | ExitCode::SUCCESS |
| 168 | } else { |
| 169 | ExitCode::FAILURE |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | fn hex(b: &[u8]) -> String { |
| 174 | b.iter().map(|x| format!("{x:02x}")).collect() |
| 175 | } |