serialize event test
This commit is contained in:
parent
8f5b33ca47
commit
999e7023e4
@ -35,7 +35,7 @@ pub trait ShowStats {}
|
|||||||
/*
|
/*
|
||||||
|
|
||||||
/// A custom event, in case a user wants to extend the features (at compile time)
|
/// A custom event, in case a user wants to extend the features (at compile time)
|
||||||
pub trait CustomEvent<C, E, I, R>
|
pub trait CustomEvent<I>
|
||||||
where
|
where
|
||||||
S: State<I, R>,
|
S: State<I, R>,
|
||||||
C: Corpus<I, R>,
|
C: Corpus<I, R>,
|
||||||
@ -52,7 +52,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct UnusedCustomEvent {}
|
struct UnusedCustomEvent {}
|
||||||
impl<C, E, I, R> CustomEvent<C, E, I, R> for UnusedCustomEvent<C, E, I, R>
|
impl<C, E, I, R> CustomEvent<I> for UnusedCustomEvent<I>
|
||||||
where
|
where
|
||||||
S: State<I, R>,
|
S: State<I, R>,
|
||||||
C: Corpus<I, R>,
|
C: Corpus<I, R>,
|
||||||
@ -69,17 +69,14 @@ where
|
|||||||
/// Events sent around in the library
|
/// Events sent around in the library
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
#[serde(bound = "I: serde::de::DeserializeOwned")]
|
#[serde(bound = "I: serde::de::DeserializeOwned")]
|
||||||
pub enum Event<'a, C, E, I, R>
|
pub enum Event<'a, I>
|
||||||
where
|
where
|
||||||
C: Corpus<I, R>,
|
|
||||||
E: Executor<I>,
|
|
||||||
I: Input,
|
I: Input,
|
||||||
R: Rand,
|
// CE: CustomEvent<I>,
|
||||||
// CE: CustomEvent<C, E, I, R>,
|
|
||||||
{
|
{
|
||||||
LoadInitial {
|
LoadInitial {
|
||||||
sender_id: u64,
|
sender_id: u64,
|
||||||
phantom: PhantomData<(C, E, I, R)>,
|
phantom: PhantomData<I>,
|
||||||
},
|
},
|
||||||
NewTestcase {
|
NewTestcase {
|
||||||
sender_id: u64,
|
sender_id: u64,
|
||||||
@ -90,37 +87,34 @@ where
|
|||||||
sender_id: u64,
|
sender_id: u64,
|
||||||
executions: usize,
|
executions: usize,
|
||||||
execs_over_sec: u64,
|
execs_over_sec: u64,
|
||||||
phantom: PhantomData<(C, E, I, R)>,
|
phantom: PhantomData<I>,
|
||||||
},
|
},
|
||||||
Crash {
|
Crash {
|
||||||
sender_id: u64,
|
sender_id: u64,
|
||||||
input: I,
|
input: I,
|
||||||
phantom: PhantomData<(C, E, I, R)>,
|
phantom: PhantomData<I>,
|
||||||
},
|
},
|
||||||
Timeout {
|
Timeout {
|
||||||
sender_id: u64,
|
sender_id: u64,
|
||||||
input: I,
|
input: I,
|
||||||
phantom: PhantomData<(C, E, I, R)>,
|
phantom: PhantomData<I>,
|
||||||
},
|
},
|
||||||
Log {
|
Log {
|
||||||
sender_id: u64,
|
sender_id: u64,
|
||||||
severity_level: u8,
|
severity_level: u8,
|
||||||
message: String,
|
message: String,
|
||||||
phantom: PhantomData<(C, E, I, R)>,
|
phantom: PhantomData<I>,
|
||||||
},
|
},
|
||||||
None {
|
None {
|
||||||
phantom: PhantomData<(C, E, I, R)>,
|
phantom: PhantomData<I>,
|
||||||
},
|
},
|
||||||
//Custom {sender_id: u64, custom_event: CE},
|
//Custom {sender_id: u64, custom_event: CE},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, C, E, I, R> Event<'a, C, E, I, R>
|
impl<'a, I> Event<'a, I>
|
||||||
where
|
where
|
||||||
C: Corpus<I, R>,
|
|
||||||
E: Executor<I>,
|
|
||||||
I: Input,
|
I: Input,
|
||||||
R: Rand,
|
//CE: CustomEvent<I>,
|
||||||
//CE: CustomEvent<C, E, I, R>,
|
|
||||||
{
|
{
|
||||||
pub fn name(&self) -> &str {
|
pub fn name(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
@ -191,7 +185,7 @@ where
|
|||||||
/// Fire an Event
|
/// Fire an Event
|
||||||
fn fire<'a>(
|
fn fire<'a>(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: Event<'a, C, E, I, R>,
|
event: Event<'a, I>,
|
||||||
state: &mut State<I, R>,
|
state: &mut State<I, R>,
|
||||||
corpus: &mut C,
|
corpus: &mut C,
|
||||||
) -> Result<(), AflError>;
|
) -> Result<(), AflError>;
|
||||||
@ -209,7 +203,7 @@ where
|
|||||||
// TODO the broker has a state? do we need to pass state and corpus?
|
// TODO the broker has a state? do we need to pass state and corpus?
|
||||||
fn handle_in_broker(
|
fn handle_in_broker(
|
||||||
&self,
|
&self,
|
||||||
event: &Event<C, E, I, R>,
|
event: &Event<I>,
|
||||||
/*broker: &dyn EventManager<C, E, I, R>,*/ _state: &mut State<I, R>,
|
/*broker: &dyn EventManager<C, E, I, R>,*/ _state: &mut State<I, R>,
|
||||||
_corpus: &mut C,
|
_corpus: &mut C,
|
||||||
) -> Result<BrokerEventResult, AflError> {
|
) -> Result<BrokerEventResult, AflError> {
|
||||||
@ -266,7 +260,7 @@ where
|
|||||||
|
|
||||||
fn handle_in_client(
|
fn handle_in_client(
|
||||||
&self,
|
&self,
|
||||||
event: Event<C, E, I, R>,
|
event: Event<I>,
|
||||||
/*client: &dyn EventManager<C, E, I, R>,*/ _state: &mut State<I, R>,
|
/*client: &dyn EventManager<C, E, I, R>,*/ _state: &mut State<I, R>,
|
||||||
corpus: &mut C,
|
corpus: &mut C,
|
||||||
) -> Result<(), AflError> {
|
) -> Result<(), AflError> {
|
||||||
@ -305,7 +299,7 @@ where
|
|||||||
pub struct LoggerEventManager<C, E, I, R, W>
|
pub struct LoggerEventManager<C, E, I, R, W>
|
||||||
where
|
where
|
||||||
W: Write,
|
W: Write,
|
||||||
//CE: CustomEvent<C, E, I, R>,
|
//CE: CustomEvent<I>,
|
||||||
{
|
{
|
||||||
writer: W,
|
writer: W,
|
||||||
count: usize,
|
count: usize,
|
||||||
@ -320,11 +314,11 @@ where
|
|||||||
I: Input,
|
I: Input,
|
||||||
R: Rand,
|
R: Rand,
|
||||||
W: Write,
|
W: Write,
|
||||||
//CE: CustomEvent<C, E, I, R>,
|
//CE: CustomEvent<I>,
|
||||||
{
|
{
|
||||||
fn fire<'a>(
|
fn fire<'a>(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: Event<'a, C, E, I, R>,
|
event: Event<'a, I>,
|
||||||
state: &mut State<I, R>,
|
state: &mut State<I, R>,
|
||||||
corpus: &mut C,
|
corpus: &mut C,
|
||||||
) -> Result<(), AflError> {
|
) -> Result<(), AflError> {
|
||||||
@ -370,7 +364,7 @@ where
|
|||||||
I: Input,
|
I: Input,
|
||||||
E: Executor<I>,
|
E: Executor<I>,
|
||||||
R: Rand,
|
R: Rand,
|
||||||
//CE: CustomEvent<C, E, I, R>,
|
//CE: CustomEvent<I>,
|
||||||
{
|
{
|
||||||
llmp_broker: llmp::LlmpBroker,
|
llmp_broker: llmp::LlmpBroker,
|
||||||
phantom: PhantomData<(C, E, I, R)>,
|
phantom: PhantomData<(C, E, I, R)>,
|
||||||
@ -390,7 +384,7 @@ where
|
|||||||
I: Input,
|
I: Input,
|
||||||
E: Executor<I>,
|
E: Executor<I>,
|
||||||
R: Rand,
|
R: Rand,
|
||||||
//CE: CustomEvent<C, E, I, R>,
|
//CE: CustomEvent<I>,
|
||||||
{
|
{
|
||||||
llmp_client: llmp::LlmpClient,
|
llmp_client: llmp::LlmpClient,
|
||||||
phantom: PhantomData<(C, E, I, R)>,
|
phantom: PhantomData<(C, E, I, R)>,
|
||||||
@ -403,12 +397,12 @@ where
|
|||||||
E: Executor<I>,
|
E: Executor<I>,
|
||||||
I: Input,
|
I: Input,
|
||||||
R: Rand,
|
R: Rand,
|
||||||
//CE: CustomEvent<C, E, I, R>,
|
//CE: CustomEvent<I>,
|
||||||
{
|
{
|
||||||
/// Fire an Event
|
/// Fire an Event
|
||||||
fn fire<'a>(
|
fn fire<'a>(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: Event<'a, C, E, I, R>,
|
event: Event<'a, I>,
|
||||||
state: &mut State<I, R>,
|
state: &mut State<I, R>,
|
||||||
corpus: &mut C,
|
corpus: &mut C,
|
||||||
) -> Result<(), AflError> {
|
) -> Result<(), AflError> {
|
||||||
@ -455,7 +449,7 @@ where
|
|||||||
|
|
||||||
fn handle_in_broker(
|
fn handle_in_broker(
|
||||||
&self,
|
&self,
|
||||||
event: &Event<C, E, I, R>,
|
event: &Event<I>,
|
||||||
/*broker: &dyn EventManager<C, E, I, R>,*/ _state: &mut State<I, R>,
|
/*broker: &dyn EventManager<C, E, I, R>,*/ _state: &mut State<I, R>,
|
||||||
_corpus: &mut C,
|
_corpus: &mut C,
|
||||||
) -> Result<BrokerEventResult, AflError> {
|
) -> Result<BrokerEventResult, AflError> {
|
||||||
@ -512,7 +506,7 @@ where
|
|||||||
|
|
||||||
fn handle_in_client(
|
fn handle_in_client(
|
||||||
&self,
|
&self,
|
||||||
event: Event<C, E, I, R>,
|
event: Event<I>,
|
||||||
/*client: &dyn EventManager<C, E, I, R>,*/ _state: &mut State<I, R>,
|
/*client: &dyn EventManager<C, E, I, R>,*/ _state: &mut State<I, R>,
|
||||||
corpus: &mut C,
|
corpus: &mut C,
|
||||||
) -> Result<(), AflError> {
|
) -> Result<(), AflError> {
|
||||||
@ -534,3 +528,45 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
|
||||||
|
use crate::events::Event;
|
||||||
|
use crate::serde_anymap::{Ptr, PtrMut};
|
||||||
|
use crate::inputs::bytes::BytesInput;
|
||||||
|
use crate::observers::{Observer, StdMapObserver};
|
||||||
|
use crate::observers::observer_serde::NamedSerdeAnyMap;
|
||||||
|
|
||||||
|
static mut MAP: [u32; 4] = [0; 4];
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_event_serde() {
|
||||||
|
let mut map = NamedSerdeAnyMap::new();
|
||||||
|
let obv = StdMapObserver::new("test", unsafe { &mut MAP });
|
||||||
|
map.insert(Box::new(obv), &"key".to_string());
|
||||||
|
|
||||||
|
let i = BytesInput::new(vec![0]);
|
||||||
|
let e = Event::NewTestcase {
|
||||||
|
sender_id: 0,
|
||||||
|
input: Ptr::Ref(&i),
|
||||||
|
observers: PtrMut::Ref(&mut map),
|
||||||
|
};
|
||||||
|
|
||||||
|
let j = serde_json::to_string(&e).unwrap();
|
||||||
|
|
||||||
|
let d: Event<BytesInput> = serde_json::from_str(&j).unwrap();
|
||||||
|
match d {
|
||||||
|
Event::NewTestcase {
|
||||||
|
sender_id: _,
|
||||||
|
input: _,
|
||||||
|
observers: obs,
|
||||||
|
} => {
|
||||||
|
let o = obs.as_ref().get::<StdMapObserver<u32>>(&"key".to_string()).unwrap();
|
||||||
|
assert_eq!("test".to_string(), *o.name());
|
||||||
|
},
|
||||||
|
_ => panic!("mistmatch".to_string())
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -364,7 +364,7 @@ macro_rules! create_serde_registry_for_trait {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, val: Box<dyn $trait_name>, name: &String) {
|
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());
|
||||||
}
|
}
|
||||||
@ -438,12 +438,23 @@ macro_rules! create_serde_registry_for_trait {
|
|||||||
create_serde_registry_for_trait!(serdeany_serde, crate::serde_anymap::SerdeAny);
|
create_serde_registry_for_trait!(serdeany_serde, crate::serde_anymap::SerdeAny);
|
||||||
pub use serdeany_serde::*;
|
pub use serdeany_serde::*;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
pub enum Ptr<'a, T: 'a + ?Sized> {
|
pub enum Ptr<'a, T: 'a + ?Sized> {
|
||||||
Ref(&'a T),
|
Ref(&'a T),
|
||||||
Owned(Box<T>),
|
Owned(Box<T>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, T: 'a + ?Sized + serde::Serialize> serde::Serialize for Ptr<'a, T> {
|
||||||
|
fn serialize<S>(&self, se: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
match *self {
|
||||||
|
Ptr::Ref(ref r) => se.serialize_some(r),
|
||||||
|
Ptr::Owned(ref b) => se.serialize_some(b.as_ref())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, 'a, T: 'a + ?Sized> Deserialize<'de> for Ptr<'a, T>
|
impl<'de, 'a, T: 'a + ?Sized> Deserialize<'de> for Ptr<'a, T>
|
||||||
where
|
where
|
||||||
Box<T>: Deserialize<'de>,
|
Box<T>: Deserialize<'de>,
|
||||||
@ -456,12 +467,33 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
impl<'a, T: Sized> Ptr<'a, T> {
|
||||||
|
pub fn as_ref(&self) -> &T {
|
||||||
|
match self {
|
||||||
|
Ptr::Ref(r) => r,
|
||||||
|
Ptr::Owned(v) => v.as_ref(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
pub enum PtrMut<'a, T: 'a + ?Sized> {
|
pub enum PtrMut<'a, T: 'a + ?Sized> {
|
||||||
Ref(&'a mut T),
|
Ref(&'a mut T),
|
||||||
Owned(Box<T>),
|
Owned(Box<T>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, T: 'a + ?Sized + serde::Serialize> serde::Serialize for PtrMut<'a, T> {
|
||||||
|
fn serialize<S>(&self, se: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
match *self {
|
||||||
|
PtrMut::Ref(ref r) => se.serialize_some(r),
|
||||||
|
PtrMut::Owned(ref b) => se.serialize_some(b.as_ref())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, 'a, T: 'a + ?Sized> Deserialize<'de> for PtrMut<'a, T>
|
impl<'de, 'a, T: 'a + ?Sized> Deserialize<'de> for PtrMut<'a, T>
|
||||||
where
|
where
|
||||||
Box<T>: Deserialize<'de>,
|
Box<T>: Deserialize<'de>,
|
||||||
@ -474,12 +506,40 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
impl<'a, T: Sized> PtrMut<'a, T> {
|
||||||
|
pub fn as_ref(&self) -> &T {
|
||||||
|
match self {
|
||||||
|
PtrMut::Ref(r) => r,
|
||||||
|
PtrMut::Owned(v) => v.as_ref(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_mut(&mut self) -> &T {
|
||||||
|
match self {
|
||||||
|
PtrMut::Ref(r) => r,
|
||||||
|
PtrMut::Owned(v) => v.as_mut(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub enum Slice<'a, T: 'a + Sized> {
|
pub enum Slice<'a, T: 'a + Sized> {
|
||||||
Ref(&'a [T]),
|
Ref(&'a [T]),
|
||||||
Owned(Vec<T>),
|
Owned(Vec<T>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, T: 'a + Sized + serde::Serialize> serde::Serialize for Slice<'a, T> {
|
||||||
|
fn serialize<S>(&self, se: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
match *self {
|
||||||
|
Slice::Ref(ref r) => se.serialize_some(r),
|
||||||
|
Slice::Owned(ref b) => se.serialize_some(b.as_slice())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, 'a, T: 'a + Sized> Deserialize<'de> for Slice<'a, T>
|
impl<'de, 'a, T: 'a + Sized> Deserialize<'de> for Slice<'a, T>
|
||||||
where
|
where
|
||||||
Vec<T>: Deserialize<'de>,
|
Vec<T>: Deserialize<'de>,
|
||||||
@ -492,12 +552,32 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
impl<'a, T: Sized> Slice<'a, T> {
|
||||||
|
pub fn as_slice(&self) -> &[T] {
|
||||||
|
match self {
|
||||||
|
Slice::Ref(r) => r,
|
||||||
|
Slice::Owned(v) => v.as_slice(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub enum SliceMut<'a, T: 'a + Sized> {
|
pub enum SliceMut<'a, T: 'a + Sized> {
|
||||||
Ref(&'a mut [T]),
|
Ref(&'a mut [T]),
|
||||||
Owned(Vec<T>),
|
Owned(Vec<T>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, T: 'a + Sized + serde::Serialize> serde::Serialize for SliceMut<'a, T> {
|
||||||
|
fn serialize<S>(&self, se: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
match *self {
|
||||||
|
SliceMut::Ref(ref r) => se.serialize_some(r),
|
||||||
|
SliceMut::Owned(ref b) => se.serialize_some(b.as_slice())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'de, 'a, T: 'a + Sized> Deserialize<'de> for SliceMut<'a, T>
|
impl<'de, 'a, T: 'a + Sized> Deserialize<'de> for SliceMut<'a, T>
|
||||||
where
|
where
|
||||||
Vec<T>: Deserialize<'de>,
|
Vec<T>: Deserialize<'de>,
|
||||||
@ -510,6 +590,22 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Sized> SliceMut<'a, T> {
|
||||||
|
pub fn as_slice(&self) -> &[T] {
|
||||||
|
match self {
|
||||||
|
SliceMut::Ref(r) => r,
|
||||||
|
SliceMut::Owned(v) => v.as_slice(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_mut_slice(&mut self) -> &[T] {
|
||||||
|
match self {
|
||||||
|
SliceMut::Ref(r) => r,
|
||||||
|
SliceMut::Owned(v) => v.as_mut_slice(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub enum Array<T: Sized + serde::Serialize> {
|
pub enum Array<T: Sized + serde::Serialize> {
|
||||||
Cptr((*const T, usize)),
|
Cptr((*const T, usize)),
|
||||||
Owned(Vec<T>),
|
Owned(Vec<T>),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user