diff --git a/libafl/src/executors/forkserver.rs b/libafl/src/executors/forkserver.rs index e4e1ef187a..666fca4194 100644 --- a/libafl/src/executors/forkserver.rs +++ b/libafl/src/executors/forkserver.rs @@ -488,7 +488,7 @@ impl Forkserver { let st_read = unsafe { BorrowedFd::borrow_raw(st_read) }; let mut readfds = FdSet::new(); - readfds.insert(&st_read); + readfds.insert(st_read); // We'll pass a copied timeout to keep the original timeout intact, because select updates timeout to indicate how much time was left. See select(2) let sret = pselect( Some(readfds.highest().unwrap().as_raw_fd() + 1), diff --git a/libafl/src/lib.rs b/libafl/src/lib.rs index 8bab35bdc9..56de8465e0 100644 --- a/libafl/src/lib.rs +++ b/libafl/src/lib.rs @@ -88,13 +88,6 @@ pub extern crate alloc; #[allow(unused_imports)] #[macro_use] extern crate libafl_derive; -/// Dummy export that will warn with a deprecation note on usage. -/// Use the `libafl_bolts` crate instead. -#[deprecated( - since = "0.11.0", - note = "All LibAFL bolts have moved to the libafl_bolts crate." -)] -pub mod bolts {} #[cfg(feature = "derive")] #[doc(hidden)] pub use libafl_derive::*; diff --git a/libafl_bolts/src/core_affinity.rs b/libafl_bolts/src/core_affinity.rs index 0614876665..b756854c8f 100644 --- a/libafl_bolts/src/core_affinity.rs +++ b/libafl_bolts/src/core_affinity.rs @@ -203,16 +203,6 @@ impl TryFrom<&str> for Cores { } } -/// Parses core binding args from user input. -/// Returns a Vec of CPU IDs. -/// * `./fuzzer --cores 1,2-4,6`: clients run in cores 1,2,3,4,6 -/// * `./fuzzer --cores all`: one client runs on each available core -#[cfg(feature = "std")] -#[deprecated(since = "0.8.1", note = "Use Cores::from_cmdline instead")] -pub fn parse_core_bind_arg(args: &str) -> Result, Error> { - Ok(Cores::from_cmdline(args)?.ids.iter().map(|x| x.0).collect()) -} - // Linux Section #[cfg(any( diff --git a/libafl_bolts/src/lib.rs b/libafl_bolts/src/lib.rs index d563a6b29d..1a9efaba89 100644 --- a/libafl_bolts/src/lib.rs +++ b/libafl_bolts/src/lib.rs @@ -199,17 +199,6 @@ use xxhash_rust::xxh3::xxh3_64; )] pub struct ClientId(pub u32); -#[cfg(feature = "std")] -use log::{Metadata, Record}; - -#[deprecated( - since = "0.11.0", - note = "The launcher module has moved out of `libafl_bolts` into `libafl::events::launcher`." -)] -/// Dummy module informing potential users that the launcher module has moved -/// out of `libafl_bolts` into `libafl::events::launcher`. -pub mod launcher {} - use core::{ array::TryFromSliceError, fmt::{self, Display}, @@ -222,6 +211,8 @@ use std::{env::VarError, io}; #[cfg(feature = "libafl_derive")] pub use libafl_derive::SerdeAny; +#[cfg(feature = "std")] +use log::{Metadata, Record}; #[cfg(feature = "alloc")] use { alloc::string::{FromUtf8Error, String}, diff --git a/libafl_bolts/src/os/mod.rs b/libafl_bolts/src/os/mod.rs index 03b27da8e1..5160bbb50b 100644 --- a/libafl_bolts/src/os/mod.rs +++ b/libafl_bolts/src/os/mod.rs @@ -12,7 +12,7 @@ pub mod unix_signals; #[cfg(unix)] pub use unix_signals::CTRL_C_EXIT; -#[cfg(all(unix, feature = "std"))] +#[cfg(all(unix, feature = "alloc"))] pub mod pipes; #[cfg(all(unix, feature = "std"))] diff --git a/libafl_bolts/src/os/pipes.rs b/libafl_bolts/src/os/pipes.rs index 65ab9ec731..2a48c03501 100644 --- a/libafl_bolts/src/os/pipes.rs +++ b/libafl_bolts/src/os/pipes.rs @@ -1,4 +1,7 @@ //! Unix `pipe` wrapper for `LibAFL` +use alloc::rc::Rc; +use core::{borrow::Borrow, cell::RefCell}; +use std::os::fd::{AsFd, AsRawFd, OwnedFd}; #[cfg(feature = "std")] use std::{ io::{self, ErrorKind, Read, Write}, @@ -6,7 +9,7 @@ use std::{ }; #[cfg(feature = "std")] -use nix::unistd::{close, pipe, read, write}; +use nix::unistd::{pipe, read, write}; use crate::Error; @@ -15,9 +18,9 @@ use crate::Error; #[derive(Debug, Clone)] pub struct Pipe { /// The read end of the pipe - read_end: Option, + read_end: Option>>, /// The write end of the pipe - write_end: Option, + write_end: Option>>, } #[cfg(feature = "std")] @@ -26,37 +29,39 @@ impl Pipe { pub fn new() -> Result { let (read_end, write_end) = pipe()?; Ok(Self { - read_end: Some(read_end), - write_end: Some(write_end), + read_end: Some(Rc::new(RefCell::new(read_end))), + write_end: Some(Rc::new(RefCell::new(write_end))), }) } /// Close the read end of a pipe pub fn close_read_end(&mut self) { - if let Some(read_end) = self.read_end { - let _: Result<(), nix::errno::Errno> = close(read_end); - self.read_end = None; - } + // `OwnedFd` closes on Drop + self.read_end = None; } /// Close the write end of a pipe pub fn close_write_end(&mut self) { - if let Some(write_end) = self.write_end { - let _: Result<(), nix::errno::Errno> = close(write_end); - self.write_end = None; - } + // `OwnedFd` closes on Drop + self.write_end = None; } /// The read end #[must_use] pub fn read_end(&self) -> Option { - self.read_end + self.read_end.as_ref().map(|fd| { + let borrowed: &RefCell = fd.borrow(); + borrowed.borrow().as_raw_fd() + }) } /// The write end #[must_use] pub fn write_end(&self) -> Option { - self.write_end + self.write_end.as_ref().map(|fd| { + let borrowed: &RefCell = fd.borrow(); + borrowed.borrow().as_raw_fd() + }) } } @@ -64,7 +69,7 @@ impl Pipe { impl Read for Pipe { /// Reads a few bytes fn read(&mut self, buf: &mut [u8]) -> Result { - match self.read_end { + match self.read_end() { Some(read_end) => match read(read_end, buf) { Ok(res) => Ok(res), Err(e) => Err(io::Error::from_raw_os_error(e as i32)), @@ -81,11 +86,14 @@ impl Read for Pipe { impl Write for Pipe { /// Writes a few bytes fn write(&mut self, buf: &[u8]) -> Result { - match self.write_end { - Some(write_end) => match write(write_end, buf) { - Ok(res) => Ok(res), - Err(e) => Err(io::Error::from_raw_os_error(e as i32)), - }, + match self.write_end.as_ref() { + Some(write_end) => { + let borrowed: &RefCell = write_end; + match write((*borrowed).borrow().as_fd(), buf) { + Ok(res) => Ok(res), + Err(e) => Err(io::Error::from_raw_os_error(e as i32)), + } + } None => Err(io::Error::new( ErrorKind::BrokenPipe, "Write pipe end was already closed", @@ -97,15 +105,3 @@ impl Write for Pipe { Ok(()) } } - -#[cfg(feature = "std")] -impl Drop for Pipe { - fn drop(&mut self) { - if let Some(read_end) = self.read_end { - let _: Result<(), nix::errno::Errno> = close(read_end); - } - if let Some(write_end) = self.write_end { - let _: Result<(), nix::errno::Errno> = close(write_end); - } - } -} diff --git a/libafl_bolts/src/os/unix_shmem_server.rs b/libafl_bolts/src/os/unix_shmem_server.rs index 38780a27fc..77f0a6208f 100644 --- a/libafl_bolts/src/os/unix_shmem_server.rs +++ b/libafl_bolts/src/os/unix_shmem_server.rs @@ -22,6 +22,7 @@ use std::{ env, io::{Read, Write}, marker::PhantomData, + os::fd::{AsFd, BorrowedFd}, rc::{Rc, Weak}, sync::{Arc, Condvar, Mutex}, thread::JoinHandle, @@ -36,6 +37,7 @@ use std::{ }; use hashbrown::HashMap; +use nix::poll::PollTimeout; #[cfg(all(feature = "std", unix))] use nix::poll::{poll, PollFd, PollFlags}; use serde::{Deserialize, Serialize}; @@ -672,7 +674,7 @@ where }; let mut poll_fds: Vec = vec![PollFd::new( - &listener, + listener.as_fd(), PollFlags::POLLIN | PollFlags::POLLRDNORM | PollFlags::POLLRDBAND, )]; @@ -681,7 +683,7 @@ where cvar.notify_one(); loop { - match poll(&mut poll_fds, -1) { + match poll(&mut poll_fds, PollTimeout::NONE) { Ok(num_fds) if num_fds > 0 => (), Ok(_) => continue, Err(e) => { @@ -718,11 +720,10 @@ where let pollfd = PollFd::new( // # Safety - // This cast will make `PollFd::new` ignore the lifetime of our stream. + // Going through a raw fd will make `PollFd::new` ignore the lifetime of our stream. // As of nix 0.27, the `PollFd` is safer, in that it checks the lifetime of the given stream. - // We did not develop this server with that new constraint in mind, but it is upheld. - // The `new` function then gets the `raw_fd` from this stream, and operate on that int internally. - unsafe { &*(&stream as *const _) }, + // We did not develop this server with that new constraint in mind, but it is upheld by our code. + unsafe { BorrowedFd::borrow_raw(stream.as_raw_fd()) }, PollFlags::POLLIN | PollFlags::POLLRDNORM | PollFlags::POLLRDBAND, ); diff --git a/libafl_bolts/src/os/unix_signals.rs b/libafl_bolts/src/os/unix_signals.rs index ed233047f0..58032c0044 100644 --- a/libafl_bolts/src/os/unix_signals.rs +++ b/libafl_bolts/src/os/unix_signals.rs @@ -18,7 +18,7 @@ use std::ffi::CString; #[cfg(target_arch = "arm")] pub use libc::c_ulong; #[cfg(feature = "std")] -use nix::errno::{errno, Errno}; +use nix::errno::Errno; /// The special exit code when the target exited through ctrl-c pub const CTRL_C_EXIT: i32 = 100; @@ -518,7 +518,7 @@ pub fn ucontext() -> Result { #[cfg(feature = "std")] Err(Error::unknown(format!( "Failed to get ucontext: {:?}", - Errno::from_i32(errno()) + Errno::last() ))) } } else { diff --git a/libafl_bolts/src/tuples.rs b/libafl_bolts/src/tuples.rs index 31593b781f..282124111e 100644 --- a/libafl_bolts/src/tuples.rs +++ b/libafl_bolts/src/tuples.rs @@ -522,6 +522,15 @@ pub struct Handle { #[cfg(feature = "alloc")] impl Handle { + /// Create a new [`Handle`] with the given name. + #[must_use] + pub fn new(name: Cow<'static, str>) -> Self { + Self { + name, + phantom: PhantomData, + } + } + /// Fetch the name of the referenced instance. /// /// We explicitly do *not* implement [`Named`], as this could potentially lead to confusion