serialize corpus

This commit is contained in:
Andrea Fioraldi 2020-12-16 13:34:00 +01:00
parent 06d5c82475
commit 6f994990c0
4 changed files with 28 additions and 55 deletions

View File

@ -1,11 +1,12 @@
pub mod testcase;
pub use testcase::{Testcase, TestcaseMetadata};
pub use testcase::{Testcase};
use alloc::borrow::ToOwned;
use alloc::vec::Vec;
use core::cell::RefCell;
use core::marker::PhantomData;
use core::ptr;
use serde::{Serialize, Deserialize};
#[cfg(feature = "std")]
use std::path::PathBuf;
@ -27,7 +28,7 @@ where
}
/// Corpus with all current testcases
pub trait Corpus<I, R>: HasTestcaseVec<I>
pub trait Corpus<I, R>: HasTestcaseVec<I> + serde::Serialize + serde::de::DeserializeOwned
where
I: Input,
R: Rand,
@ -118,6 +119,8 @@ where
}
/// A corpus handling all important fuzzing in memory.
#[derive(Serialize, Deserialize)]
#[serde(bound = "I: serde::de::DeserializeOwned")]
pub struct InMemoryCorpus<I, R>
where
I: Input,
@ -182,6 +185,8 @@ where
/// A corpus able to store testcases to dis, and load them from disk, when they are being used.
#[cfg(feature = "std")]
#[derive(Serialize, Deserialize)]
#[serde(bound = "I: serde::de::DeserializeOwned")]
pub struct OnDiskCorpus<I, R>
where
I: Input,
@ -268,6 +273,8 @@ where
}
/// A Queue-like corpus, wrapping an existing Corpus instance
#[derive(Serialize, Deserialize)]
#[serde(bound = "I: serde::de::DeserializeOwned")]
pub struct QueueCorpus<C, I, R>
where
C: Corpus<I, R>,

View File

@ -14,35 +14,6 @@ use crate::AflError;
//#[cfg(feature = "std")]
//use std::path::PathBuf;
// TODO: Give example
/// Metadata for a testcase
pub trait TestcaseMetadata: SerdeAny {
/// The name of this metadata - used to find it in the list of avaliable metadatas
fn name(&self) -> &'static str;
}
/*
/// Just a wrapper of Boxed TestcaseMetadata trait object for Clone
#[derive(Serialize, Deserialize)]
pub struct TestcaseMetadataContainer {
meta: Box<dyn TestcaseMetadata>,
}
impl Clone for TestcaseMetadataContainer {
fn clone(&self) -> Self {
TestcaseMetadataContainer {
meta: self.meta.clone(),
}
}
}
impl TestcaseMetadataContainer {
pub fn meta(&self) -> &Box<dyn TestcaseMetadata> {
&self.meta
}
pub fn meta_mut(&mut self) -> &mut Box<dyn TestcaseMetadata> {
&mut self.meta
}
}*/
/// An entry in the Testcase Corpus
#[derive(Default, Serialize, Deserialize)]
#[serde(bound = "I: serde::de::DeserializeOwned")]
@ -145,9 +116,9 @@ where
/// Add a metadata
#[inline]
pub fn add_metadata<TM>(&mut self, meta: TM)
pub fn add_metadata<M>(&mut self, meta: M)
where
TM: TestcaseMetadata + 'static,
M: SerdeAny,
{
self.metadatas.insert(meta);
}

View File

@ -1,9 +1,8 @@
//! The engine is the core piece of every good fuzzer
use alloc::boxed::Box;
use core::fmt::Debug;
use core::marker::PhantomData;
use hashbrown::HashMap;
use serde::{Serialize, Deserialize};
use crate::corpus::{Corpus, Testcase};
use crate::events::EventManager;
@ -13,6 +12,7 @@ use crate::generators::Generator;
use crate::inputs::Input;
use crate::observers::ObserversTuple;
use crate::stages::StagesTuple;
use crate::serde_anymap::{SerdeAny, SerdeAnyMap};
use crate::tuples::{tuple_list, tuple_list_type};
use crate::utils::{current_milliseconds, Rand};
use crate::AflError;
@ -23,6 +23,8 @@ pub trait StateMetadata: Debug {
}
/// The state a fuzz run.
#[derive(Serialize, Deserialize)]
#[serde(bound = "FT: serde::de::DeserializeOwned")]
pub struct State<I, R, FT, OT>
where
I: Input,
@ -35,8 +37,9 @@ where
/// At what time the fuzzing started
start_time: u64,
/// Metadata stored for this state by one of the components
metadatas: HashMap<&'static str, Box<dyn StateMetadata>>,
// additional_corpuses: HashMap<&'static str, Box<dyn Corpus>>,
metadatas: SerdeAnyMap,
// additional_corpuses, maybe another TupleList?
// Feedbacks used to evaluate an input
feedbacks: FT,
phantom: PhantomData<(I, R, OT)>,
}
@ -86,20 +89,23 @@ where
/// Get all the metadatas into an HashMap
#[inline]
pub fn metadatas(&self) -> &HashMap<&'static str, Box<dyn StateMetadata>> {
pub fn metadatas(&self) -> &SerdeAnyMap {
&self.metadatas
}
/// Get all the metadatas into an HashMap (mutable)
#[inline]
pub fn metadatas_mut(&mut self) -> &mut HashMap<&'static str, Box<dyn StateMetadata>> {
pub fn metadatas_mut(&mut self) -> &mut SerdeAnyMap {
&mut self.metadatas
}
/// Add a metadata
#[inline]
pub fn add_metadata(&mut self, meta: Box<dyn StateMetadata>) {
self.metadatas_mut().insert(meta.name(), meta);
pub fn add_metadata<M>(&mut self, meta: M)
where
M: SerdeAny,
{
self.metadatas.insert(meta);
}
/// Returns vector of feebacks
@ -225,7 +231,7 @@ where
Self {
executions: 0,
start_time: current_milliseconds(),
metadatas: HashMap::default(),
metadatas: SerdeAnyMap::default(),
feedbacks: feedbacks,
phantom: PhantomData,
}
@ -405,9 +411,6 @@ where
#[cfg(test)]
mod tests {
#[cfg(feature = "std")]
use std::io::stderr;
use crate::corpus::{Corpus, InMemoryCorpus, Testcase};
use crate::engines::{Engine, Fuzzer, State, StdFuzzer};
#[cfg(feature = "std")]

View File

@ -4,21 +4,13 @@ use std::{ffi::CStr, mem::size_of};
use crate::AflError;
extern "C" {
#[no_mangle]
fn snprintf(_: *mut c_char, _: c_ulong, _: *const c_char, _: ...) -> c_int;
#[no_mangle]
fn strncpy(_: *mut c_char, _: *const c_char, _: c_ulong) -> *mut c_char;
#[no_mangle]
fn strlen(_: *const c_char) -> c_ulong;
#[no_mangle]
//fn strlen(_: *const c_char) -> c_ulong;
fn shmctl(__shmid: c_int, __cmd: c_int, __buf: *mut shmid_ds) -> c_int;
#[no_mangle]
fn shmget(__key: c_int, __size: c_ulong, __shmflg: c_int) -> c_int;
#[no_mangle]
fn shmat(__shmid: c_int, __shmaddr: *const c_void, __shmflg: c_int) -> *mut c_void;
#[no_mangle]
fn strtol(_: *const c_char, _: *mut *mut c_char, _: c_int) -> c_long;
#[no_mangle]
//fn strtol(_: *const c_char, _: *mut *mut c_char, _: c_int) -> c_long;
fn setenv(__name: *const c_char, __value: *const c_char, __replace: c_int) -> c_int;
}
#[derive(Copy, Clone)]