afl_exec_sec feature, disabled by default (#555)

This commit is contained in:
Andrea Fioraldi 2022-02-23 16:06:22 +01:00 committed by GitHub
parent 05b10ad56d
commit 04c8e96923
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 5 deletions

View File

@ -24,6 +24,7 @@ tui_monitor = ["tui", "crossterm"] # enable TuiMonitor with crossterm
cli = ["clap"] # expose bolts::cli cli = ["clap"] # expose bolts::cli
qemu_cli = ["cli"] qemu_cli = ["cli"]
frida_cli = ["cli"] frida_cli = ["cli"]
afl_exec_sec = [] # calculate exec/sec like AFL
# features hiding dependencies licensed under GPL # features hiding dependencies licensed under GPL
gpl = [] gpl = []

View File

@ -18,6 +18,7 @@ use serde::{Deserialize, Serialize};
use crate::bolts::{current_time, format_duration_hms}; use crate::bolts::{current_time, format_duration_hms};
#[cfg(feature = "afl_exec_sec")]
const CLIENT_STATS_TIME_WINDOW_SECS: u64 = 5; // 5 seconds const CLIENT_STATS_TIME_WINDOW_SECS: u64 = 5; // 5 seconds
/// User-defined stat types /// User-defined stat types
@ -62,11 +63,13 @@ pub struct ClientStats {
/// The size of the objectives corpus for this client /// The size of the objectives corpus for this client
pub objective_size: u64, pub objective_size: u64,
/// The last reported executions for this client /// The last reported executions for this client
#[cfg(feature = "afl_exec_sec")]
pub last_window_executions: u64, pub last_window_executions: u64,
/// The last executions per sec
#[cfg(feature = "afl_exec_sec")]
pub last_execs_per_sec: f64,
/// The last time we got this information /// The last time we got this information
pub last_window_time: Duration, pub last_window_time: Duration,
/// The last executions per sec
pub last_execs_per_sec: f32,
/// User-defined monitor /// User-defined monitor
pub user_monitor: HashMap<String, UserStats>, pub user_monitor: HashMap<String, UserStats>,
/// Client performance statistics /// Client performance statistics
@ -76,6 +79,7 @@ pub struct ClientStats {
impl ClientStats { impl ClientStats {
/// We got a new information about executions for this client, insert them. /// We got a new information about executions for this client, insert them.
#[cfg(feature = "afl_exec_sec")]
pub fn update_executions(&mut self, executions: u64, cur_time: Duration) { pub fn update_executions(&mut self, executions: u64, cur_time: Duration) {
let diff = cur_time let diff = cur_time
.checked_sub(self.last_window_time) .checked_sub(self.last_window_time)
@ -88,6 +92,12 @@ impl ClientStats {
self.executions = executions; self.executions = executions;
} }
/// We got a new information about executions for this client, insert them.
#[cfg(not(feature = "afl_exec_sec"))]
pub fn update_executions(&mut self, executions: u64, _cur_time: Duration) {
self.executions = executions;
}
/// We got a new information about corpus size for this client, insert them. /// We got a new information about corpus size for this client, insert them.
pub fn update_corpus_size(&mut self, corpus_size: u64) { pub fn update_corpus_size(&mut self, corpus_size: u64) {
self.corpus_size = corpus_size; self.corpus_size = corpus_size;
@ -100,6 +110,7 @@ impl ClientStats {
/// Get the calculated executions per second for this client /// Get the calculated executions per second for this client
#[allow(clippy::cast_sign_loss, clippy::cast_precision_loss)] #[allow(clippy::cast_sign_loss, clippy::cast_precision_loss)]
#[cfg(feature = "afl_exec_sec")]
pub fn execs_per_sec(&mut self, cur_time: Duration) -> u64 { pub fn execs_per_sec(&mut self, cur_time: Duration) -> u64 {
if self.executions == 0 { if self.executions == 0 {
return 0; return 0;
@ -107,12 +118,12 @@ impl ClientStats {
let elapsed = cur_time let elapsed = cur_time
.checked_sub(self.last_window_time) .checked_sub(self.last_window_time)
.map_or(0, |d| d.as_secs()); .map_or(0.0, |d| d.as_secs_f64());
if elapsed == 0 { if elapsed as u64 == 0 {
return self.last_execs_per_sec as u64; return self.last_execs_per_sec as u64;
} }
let cur_avg = ((self.executions - self.last_window_executions) as f32) / (elapsed as f32); let cur_avg = ((self.executions - self.last_window_executions) as f64) / elapsed;
if self.last_window_executions == 0 { if self.last_window_executions == 0 {
self.last_execs_per_sec = cur_avg; self.last_execs_per_sec = cur_avg;
return self.last_execs_per_sec as u64; return self.last_execs_per_sec as u64;
@ -128,6 +139,24 @@ impl ClientStats {
self.last_execs_per_sec as u64 self.last_execs_per_sec as u64
} }
/// Get the calculated executions per second for this client
#[allow(clippy::cast_sign_loss, clippy::cast_precision_loss)]
#[cfg(not(feature = "afl_exec_sec"))]
pub fn execs_per_sec(&mut self, cur_time: Duration) -> u64 {
if self.executions == 0 {
return 0;
}
let elapsed = cur_time
.checked_sub(self.last_window_time)
.map_or(0.0, |d| d.as_secs_f64());
if elapsed as u64 == 0 {
return 0;
}
((self.executions as f64) / elapsed) as u64
}
/// Update the user-defined stat with name and value /// Update the user-defined stat with name and value
pub fn update_user_stats(&mut self, name: String, value: UserStats) { pub fn update_user_stats(&mut self, name: String, value: UserStats) {
self.user_monitor.insert(name, value); self.user_monitor.insert(name, value);