From b2f9e23975d8952db48d6e6775cb90b0b0bcc152 Mon Sep 17 00:00:00 2001 From: "Dongjia \"toka\" Zhang" Date: Sat, 29 Apr 2023 23:42:51 +0200 Subject: [PATCH] Fix double crash for solutions with the same filename (#1232) (#1236) * fix * mre * why delete it??? * fmt * clp * comment --- libafl/.fancyfile.metadata | 7 ------- libafl/src/corpus/inmemory_ondisk.rs | 10 +++++----- libafl/src/inputs/mod.rs | 2 +- libafl/src/schedulers/queue.rs | 2 +- 4 files changed, 7 insertions(+), 14 deletions(-) delete mode 100644 libafl/.fancyfile.metadata diff --git a/libafl/.fancyfile.metadata b/libafl/.fancyfile.metadata deleted file mode 100644 index a81ca655e0..0000000000 --- a/libafl/.fancyfile.metadata +++ /dev/null @@ -1,7 +0,0 @@ -{ - "metadata": { - "map": {} - }, - "exec_time": null, - "executions": 0 -} \ No newline at end of file diff --git a/libafl/src/corpus/inmemory_ondisk.rs b/libafl/src/corpus/inmemory_ondisk.rs index 819f4bbc6a..b6353e3bb3 100644 --- a/libafl/src/corpus/inmemory_ondisk.rs +++ b/libafl/src/corpus/inmemory_ondisk.rs @@ -293,6 +293,7 @@ where *testcase.metadata_path_mut() = new_metadata_path; *testcase.filename_mut() = Some(new_filename); *testcase.file_path_mut() = Some(new_file_path); + Ok(()) } else { Err(Error::illegal_argument( @@ -304,6 +305,7 @@ where fn save_testcase(&self, testcase: &mut Testcase, idx: CorpusId) -> Result<(), Error> { let file_name_orig = testcase.filename_mut().take().unwrap_or_else(|| { // TODO walk entry metadata to ask for pieces of filename (e.g. :havoc in AFL) + testcase.input().as_ref().unwrap().generate_name(idx.0) }); if testcase.file_path().is_some() { @@ -314,17 +316,17 @@ where let mut file_name = file_name_orig.clone(); let mut ctr = 2; - let (file_name, lockfile_path) = loop { + let file_name = loop { let lockfile_name = format!(".{file_name}.lafl_lock"); let lockfile_path = self.dir_path.join(lockfile_name); if OpenOptions::new() .write(true) .create_new(true) - .open(&lockfile_path) + .open(lockfile_path) .is_ok() { - break (file_name, lockfile_path); + break file_name; } file_name = format!("{file_name_orig}-{ctr}"); @@ -333,8 +335,6 @@ where *testcase.file_path_mut() = Some(self.dir_path.join(&file_name)); *testcase.filename_mut() = Some(file_name); - - fs::remove_file(lockfile_path)?; } if self.meta_format.is_some() { diff --git a/libafl/src/inputs/mod.rs b/libafl/src/inputs/mod.rs index bdbed10399..80ba4c33ff 100644 --- a/libafl/src/inputs/mod.rs +++ b/libafl/src/inputs/mod.rs @@ -73,7 +73,7 @@ pub trait Input: Clone + Serialize + serde::de::DeserializeOwned + Debug { Ok(postcard::from_bytes(&bytes)?) } - /// Generate a name for this input + /// Generate a name for this input, the user is responsible for making each name of testcase unique. fn generate_name(&self, idx: usize) -> String; /// An hook executed if the input is stored as `Testcase` diff --git a/libafl/src/schedulers/queue.rs b/libafl/src/schedulers/queue.rs index 7de18e4e12..9e2ebaee0e 100644 --- a/libafl/src/schedulers/queue.rs +++ b/libafl/src/schedulers/queue.rs @@ -132,6 +132,6 @@ mod tests { assert_eq!(filename, "fancyfile"); - fs::remove_dir_all("target/.test/fancy").unwrap(); + fs::remove_dir_all("target/.test/fancy/path").unwrap(); } }