Skip to content

Commit

Permalink
Add file descriptor logger (#1742)
Browse files Browse the repository at this point in the history
* Add file descriptor logger

* clippy

* Fix closing fds

* fix no_std, fmt

---------

Co-authored-by: Dongjia "toka" Zhang <tokazerkje@outlook.com>
  • Loading branch information
domenukk and tokatoka committed Dec 20, 2023
1 parent 57a64e8 commit 68e7b20
Showing 1 changed file with 106 additions and 25 deletions.
131 changes: 106 additions & 25 deletions libafl_bolts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ pub mod staterestore;
#[cfg(any(feature = "xxh3", feature = "alloc"))]
pub mod tuples;

/// The purpose of this module is to alleviate imports of the bolts by adding a glob import.
#[cfg(feature = "prelude")]
pub mod bolts_prelude {
#[cfg(feature = "std")]
pub use super::build_id::*;
#[cfg(all(
any(feature = "cli", feature = "frida_cli", feature = "qemu_cli"),
feature = "std"
))]
pub use super::cli::*;
#[cfg(feature = "gzip")]
pub use super::compress::*;
#[cfg(feature = "std")]
pub use super::core_affinity::*;
#[cfg(feature = "std")]
pub use super::fs::*;
#[cfg(all(feature = "std", unix))]
pub use super::minibsod::*;
#[cfg(feature = "std")]
pub use super::staterestore::*;
#[cfg(feature = "alloc")]
pub use super::{anymap::*, llmp::*, ownedref::*, rands::*, serdeany::*, shmem::*, tuples::*};
pub use super::{cpu::*, os::*};
}

#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(all(not(feature = "xxh3"), feature = "alloc"))]
Expand All @@ -142,6 +167,13 @@ use core::hash::BuildHasher;
use core::hash::Hasher;
#[cfg(feature = "std")]
use std::time::{SystemTime, UNIX_EPOCH};
#[cfg(all(unix, feature = "std"))]
use std::{
fs::File,
io::Write,
mem,
os::fd::{FromRawFd, RawFd},
};

// There's a bug in ahash that doesn't let it build in `alloc` without once_cell right now.
// TODO: re-enable once <https://github.com/tkaitchuck/aHash/issues/155> is resolved.
Expand Down Expand Up @@ -777,7 +809,7 @@ impl Default for SimpleStdoutLogger {

#[cfg(feature = "std")]
impl SimpleStdoutLogger {
/// Create a new [`log::Log`] logger that will wrte log to stdout
/// Create a new [`log::Log`] logger that will write log to stdout
#[must_use]
pub const fn new() -> Self {
Self {}
Expand Down Expand Up @@ -823,7 +855,7 @@ impl Default for SimpleStderrLogger {

#[cfg(feature = "std")]
impl SimpleStderrLogger {
/// Create a new [`log::Log`] logger that will wrte log to stdout
/// Create a new [`log::Log`] logger that will write log to stdout
#[must_use]
pub const fn new() -> Self {
Self {}
Expand Down Expand Up @@ -854,29 +886,55 @@ impl log::Log for SimpleStderrLogger {

fn flush(&self) {}
}
/// The purpose of this module is to alleviate imports of the bolts by adding a glob import.
#[cfg(feature = "prelude")]
pub mod bolts_prelude {
#[cfg(feature = "std")]
pub use super::build_id::*;
#[cfg(all(
any(feature = "cli", feature = "frida_cli", feature = "qemu_cli"),
feature = "std"
))]
pub use super::cli::*;
#[cfg(feature = "gzip")]
pub use super::compress::*;
#[cfg(feature = "std")]
pub use super::core_affinity::*;
#[cfg(feature = "std")]
pub use super::fs::*;
#[cfg(all(feature = "std", unix))]
pub use super::minibsod::*;
#[cfg(feature = "std")]
pub use super::staterestore::*;
#[cfg(feature = "alloc")]
pub use super::{anymap::*, llmp::*, ownedref::*, rands::*, serdeany::*, shmem::*, tuples::*};
pub use super::{cpu::*, os::*};

/// A simple logger struct that logs to a `RawFd` when used with [`log::set_logger`].
#[derive(Debug)]
#[cfg(all(feature = "std", unix))]
pub struct SimpleFdLogger {
fd: RawFd,
}

#[cfg(all(feature = "std", unix))]
impl SimpleFdLogger {
/// Create a new [`log::Log`] logger that will write the log to the given `fd`
///
/// # Safety
/// Needs a valid raw file descriptor opened for writing.
#[must_use]
pub const unsafe fn new(fd: RawFd) -> Self {
Self { fd }
}

/// Sets the `fd` this logger will write to
///
/// # Safety
/// Needs a valid raw file descriptor opened for writing.
pub unsafe fn set_fd(&mut self, fd: RawFd) {
self.fd = fd;
}
}

#[cfg(all(feature = "std", unix))]
impl log::Log for SimpleFdLogger {
#[inline]
fn enabled(&self, _metadata: &Metadata) -> bool {
true
}

fn log(&self, record: &Record) {
let mut f = unsafe { File::from_raw_fd(self.fd) };
writeln!(
f,
"[{:?}] {}: {}",
current_time(),
record.level(),
record.args()
)
.unwrap_or_else(|err| println!("Failed to log to fd {}: {err}", self.fd));
mem::forget(f);
}

fn flush(&self) {}
}

#[cfg(feature = "python")]
Expand Down Expand Up @@ -1020,3 +1078,26 @@ pub mod pybind {
Ok(())
}
}

#[cfg(test)]
mod tests {

#[cfg(all(feature = "std", unix))]
use crate::SimpleFdLogger;

#[cfg(all(feature = "std", unix))]
pub static mut LOGGER: SimpleFdLogger = unsafe { SimpleFdLogger::new(1) };

#[test]
#[cfg(all(unix, feature = "std"))]
fn test_logger() {
use std::{io::stdout, os::fd::AsRawFd};

unsafe { LOGGER.fd = stdout().as_raw_fd() };
unsafe {
log::set_logger(&LOGGER).unwrap();
}
log::set_max_level(log::LevelFilter::Debug);
log::info!("Test");
}
}

0 comments on commit 68e7b20

Please sign in to comment.