From 3124d036654eed0e44e6e574cf825c5c0a2abdcb Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Thu, 22 Apr 2021 15:55:13 +0200 Subject: [PATCH] bytesinput to_file raw --- libafl/src/events/simple.rs | 16 ++++++++++++---- libafl/src/inputs/bytes.rs | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/libafl/src/events/simple.rs b/libafl/src/events/simple.rs index 9b2a1ed022..89e9ae23b0 100644 --- a/libafl/src/events/simple.rs +++ b/libafl/src/events/simple.rs @@ -84,8 +84,12 @@ where time, executions, } => { - stats.client_stats_mut_for(0).update_corpus_size(*corpus_size as u64); - stats.client_stats_mut_for(0).update_executions(*executions as u64, *time); + stats + .client_stats_mut_for(0) + .update_corpus_size(*corpus_size as u64); + stats + .client_stats_mut_for(0) + .update_executions(*executions as u64, *time); stats.display(event.name().to_string()); Ok(BrokerEventResult::Handled) } @@ -95,12 +99,16 @@ where phantom: _, } => { // TODO: The stats buffer should be added on client add. - stats.client_stats_mut_for(0).update_executions(*executions as u64, *time); + stats + .client_stats_mut_for(0) + .update_executions(*executions as u64, *time); stats.display(event.name().to_string()); Ok(BrokerEventResult::Handled) } Event::Objective { objective_size } => { - stats.client_stats_mut_for(0).update_objective_size(*objective_size as u64); + stats + .client_stats_mut_for(0) + .update_objective_size(*objective_size as u64); stats.display(event.name().to_string()); Ok(BrokerEventResult::Handled) } diff --git a/libafl/src/inputs/bytes.rs b/libafl/src/inputs/bytes.rs index 7fe08a105c..7ceb24a9d9 100644 --- a/libafl/src/inputs/bytes.rs +++ b/libafl/src/inputs/bytes.rs @@ -4,8 +4,17 @@ use alloc::{borrow::ToOwned, rc::Rc, vec::Vec}; use core::{cell::RefCell, convert::From}; use serde::{Deserialize, Serialize}; +#[cfg(feature = "std")] +use std::{ + fs::File, + io::{Read, Write}, + path::Path, +}; -use crate::inputs::{HasBytesVec, HasLen, HasTargetBytes, Input, TargetBytes}; +use crate::{ + inputs::{HasBytesVec, HasLen, HasTargetBytes, Input, TargetBytes}, + Error, +}; /// A bytes input is the basic input #[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)] @@ -14,7 +23,30 @@ pub struct BytesInput { bytes: Vec, } -impl Input for BytesInput {} +impl Input for BytesInput { + #[cfg(feature = "std")] + /// Write this input to the file + fn to_file

(&self, path: P) -> Result<(), Error> + where + P: AsRef, + { + let mut file = File::create(path)?; + file.write_all(&self.bytes)?; + Ok(()) + } + + /// Load the contents of this input from a file + #[cfg(feature = "std")] + fn from_file

(path: P) -> Result + where + P: AsRef, + { + let mut file = File::open(path)?; + let mut bytes: Vec = vec![]; + file.read_to_end(&mut bytes)?; + Ok(BytesInput::new(bytes)) + } +} /// Rc Ref-cell from Input impl From for Rc> {