now serde of observers really works

This commit is contained in:
Andrea Fioraldi 2020-12-10 10:29:42 +01:00
parent 892fb2ed76
commit 238d3260bd
5 changed files with 41 additions and 35 deletions

View File

@ -11,6 +11,7 @@ criterion = "0.3" # Benchmarking
ahash = "0.6.1" # another hash ahash = "0.6.1" # another hash
fxhash = "0.2.1" # yet another hash fxhash = "0.2.1" # yet another hash
xxhash-rust = { version = "0.8.0", features = ["const_xxh3", "xxh3"] } # xxh3 hashing for rust xxhash-rust = { version = "0.8.0", features = ["const_xxh3", "xxh3"] } # xxh3 hashing for rust
serde_json = "1.0.60"
[[bench]] [[bench]]
name = "rand_speeds" name = "rand_speeds"

View File

@ -31,8 +31,8 @@ where
/// Add a linked observer /// Add a linked observer
fn add_observer(&mut self, observer: Box<dyn Observer>) { fn add_observer(&mut self, observer: Box<dyn Observer>) {
let name = observer.name(); let name = observer.name().clone();
self.observers_mut().insert(observer, name); self.observers_mut().insert(observer, &name);
} }
/// Reset the state of all the observes linked to this executor /// Reset the state of all the observes linked to this executor

View File

@ -35,7 +35,7 @@ where
} }
/// The name of this feedback /// The name of this feedback
fn name(&self) -> &'static str; fn name(&self) -> &String;
} }
/// A Reducer function is used to aggregate values for the novelty search /// A Reducer function is used to aggregate values for the novelty search
@ -96,7 +96,7 @@ where
/// Contains information about untouched entries /// Contains information about untouched entries
history_map: Vec<T>, history_map: Vec<T>,
/// Name identifier of this instance /// Name identifier of this instance
name: &'static str, name: String,
/// Phantom Data of Reducer /// Phantom Data of Reducer
phantom: PhantomData<(R, O)>, phantom: PhantomData<(R, O)>,
} }
@ -115,7 +115,7 @@ where
) -> Result<u32, AflError> { ) -> Result<u32, AflError> {
let mut interesting = 0; let mut interesting = 0;
// TODO optimize // TODO optimize
let observer = observers.get::<O>(self.name).unwrap(); let observer = observers.get::<O>(&self.name).unwrap();
let size = observer.map().len(); let size = observer.map().len();
for i in 0..size { for i in 0..size {
let history = self.history_map[i]; let history = self.history_map[i];
@ -130,8 +130,8 @@ where
Ok(interesting) Ok(interesting)
} }
fn name(&self) -> &'static str { fn name(&self) -> &String {
self.name &self.name
} }
} }
@ -145,7 +145,7 @@ where
pub fn new(name: &'static str, map_size: usize) -> Self { pub fn new(name: &'static str, map_size: usize) -> Self {
Self { Self {
history_map: vec![T::default(); map_size], history_map: vec![T::default(); map_size],
name: name, name: name.to_string(),
phantom: PhantomData, phantom: PhantomData,
} }
} }
@ -162,7 +162,7 @@ where
pub fn with_history_map(name: &'static str, history_map: Vec<T>) -> Self { pub fn with_history_map(name: &'static str, history_map: Vec<T>) -> Self {
Self { Self {
history_map: history_map, history_map: history_map,
name: name, name: name.to_string(),
phantom: PhantomData, phantom: PhantomData,
} }
} }

View File

@ -24,7 +24,7 @@ pub trait Observer: SerdeAny + 'static {
Ok(()) Ok(())
} }
fn name(&self) -> &'static str; fn name(&self) -> &String;
} }
crate::create_serde_registry_for_trait!(observer_serde, crate::observers::Observer); crate::create_serde_registry_for_trait!(observer_serde, crate::observers::Observer);
@ -63,14 +63,15 @@ where
/// The Map Observer retrieves the state of a map, /// The Map Observer retrieves the state of a map,
/// that will get updated by the target. /// that will get updated by the target.
/// A well-known example is the AFL-Style coverage map. /// A well-known example is the AFL-Style coverage map.
#[derive(Serialize)] #[derive(Serialize, Deserialize)]
#[serde(bound = "T: serde::de::DeserializeOwned")]
pub struct StdMapObserver<T> pub struct StdMapObserver<T>
where where
T: Default + Copy + 'static + serde::Serialize + serde::de::DeserializeOwned, T: Default + Copy + 'static + serde::Serialize + serde::de::DeserializeOwned,
{ {
map: ArrayMut<T>, map: ArrayMut<T>,
initial: T, initial: T,
name: &'static str, name: String,
} }
impl<T> Observer for StdMapObserver<T> impl<T> Observer for StdMapObserver<T>
@ -81,8 +82,8 @@ where
self.reset_map() self.reset_map()
} }
fn name(&self) -> &'static str { fn name(&self) -> &String {
self.name &self.name
} }
} }
@ -123,19 +124,6 @@ where
} }
} }
impl<'de, T> Deserialize<'de> for StdMapObserver<T>
where
T: Default + Copy + 'static + serde::Serialize + serde::de::DeserializeOwned,
{
fn deserialize<D>(de: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut erased = erased_serde::Deserializer::erase(de);
erased_serde::deserialize(&mut erased).map_err(serde::de::Error::custom)
}
}
impl<T> StdMapObserver<T> impl<T> StdMapObserver<T>
where where
T: Default + Copy + 'static + serde::Serialize + serde::de::DeserializeOwned, T: Default + Copy + 'static + serde::Serialize + serde::de::DeserializeOwned,
@ -147,7 +135,7 @@ where
Self { Self {
map: ArrayMut::Cptr((map.as_mut_ptr(), map.len())), map: ArrayMut::Cptr((map.as_mut_ptr(), map.len())),
initial: initial, initial: initial,
name: name, name: name.to_string(),
} }
} }
@ -159,8 +147,25 @@ where
StdMapObserver { StdMapObserver {
map: ArrayMut::Cptr((map_ptr, len)), map: ArrayMut::Cptr((map_ptr, len)),
initial: initial, initial: initial,
name: name, name: name.to_string(),
} }
} }
} }
} }
#[cfg(feature = "std")]
#[cfg(test)]
mod tests {
use crate::observers::{Observer, StdMapObserver};
static mut map: [u32; 4] = [0; 4];
#[test]
fn test_observer_serde() {
let o: Box<dyn Observer> = Box::new(StdMapObserver::<u32>::new("test", unsafe { &mut map }));
let s = serde_json::to_string(&o).unwrap();
println!("{}", s);
let d: Box<dyn Observer> = serde_json::from_str(&s).unwrap();
assert_eq!(d.name(), o.name());
}
}

View File

@ -211,7 +211,7 @@ macro_rules! create_serde_registry_for_trait {
} }
impl NamedSerdeAnyMap { impl NamedSerdeAnyMap {
pub fn get<T>(&self, name: &'static str) -> Option<&T> pub fn get<T>(&self, name: &String) -> Option<&T>
where where
T: Any, T: Any,
{ {
@ -225,7 +225,7 @@ macro_rules! create_serde_registry_for_trait {
pub fn by_typeid( pub fn by_typeid(
&self, &self,
name: &'static str, name: &String,
typeid: &TypeId, typeid: &TypeId,
) -> Option<&dyn $trait_name> { ) -> Option<&dyn $trait_name> {
match self.map.get(&unpack_type_id(*typeid)) { match self.map.get(&unpack_type_id(*typeid)) {
@ -236,7 +236,7 @@ macro_rules! create_serde_registry_for_trait {
} }
} }
pub fn get_mut<T>(&mut self, name: &'static str) -> Option<&mut T> pub fn get_mut<T>(&mut self, name: &String) -> Option<&mut T>
where where
T: Any, T: Any,
{ {
@ -250,7 +250,7 @@ macro_rules! create_serde_registry_for_trait {
pub fn by_typeid_mut( pub fn by_typeid_mut(
&mut self, &mut self,
name: &'static str, name: &String,
typeid: &TypeId, typeid: &TypeId,
) -> Option<&mut dyn $trait_name> { ) -> Option<&mut dyn $trait_name> {
match self.map.get_mut(&unpack_type_id(*typeid)) { match self.map.get_mut(&unpack_type_id(*typeid)) {
@ -363,7 +363,7 @@ macro_rules! create_serde_registry_for_trait {
Ok(()) Ok(())
} }
pub fn insert(&mut self, val: Box<dyn $trait_name>, name: &'static str) { pub fn insert(&mut self, val: Box<dyn $trait_name>, name: &String) {
let id = unpack_type_id(val.type_id()); let id = unpack_type_id(val.type_id());
if !self.map.contains_key(&id) { if !self.map.contains_key(&id) {
self.map.insert(id, HashMap::default()); self.map.insert(id, HashMap::default());
@ -385,7 +385,7 @@ macro_rules! create_serde_registry_for_trait {
self.map.contains_key(&unpack_type_id(TypeId::of::<T>())) self.map.contains_key(&unpack_type_id(TypeId::of::<T>()))
} }
pub fn contains<T>(&self, name: &'static str) -> bool pub fn contains<T>(&self, name: &String) -> bool
where where
T: Any, T: Any,
{ {