From 82b01df1dbaf22c66247b83803ddc51958f5bd3b Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Mon, 7 Dec 2020 15:30:17 +0100 Subject: [PATCH] cargo fmt --- afl/src/feedbacks/mod.rs | 2 +- afl/src/lib.rs | 4 +- afl/src/metamap.rs | 209 ++++++++++++++++++++++++++------------- afl/src/serde_anymap.rs | 107 +++++++++++++------- afl/src/utils.rs | 1 - 5 files changed, 215 insertions(+), 108 deletions(-) diff --git a/afl/src/feedbacks/mod.rs b/afl/src/feedbacks/mod.rs index 294b9eed85..c3cb878b80 100644 --- a/afl/src/feedbacks/mod.rs +++ b/afl/src/feedbacks/mod.rs @@ -1,8 +1,8 @@ use alloc::rc::Rc; use alloc::vec::Vec; +use core::any::Any; use core::cell::RefCell; use core::marker::PhantomData; -use core::any::Any; use num::Integer; use serde::{Deserialize, Serialize}; diff --git a/afl/src/lib.rs b/afl/src/lib.rs index 8b21f5220c..c19f048093 100644 --- a/afl/src/lib.rs +++ b/afl/src/lib.rs @@ -10,12 +10,12 @@ pub mod executors; pub mod feedbacks; pub mod generators; pub mod inputs; +pub mod metamap; pub mod mutators; pub mod observers; +pub mod serde_anymap; pub mod stages; pub mod utils; -pub mod serde_anymap; -pub mod metamap; use alloc::string::String; use core::fmt; diff --git a/afl/src/metamap.rs b/afl/src/metamap.rs index 1a04fdab00..0fbe44c1c3 100644 --- a/afl/src/metamap.rs +++ b/afl/src/metamap.rs @@ -1,64 +1,92 @@ -use hashbrown::HashMap; -use hashbrown::hash_map::{ValuesMut, Values}; use alloc::boxed::Box; use alloc::vec::Vec; +use core::any::{Any, TypeId}; use core::slice::{Iter, IterMut}; -use core::any::{TypeId, Any}; +use hashbrown::hash_map::{Values, ValuesMut}; +use hashbrown::HashMap; pub struct MetaMap { - map: HashMap> + map: HashMap>, } impl MetaMap { - pub fn get(&self) -> Option<&T> where T: Any { - self.map.get(&TypeId::of::()).map(|x| x.as_ref().downcast_ref::().unwrap()) + pub fn get(&self) -> Option<&T> + where + T: Any, + { + self.map + .get(&TypeId::of::()) + .map(|x| x.as_ref().downcast_ref::().unwrap()) } - - pub fn get_mut(&mut self) -> Option<&mut T> where T: Any { - self.map.get_mut(&TypeId::of::()).map(|x| x.as_mut().downcast_mut::().unwrap()) + + pub fn get_mut(&mut self) -> Option<&mut T> + where + T: Any, + { + self.map + .get_mut(&TypeId::of::()) + .map(|x| x.as_mut().downcast_mut::().unwrap()) } - - pub fn insert(&mut self, t: T) where T: Any { + + pub fn insert(&mut self, t: T) + where + T: Any, + { self.map.insert(TypeId::of::(), Box::new(t)); } - + pub fn len(&self) -> usize { self.map.len() } - - pub fn contains(&self) -> bool where T: Any { + + pub fn contains(&self) -> bool + where + T: Any, + { self.map.contains_key(&TypeId::of::()) } - + pub fn new() -> Self { - Self { map: HashMap::default() } + Self { + map: HashMap::default(), + } } } pub struct MultiMetaMap { - map: HashMap>> + map: HashMap>>, } impl MultiMetaMap { - pub fn get(&self) -> Option>, fn(&Box) -> &T>> where T: Any { + pub fn get(&self) -> Option>, fn(&Box) -> &T>> + where + T: Any, + { match self.map.get(&TypeId::of::()) { None => None, - Some(v) => { - Some(v.iter().map(|x| x.as_ref().downcast_ref::().unwrap())) - } + Some(v) => Some(v.iter().map(|x| x.as_ref().downcast_ref::().unwrap())), } } - pub fn get_mut(&mut self) -> Option>, fn(&mut Box) -> &mut T>> where T: Any { + pub fn get_mut( + &mut self, + ) -> Option>, fn(&mut Box) -> &mut T>> + where + T: Any, + { match self.map.get_mut(&TypeId::of::()) { None => None, - Some(v) => { - Some(v.iter_mut().map(|x| x.as_mut().downcast_mut::().unwrap())) - } + Some(v) => Some( + v.iter_mut() + .map(|x| x.as_mut().downcast_mut::().unwrap()), + ), } } - - pub fn insert(&mut self, t: T) where T: Any { + + pub fn insert(&mut self, t: T) + where + T: Any, + { let typeid = TypeId::of::(); if !self.map.contains_key(&typeid) { self.map.insert(typeid, vec![Box::new(t)]); @@ -66,104 +94,149 @@ impl MultiMetaMap { self.map.get_mut(&typeid).unwrap().push(Box::new(t)); } } - + pub fn len(&self) -> usize { self.map.len() } - - pub fn contains(&self) -> bool where T: Any { + + pub fn contains(&self) -> bool + where + T: Any, + { self.map.contains_key(&TypeId::of::()) } - + pub fn new() -> Self { - Self { map: HashMap::default() } + Self { + map: HashMap::default(), + } } } - pub struct MetaInstanceMap { - map: HashMap>> + map: HashMap>>, } impl MetaInstanceMap { - pub fn get(&self, instance: &U) -> Option<&T> where T: Any { + pub fn get(&self, instance: &U) -> Option<&T> + where + T: Any, + { self.get_ptr::(instance as *const _ as *const ()) } - pub fn get_ptr(&self, instance: *const ()) -> Option<&T> where T: Any { + pub fn get_ptr(&self, instance: *const ()) -> Option<&T> + where + T: Any, + { match self.map.get(&TypeId::of::()) { None => None, - Some(h) => { - h.get(&instance).map(|x| x.as_ref().downcast_ref::().unwrap()) - } + Some(h) => h + .get(&instance) + .map(|x| x.as_ref().downcast_ref::().unwrap()), } } - - pub fn get_mut(&mut self, instance: &U) -> Option<&mut T> where T: Any { + + pub fn get_mut(&mut self, instance: &U) -> Option<&mut T> + where + T: Any, + { self.get_mut_ptr::(instance as *const _ as *const ()) } - pub fn get_mut_ptr(&mut self, instance: *const ()) -> Option<&mut T> where T: Any { + pub fn get_mut_ptr(&mut self, instance: *const ()) -> Option<&mut T> + where + T: Any, + { match self.map.get_mut(&TypeId::of::()) { None => None, - Some(h) => { - h.get_mut(&instance).map(|x| x.as_mut().downcast_mut::().unwrap()) - } + Some(h) => h + .get_mut(&instance) + .map(|x| x.as_mut().downcast_mut::().unwrap()), } } - pub fn get_all(&self) -> Option>, fn(&Box) -> &T>> where T: Any { + pub fn get_all( + &self, + ) -> Option>, fn(&Box) -> &T>> + where + T: Any, + { match self.map.get(&TypeId::of::()) { None => None, - Some(h) => { - Some(h.values().map(|x| x.as_ref().downcast_ref::().unwrap())) - } + Some(h) => Some(h.values().map(|x| x.as_ref().downcast_ref::().unwrap())), } } - pub fn get_all_mut(&mut self) -> Option>, fn(&mut Box) -> &mut T>> where T: Any { + pub fn get_all_mut( + &mut self, + ) -> Option< + core::iter::Map>, fn(&mut Box) -> &mut T>, + > + where + T: Any, + { match self.map.get_mut(&TypeId::of::()) { None => None, - Some(h) => { - Some(h.values_mut().map(|x| x.as_mut().downcast_mut::().unwrap())) - } + Some(h) => Some( + h.values_mut() + .map(|x| x.as_mut().downcast_mut::().unwrap()), + ), } } - - pub fn insert(&mut self, t: T, instance: &U) where T: Any { + + pub fn insert(&mut self, t: T, instance: &U) + where + T: Any, + { self.insert_ptr(t, instance as *const _ as *const ()) } - pub fn insert_ptr(&mut self, t: T, instance: *const ()) where T: Any { + pub fn insert_ptr(&mut self, t: T, instance: *const ()) + where + T: Any, + { let typeid = TypeId::of::(); if !self.map.contains_key(&typeid) { self.map.insert(typeid, HashMap::default()); } - self.map.get_mut(&typeid).unwrap().insert(instance, Box::new(t)); + self.map + .get_mut(&typeid) + .unwrap() + .insert(instance, Box::new(t)); } - + pub fn len(&self) -> usize { self.map.len() } - pub fn contains_type(&self) -> bool where T: Any { + pub fn contains_type(&self) -> bool + where + T: Any, + { self.map.contains_key(&TypeId::of::()) } - - pub fn contains(&self, instance: &U) -> bool where T: Any { + + pub fn contains(&self, instance: &U) -> bool + where + T: Any, + { self.contains_ptr::(instance as *const _ as *const ()) } - pub fn contains_ptr(&self, instance: *const ()) -> bool where T: Any { + pub fn contains_ptr(&self, instance: *const ()) -> bool + where + T: Any, + { match self.map.get(&TypeId::of::()) { None => false, - Some(h) => { - h.contains_key(&instance) - } + Some(h) => h.contains_key(&instance), } } - + pub fn new() -> Self { - Self { map: HashMap::default() } + Self { + map: HashMap::default(), + } } -} \ No newline at end of file +} diff --git a/afl/src/serde_anymap.rs b/afl/src/serde_anymap.rs index 7d88f94cc6..72d8bf7fd6 100644 --- a/afl/src/serde_anymap.rs +++ b/afl/src/serde_anymap.rs @@ -1,29 +1,26 @@ use hashbrown::HashMap; -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; use alloc::boxed::Box; +use core::any::{Any, TypeId}; use core::default::Default; -use core::any::{TypeId, Any}; use core::fmt; pub fn pack_type_id(id: u64) -> TypeId { - unsafe { - *(&id as *const u64 as *const TypeId) - } + unsafe { *(&id as *const u64 as *const TypeId) } } pub fn unpack_type_id(id: TypeId) -> u64 { - unsafe { - *(&id as *const _ as *const u64) - } + unsafe { *(&id as *const _ as *const u64) } } -pub trait SerdeAny : Any + erased_serde::Serialize { +pub trait SerdeAny: Any + erased_serde::Serialize { fn as_any(&self) -> &dyn Any; fn as_any_mut(&mut self) -> &mut dyn Any; } -type DeserializeCallback = fn(&mut dyn erased_serde::Deserializer) -> Result, erased_serde::Error>; +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> @@ -40,7 +37,8 @@ where impl<'a> serde::Serialize for dyn SerdeAny + 'a { fn serialize(&self, se: S) -> Result - where S: serde::Serializer + where + S: serde::Serializer, { use serde::ser::SerializeSeq; @@ -56,7 +54,7 @@ struct DeserializeCallbackSeed { pub cb: DeserializeCallback, } -impl<'de> serde::de::DeserializeSeed<'de> for DeserializeCallbackSeed{ +impl<'de> serde::de::DeserializeSeed<'de> for DeserializeCallbackSeed { type Value = Box; fn deserialize(self, deserializer: D) -> Result @@ -75,13 +73,20 @@ impl<'de> serde::de::Visitor<'de> for BoxAnyVisitor { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("Expecting a serialized SerdeAny trait object (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 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) @@ -99,34 +104,45 @@ impl<'de> Deserialize<'de> for Box { pub struct Registry { deserializers: Option>, - finalized: bool + finalized: bool, } impl Registry { - pub fn register(&mut self) where T: SerdeAny + Serialize + serde::de::DeserializeOwned { + pub fn register(&mut self) + where + T: SerdeAny + Serialize + serde::de::DeserializeOwned, + { if self.finalized { panic!("Global Registry of SerdeAny types 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)?))); + 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 }; +static mut REGISTRY: Registry = Registry { + deserializers: None, + finalized: false, +}; pub struct RegistryBuilder {} impl RegistryBuilder { - pub fn register() where T: SerdeAny + Serialize + serde::de::DeserializeOwned { + pub fn register() + where + T: SerdeAny + Serialize + serde::de::DeserializeOwned, + { unsafe { REGISTRY.register::(); } } - + pub fn finalize() { unsafe { REGISTRY.finalize(); @@ -136,31 +152,50 @@ impl RegistryBuilder { #[derive(Default, Serialize, Deserialize)] pub struct SerdeAnyMap { - map: HashMap> + 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(&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 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 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 { + + 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() } + SerdeAnyMap { + map: HashMap::default(), + } } } diff --git a/afl/src/utils.rs b/afl/src/utils.rs index 9ee7ffb559..db7eb3b127 100644 --- a/afl/src/utils.rs +++ b/afl/src/utils.rs @@ -339,5 +339,4 @@ mod tests { assert_eq!(next_pow2(1000), 1024); assert_eq!(next_pow2(0xFFFFFFFF as u64), (0xFFFFFFFF as u64) + 1); } - }