tidying code

This commit is contained in:
Dominik Maier 2020-11-13 03:58:38 +01:00
parent 003c346439
commit dec37fcfdd
6 changed files with 35 additions and 5 deletions

View File

@ -98,8 +98,11 @@ where
}
/// Create a new Testcase instace given an input behind a Rc RefCell
pub fn new_rr(input: I) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self::new(input)))
pub fn new_rr<T>(input: T) -> Rc<RefCell<Self>>
where
T: Into<I>,
{
Rc::new(RefCell::new(Self::new(input.into())))
}
/// Create a new Testcase instace given an input and a filename behind a Rc RefCell

View File

@ -110,10 +110,10 @@ mod tests {
#[test]
fn test_engine() {
let rand = DefaultRand::preseeded_rr();
let rand: Rc<_> = DefaultRand::preseeded().into();
let mut corpus = InMemoryCorpus::<BytesInput, _>::new(&rand);
let testcase = Testcase::new_rr(BytesInput::new(vec![0; 4]));
let testcase = Testcase::new_rr(vec![0; 4]);
corpus.add(testcase);
let executor: Rc<RefCell<InMemoryExecutor<BytesInput>>> = InMemoryExecutor::new_rr(harness);
let mut engine = DefaultEngine::new();

View File

@ -1,5 +1,7 @@
extern crate alloc;
use core::convert::From;
use crate::inputs::{HasBytesVec, HasTargetBytes, Input};
use crate::AflError;
@ -35,6 +37,18 @@ impl HasTargetBytes for BytesInput {
}
}
impl From<Vec<u8>> for BytesInput {
fn from(bytes: Vec<u8>) -> Self {
Self::new(bytes)
}
}
impl From<&[u8]> for BytesInput {
fn from(bytes: &[u8]) -> Self {
Self::new(bytes.to_owned())
}
}
impl BytesInput {
/// Creates a new bytes input using the given bytes
pub fn new(bytes: Vec<u8>) -> Self {

View File

@ -14,6 +14,7 @@ pub mod utils;
use std::io;
use thiserror::Error;
/// Main error struct for AFL
#[derive(Error, Debug)]
pub enum AflError {
#[error("Error in Serialization: `{0}`")]

View File

@ -281,6 +281,7 @@ where
I: Input + HasBytesVec,
S: ScheduledMutator<C, I>,
{
/// Mutate bytes
fn mutate(&mut self, corpus: &mut C, input: &mut I, stage_idx: i32) -> Result<(), AflError> {
self.scheduled.mutate(corpus, input, stage_idx)
}

View File

@ -109,6 +109,13 @@ impl Rand for Xoshiro256StarRand {
}
}
use std::convert::Into;
impl Into<Rc<RefCell<Self>>> for Xoshiro256StarRand {
fn into(self) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(self))
}
}
impl Xoshiro256StarRand {
/// Creates a new Xoshiro rand with the given seed
pub fn new(seed: u64) -> Self {
@ -117,6 +124,10 @@ impl Xoshiro256StarRand {
ret
}
pub fn to_rc_refcell(self) -> Rc<RefCell<Self>> {
self.into()
}
/// Creates a new Xoshiro rand with the given seed, wrapped in a Rc<RefCell<T>>.
pub fn new_rr(seed: u64) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self::new(seed)))
@ -137,8 +148,8 @@ impl Xoshiro256StarRand {
}
}
#[cfg(test)]
/// fake rand, for testing purposes
#[cfg(test)]
#[derive(Copy, Clone, Debug, Default)]
pub struct XKCDRand {
val: u64,