From 42d213737da0c361d0c00c01f8370af605b0a18d Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Mon, 6 Sep 2021 10:25:32 +0200 Subject: [PATCH] Build id configuration in std (#286) * Build id configuration in std * uuid only on std --- libafl/Cargo.toml | 4 +++- libafl/src/events/mod.rs | 29 +++++++++++++++++++++++++++-- libafl/src/fuzzer/mod.rs | 16 ++++++++-------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/libafl/Cargo.toml b/libafl/Cargo.toml index bffabfab36..1acab24f96 100644 --- a/libafl/Cargo.toml +++ b/libafl/Cargo.toml @@ -38,7 +38,7 @@ harness = false [features] default = ["std", "anymap_debug", "derive", "llmp_compression"] -std = ["serde_json", "hostname", "core_affinity", "nix", "serde/std", "bincode", "wait-timeout", "regex"] # print, env, launcher ... support +std = ["serde_json", "hostname", "core_affinity", "nix", "serde/std", "bincode", "wait-timeout", "regex", "build_id", "uuid"] # print, env, launcher ... support anymap_debug = ["serde_json"] # uses serde_json to Debug the anymap trait. Disable for smaller footprint. derive = ["libafl_derive"] # provide derive(SerdeAny) macro. rand_trait = ["rand_core"] # If set, libafl's rand implementations will implement `rand::Rng` @@ -78,6 +78,8 @@ rand = { version = "0.8.1", optional = true } # rand_core = { version = "0.6.2", optional = true } # This dependency allows us to export our RomuRand as rand::Rng. nix = { version = "0.20.0", optional = true } regex = { version = "1", optional = true } +build_id = { version = "0.2.1", optional = true } +uuid = { version = "0.8.2", optional = true, features = ["serde"] } libm = "0.2.1" wait-timeout = { version = "0.2", optional = true } # used by CommandExecutor to wait for child process diff --git a/libafl/src/events/mod.rs b/libafl/src/events/mod.rs index df6080a82e..fadb0f6641 100644 --- a/libafl/src/events/mod.rs +++ b/libafl/src/events/mod.rs @@ -10,6 +10,9 @@ use alloc::{string::String, vec::Vec}; use core::{fmt, hash::Hasher, marker::PhantomData, time::Duration}; use serde::{Deserialize, Serialize}; +#[cfg(feature = "std")] +use uuid::Uuid; + use crate::{ executors::ExitKind, inputs::Input, observers::ObserversTuple, stats::UserStats, Error, }; @@ -67,10 +70,16 @@ pub enum BrokerEventResult { } /// Distinguish a fuzzer by its config -#[derive(Serialize, Deserialize, Debug, Clone, Copy)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)] pub enum EventConfig { AlwaysUnique, - FromName { name_hash: u64 }, + FromName { + name_hash: u64, + }, + #[cfg(feature = "std")] + BuildID { + id: Uuid, + }, } impl EventConfig { @@ -83,13 +92,29 @@ impl EventConfig { } } + #[cfg(feature = "std")] + #[must_use] + pub fn from_build_id() -> Self { + EventConfig::BuildID { + id: build_id::get(), + } + } + #[must_use] pub fn match_with(&self, other: &EventConfig) -> bool { match self { EventConfig::AlwaysUnique => false, EventConfig::FromName { name_hash: a } => match other { + #[cfg(not(feature = "std"))] EventConfig::AlwaysUnique => false, EventConfig::FromName { name_hash: b } => (a == b), + #[cfg(feature = "std")] + EventConfig::AlwaysUnique | EventConfig::BuildID { id: _ } => false, + }, + #[cfg(feature = "std")] + EventConfig::BuildID { id: a } => match other { + EventConfig::AlwaysUnique | EventConfig::FromName { name_hash: _ } => false, + EventConfig::BuildID { id: b } => (a == b), }, } } diff --git a/libafl/src/fuzzer/mod.rs b/libafl/src/fuzzer/mod.rs index 67ac7ddcdc..ca946b73d8 100644 --- a/libafl/src/fuzzer/mod.rs +++ b/libafl/src/fuzzer/mod.rs @@ -375,11 +375,10 @@ where if send_events { // TODO set None for fast targets - let observers_buf = match manager.configuration() { - EventConfig::AlwaysUnique => None, - EventConfig::FromName { .. } => { - Some(manager.serialize_observers(observers)?) - } + let observers_buf = if manager.configuration() == EventConfig::AlwaysUnique { + None + } else { + Some(manager.serialize_observers(observers)?) }; manager.fire( state, @@ -500,9 +499,10 @@ where let idx = state.corpus_mut().add(testcase)?; self.scheduler_mut().on_add(state, idx)?; - let observers_buf = match manager.configuration() { - EventConfig::AlwaysUnique => None, - EventConfig::FromName { .. } => Some(manager.serialize_observers(observers)?), + let observers_buf = if manager.configuration() == EventConfig::AlwaysUnique { + None + } else { + Some(manager.serialize_observers(observers)?) }; manager.fire( state,