Skip to content

Commit be74774

Browse files
authored
Use Arc<()> to ref-counting InputFile (#3240)
* clean lib_bolts::fs * clippy * avoid racy
1 parent 7a9f46b commit be74774

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

libafl_bolts/src/fs.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
//! `LibAFL` functionality for filesystem interaction
22
3-
#[cfg(feature = "std")]
4-
use alloc::{borrow::ToOwned, vec::Vec};
5-
use alloc::{rc::Rc, string::String};
6-
use core::cell::RefCell;
7-
#[cfg(feature = "std")]
8-
use core::time::Duration;
93
#[cfg(unix)]
104
use std::os::unix::prelude::{AsRawFd, RawFd};
11-
#[cfg(feature = "std")]
5+
6+
use alloc::string::String;
7+
use alloc::sync::Arc;
8+
use alloc::{borrow::ToOwned, vec::Vec};
9+
use core::time::Duration;
10+
11+
use crate::Error;
1212
use std::time::SystemTime;
1313
use std::{
1414
fs::{self, File, OpenOptions, remove_file},
1515
io::{Seek, Write},
1616
path::{Path, PathBuf},
1717
};
1818

19-
use crate::Error;
20-
2119
/// The default filename to use to deliver testcases to the target
2220
pub const INPUTFILE_STD: &str = ".cur_input";
2321

@@ -62,7 +60,6 @@ where
6260

6361
/// An [`InputFile`] to write fuzzer input to.
6462
/// The target/forkserver will read from this file.
65-
#[cfg(feature = "std")]
6663
#[derive(Debug)]
6764
pub struct InputFile {
6865
/// The filename/path too this [`InputFile`]
@@ -71,7 +68,7 @@ pub struct InputFile {
7168
pub file: File,
7269
/// The ref count for this [`InputFile`].
7370
/// Once it reaches 0, the underlying [`File`] will be removed.
74-
pub rc: Rc<RefCell<usize>>,
71+
pub rc: Arc<()>,
7572
}
7673

7774
impl Eq for InputFile {}
@@ -84,11 +81,6 @@ impl PartialEq for InputFile {
8481

8582
impl Clone for InputFile {
8683
fn clone(&self) -> Self {
87-
{
88-
let mut rc = self.rc.borrow_mut();
89-
assert_ne!(*rc, usize::MAX, "InputFile rc overflow");
90-
*rc += 1;
91-
}
9284
Self {
9385
path: self.path.clone(),
9486
file: self.file.try_clone().unwrap(),
@@ -113,7 +105,7 @@ impl InputFile {
113105
Ok(Self {
114106
path: filename.as_ref().to_owned(),
115107
file: f,
116-
rc: Rc::new(RefCell::new(1)),
108+
rc: Arc::new(()),
117109
})
118110
}
119111

@@ -147,7 +139,6 @@ impl InputFile {
147139
/// Finds new files in the given directory, taking the last time we looked at this path as parameter.
148140
/// This method works recursively.
149141
/// If `last` is `None`, it'll load all file.
150-
#[cfg(feature = "std")]
151142
pub fn find_new_files_rec<P: AsRef<Path>>(
152143
dir: P,
153144
last_check: &Option<Duration>,
@@ -182,13 +173,9 @@ pub fn find_new_files_rec<P: AsRef<Path>>(
182173
Ok(new_files)
183174
}
184175

185-
#[cfg(feature = "std")]
186176
impl Drop for InputFile {
187177
fn drop(&mut self) {
188-
let mut rc = self.rc.borrow_mut();
189-
assert_ne!(*rc, 0, "InputFile rc should never be 0");
190-
*rc -= 1;
191-
if *rc == 0 {
178+
if Arc::into_inner(core::mem::take(&mut self.rc)).is_some() {
192179
// try to remove the file, but ignore errors
193180
drop(remove_file(&self.path));
194181
}

0 commit comments

Comments
 (0)