use PathBuf instead of String for filenames in corpus

This commit is contained in:
Andrea Fioraldi 2020-10-30 12:45:25 +01:00
parent 3653217f16
commit 6366db92bc
2 changed files with 25 additions and 13 deletions

View File

@ -4,6 +4,8 @@ pub use testcase::{Testcase, SimpleTestcase};
use crate::utils::Rand; use crate::utils::Rand;
use crate::AflError; use crate::AflError;
use std::path::PathBuf;
/// Corpus with all current testcases /// Corpus with all current testcases
pub trait Corpus { pub trait Corpus {
/// Returns the number of elements /// Returns the number of elements
@ -24,7 +26,7 @@ pub trait Corpus {
pub struct BaseCorpus<'a, RandT: Rand> { pub struct BaseCorpus<'a, RandT: Rand> {
rand: &'a mut RandT, rand: &'a mut RandT,
entries: Vec<Box<dyn Testcase>>, entries: Vec<Box<dyn Testcase>>,
dir_path: String, dir_path: PathBuf,
} }
impl<RandT: Rand> Corpus for BaseCorpus<'_, RandT> { impl<RandT: Rand> Corpus for BaseCorpus<'_, RandT> {
@ -33,7 +35,13 @@ impl<RandT: Rand> Corpus for BaseCorpus<'_, RandT> {
self.entries.len() self.entries.len()
} }
fn add(&mut self, entry: Box<dyn Testcase>) { fn add(&mut self, mut entry: Box<dyn Testcase>) {
if entry.get_filename() == None {
// TODO walk entry metadatas to ask for pices of filename (e.g. :havoc in AFL)
let filename = &(String::from("id:") + &self.entries.len().to_string());
let filename = self.dir_path.join(filename);
entry.set_filename(filename);
}
self.entries.push(entry); self.entries.push(entry);
} }
@ -67,9 +75,9 @@ impl<RandT: Rand> Corpus for BaseCorpus<'_, RandT> {
} }
impl<RandT: Rand> BaseCorpus<'_, RandT> { impl<RandT: Rand> BaseCorpus<'_, RandT> {
pub fn new<'a>(rand: &'a mut RandT, dir_path: &str) -> BaseCorpus<'a, RandT> { pub fn new<'a>(rand: &'a mut RandT, dir_path: PathBuf) -> BaseCorpus<'a, RandT> {
BaseCorpus { BaseCorpus {
dir_path: dir_path.to_owned(), dir_path: dir_path,
entries: vec![], entries: vec![],
rand: rand, rand: rand,
} }
@ -118,7 +126,7 @@ impl<RandT: Rand> Corpus for QueueCorpus<'_, RandT> {
} }
impl<RandT: Rand> QueueCorpus<'_, RandT> { impl<RandT: Rand> QueueCorpus<'_, RandT> {
pub fn new<'a>(rand: &'a mut RandT, dir_path: &str) -> QueueCorpus<'a, RandT> { pub fn new<'a>(rand: &'a mut RandT, dir_path: PathBuf) -> QueueCorpus<'a, RandT> {
QueueCorpus { QueueCorpus {
base: BaseCorpus::new(rand, dir_path), base: BaseCorpus::new(rand, dir_path),
cycles: 0, cycles: 0,
@ -144,16 +152,19 @@ mod tests {
use crate::inputs::bytes::BytesInput; use crate::inputs::bytes::BytesInput;
use crate::utils::Xoshiro256StarRand; use crate::utils::Xoshiro256StarRand;
use std::path::PathBuf;
#[test] #[test]
fn test_queuecorpus() { fn test_queuecorpus() {
let mut rand = Xoshiro256StarRand::new(); let mut rand = Xoshiro256StarRand::new();
let mut q = QueueCorpus::new(&mut rand, "fancy/path"); let mut q = QueueCorpus::new(&mut rand, PathBuf::from("fancy/path"));
let i = Box::new(BytesInput::new(vec![0; 4])); let i = Box::new(BytesInput::new(vec![0; 4]));
let mut t = Box::new(SimpleTestcase::new(i)); let mut t = Box::new(SimpleTestcase::new(i));
t.set_filename("fancyfile".to_string()); t.set_filename(PathBuf::from("fancyfile"));
q.add(t); q.add(t);
let filename = q.get().unwrap().get_filename().unwrap().to_owned(); let filename = q.get().unwrap().get_filename().unwrap().to_owned();
assert_eq!(filename, q.get().unwrap().get_filename().unwrap().to_owned()); assert_eq!(filename, q.get().unwrap().get_filename().unwrap().to_owned());
assert_eq!(filename, "fancyfile".to_string()); assert_eq!(filename, PathBuf::from("fancyfile"));
} }
} }

View File

@ -2,6 +2,7 @@ use crate::inputs::Input;
use crate::AflError; use crate::AflError;
use hashbrown::HashMap; use hashbrown::HashMap;
use std::path::PathBuf;
pub trait TestcaseMetadata {} pub trait TestcaseMetadata {}
@ -13,9 +14,9 @@ pub trait Testcase {
fn is_on_disk(&self) -> bool; fn is_on_disk(&self) -> bool;
fn get_filename(&self) -> Option<& String>; fn get_filename(&self) -> Option<& PathBuf>;
fn set_filename(&mut self, filename: String); fn set_filename(&mut self, filename: PathBuf);
fn get_metadatas(&mut self) -> &mut HashMap<String, Box<dyn TestcaseMetadata>>; fn get_metadatas(&mut self) -> &mut HashMap<String, Box<dyn TestcaseMetadata>>;
@ -25,7 +26,7 @@ pub trait Testcase {
pub struct SimpleTestcase { pub struct SimpleTestcase {
input: Option<Box<dyn Input>>, input: Option<Box<dyn Input>>,
// is_on_disk: bool, // not needed, look at the Option // is_on_disk: bool, // not needed, look at the Option
filename: Option<String>, filename: Option<PathBuf>,
metadatas: HashMap<String, Box<dyn TestcaseMetadata>>, metadatas: HashMap<String, Box<dyn TestcaseMetadata>>,
} }
@ -43,11 +44,11 @@ impl Testcase for SimpleTestcase {
!self.input.is_some() && self.filename.is_some() !self.input.is_some() && self.filename.is_some()
} }
fn get_filename(&self) -> Option<& String> { fn get_filename(&self) -> Option<& PathBuf> {
self.filename.as_ref() self.filename.as_ref()
} }
fn set_filename(&mut self, filename: String) { fn set_filename(&mut self, filename: PathBuf) {
self.filename = Some(filename) self.filename = Some(filename)
} }