Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix testcase set_filename #1092

Merged
merged 18 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions libafl/src/corpus/inmemory_ondisk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,23 +199,25 @@ where
let mut ctr = 2;
let filename = loop {
let lockfile = format!(".{file}.lafl_lock");
// try to create lockfile.

if OpenOptions::new()
.write(true)
.create_new(true)
.open(self.dir_path.join(lockfile))
.is_ok()
{
break self.dir_path.join(file);
break file;
}

file = format!("{file_orig}-{ctr}");
ctr += 1;
};

let filename_str = filename.to_str().expect("Invalid Path");
testcase.set_filename(filename_str.into());
let file_path = self.dir_path.join(filename.clone());
let filename_str = file_path.to_str().expect("Invalid Path");
testcase.set_filename(filename_str.into())?;

fs::remove_file(format!(".{filename}.lafl_lock"))?;
};
if self.meta_format.is_some() {
let mut filename = PathBuf::from(testcase.filename().as_ref().unwrap());
Expand Down
50 changes: 49 additions & 1 deletion libafl/src/corpus/testcase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use alloc::string::String;
use core::{default::Default, option::Option, time::Duration};

#[cfg(feature = "std")]
use std::fs;

use serde::{Deserialize, Serialize};

use crate::{
Expand Down Expand Up @@ -124,8 +127,53 @@ where

/// Set the filename
#[inline]
pub fn set_filename(&mut self, filename: String) {
#[cfg(feature = "std")]
pub fn set_filename(&mut self, filename: String) -> Result<(), Error> {
use std::fs::OpenOptions;

if self.filename.is_some() {
let f = self.filename.clone().unwrap();
let old_filename = f.as_str();

let new_filename = filename.as_str();

// Do operations below when new filename is specified
if old_filename.eq(new_filename) {
return Ok(());
}

let new_lock_filename = format!(".{new_filename}.lafl_lock");

// Try to create lock file for new testcases
if OpenOptions::new()
.create(true)
.write(true)
.open(&new_lock_filename)
.is_err()
{
return Err(Error::illegal_state(
"unable to create lock file for new testcase",
));
}

fs::rename(old_filename, new_filename)?;

let old_metadata_filename = format!(".{old_filename}.metadata");
let new_metadata_filename = format!(".{new_filename}.metadata");
fs::rename(old_metadata_filename, new_metadata_filename)?;

fs::remove_file(&new_lock_filename)?;
}

self.filename = Some(filename);
Ok(())
}

#[inline]
#[cfg(feature = "no_std")]
pub fn set_filename(&mut self, filename: String) -> Result<(), Error> {
self.filename = Some(filename);
Ok(())
}

/// Get the execution time of the testcase
Expand Down