Dedup CentralizedEventManager's serialize_observers() (#2034)

* why

* clp
This commit is contained in:
Dongjia "toka" Zhang 2024-04-10 16:13:03 +02:00 committed by GitHub
parent 374f8735fa
commit 0d5c6219d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,8 +10,6 @@
use alloc::{boxed::Box, string::String, vec::Vec}; use alloc::{boxed::Box, string::String, vec::Vec};
use core::{marker::PhantomData, num::NonZeroUsize, time::Duration}; use core::{marker::PhantomData, num::NonZeroUsize, time::Duration};
#[cfg(feature = "adaptive_serialization")]
use libafl_bolts::current_time;
#[cfg(feature = "llmp_compression")] #[cfg(feature = "llmp_compression")]
use libafl_bolts::{ use libafl_bolts::{
compress::GzipCompressor, compress::GzipCompressor,
@ -332,7 +330,6 @@ where
self.inner.log(state, severity_level, message) self.inner.log(state, severity_level, message)
} }
#[cfg(not(feature = "adaptive_serialization"))]
fn serialize_observers<OT>(&mut self, observers: &OT) -> Result<Option<Vec<u8>>, Error> fn serialize_observers<OT>(&mut self, observers: &OT) -> Result<Option<Vec<u8>>, Error>
where where
OT: ObserversTuple<Self::State> + Serialize, OT: ObserversTuple<Self::State> + Serialize,
@ -340,47 +337,6 @@ where
self.inner.serialize_observers(observers) self.inner.serialize_observers(observers)
} }
#[cfg(feature = "adaptive_serialization")]
fn serialize_observers<OT>(&mut self, observers: &OT) -> Result<Option<Vec<u8>>, Error>
where
OT: ObserversTuple<Self::State> + Serialize,
{
const SERIALIZE_TIME_FACTOR: u32 = 4;
const SERIALIZE_PERCENTAGE_TRESHOLD: usize = 80;
let exec_time = observers
.match_name::<crate::observers::TimeObserver>("time")
.map(|o| o.last_runtime().unwrap_or(Duration::ZERO))
.unwrap();
let mut must_ser = (self.serialization_time() + self.deserialization_time())
* SERIALIZE_TIME_FACTOR
< exec_time;
if must_ser {
*self.should_serialize_cnt_mut() += 1;
}
if self.serializations_cnt() > 32 {
must_ser = (self.should_serialize_cnt() * 100 / self.serializations_cnt())
> SERIALIZE_PERCENTAGE_TRESHOLD;
}
if self.inner.serialization_time() == Duration::ZERO
|| must_ser
|| self.serializations_cnt().trailing_zeros() >= 8
{
let start = current_time();
let ser = postcard::to_allocvec(observers)?;
*self.inner.serialization_time_mut() = current_time() - start;
*self.serializations_cnt_mut() += 1;
Ok(Some(ser))
} else {
*self.serializations_cnt_mut() += 1;
Ok(None)
}
}
fn configuration(&self) -> EventConfig { fn configuration(&self) -> EventConfig {
self.inner.configuration() self.inner.configuration()
} }