serialize testcase

This commit is contained in:
Andrea Fioraldi 2020-11-27 18:01:10 +01:00
parent 7771071491
commit e1779e4503
4 changed files with 43 additions and 25 deletions

View File

@ -19,5 +19,5 @@ libc = "0.2" # For (*nix) libc
num = "*"
xxhash-rust = { version = "0.8.0-beta.5", features = ["xxh3"] } # xxh3 hashing for rust
serde = { version = "1.0", default-features = false, features = ["alloc"] } # serialization lib
serde_traitobject = { version = "0.2.7" } # trait obj serialization lib
typetag = "0.1"
postcard = "0.5.1" # no_std compatible serde serialization fromat

View File

@ -1,13 +1,13 @@
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::String;
use core::any::Any;
use core::any::{Any, TypeId};
use core::cell::RefCell;
use core::convert::Into;
use core::default::Default;
use core::option::Option;
use hashbrown::HashMap;
use serde_traitobject::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use crate::inputs::Input;
use crate::AflError;
@ -18,7 +18,8 @@ use crate::AflError;
// TODO: Give example
/// Metadata for a testcase
pub trait TestcaseMetadata: Any + Serialize + Deserialize<'static> {
#[typetag::serde(tag = "type")]
pub trait TestcaseMetadata: Any {
/// The name of this metadata - used to find it in the list of avaliable metadatas
fn name(&self) -> &'static str;
}
@ -36,7 +37,7 @@ where
/// Accumulated fitness from all the feedbacks
fitness: u32,
/// Map of metadatas associated with this testcase
metadatas: HashMap<&'static str, Box<dyn TestcaseMetadata>>,
metadatas: HashMap<String, Box<dyn TestcaseMetadata>>,
}
impl<I> Into<Rc<RefCell<Self>>> for Testcase<I>
@ -99,12 +100,12 @@ where
}
/// Get all the metadatas into an HashMap (mutable)
pub fn metadatas(&mut self) -> &mut HashMap<&'static str, Box<dyn TestcaseMetadata>> {
pub fn metadatas(&mut self) -> &mut HashMap<String, Box<dyn TestcaseMetadata>> {
&mut self.metadatas
}
/// Add a metadata
pub fn add_metadata(&mut self, meta: Box<dyn TestcaseMetadata>) {
self.metadatas.insert(meta.name(), meta);
self.metadatas.insert(meta.name().to_string(), meta);
}
/// Create a new Testcase instace given an input

View File

@ -106,6 +106,9 @@ where
message: String,
phantom: PhantomData<(S, C, E, I, R)>,
},
None {
phantom: PhantomData<(S, C, E, I, R)>,
},
//Custom {sender_id: u64, custom_event: CE},
}
@ -122,34 +125,35 @@ where
match self {
Event::LoadInitial {
sender_id: _,
phantom,
phantom: _,
} => "Initial",
Event::NewTestcase {
sender_id: _,
testcase: _,
phantom,
phantom: _,
} => "New Testcase",
Event::UpdateStats {
sender_id: _,
new_execs: _,
phantom,
phantom: _,
} => "Stats",
Event::Crash {
sender_id: _,
input: _,
phantom,
phantom: _,
} => "Crash",
Event::Timeout {
sender_id: _,
input: _,
phantom,
phantom: _,
} => "Timeout",
Event::Log {
sender_id: _,
severity_level: _,
message: _,
phantom,
phantom: _,
} => "Log",
Event::None { phantom: _ } => "None",
//Event::Custom {sender_id, custom_event} => custom_event.name(),
}
}
@ -160,16 +164,19 @@ where
_corpus: &mut C,
) -> Result<BrokerEventResult, AflError> {
match self {
Event::LoadInitial { sender_id: _, phantom } => Ok(BrokerEventResult::Handled),
Event::LoadInitial {
sender_id: _,
phantom: _,
} => Ok(BrokerEventResult::Handled),
Event::NewTestcase {
sender_id: _,
testcase: _,
phantom,
phantom: _,
} => Ok(BrokerEventResult::Forward),
Event::UpdateStats {
sender_id: _,
new_execs: _,
phantom,
phantom: _,
} => {
// TODO
Ok(BrokerEventResult::Handled)
@ -177,12 +184,12 @@ where
Event::Crash {
sender_id: _,
input: _,
phantom,
phantom: _,
} => Ok(BrokerEventResult::Handled),
Event::Timeout {
sender_id: _,
input: _,
phantom,
phantom: _,
} => {
// TODO
Ok(BrokerEventResult::Handled)
@ -191,30 +198,38 @@ where
sender_id,
severity_level,
message,
phantom,
phantom: _,
} => {
//TODO: broker.log()
#[cfg(feature = "std")]
println!("{}[{}]: {}", sender_id, severity_level, message);
Ok(BrokerEventResult::Handled)
}
},
Event::None {
phantom: _,
} => Ok(BrokerEventResult::Handled)
//Event::Custom {sender_id, custom_event} => custom_event.handle_in_broker(state, corpus),
//_ => Ok(BrokerEventResult::Forward),
}
}
fn handle_in_client(
&self,
&mut self,
/*client: &dyn EventManager<S, C, E, I, R>,*/ _state: &mut S,
corpus: &mut C,
) -> Result<(), AflError> {
match self {
match std::mem::replace(
self,
Event::None {
phantom: PhantomData,
},
) {
Event::NewTestcase {
sender_id: _,
testcase,
phantom,
phantom: _,
} => {
corpus.add(*testcase);
corpus.add(testcase);
Ok(())
}
_ => Err(AflError::Unknown(
@ -307,7 +322,7 @@ where
}
handled
.iter()
.zip(self.events.iter())
.zip(self.events.iter_mut())
.map(|(x, event)| match x {
BrokerEventResult::Forward => event.handle_in_client(state, corpus),
// Ignore broker-only events

View File

@ -170,6 +170,8 @@ where
pub struct MapNoveltiesMetadata {
novelties: Vec<usize>,
}
#[typetag::serde]
impl TestcaseMetadata for MapNoveltiesMetadata {
fn name(&self) -> &'static str {
"MapNoveltiesMetadata"