got rid of event cpy

This commit is contained in:
Dominik Maier 2020-12-14 01:01:06 +01:00
parent 52e1f52e1d
commit e757a5da9a
5 changed files with 68 additions and 82 deletions

View File

@ -23,11 +23,12 @@ pub trait StateMetadata: Debug {
} }
/// The state a fuzz run. /// The state a fuzz run.
pub struct State<I, R, FT> pub struct State<I, R, FT, OT>
where where
I: Input, I: Input,
R: Rand, R: Rand,
FT: FeedbacksTuple<I>, FT: FeedbacksTuple<I>,
OT: ObserversTuple,
{ {
/// How many times the executor ran the harness/target /// How many times the executor ran the harness/target
executions: usize, executions: usize,
@ -37,14 +38,15 @@ where
metadatas: HashMap<&'static str, Box<dyn StateMetadata>>, metadatas: HashMap<&'static str, Box<dyn StateMetadata>>,
// additional_corpuses: HashMap<&'static str, Box<dyn Corpus>>, // additional_corpuses: HashMap<&'static str, Box<dyn Corpus>>,
feedbacks: FT, feedbacks: FT,
phantom: PhantomData<(I, R)>, phantom: PhantomData<(I, R, OT)>,
} }
impl<I, R, FT> State<I, R, FT> impl<I, R, FT, OT> State<I, R, FT, OT>
where where
I: Input, I: Input,
R: Rand, R: Rand,
FT: FeedbacksTuple<I>, FT: FeedbacksTuple<I>,
OT: ObserversTuple,
{ {
/// Get executions /// Get executions
#[inline] #[inline]
@ -113,7 +115,7 @@ where
// TODO move some of these, like evaluate_input, to FuzzingEngine // TODO move some of these, like evaluate_input, to FuzzingEngine
#[inline] #[inline]
pub fn is_interesting<OT>(&mut self, input: &I, observers: &OT) -> Result<u32, AflError> pub fn is_interesting(&mut self, input: &I, observers: &OT) -> Result<u32, AflError>
where where
OT: ObserversTuple, OT: ObserversTuple,
{ {
@ -121,10 +123,9 @@ where
} }
/// Runs the input and triggers observers and feedback /// Runs the input and triggers observers and feedback
pub fn evaluate_input<E, OT>(&mut self, input: &I, executor: &mut E) -> Result<u32, AflError> pub fn evaluate_input<E>(&mut self, input: &I, executor: &mut E) -> Result<u32, AflError>
where where
E: Executor<I> + HasObservers<OT>, E: Executor<I> + HasObservers<OT>,
OT: ObserversTuple,
{ {
executor.reset_observers()?; executor.reset_observers()?;
executor.run_target(&input)?; executor.run_target(&input)?;
@ -187,7 +188,7 @@ where
} }
} }
pub fn generate_initial_inputs<G, C, E, OT, ET, EM>( pub fn generate_initial_inputs<G, C, E, ET, EM>(
&mut self, &mut self,
rand: &mut R, rand: &mut R,
corpus: &mut C, corpus: &mut C,
@ -200,7 +201,6 @@ where
G: Generator<I, R>, G: Generator<I, R>,
C: Corpus<I, R>, C: Corpus<I, R>,
E: Executor<I> + HasObservers<OT>, E: Executor<I> + HasObservers<OT>,
OT: ObserversTuple,
ET: ExecutorsTuple<I>, ET: ExecutorsTuple<I>,
EM: EventManager<C, E, OT, FT, I, R>, EM: EventManager<C, E, OT, FT, I, R>,
{ {
@ -311,7 +311,7 @@ where
fn fuzz_one( fn fuzz_one(
&mut self, &mut self,
rand: &mut R, rand: &mut R,
state: &mut State<I, R, FT>, state: &mut State<I, R, FT, OT>,
corpus: &mut C, corpus: &mut C,
engine: &mut Engine<E, OT, ET, I>, engine: &mut Engine<E, OT, ET, I>,
manager: &mut EM, manager: &mut EM,
@ -328,7 +328,7 @@ where
fn fuzz_loop( fn fuzz_loop(
&mut self, &mut self,
rand: &mut R, rand: &mut R,
state: &mut State<I, R, FT>, state: &mut State<I, R, FT, OT>,
corpus: &mut C, corpus: &mut C,
engine: &mut Engine<E, OT, ET, I>, engine: &mut Engine<E, OT, ET, I>,
manager: &mut EM, manager: &mut EM,

View File

@ -5,7 +5,6 @@ pub mod shmem_translated;
use alloc::string::String; use alloc::string::String;
use core::{marker::PhantomData, time}; use core::{marker::PhantomData, time};
use tuple_list::tuple_list_type;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -35,10 +34,7 @@ pub enum BrokerEventResult {
pub struct ClientStats { pub struct ClientStats {
// stats (maybe we need a separated struct?) // stats (maybe we need a separated struct?)
id: usize,
executions: u64, executions: u64,
execs_over_sec: u64,
corpus_size: usize,
} }
/// A custom event, for own messages, with own handler. /// A custom event, for own messages, with own handler.
@ -180,6 +176,44 @@ where
} }
} }
/// Client fun
fn handle_in_client<C, OT, FT, I, R> (
event: Event<I>,
state: &mut State<I, R, FT, OT>,
corpus: &mut C,
) -> Result<(), AflError>
where
C: Corpus<I, R>,
OT: ObserversTuple,
FT: FeedbacksTuple<I>,
I: Input,
R: Rand,
{
match event {
Event::NewTestcase {
sender_id: _,
input,
observers_buf,
client_config: _,
} => {
// TODO: here u should match client_config, if equal to the current one do not re-execute
// we need to pass engine to process() too, TODO
#[cfg(feature = "std")]
println!("Received new Testcase");
let observers = postcard::from_bytes(&observers_buf)?;
let interestingness = state.is_interesting(&input, &observers)?;
state.add_if_interesting(corpus, input, interestingness)?;
Ok(())
}
_ => Err(AflError::Unknown(
format!("Received illegal message that message should not have arrived: {:?}.", event),
)),
}
}
pub trait EventManager<C, E, OT, FT, I, R> pub trait EventManager<C, E, OT, FT, I, R>
where where
C: Corpus<I, R>, C: Corpus<I, R>,
@ -194,7 +228,7 @@ where
/// Lookup for incoming events and process them. /// Lookup for incoming events and process them.
/// Return the number of processes events or an error /// Return the number of processes events or an error
fn process(&mut self, state: &mut State<I, R, FT>, corpus: &mut C) -> Result<usize, AflError>; fn process(&mut self, state: &mut State<I, R, FT, OT>, corpus: &mut C) -> Result<usize, AflError>;
/// the client stat, mutable /// the client stat, mutable
fn client_stats_mut(&mut self) -> &mut Vec<ClientStats>; fn client_stats_mut(&mut self) -> &mut Vec<ClientStats>;
@ -262,11 +296,8 @@ where
} => { } => {
// TODO: The stats buffer should be added on client add. // TODO: The stats buffer should be added on client add.
let client_stat_count = self.client_stats().len(); let client_stat_count = self.client_stats().len();
for i in client_stat_count..(*sender_id + 1) as usize { for _ in client_stat_count..(*sender_id + 1) as usize {
self.client_stats_mut().push(ClientStats { self.client_stats_mut().push(ClientStats {
id: client_stat_count + i,
corpus_size: 0,
execs_over_sec: 0,
executions: 0, executions: 0,
}) })
} }
@ -307,35 +338,6 @@ where
} }
} }
/// Client fun
fn handle_in_client(
&mut self,
event: Event<I>,
state: &mut State<I, R, FT>,
corpus: &mut C,
) -> Result<(), AflError> {
match event {
Event::NewTestcase {
sender_id: _,
input,
observers_buf,
client_config: _,
} => {
// TODO: here u should match client_config, if equal to the current one do not re-execute
// we need to pass engine to process() too, TODO
#[cfg(feature = "std")]
println!("Received new Testcase");
let observers: OT = self.deserialize_observers(&observers_buf)?;
let interestingness = state.is_interesting(&input, &observers)?;
state.add_if_interesting(corpus, input, interestingness)?;
Ok(())
}
_ => Err(AflError::Unknown(
format!("Received illegal message that message should not have arrived: {:?}.", event),
)),
}
}
fn serialize_observers(&mut self, observers: &OT) -> Result<Vec<u8>, AflError> { fn serialize_observers(&mut self, observers: &OT) -> Result<Vec<u8>, AflError> {
Ok(postcard::to_allocvec(observers)?) Ok(postcard::to_allocvec(observers)?)
} }
@ -358,12 +360,9 @@ where
//CE: CustomEvent<I, OT>, //CE: CustomEvent<I, OT>,
{ {
writer: W, writer: W,
count: usize,
events: Vec<Event<I>>, events: Vec<Event<I>>,
// stats (maybe we need a separated struct?) // stats (maybe we need a separated struct?)
executions: usize,
execs_over_sec: u64,
corpus_size: usize, corpus_size: usize,
start_time: time::Duration, start_time: time::Duration,
client_stats: Vec<ClientStats>, client_stats: Vec<ClientStats>,
@ -395,12 +394,11 @@ where
fn process( fn process(
&mut self, &mut self,
state: &mut State<I, R, FT>, state: &mut State<I, R, FT, OT>,
corpus: &mut C, corpus: &mut C,
) -> Result<usize, AflError> { ) -> Result<usize, AflError> {
let count = self.events.len(); let count = self.events.len();
let events: Vec<Event<I>> = self.events.drain(..).collect(); self.events.drain(..).try_for_each(|event| handle_in_client(event, state, corpus))?;
events.into_iter().try_for_each(|x| self.handle_in_client(x, state, corpus))?;
Ok(count) Ok(count)
} }
@ -443,9 +441,6 @@ where
start_time: utils::current_time(), start_time: utils::current_time(),
client_stats: vec![], client_stats: vec![],
writer: writer, writer: writer,
count: 0,
executions: 0,
execs_over_sec: 0,
corpus_size: 0, corpus_size: 0,
phantom: PhantomData, phantom: PhantomData,
events: vec![], events: vec![],
@ -500,25 +495,24 @@ where
fn process( fn process(
&mut self, &mut self,
state: &mut State<I, R, FT>, state: &mut State<I, R, FT, OT>,
corpus: &mut C, corpus: &mut C,
) -> Result<usize, AflError> { ) -> Result<usize, AflError> {
// TODO: Get around local event copy by moving handle_in_client // TODO: Get around local event copy by moving handle_in_client
let mut events = vec![]; Ok(match &mut self.llmp {
match &mut self.llmp {
llmp::LlmpConnection::IsClient {client} => { llmp::LlmpConnection::IsClient {client} => {
let mut msg_count = 0; let mut count = 0;
loop { loop {
match client.recv_buf()? { match client.recv_buf()? {
Some((tag, event_buf)) => { Some((tag, event_buf)) => {
if tag == _LLMP_TAG_EVENT_TO_BROKER { if tag == _LLMP_TAG_EVENT_TO_BROKER {
continue; continue;
} }
let event: Event<I> = postcard::from_bytes(event_buf)?; let event: Event<I> = postcard::from_bytes(event_buf)?;
events.push(event); handle_in_client(event, state, corpus)?;
count += 1;
}, },
None => break msg_count, None => break count,
} }
} }
}, },
@ -526,10 +520,7 @@ where
dbg!("Skipping process in broker"); dbg!("Skipping process in broker");
0 0
} }
}; })
let count = events.len();
events.into_iter().try_for_each(|event| self.handle_in_client(event, state, corpus))?;
Ok(count)
} }
fn client_stats_mut(&mut self) -> &mut Vec<ClientStats> { fn client_stats_mut(&mut self) -> &mut Vec<ClientStats> {
@ -558,22 +549,17 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::io::stderr;
use crate::events::EventManager;
use crate::inputs::bytes::BytesInput; use crate::inputs::bytes::BytesInput;
use crate::observers::StdMapObserver; use crate::observers::StdMapObserver;
use crate::serde_anymap::{Ptr, PtrMut}; use crate::tuples::{tuple_list, MatchNameAndType, Named};
use crate::tuples::{tuple_list, tuple_list_type, MatchNameAndType, Named};
use crate::{events::Event, observers::ObserversTuple}; use crate::{events::Event, observers::ObserversTuple};
use super::LoggerEventManager;
static mut MAP: [u32; 4] = [0; 4]; static mut MAP: [u32; 4] = [0; 4];
#[test] #[test]
fn test_event_serde() { fn test_event_serde() {
let obv = StdMapObserver::new("test", unsafe { &mut MAP }); let obv = StdMapObserver::new("test", unsafe { &mut MAP });
let mut map = tuple_list!(obv); let map = tuple_list!(obv);
let observers_buf = map.serialize().unwrap(); let observers_buf = map.serialize().unwrap();
// test_event_mgr.serialize_observers(&map).unwrap(); // test_event_mgr.serialize_observers(&map).unwrap();

View File

@ -195,7 +195,7 @@ mod tests {
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize, Debug)]
struct NopInput {} struct NopInput {}
impl Input for NopInput {} impl Input for NopInput {}
impl HasTargetBytes for NopInput { impl HasTargetBytes for NopInput {

View File

@ -29,7 +29,7 @@ where
fn perform( fn perform(
&mut self, &mut self,
rand: &mut R, rand: &mut R,
state: &mut State<I, R, FT>, state: &mut State<I, R, FT, OT>,
corpus: &mut C, corpus: &mut C,
engine: &mut Engine<E, OT, ET, I>, engine: &mut Engine<E, OT, ET, I>,
manager: &mut EM, manager: &mut EM,
@ -51,7 +51,7 @@ where
fn perform_all( fn perform_all(
&mut self, &mut self,
rand: &mut R, rand: &mut R,
state: &mut State<I, R, FT>, state: &mut State<I, R, FT, OT>,
corpus: &mut C, corpus: &mut C,
engine: &mut Engine<E, OT, ET, I>, engine: &mut Engine<E, OT, ET, I>,
manager: &mut EM, manager: &mut EM,
@ -75,7 +75,7 @@ where
fn perform_all( fn perform_all(
&mut self, &mut self,
_rand: &mut R, _rand: &mut R,
_state: &mut State<I, R, FT>, _state: &mut State<I, R, FT, OT>,
_corpus: &mut C, _corpus: &mut C,
_engine: &mut Engine<E, OT, ET, I>, _engine: &mut Engine<E, OT, ET, I>,
_manager: &mut EM, _manager: &mut EM,
@ -104,7 +104,7 @@ where
fn perform_all( fn perform_all(
&mut self, &mut self,
rand: &mut R, rand: &mut R,
state: &mut State<I, R, FT>, state: &mut State<I, R, FT, OT>,
corpus: &mut C, corpus: &mut C,
engine: &mut Engine<E, OT, ET, I>, engine: &mut Engine<E, OT, ET, I>,
manager: &mut EM, manager: &mut EM,

View File

@ -47,7 +47,7 @@ where
fn perform_mutational( fn perform_mutational(
&mut self, &mut self,
rand: &mut R, rand: &mut R,
state: &mut State<I, R, FT>, state: &mut State<I, R, FT, OT>,
corpus: &mut C, corpus: &mut C,
engine: &mut Engine<E, OT, ET, I>, engine: &mut Engine<E, OT, ET, I>,
manager: &mut EM, manager: &mut EM,
@ -143,7 +143,7 @@ where
fn perform( fn perform(
&mut self, &mut self,
rand: &mut R, rand: &mut R,
state: &mut State<I, R, FT>, state: &mut State<I, R, FT, OT>,
corpus: &mut C, corpus: &mut C,
engine: &mut Engine<E, OT, ET, I>, engine: &mut Engine<E, OT, ET, I>,
manager: &mut EM, manager: &mut EM,