Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇
Go aicertificationcloud-engineeringcybercybersecuritycybersecurity-analystcybersecurity-certificationcybersecurity-certificationscybersecurity-educationcybersecurity-jobscybersecurity-portfoliocybersecurity-projectscybersecurity-toolsgrchackinglearningpentestingproject-repositoryprojetcspython
// ©AngelaMos | 2026
// stats.rs
pub const STATS_FILE_NAME: &str = "fuzzer_stats";
const FIELD_EXECS_DONE: &str = "execs_done";
const FIELD_EXECS_PER_SEC: &str = "execs_per_sec";
const FIELD_SAVED_CRASHES: &str = "saved_crashes";
const FIELD_BITMAP_CVG: &str = "bitmap_cvg";
const FIELD_EDGES_FOUND: &str = "edges_found";
const FIELD_TOTAL_EDGES: &str = "total_edges";
const FIELD_AFL_VERSION: &str = "afl_version";
const PERCENT_SUFFIX: char = '%';
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum StatsError {
#[error("fuzzer_stats is missing field {0}")]
MissingField(&'static str),
#[error("fuzzer_stats field {field} has unparsable value {value}")]
BadValue { field: &'static str, value: String },
}
#[derive(Debug, Clone, PartialEq)]
pub struct FuzzerStats {
pub execs_done: u64,
pub execs_per_sec: f64,
pub saved_crashes: u64,
pub bitmap_coverage_pct: f64,
pub edges_found: u64,
pub total_edges: u64,
pub afl_version: String,
}
impl FuzzerStats {
pub fn parse(text: &str) -> Result<FuzzerStats, StatsError> {
Ok(FuzzerStats {
execs_done: integer(text, FIELD_EXECS_DONE)?,
execs_per_sec: decimal(text, FIELD_EXECS_PER_SEC)?,
saved_crashes: integer(text, FIELD_SAVED_CRASHES)?,
bitmap_coverage_pct: decimal(text, FIELD_BITMAP_CVG)?,
edges_found: integer(text, FIELD_EDGES_FOUND)?,
total_edges: integer(text, FIELD_TOTAL_EDGES)?,
afl_version: field(text, FIELD_AFL_VERSION)?.to_owned(),
})
}
}
fn field<'a>(text: &'a str, name: &'static str) -> Result<&'a str, StatsError> {
for line in text.lines() {
let Some((key, value)) = line.split_once(':') else {
continue;
};
if key.trim() == name {
return Ok(value.trim());
}
}
Err(StatsError::MissingField(name))
}
fn integer(text: &str, name: &'static str) -> Result<u64, StatsError> {
let raw = field(text, name)?;
raw.parse().map_err(|_| StatsError::BadValue {
field: name,
value: raw.to_owned(),
})
}
fn decimal(text: &str, name: &'static str) -> Result<f64, StatsError> {
let raw = field(text, name)?;
let cleaned = raw.trim_end_matches(PERCENT_SUFFIX);
cleaned.parse().map_err(|_| StatsError::BadValue {
field: name,
value: raw.to_owned(),
})
}
#[cfg(test)]
mod tests {
use super::*;
const REAL_STATS: &str = "\
start_time : 1784982517
last_update : 1784983139
run_time : 0
fuzzer_pid : 2790
cycles_done : 0
execs_done : 17895
execs_per_sec : 31728.72
execs_ps_last_min : 0.00
corpus_count : 161
bitmap_cvg : 11.13%
saved_crashes : 1
saved_hangs : 0
edges_found : 1791
total_edges : 16089
auto_dict_entries : 15
afl_banner : /opt/magma-work/out/libpng/afl/lisdex_libpng_read
afl_version : ++5.02c
target_mode : persistent shmem_testcase deferred
";
#[test]
fn parses_real_afl_stats_file() {
let stats = FuzzerStats::parse(REAL_STATS).unwrap();
assert_eq!(stats.execs_done, 17895);
assert!((stats.execs_per_sec - 31728.72).abs() < f64::EPSILON);
assert_eq!(stats.saved_crashes, 1);
assert!((stats.bitmap_coverage_pct - 11.13).abs() < f64::EPSILON);
assert_eq!(stats.edges_found, 1791);
assert_eq!(stats.total_edges, 16089);
assert_eq!(stats.afl_version, "++5.02c");
}
#[test]
fn strips_percent_suffix_from_coverage() {
let stats = FuzzerStats::parse(REAL_STATS).unwrap();
assert!((stats.bitmap_coverage_pct - 11.13).abs() < f64::EPSILON);
}
#[test]
fn missing_field_is_an_error_not_a_silent_zero() {
let err = FuzzerStats::parse("execs_done : 5\n").unwrap_err();
assert!(matches!(err, StatsError::MissingField(_)));
}
#[test]
fn unparsable_value_is_an_error() {
let text = REAL_STATS.replace("execs_done : 17895", "execs_done : lots");
let err = FuzzerStats::parse(&text).unwrap_err();
assert_eq!(
err,
StatsError::BadValue {
field: FIELD_EXECS_DONE,
value: "lots".to_owned()
}
);
}
#[test]
fn tolerates_trailing_whitespace_in_values() {
let stats = FuzzerStats::parse(REAL_STATS).unwrap();
assert_eq!(stats.afl_version, "++5.02c");
}
}