From 87cd44b762e6cfd23047bd6918b8e094c2bde7d7 Mon Sep 17 00:00:00 2001 From: Dongjia Zhang Date: Fri, 7 Jan 2022 19:07:39 +0900 Subject: [PATCH] Use UserStats for Stability (#451) * stability:serstats * tostring * fix no_std * fix * fmt * clippy --- libafl/src/events/llmp.rs | 9 -------- libafl/src/events/mod.rs | 26 +++++++++++++--------- libafl/src/events/simple.rs | 8 ------- libafl/src/monitors/mod.rs | 44 ++++++++----------------------------- 4 files changed, 25 insertions(+), 62 deletions(-) diff --git a/libafl/src/events/llmp.rs b/libafl/src/events/llmp.rs index 5485a38c57..5321e46ded 100644 --- a/libafl/src/events/llmp.rs +++ b/libafl/src/events/llmp.rs @@ -170,15 +170,11 @@ where Event::UpdateExecStats { time, executions, - stability, phantom: _, } => { // TODO: The monitor buffer should be added on client add. let client = monitor.client_stats_mut_for(client_id); client.update_executions(*executions as u64, *time); - if let Some(stability) = stability { - client.update_stability(*stability); - } monitor.display(event.name().to_string(), client_id); Ok(BrokerEventResult::Handled) } @@ -196,7 +192,6 @@ where Event::UpdatePerfMonitor { time, executions, - stability, introspection_monitor, phantom: _, } => { @@ -208,10 +203,6 @@ where // Update the normal monitor for this client client.update_executions(*executions as u64, *time); - if let Some(stability) = stability { - client.update_stability(*stability); - } - // Update the performance monitor for this client client.update_introspection_monitor((**introspection_monitor).clone()); diff --git a/libafl/src/events/mod.rs b/libafl/src/events/mod.rs index be9bdf0361..b130ab76a9 100644 --- a/libafl/src/events/mod.rs +++ b/libafl/src/events/mod.rs @@ -6,7 +6,10 @@ pub mod llmp; pub use llmp::*; use ahash::AHasher; -use alloc::{string::String, vec::Vec}; +use alloc::{ + string::{String, ToString}, + vec::Vec, +}; use core::{fmt, hash::Hasher, marker::PhantomData, time::Duration}; use serde::{Deserialize, Serialize}; @@ -187,8 +190,6 @@ where UpdateExecStats { /// The time of generation of the [`Event`] time: Duration, - /// The stability of this fuzzer node, if known - stability: Option, /// The executions of this client executions: usize, /// [`PhantomData`] @@ -210,8 +211,6 @@ where time: Duration, /// The executions of this client executions: usize, - /// The stability of this fuzzer node, if known - stability: Option, /// Current performance statistics introspection_monitor: Box, @@ -256,7 +255,6 @@ where } => "Testcase", Event::UpdateExecStats { time: _, - stability: _, executions: _, phantom: _, } @@ -269,7 +267,6 @@ where Event::UpdatePerfMonitor { time: _, executions: _, - stability: _, introspection_monitor: _, phantom: _, } => "PerfMonitor", @@ -351,7 +348,6 @@ where S: HasExecutions + HasClientPerfMonitor, { let executions = *state.executions(); - let stability = *state.stability(); let cur = current_time(); // default to 0 here to avoid crashes on clock skew if cur.checked_sub(last_report_time).unwrap_or_default() > monitor_timeout { @@ -361,12 +357,23 @@ where state, Event::UpdateExecStats { executions, - stability, time: cur, phantom: PhantomData, }, )?; + if let Some(x) = state.stability() { + let stability = f64::from(*x); + self.fire( + state, + Event::UpdateUserStats { + name: "stability".to_string(), + value: UserStats::Float(stability), + phantom: PhantomData, + }, + )?; + } + // If performance monitor are requested, fire the `UpdatePerfMonitor` event #[cfg(feature = "introspection")] { @@ -381,7 +388,6 @@ where Event::UpdatePerfMonitor { executions, time: cur, - stability, introspection_monitor: Box::new(state.introspection_monitor().clone()), phantom: PhantomData, }, diff --git a/libafl/src/events/simple.rs b/libafl/src/events/simple.rs index 6c65301022..3fbd6983cb 100644 --- a/libafl/src/events/simple.rs +++ b/libafl/src/events/simple.rs @@ -150,16 +150,12 @@ where Event::UpdateExecStats { time, executions, - stability, phantom: _, } => { // TODO: The monitor buffer should be added on client add. let client = monitor.client_stats_mut_for(0); client.update_executions(*executions as u64, *time); - if let Some(stability) = stability { - client.update_stability(*stability); - } monitor.display(event.name().to_string(), 0); Ok(BrokerEventResult::Handled) @@ -179,7 +175,6 @@ where Event::UpdatePerfMonitor { time, executions, - stability, introspection_monitor, phantom: _, } => { @@ -187,9 +182,6 @@ where let client = &mut monitor.client_stats_mut()[0]; client.update_executions(*executions as u64, *time); client.update_introspection_monitor((**introspection_monitor).clone()); - if let Some(stability) = stability { - client.update_stability(*stability); - } monitor.display(event.name().to_string(), 0); Ok(BrokerEventResult::Handled) } diff --git a/libafl/src/monitors/mod.rs b/libafl/src/monitors/mod.rs index 043c2b791f..028fcbadd8 100644 --- a/libafl/src/monitors/mod.rs +++ b/libafl/src/monitors/mod.rs @@ -3,10 +3,11 @@ pub mod multi; pub use multi::MultiMonitor; -use alloc::{ - string::{String, ToString}, - vec::Vec, -}; +use alloc::{string::String, vec::Vec}; + +#[cfg(feature = "introspection")] +use alloc::string::ToString; + use core::{fmt, time::Duration}; use hashbrown::HashMap; use serde::{Deserialize, Serialize}; @@ -20,6 +21,8 @@ const CLIENT_STATS_TIME_WINDOW_SECS: u64 = 5; // 5 seconds pub enum UserStats { /// A numerical value Number(u64), + /// A Float value + Float(f64), /// A `String` String(String), /// A ratio of two values @@ -30,6 +33,7 @@ impl fmt::Display for UserStats { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { UserStats::Number(n) => write!(f, "{}", n), + UserStats::Float(n) => write!(f, "{}", n), UserStats::String(s) => write!(f, "{}", s), UserStats::Ratio(a, b) => { if *b == 0 { @@ -60,8 +64,6 @@ pub struct ClientStats { pub last_execs_per_sec: f32, /// User-defined monitor pub user_monitor: HashMap, - /// Stability, and if we ever received a stability value - pub stability: Option, /// Client performance statistics #[cfg(feature = "introspection")] pub introspection_monitor: ClientPerfMonitor, @@ -91,11 +93,6 @@ impl ClientStats { self.objective_size = objective_size; } - /// we got a new information about stability for this client, insert it. - pub fn update_stability(&mut self, stability: f32) { - self.stability = Some(stability); - } - /// Get the calculated executions per second for this client #[allow(clippy::cast_sign_loss, clippy::cast_precision_loss)] pub fn execs_per_sec(&mut self, cur_time: Duration) -> u64 { @@ -157,24 +154,6 @@ pub trait Monitor { /// show the monitor to the user fn display(&mut self, event_msg: String, sender_id: u32); - /// Show the Stabiliity - fn stability(&self) -> Option { - let mut stability_total = 0_f32; - let mut num = 0_usize; - for stat in self.client_stats() { - if let Some(stability) = stat.stability { - stability_total += stability; - num += 1; - } - } - if num == 0 { - None - } else { - #[allow(clippy::cast_precision_loss)] - Some(stability_total / num as f32) - } - } - /// Amount of elements in the corpus (combined for all children) fn corpus_size(&self) -> u64 { self.client_stats() @@ -295,7 +274,7 @@ where fn display(&mut self, event_msg: String, sender_id: u32) { let fmt = format!( - "[{} #{}] run time: {}, clients: {}, corpus: {}, objectives: {}, executions: {}{}, exec/sec: {}", + "[{} #{}] run time: {}, clients: {}, corpus: {}, objectives: {}, executions: {}, exec/sec: {}", event_msg, sender_id, format_duration_hms(&(current_time() - self.start_time)), @@ -303,11 +282,6 @@ where self.corpus_size(), self.objective_size(), self.total_execs(), - if let Some(stability) = self.stability() { - format!(", stability: {:.2}", stability) - } else { - "".to_string() - }, self.execs_per_sec() ); (self.print_fn)(fmt);