Skip to content

Commit

Permalink
Expose find_new_files_rec in libafl_bolts::fs (AFLplusplus#2404)
Browse files Browse the repository at this point in the history
  • Loading branch information
domenukk authored Jul 16, 2024
1 parent dc93f6c commit 4370a84
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 49 deletions.
55 changes: 8 additions & 47 deletions libafl/src/stages/sync.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//! The [`SyncFromDiskStage`] is a stage that imports inputs from disk for e.g. sync with AFL
use alloc::borrow::{Cow, ToOwned};
use core::{marker::PhantomData, time::Duration};
use std::{
fs,
path::{Path, PathBuf},
time::SystemTime,
use alloc::{
borrow::{Cow, ToOwned},
vec::Vec,
};
use core::{marker::PhantomData, time::Duration};
use std::path::{Path, PathBuf};

use libafl_bolts::{current_time, shmem::ShMemProvider, Named};
use libafl_bolts::{current_time, fs::find_new_files_rec, shmem::ShMemProvider, Named};
use serde::{Deserialize, Serialize};

#[cfg(feature = "introspection")]
Expand All @@ -25,6 +23,9 @@ use crate::{
Error, HasMetadata, HasNamedMetadata,
};

/// Default name for `SyncFromDiskStage`; derived from AFL++
pub const SYNC_FROM_DISK_STAGE_NAME: &str = "sync";

/// Metadata used to store information about disk sync time
#[cfg_attr(
any(not(feature = "serdeany_autoreg"), miri),
Expand All @@ -51,46 +52,6 @@ impl SyncFromDiskMetadata {
}
}

/// Default name for `SyncFromDiskStage`; derived from AFL++
pub const SYNC_FROM_DISK_STAGE_NAME: &str = "sync";

/// Finds new files in the given directory, taking the last time we looked at this path as parameter.
/// This method works recursively.
/// If `last` is `None`, it'll load all file.
fn find_new_files_rec<P: AsRef<Path>>(
dir: P,
last_check: &Option<Duration>,
) -> Result<Vec<PathBuf>, Error> {
let mut new_files = Vec::<PathBuf>::new();
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
let attributes = fs::metadata(&path);

if attributes.is_err() {
continue;
}

let attr = attributes?;

if attr.is_file() && attr.len() > 0 {
if let Ok(time) = attr.modified() {
if let Some(last_check) = last_check {
if time.duration_since(SystemTime::UNIX_EPOCH).unwrap() < *last_check {
continue;
}
}
new_files.push(path.clone());
}
} else if attr.is_dir() {
let dir_left_to_sync = find_new_files_rec(&entry.path(), last_check)?;
new_files.extend(dir_left_to_sync);
}
}

Ok(new_files)
}

/// A stage that loads testcases from disk to sync with other fuzzers such as AFL++
#[derive(Debug)]
pub struct SyncFromDiskStage<CB, E, EM, Z> {
Expand Down
46 changes: 44 additions & 2 deletions libafl_bolts/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//! `LibAFL` functionality for filesystem interaction
#[cfg(feature = "std")]
use alloc::borrow::ToOwned;
use alloc::rc::Rc;
#[cfg(feature = "std")]
use alloc::{borrow::ToOwned, vec::Vec};
use core::cell::RefCell;
#[cfg(feature = "std")]
use core::time::Duration;
#[cfg(unix)]
use std::os::unix::prelude::{AsRawFd, RawFd};
#[cfg(feature = "std")]
use std::time::SystemTime;
use std::{
fs::{self, remove_file, File, OpenOptions},
io::{Seek, Write},
Expand Down Expand Up @@ -140,6 +144,44 @@ impl InputFile {
}
}

/// Finds new files in the given directory, taking the last time we looked at this path as parameter.
/// This method works recursively.
/// If `last` is `None`, it'll load all file.
#[cfg(feature = "std")]
pub fn find_new_files_rec<P: AsRef<Path>>(
dir: P,
last_check: &Option<Duration>,
) -> Result<Vec<PathBuf>, Error> {
let mut new_files = Vec::<PathBuf>::new();
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
let attributes = fs::metadata(&path);

if attributes.is_err() {
continue;
}

let attr = attributes?;

if attr.is_file() && attr.len() > 0 {
if let Ok(time) = attr.modified() {
if let Some(last_check) = last_check {
if time.duration_since(SystemTime::UNIX_EPOCH).unwrap() < *last_check {
continue;
}
}
new_files.push(path.clone());
}
} else if attr.is_dir() {
let dir_left_to_sync = find_new_files_rec(entry.path(), last_check)?;
new_files.extend(dir_left_to_sync);
}
}

Ok(new_files)
}

#[cfg(feature = "std")]
impl Drop for InputFile {
fn drop(&mut self) {
Expand Down

0 comments on commit 4370a84

Please sign in to comment.