Skip to content

Commit

Permalink
fix: clippy + correctly apply AFL_* environment variables again (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xricksanchez authored Apr 25, 2024
1 parent df56891 commit ce002ad
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 134 deletions.
36 changes: 16 additions & 20 deletions src/afl_cmd_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl AflCmd {
}

/// Sets the environment variables for the AFL command
pub fn set_env(&mut self, env: Vec<String>) {
self.env = env;
pub fn extend_env(&mut self, env: Vec<String>) {
self.env.extend(env);
}

/// Sets the input directory for AFL
Expand Down Expand Up @@ -206,10 +206,10 @@ impl AFLCmdGenerator {
.ok()?;
if output.status.success() {
let afl_path = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !afl_path.is_empty() {
Some(PathBuf::from(afl_path))
} else {
if afl_path.is_empty() {
None
} else {
Some(PathBuf::from(afl_path))
}
} else {
None
Expand Down Expand Up @@ -250,7 +250,7 @@ impl AFLCmdGenerator {
.filter(|env| !cmd.env.iter().any(|e| e.starts_with(*env)))
.cloned()
.collect::<Vec<String>>();
cmd.set_env(to_apply);
cmd.extend_env(to_apply);
}

let cmd_strings = cmds.into_iter().map(|cmd| cmd.assemble()).collect();
Expand All @@ -268,11 +268,10 @@ impl AFLCmdGenerator {

let free_mem = get_free_mem();
for c in &mut configs {
if ((self.runners * 500 + 4096) as u64) < free_mem {
c.testcache_size = 500;
}
if ((self.runners * 250 + 4096) as u64) < free_mem {
c.testcache_size = 250;
match self.runners * 500 + 4096 {
x if u64::from(x) < free_mem => c.testcache_size = 500,
x if u64::from(x) < free_mem => c.testcache_size = 250,
_ => {}
}
}

Expand All @@ -288,7 +287,7 @@ impl AFLCmdGenerator {
.iter()
.map(|config| {
let mut cmd = AflCmd::new(afl_binary.clone(), target_binary.clone());
cmd.set_env(config.generate_afl_env_cmd());
cmd.extend_env(config.generate_afl_env_cmd());
if let Some(raw_afl_flags) = &self.raw_afl_flags {
cmd.set_misc_afl_flags(
raw_afl_flags
Expand Down Expand Up @@ -443,14 +442,11 @@ impl AFLCmdGenerator {
/// Applies CMPCOV instrumentation to AFL commands
fn apply_cmpcov(&self, cmds: &mut [AflCmd], rng: &mut impl Rng) {
if let Some(cmpcov_binary) = self.harness.cmpcov_binary.as_ref() {
let max_cmpcov_instances = if self.runners >= 16 {
3
} else if self.runners >= 8 {
2
} else if self.runners >= 3 {
1
} else {
0
let max_cmpcov_instances = match self.runners {
0..=2 => 0,
3..=7 => 1,
8..=15 => 2,
_ => 3,
};

let mut cmpcov_indices = (1..cmds.len())
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl RunArgs {
"screen" => Some(SessionRunner::Screen),
_ => None,
})
.unwrap_or(self.session_runner.clone());
.unwrap_or_else(|| self.session_runner.clone());
Self {
gen_args,
dry_run: self.dry_run || config.session.dry_run.unwrap_or(false),
Expand Down
92 changes: 38 additions & 54 deletions src/data_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,20 @@ impl DataFetcher {

fn process_corpus_count(value: &str, session_data: &mut CampaignData) {
let corpus_count = value.parse::<usize>().unwrap_or(0);
if corpus_count > session_data.corpus.count_max {
session_data.corpus.count_max = corpus_count;
} else if corpus_count < session_data.corpus.count_min || session_data.corpus.count_min == 0
{
session_data.corpus.count_min = corpus_count;
if corpus_count > session_data.corpus.max {
session_data.corpus.max = corpus_count;
} else if corpus_count < session_data.corpus.min || session_data.corpus.min == 0 {
session_data.corpus.min = corpus_count;
}
session_data.corpus.count_cum += corpus_count;
session_data.corpus.cum += corpus_count;
}

fn process_bitmap_cvg(value: &str, session_data: &mut CampaignData) {
let cvg = value.trim_end_matches('%').parse::<f64>().unwrap_or(0.0);
if cvg < session_data.coverage.bitmap_min || session_data.coverage.bitmap_min == 0.0 {
session_data.coverage.bitmap_min = cvg;
} else if cvg > session_data.coverage.bitmap_max {
session_data.coverage.bitmap_max = cvg;
if cvg < session_data.coverage.min || session_data.coverage.min == 0.0 {
session_data.coverage.min = cvg;
} else if cvg > session_data.coverage.max {
session_data.coverage.max = cvg;
}
}

Expand All @@ -174,24 +173,22 @@ impl DataFetcher {

fn process_saved_crashes(value: &str, session_data: &mut CampaignData) {
let saved_crashes = value.parse::<usize>().unwrap_or(0);
if saved_crashes > session_data.crashes.saved_max {
session_data.crashes.saved_max = saved_crashes;
} else if saved_crashes < session_data.crashes.saved_min
|| session_data.crashes.saved_min == 0
{
session_data.crashes.saved_min = saved_crashes;
if saved_crashes > session_data.crashes.max {
session_data.crashes.max = saved_crashes;
} else if saved_crashes < session_data.crashes.min || session_data.crashes.min == 0 {
session_data.crashes.min = saved_crashes;
}
session_data.crashes.saved_cum += saved_crashes;
session_data.crashes.cum += saved_crashes;
}

fn process_saved_hangs(value: &str, session_data: &mut CampaignData) {
let saved_hangs = value.parse::<usize>().unwrap_or(0);
if saved_hangs > session_data.hangs.saved_max {
session_data.hangs.saved_max = saved_hangs;
} else if saved_hangs < session_data.hangs.saved_min || session_data.hangs.saved_min == 0 {
session_data.hangs.saved_min = saved_hangs;
if saved_hangs > session_data.hangs.max {
session_data.hangs.max = saved_hangs;
} else if saved_hangs < session_data.hangs.min || session_data.hangs.min == 0 {
session_data.hangs.min = saved_hangs;
}
session_data.hangs.saved_cum += saved_hangs;
session_data.hangs.cum += saved_hangs;
}

fn process_last_find(value: &str, session_data: &mut CampaignData) {
Expand Down Expand Up @@ -225,43 +222,30 @@ impl DataFetcher {
}

fn calculate_averages(session_data: &mut CampaignData, fuzzers_alive: usize) {
session_data.executions.ps_avg = if fuzzers_alive > 0 {
let is_fuzzers_alive = fuzzers_alive > 0;

session_data.executions.ps_avg = if is_fuzzers_alive {
session_data.executions.ps_cum / fuzzers_alive as f64
} else {
0.0
};
session_data.executions.avg = if fuzzers_alive > 0 {
session_data.executions.cum / fuzzers_alive
} else {
0
};
session_data.pending.favorites_avg = if fuzzers_alive > 0 {
session_data.pending.favorites_cum / fuzzers_alive
} else {
0
};
session_data.pending.total_avg = if fuzzers_alive > 0 {
session_data.pending.total_cum / fuzzers_alive
} else {
0
};
session_data.corpus.count_avg = if fuzzers_alive > 0 {
session_data.corpus.count_cum / fuzzers_alive
} else {
0
};
session_data.crashes.saved_avg = if fuzzers_alive > 0 {
session_data.crashes.saved_cum / fuzzers_alive
} else {
0
};
session_data.hangs.saved_avg = if fuzzers_alive > 0 {
session_data.hangs.saved_cum / fuzzers_alive
} else {
0

let cumulative_avg = |cum: usize| {
if is_fuzzers_alive {
cum / fuzzers_alive
} else {
0
}
};
session_data.coverage.bitmap_avg =
(session_data.coverage.bitmap_min + session_data.coverage.bitmap_max) / 2.0;

session_data.executions.avg = cumulative_avg(session_data.executions.cum);
session_data.pending.favorites_avg = cumulative_avg(session_data.pending.favorites_cum);
session_data.pending.total_avg = cumulative_avg(session_data.pending.total_cum);
session_data.corpus.avg = cumulative_avg(session_data.corpus.cum);
session_data.crashes.avg = cumulative_avg(session_data.crashes.cum);
session_data.hangs.avg = cumulative_avg(session_data.hangs.cum);

session_data.coverage.avg = (session_data.coverage.min + session_data.coverage.max) / 2.0;
session_data.stability.avg =
(session_data.stability.min + session_data.stability.max) / 2.0;
session_data.cycles.done_avg =
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn handle_run_command(run_args: &cli::RunArgs) -> Result<()> {
let sname = generate_session_name(&merged_args, &target_args);
let srunner: Box<dyn Runner> = match &merged_args.session_runner {
SessionRunner::Screen => Box::new(Screen::new(&sname, &afl_cmds)),
_ => Box::new(Tmux::new(&sname, &afl_cmds)),
SessionRunner::Tmux => Box::new(Tmux::new(&sname, &afl_cmds)),
};

if merged_args.tui {
Expand Down
16 changes: 7 additions & 9 deletions src/runners/runner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use std::fs::{self};
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
Expand All @@ -23,20 +23,20 @@ pub trait Runner {
fn run_with_tui(&self, out_dir: &Path) -> Result<()>;
}

pub struct RunnerImpl {
pub struct Session {
pub name: String,
pub commands: Vec<String>,
pub log_file: PathBuf,
pub runner_name: &'static str,
}

impl RunnerImpl {
impl Session {
pub fn new(session_name: &str, commands: &[String], runner_name: &'static str) -> Self {
let commands = commands
.iter()
.map(|c| c.replace('"', "\\\""))
.collect::<Vec<String>>();
let log_file = PathBuf::from(format!("/tmp/{}_{session_name}.log", runner_name));
let log_file = PathBuf::from(format!("/tmp/{runner_name}_{session_name}.log"));
if log_file.exists() {
std::fs::remove_file(&log_file).unwrap();
}
Expand All @@ -62,7 +62,7 @@ impl RunnerImpl {
.map_err(|e| anyhow::anyhow!("Error creating bash script: {}", e))
}

pub fn run_command(&self, mut cmd: Command) -> Result<()> {
pub fn run_command(mut cmd: Command) -> Result<()> {
let mut child = cmd.spawn()?;
let _ = child.wait()?;
Ok(())
Expand Down Expand Up @@ -116,7 +116,7 @@ impl RunnerImpl {
.arg(templ)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
self.run_command(cmd)?;
Self::run_command(cmd)?;
} else {
return Err(anyhow::anyhow!("Error creating bash script"));
}
Expand All @@ -137,9 +137,7 @@ impl RunnerImpl {
}

pub fn run_detached(&self) -> Result<()> {
if let Err(e) = self.run() {
return Err(e);
}
self.run()?;
println!("Session {} started in detached mode", self.name);
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions src/runners/screen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::runners::runner::{Runner, RunnerImpl};
use crate::runners::runner::{Runner, Session};
use anyhow::Result;
use std::{
path::Path,
Expand All @@ -8,13 +8,13 @@ use std::{
pub const SCREEN_TEMPLATE: &str = include_str!("../templates/screen.txt");

pub struct Screen {
inner: RunnerImpl,
inner: Session,
}

impl Runner for Screen {
fn new(session_name: &str, commands: &[String]) -> Self {
Self {
inner: RunnerImpl::new(session_name, commands, "screen"),
inner: Session::new(session_name, commands, "screen"),
}
}

Expand All @@ -30,7 +30,7 @@ impl Runner for Screen {
.arg("quit")
.stdout(Stdio::null())
.stderr(Stdio::null());
self.inner.run_command(cmd)
Session::run_command(cmd)
}

fn attach(&self) -> Result<()> {
Expand All @@ -39,7 +39,7 @@ impl Runner for Screen {
.arg(&self.inner.name)
.stdout(Stdio::null())
.stderr(Stdio::null());
self.inner.run_command(cmd)
Session::run_command(cmd)
}

fn run(&self) -> Result<()> {
Expand Down
10 changes: 5 additions & 5 deletions src/runners/tmux.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::runners::runner::{Runner, RunnerImpl};
use crate::runners::runner::{Runner, Session};
use anyhow::Result;
use std::{
path::Path,
Expand All @@ -8,13 +8,13 @@ use std::{
pub const TMUX_TEMPLATE: &str = include_str!("../templates/tmux.txt");

pub struct Tmux {
inner: RunnerImpl,
inner: Session,
}

impl Runner for Tmux {
fn new(session_name: &str, commands: &[String]) -> Self {
Self {
inner: RunnerImpl::new(session_name, commands, "tmux"),
inner: Session::new(session_name, commands, "tmux"),
}
}

Expand All @@ -29,7 +29,7 @@ impl Runner for Tmux {
.arg(&self.inner.name)
.stdout(Stdio::null())
.stderr(Stdio::null());
self.inner.run_command(cmd)
Session::run_command(cmd)
}

fn attach(&self) -> Result<()> {
Expand All @@ -39,7 +39,7 @@ impl Runner for Tmux {
cmd.args(["attach-session", "-t", &target])
.stdout(Stdio::null())
.stderr(Stdio::null());
self.inner.run_command(cmd)
Session::run_command(cmd)
}

fn run(&self) -> Result<()> {
Expand Down
Loading

0 comments on commit ce002ad

Please sign in to comment.