diff --git a/afl/src/engines/mod.rs b/afl/src/engines/mod.rs index aa3ca3892c..776984fd84 100644 --- a/afl/src/engines/mod.rs +++ b/afl/src/engines/mod.rs @@ -202,7 +202,7 @@ where engine.events_manager_mut().fire(Event::LoadInitial { sender_id: 0, phantom: PhantomData, - })?; + }, self, corpus)?; } engine.events_manager_mut().process(self, corpus)?; Ok(()) @@ -328,7 +328,7 @@ where sender_id: 0, new_execs: 1, phantom: PhantomData, - })?; // TODO self.new_execs}); + }, state, corpus)?; // TODO self.new_execs}); } } } diff --git a/afl/src/events/mod.rs b/afl/src/events/mod.rs index 94e9e57955..5351962819 100644 --- a/afl/src/events/mod.rs +++ b/afl/src/events/mod.rs @@ -1,18 +1,18 @@ -#[cfg(feature = "std")] -pub mod llmp; +//#[cfg(feature = "std")] +//pub mod llmp; use alloc::string::String; use core::marker::PhantomData; use serde::{Deserialize, Serialize}; -#[cfg(feature = "std")] -pub mod llmp_translated; // TODO: Abstract away. -#[cfg(feature = "std")] -pub mod shmem_translated; +//#[cfg(feature = "std")] +//pub mod llmp_translated; // TODO: Abstract away. +//#[cfg(feature = "std")] +//pub mod shmem_translated; -#[cfg(feature = "std")] -pub use crate::events::llmp::LLMPEventManager; +//#[cfg(feature = "std")] +//pub use crate::events::llmp::LLMPEventManager; #[cfg(feature = "std")] use std::io::Write; @@ -22,6 +22,7 @@ use crate::engines::State; use crate::executors::Executor; use crate::inputs::Input; use crate::utils::Rand; +use crate::serde_anymap::{Ptr, PtrMut}; use crate::AflError; /// Indicate if an event worked or not enum BrokerEventResult { @@ -67,7 +68,7 @@ where /// Events sent around in the library #[derive(Serialize, Deserialize)] -pub enum Event +pub enum Event<'a, C, E, I, R> where C: Corpus, E: Executor, @@ -84,6 +85,11 @@ where testcase: Testcase, phantom: PhantomData<(C, E, I, R)>, }, + NewTestcase2 { + sender_id: u64, + input: Ptr<'a, I>, + observers: PtrMut<'a, crate::observers::observer_serde::NamedSerdeAnyMap> + }, UpdateStats { sender_id: u64, new_execs: usize, @@ -111,7 +117,7 @@ where //Custom {sender_id: u64, custom_event: CE}, } -impl Event +impl<'a, C, E, I, R> Event<'a, C, E, I, R> where C: Corpus, E: Executor, @@ -130,6 +136,11 @@ where testcase: _, phantom: _, } => "New Testcase", + Event::NewTestcase2 { + sender_id: _, + input: _, + observers: _, + } => "New Testcase 2", Event::UpdateStats { sender_id: _, new_execs: _, @@ -171,6 +182,11 @@ where testcase: _, phantom: _, } => Ok(BrokerEventResult::Forward), + Event::NewTestcase2 { + sender_id: _, + input: _, + observers: _, + } => Ok(BrokerEventResult::Forward), Event::UpdateStats { sender_id: _, new_execs: _, @@ -246,7 +262,7 @@ where fn enabled(&self) -> bool; /// Fire an Event - fn fire(&mut self, event: Event) -> Result<(), AflError>; + fn fire<'a>(&mut self, event: Event<'a, C, E, I, R>, state: &mut State, corpus: &mut C) -> Result<(), AflError>; /// Lookup for incoming events and process them. /// Return the number of processes events or an error @@ -274,15 +290,12 @@ where #[cfg(feature = "std")] pub struct LoggerEventManager where - C: Corpus, - I: Input, - E: Executor, - R: Rand, W: Write, //CE: CustomEvent, { - events: Vec>, writer: W, + count: usize, + phantom: PhantomData<(C, E, I, R)> } #[cfg(feature = "std")] @@ -299,28 +312,19 @@ where true } - fn fire(&mut self, event: Event) -> Result<(), AflError> { - self.events.push(event); + fn fire<'a>(&mut self, event: Event<'a, C, E, I, R>, state: &mut State, corpus: &mut C) -> Result<(), AflError> { + match event.handle_in_broker(state, corpus)? { + BrokerEventResult::Forward => event.handle_in_client(state, corpus)?, + // Ignore broker-only events + BrokerEventResult::Handled => (), + } Ok(()) } fn process(&mut self, state: &mut State, corpus: &mut C) -> Result { - // TODO: iterators - let mut handled = vec![]; - for x in self.events.iter() { - handled.push(x.handle_in_broker(state, corpus)?); - } - let count = self.events.len(); - while self.events.len() > 0 { - let event = self.events.pop().unwrap(); - match handled.pop().unwrap() { - BrokerEventResult::Forward => event.handle_in_client(state, corpus)?, - // Ignore broker-only events - BrokerEventResult::Handled => (), - } - } - dbg!("Handled {} events", count); - Ok(count) + let c = self.count; + self.count = 0; + Ok(c) } } @@ -336,8 +340,9 @@ where { pub fn new(writer: W) -> Self { Self { - events: vec![], writer: writer, + count: 0, + phantom: PhantomData } } } diff --git a/afl/src/executors/inmemory.rs b/afl/src/executors/inmemory.rs index bcb2f2fbfd..122af61352 100644 --- a/afl/src/executors/inmemory.rs +++ b/afl/src/executors/inmemory.rs @@ -4,7 +4,7 @@ use core::ptr; use crate::executors::{Executor, ExitKind}; use crate::inputs::Input; use crate::observers::Observer; -use crate::serde_anymap::NamedSerdeAnyMap; +use crate::observers::observer_serde::NamedSerdeAnyMap; use crate::AflError; type HarnessFunction = fn(&dyn Executor, &[u8]) -> ExitKind; @@ -14,7 +14,7 @@ where I: Input, { harness: HarnessFunction, - observers: NamedSerdeAnyMap, + observers: NamedSerdeAnyMap, } static mut CURRENT_INMEMORY_EXECUTOR_PTR: *const c_void = ptr::null(); @@ -35,11 +35,11 @@ where Ok(ret) } - fn observers(&self) -> &NamedSerdeAnyMap { + fn observers(&self) -> &NamedSerdeAnyMap { &self.observers } - fn observers_mut(&mut self) -> &mut NamedSerdeAnyMap { + fn observers_mut(&mut self) -> &mut NamedSerdeAnyMap { &mut self.observers } } diff --git a/afl/src/executors/mod.rs b/afl/src/executors/mod.rs index 698584b1f1..b42afdb2aa 100644 --- a/afl/src/executors/mod.rs +++ b/afl/src/executors/mod.rs @@ -2,7 +2,7 @@ pub mod inmemory; use crate::inputs::Input; use crate::observers::Observer; -use crate::serde_anymap::NamedSerdeAnyMap; +use crate::observers::observer_serde::NamedSerdeAnyMap; use crate::AflError; pub enum ExitKind { @@ -20,10 +20,10 @@ where fn run_target(&mut self, input: &I) -> Result; /// Get the linked observers - fn observers(&self) -> &NamedSerdeAnyMap; + fn observers(&self) -> &NamedSerdeAnyMap; /// Get the linked observers - fn observers_mut(&mut self) -> &mut NamedSerdeAnyMap; + fn observers_mut(&mut self) -> &mut NamedSerdeAnyMap; /// Add a linked observer fn add_observer(&mut self, observer: Box) { diff --git a/afl/src/feedbacks/mod.rs b/afl/src/feedbacks/mod.rs index 450bfa1b9f..b6f6e7aa3b 100644 --- a/afl/src/feedbacks/mod.rs +++ b/afl/src/feedbacks/mod.rs @@ -5,7 +5,7 @@ use num::Integer; use crate::corpus::{Testcase, TestcaseMetadata}; use crate::inputs::Input; use crate::observers::{MapObserver, Observer}; -use crate::serde_anymap::NamedSerdeAnyMap; +use crate::observers::observer_serde::NamedSerdeAnyMap; use crate::AflError; pub trait Feedback @@ -16,7 +16,7 @@ where fn is_interesting( &mut self, input: &I, - observers: &NamedSerdeAnyMap, + observers: &NamedSerdeAnyMap, ) -> Result; /// Append to the testcase the generated metadata in case of a new corpus item @@ -105,7 +105,7 @@ where fn is_interesting( &mut self, _input: &I, - observers: &NamedSerdeAnyMap, + observers: &NamedSerdeAnyMap, ) -> Result { let mut interesting = 0; // TODO optimize diff --git a/afl/src/metamap.rs b/afl/src/metamap.rs index dbc33500ae..5eaba2ccba 100644 --- a/afl/src/metamap.rs +++ b/afl/src/metamap.rs @@ -5,6 +5,7 @@ use core::slice::{Iter, IterMut}; use hashbrown::hash_map::{Keys, Values, ValuesMut}; use hashbrown::HashMap; +#[derive(Default)] pub struct MetaMap { map: HashMap>, } diff --git a/afl/src/observers/mod.rs b/afl/src/observers/mod.rs index 1ed544438f..791635f77f 100644 --- a/afl/src/observers/mod.rs +++ b/afl/src/observers/mod.rs @@ -24,6 +24,34 @@ pub trait Observer: SerdeAny + 'static { fn name(&self) -> &'static str; } +crate::create_serde_registry_for_trait!(observer_serde, crate::observers::Observer); + +#[derive(Serialize, Deserialize)] +pub struct NopObserver { + +} +impl Observer for NopObserver { + fn name(&self) -> &'static str { + "aa" + } + + fn reset(&mut self) -> Result<(), AflError> { Ok(()) } +} +impl SerdeAny for NopObserver +{ + fn as_any(&self) -> &dyn Any { + self + } + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } +} +impl NopObserver { + pub fn new() -> Self { + Self {} + } +} + /// A MapObserver observes the static map, as oftentimes used for afl-like coverage information pub trait MapObserver where diff --git a/afl/src/serde_anymap.rs b/afl/src/serde_anymap.rs index 7e89b43228..f55671107f 100644 --- a/afl/src/serde_anymap.rs +++ b/afl/src/serde_anymap.rs @@ -4,8 +4,6 @@ use serde::{Deserialize, Serialize}; use alloc::boxed::Box; use core::any::{Any, TypeId}; use core::default::Default; -use core::fmt; -use core::slice::{Iter, IterMut}; use hashbrown::hash_map::{Keys, Values, ValuesMut}; use crate::AflError; @@ -23,11 +21,8 @@ pub trait SerdeAny: Any + erased_serde::Serialize { fn as_any_mut(&mut self) -> &mut dyn Any; } -type DeserializeCallback = - fn(&mut dyn erased_serde::Deserializer) -> Result, erased_serde::Error>; - -struct Wrap<'a, T: ?Sized>(pub &'a T); -impl<'a, T> Serialize for Wrap<'a, T> +pub struct Wrap<'a, T: ?Sized>(pub &'a T); +impl<'a, T> serde::Serialize for Wrap<'a, T> where T: ?Sized + erased_serde::Serialize + 'a, { @@ -39,27 +34,15 @@ where } } -impl<'a> serde::Serialize for dyn SerdeAny + 'a { - fn serialize(&self, se: S) -> Result - where - S: serde::Serializer, - { - use serde::ser::SerializeSeq; +pub type DeserializeCallback = + fn(&mut dyn erased_serde::Deserializer) -> Result, erased_serde::Error>; - let id = unpack_type_id(self.type_id()); - let mut seq = se.serialize_seq(Some(2))?; - seq.serialize_element(&id)?; - seq.serialize_element(&Wrap(self))?; - seq.end() - } +pub struct DeserializeCallbackSeed where B: ?Sized { + pub cb: DeserializeCallback, } -struct DeserializeCallbackSeed { - pub cb: DeserializeCallback, -} - -impl<'de> serde::de::DeserializeSeed<'de> for DeserializeCallbackSeed { - type Value = Box; +impl<'de, B> serde::de::DeserializeSeed<'de> for DeserializeCallbackSeed where B: ?Sized { + type Value = Box; fn deserialize(self, deserializer: D) -> Result where @@ -70,316 +53,361 @@ impl<'de> serde::de::DeserializeSeed<'de> for DeserializeCallbackSeed { } } -struct BoxAnyVisitor {} -impl<'de> serde::de::Visitor<'de> for BoxAnyVisitor { - type Value = Box; +#[macro_export] +macro_rules! create_serde_registry_for_trait { + ($mod_name:ident, $trait_name:path) => { + + pub mod $mod_name { + + use alloc::boxed::Box; + use core::fmt; + use core::any::{Any, TypeId}; + use serde::{Deserialize, Serialize}; + + use hashbrown::HashMap; + use hashbrown::hash_map::{Keys, Values, ValuesMut}; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("Expecting a serialized SerdeAny trait object (Box)") - } + use $crate::AflError; + use $crate::serde_anymap::{DeserializeCallback, DeserializeCallbackSeed, pack_type_id, unpack_type_id}; + + pub struct BoxDynVisitor {} + impl<'de> serde::de::Visitor<'de> for BoxDynVisitor { + type Value = Box; - fn visit_seq(self, mut visitor: V) -> Result, V::Error> - where - V: serde::de::SeqAccess<'de>, - { - let id: u64 = visitor.next_element()?.unwrap(); - let cb = unsafe { - *REGISTRY - .deserializers - .as_ref() - .unwrap() - .get(&id) - .expect("Cannot deserialize an unregistered SerdeAny") - }; - let seed = DeserializeCallbackSeed { cb: cb }; - let obj: Box = visitor.next_element_seed(seed)?.unwrap(); - Ok(obj) - } -} + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("Expecting a serialized trait object") + } -impl<'de> Deserialize<'de> for Box { - fn deserialize(deserializer: D) -> Result, D::Error> - where - D: serde::Deserializer<'de>, - { - deserializer.deserialize_seq(BoxAnyVisitor {}) - } -} + fn visit_seq(self, mut visitor: V) -> Result + where + V: serde::de::SeqAccess<'de>, + { + /*let id: u64 = visitor.next_element()?.unwrap(); + let cb = unsafe { + *REGISTRY + .deserializers + .as_ref() + .unwrap() + .get(&id) + .expect("Cannot deserialize an unregistered type") + };*/ + //let seed = DeserializeCallbackSeed:: { cb: cb }; + //let obj: Self::Value = visitor.next_element_seed(seed)?.unwrap(); + let obj = Box::new(crate::observers::NopObserver::new()); + Ok(obj) + } + } + + struct Registry { + deserializers: Option>>, + finalized: bool, + } -pub struct Registry { - deserializers: Option>, - finalized: bool, -} + impl Registry { + pub fn register(&mut self) + where + T: $trait_name + serde::Serialize + serde::de::DeserializeOwned, + { + if self.finalized { + panic!("Registry is already finalized!"); + } + + let deserializers = self.deserializers.get_or_insert_with(|| HashMap::default()); + deserializers.insert(unpack_type_id(TypeId::of::()), |de| { + Ok(Box::new(erased_serde::deserialize::(de)?)) + }); + } + + pub fn finalize(&mut self) { + self.finalized = true; + } + } + + static mut REGISTRY: Registry = Registry { + deserializers: None, + finalized: false, + }; + + pub struct RegistryBuilder {} + impl RegistryBuilder { + pub fn register() + where + T: $trait_name + serde::Serialize + serde::de::DeserializeOwned, + { + unsafe { + REGISTRY.register::(); + } + } + + pub fn finalize() { + unsafe { + REGISTRY.finalize(); + } + } + } + + #[derive(Serialize, Deserialize)] + pub struct SerdeAnyMap + { + map: HashMap>, + } + + impl SerdeAnyMap + { + pub fn get(&self) -> Option<&T> + where + T: $trait_name, + { + self.map + .get(&unpack_type_id(TypeId::of::())) + .map(|x| x.as_ref().as_any().downcast_ref::().unwrap()) + } + + pub fn get_mut(&mut self) -> Option<&mut T> + where + T: $trait_name, + { + self.map + .get_mut(&unpack_type_id(TypeId::of::())) + .map(|x| x.as_mut().as_any_mut().downcast_mut::().unwrap()) + } + + pub fn insert(&mut self, t: T) + where + T: $trait_name, + { + self.map + .insert(unpack_type_id(TypeId::of::()), Box::new(t)); + } + + pub fn len(&self) -> usize { + self.map.len() + } + + pub fn contains(&self) -> bool + where + T: $trait_name, + { + self.map.contains_key(&unpack_type_id(TypeId::of::())) + } + + pub fn new() -> Self { + SerdeAnyMap { + map: HashMap::default(), + } + } + } + + impl Default for SerdeAnyMap + { + fn default() -> Self { + Self::new() + } + } + + + #[derive(Serialize, Deserialize)] + pub struct NamedSerdeAnyMap + { + map: HashMap>>, + } + + impl NamedSerdeAnyMap + { + pub fn get(&self, name: &'static str) -> Option<&T> + where + T: Any, + { + match self.map.get(&unpack_type_id(TypeId::of::())) { + None => None, + Some(h) => h + .get(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())) + .map(|x| x.as_any().downcast_ref::().unwrap()), + } + } + + pub fn by_typeid(&self, name: &'static str, typeid: &TypeId) -> Option<&dyn $trait_name> { + match self.map.get(&unpack_type_id(*typeid)) { + None => None, + Some(h) => h + .get(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())) + .map(|x| x.as_ref()), + } + } + + pub fn get_mut(&mut self, name: &'static str) -> Option<&mut T> + where + T: Any, + { + match self.map.get_mut(&unpack_type_id(TypeId::of::())) { + None => None, + Some(h) => h + .get_mut(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())) + .map(|x| x.as_any_mut().downcast_mut::().unwrap()), + } + } + + pub fn by_typeid_mut(&mut self, name: &'static str, typeid: &TypeId) -> Option<&mut dyn $trait_name> { + match self.map.get_mut(&unpack_type_id(*typeid)) { + None => None, + Some(h) => h + .get_mut(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())) + .map(|x| x.as_mut()), + } + } + + pub fn get_all(&self) -> Option>, fn(&Box) -> &T>> + where + T: Any, + { + match self.map.get(&unpack_type_id(TypeId::of::())) { + None => None, + Some(h) => Some(h.values().map(|x| x.as_any().downcast_ref::().unwrap())), + } + } + + pub fn all_by_typeid( + &self, + typeid: &TypeId, + ) -> Option>, fn(&Box) -> &dyn $trait_name>> { + match self.map.get(&unpack_type_id(*typeid)) { + None => None, + Some(h) => Some(h.values().map(|x| x.as_ref())), + } + } + + pub fn get_all_mut( + &mut self, + ) -> Option>, fn(&mut Box) -> &mut T>> + where + T: Any, + { + match self.map.get_mut(&unpack_type_id(TypeId::of::())) { + None => None, + Some(h) => Some( + h.values_mut() + .map(|x| x.as_any_mut().downcast_mut::().unwrap()), + ), + } + } + + pub fn all_by_typeid_mut( + &mut self, + typeid: &TypeId, + ) -> Option>, fn(&mut Box) -> &mut dyn $trait_name>> { + match self.map.get_mut(&unpack_type_id(*typeid)) { + None => None, + Some(h) => Some(h.values_mut().map(|x| x.as_mut())), + } + } + + pub fn all_typeids( + &self, + ) -> core::iter::Map>>, fn(&u64) -> TypeId> { + self.map.keys().map(|x| pack_type_id(*x)) + } + + pub fn for_each( + &self, + func: fn(&TypeId, &Box) -> Result<(), AflError>, + ) -> Result<(), AflError> { + for (id, h) in self.map.iter() { + for x in h.values() { + func(&pack_type_id(*id), x)?; + } + } + Ok(()) + } + + pub fn for_each_mut( + &mut self, + func: fn(&TypeId, &mut Box) -> Result<(), AflError>, + ) -> Result<(), AflError> { + for (id, h) in self.map.iter_mut() { + for x in h.values_mut() { + func(&pack_type_id(*id), x)?; + } + } + Ok(()) + } + + pub fn insert(&mut self, val: Box, name: &'static str) { + let id = unpack_type_id(val.type_id()); + if !self.map.contains_key(&id) { + self.map.insert(id, HashMap::default()); + } + self.map + .get_mut(&id) + .unwrap() + .insert(xxhash_rust::xxh3::xxh3_64(name.as_bytes()), val); + } + + pub fn len(&self) -> usize { + self.map.len() + } + + pub fn contains_type(&self) -> bool + where + T: Any, + { + self.map.contains_key(&unpack_type_id(TypeId::of::())) + } + + pub fn contains(&self, name: &'static str) -> bool + where + T: Any, + { + match self.map.get(&unpack_type_id(TypeId::of::())) { + None => false, + Some(h) => h.contains_key(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())), + } + } + + pub fn new() -> Self { + Self { + map: HashMap::default(), + } + } + } + + impl Default for NamedSerdeAnyMap + { + fn default() -> Self { + Self::new() + } + } -impl Registry { - pub fn register(&mut self) - where - T: SerdeAny + Serialize + serde::de::DeserializeOwned, - { - if self.finalized { - panic!("Global Registry of SerdeAny types is already finalized!"); } + + impl<'a> serde::Serialize for dyn $trait_name { + fn serialize(&self, se: S) -> Result + where + S: serde::Serializer, + { + use serde::ser::SerializeSeq; - let deserializers = self.deserializers.get_or_insert_with(|| HashMap::default()); - deserializers.insert(unpack_type_id(TypeId::of::()), |de| { - Ok(Box::new(erased_serde::deserialize::(de)?)) - }); - } - - pub fn finalize(&mut self) { - self.finalized = true; - } -} - -static mut REGISTRY: Registry = Registry { - deserializers: None, - finalized: false, -}; - -pub struct RegistryBuilder {} -impl RegistryBuilder { - pub fn register() - where - T: SerdeAny + Serialize + serde::de::DeserializeOwned, - { - unsafe { - REGISTRY.register::(); - } - } - - pub fn finalize() { - unsafe { - REGISTRY.finalize(); - } - } -} - -#[derive(Default, Serialize, Deserialize)] -pub struct SerdeAnyMap { - map: HashMap>, -} - -impl SerdeAnyMap { - pub fn get(&self) -> Option<&T> - where - T: SerdeAny, - { - self.map - .get(&unpack_type_id(TypeId::of::())) - .map(|x| x.as_ref().as_any().downcast_ref::().unwrap()) - } - - pub fn get_mut(&mut self) -> Option<&mut T> - where - T: SerdeAny, - { - self.map - .get_mut(&unpack_type_id(TypeId::of::())) - .map(|x| x.as_mut().as_any_mut().downcast_mut::().unwrap()) - } - - pub fn insert(&mut self, t: T) - where - T: SerdeAny, - { - self.map - .insert(unpack_type_id(TypeId::of::()), Box::new(t)); - } - - pub fn len(&self) -> usize { - self.map.len() - } - - pub fn contains(&self) -> bool - where - T: SerdeAny, - { - self.map.contains_key(&unpack_type_id(TypeId::of::())) - } - - pub fn new() -> Self { - SerdeAnyMap { - map: HashMap::default(), - } - } -} - -#[derive(Serialize, Deserialize)] -pub struct NamedSerdeAnyMap -where - B: ?Sized + SerdeAny, -{ - map: HashMap>>, -} - -impl NamedSerdeAnyMap -where - B: ?Sized + SerdeAny, -{ - pub fn get(&self, name: &'static str) -> Option<&T> - where - T: Any, - { - match self.map.get(&unpack_type_id(TypeId::of::())) { - None => None, - Some(h) => h - .get(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())) - .map(|x| x.as_any().downcast_ref::().unwrap()), - } - } - - pub fn by_typeid(&self, name: &'static str, typeid: &TypeId) -> Option<&B> { - match self.map.get(&unpack_type_id(*typeid)) { - None => None, - Some(h) => h - .get(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())) - .map(|x| x.as_ref()), - } - } - - pub fn get_mut(&mut self, name: &'static str) -> Option<&mut T> - where - T: Any, - { - match self.map.get_mut(&unpack_type_id(TypeId::of::())) { - None => None, - Some(h) => h - .get_mut(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())) - .map(|x| x.as_any_mut().downcast_mut::().unwrap()), - } - } - - pub fn by_typeid_mut(&mut self, name: &'static str, typeid: &TypeId) -> Option<&mut B> { - match self.map.get_mut(&unpack_type_id(*typeid)) { - None => None, - Some(h) => h - .get_mut(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())) - .map(|x| x.as_mut()), - } - } - - pub fn get_all(&self) -> Option>, fn(&Box) -> &T>> - where - T: Any, - { - match self.map.get(&unpack_type_id(TypeId::of::())) { - None => None, - Some(h) => Some(h.values().map(|x| x.as_any().downcast_ref::().unwrap())), - } - } - - pub fn all_by_typeid( - &self, - typeid: &TypeId, - ) -> Option>, fn(&Box) -> &B>> { - match self.map.get(&unpack_type_id(*typeid)) { - None => None, - Some(h) => Some(h.values().map(|x| x.as_ref())), - } - } - - pub fn get_all_mut( - &mut self, - ) -> Option>, fn(&mut Box) -> &mut T>> - where - T: Any, - { - match self.map.get_mut(&unpack_type_id(TypeId::of::())) { - None => None, - Some(h) => Some( - h.values_mut() - .map(|x| x.as_any_mut().downcast_mut::().unwrap()), - ), - } - } - - pub fn all_by_typeid_mut( - &mut self, - typeid: &TypeId, - ) -> Option>, fn(&mut Box) -> &mut B>> { - match self.map.get_mut(&unpack_type_id(*typeid)) { - None => None, - Some(h) => Some(h.values_mut().map(|x| x.as_mut())), - } - } - - pub fn all_typeids( - &self, - ) -> core::iter::Map>>, fn(&u64) -> TypeId> { - self.map.keys().map(|x| pack_type_id(*x)) - } - - pub fn for_each( - &self, - func: fn(&TypeId, &Box) -> Result<(), AflError>, - ) -> Result<(), AflError> { - for (id, h) in self.map.iter() { - for x in h.values() { - func(&pack_type_id(*id), x)?; + let id = $crate::serde_anymap::unpack_type_id(self.type_id()); + let mut seq = se.serialize_seq(Some(2))?; + seq.serialize_element(&id)?; + seq.serialize_element(&$crate::serde_anymap::Wrap(self))?; + seq.end() } } - Ok(()) - } - - pub fn for_each_mut( - &mut self, - func: fn(&TypeId, &mut Box) -> Result<(), AflError>, - ) -> Result<(), AflError> { - for (id, h) in self.map.iter_mut() { - for x in h.values_mut() { - func(&pack_type_id(*id), x)?; + + impl<'de> serde::Deserialize<'de> for Box { + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: serde::Deserializer<'de>, + { + deserializer.deserialize_seq($mod_name::BoxDynVisitor {}) } } - Ok(()) - } - - pub fn insert(&mut self, val: Box, name: &'static str) { - let id = unpack_type_id(val.type_id()); - if !self.map.contains_key(&id) { - self.map.insert(id, HashMap::default()); - } - self.map - .get_mut(&id) - .unwrap() - .insert(xxhash_rust::xxh3::xxh3_64(name.as_bytes()), val); - } - - pub fn len(&self) -> usize { - self.map.len() - } - - pub fn contains_type(&self) -> bool - where - T: Any, - { - self.map.contains_key(&unpack_type_id(TypeId::of::())) - } - - pub fn contains(&self, name: &'static str) -> bool - where - T: Any, - { - match self.map.get(&unpack_type_id(TypeId::of::())) { - None => false, - Some(h) => h.contains_key(&xxhash_rust::xxh3::xxh3_64(name.as_bytes())), - } - } - - pub fn new() -> Self { - Self { - map: HashMap::default(), - } - } + + }; } -impl Default for NamedSerdeAnyMap -where - B: ?Sized + SerdeAny, -{ - fn default() -> Self { - Self::new() - } -} +create_serde_registry_for_trait!(serdeany_serde, crate::serde_anymap::SerdeAny); +pub use serdeany_serde::*; #[derive(Serialize)] pub enum Ptr<'a, T: 'a + ?Sized> { diff --git a/afl/src/stages/mod.rs b/afl/src/stages/mod.rs index fa11984e90..49fd2d81fa 100644 --- a/afl/src/stages/mod.rs +++ b/afl/src/stages/mod.rs @@ -22,7 +22,7 @@ where &mut self, rand: &mut R, state: &mut State, - corpus: &C, + corpus: &mut C, engine: &mut Engine, input: &I, ) -> Result<(), AflError>; diff --git a/afl/src/stages/mutational.rs b/afl/src/stages/mutational.rs index 6f5ee2ba3b..5a1e41d13b 100644 --- a/afl/src/stages/mutational.rs +++ b/afl/src/stages/mutational.rs @@ -38,7 +38,7 @@ where &mut self, rand: &mut R, state: &mut State, - corpus: &C, + corpus: &mut C, engine: &mut Engine, input: &I, ) -> Result<(), AflError> { @@ -60,7 +60,7 @@ where sender_id: 0, testcase: testcase, phantom: PhantomData, - })?; + }, state, corpus)?; } } Ok(()) @@ -114,7 +114,7 @@ where &mut self, rand: &mut R, state: &mut State, - corpus: &C, + corpus: &mut C, engine: &mut Engine, input: &I, ) -> Result<(), AflError> {