From 8ec8be1ce5ed5894b345bed4647820518925b728 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Mon, 8 Nov 2021 02:53:53 +0100 Subject: [PATCH] make dump_registers method public (#367) * make dump_registers method public * be smart about getting the ucontext * more docu --- libafl/src/bolts/minibsod.rs | 15 +++++++++++---- libafl/src/bolts/os/unix_signals.rs | 10 ++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/libafl/src/bolts/minibsod.rs b/libafl/src/bolts/minibsod.rs index 44c28970d2..04b655de53 100644 --- a/libafl/src/bolts/minibsod.rs +++ b/libafl/src/bolts/minibsod.rs @@ -1,12 +1,16 @@ //! Implements a mini-bsod generator +//! It dumps all important registers and prints a stacktrace. +//! You may use the [`crate::bolts::os::unix_signals::ucontext`] +//! function to get a [`ucontext_t`]. use libc::siginfo_t; use std::io::{BufWriter, Write}; use crate::bolts::os::unix_signals::{ucontext_t, Signal}; +/// Write the contens of all important registers #[cfg(all(target_os = "linux", target_arch = "x86_64"))] -fn dump_registers( +pub fn dump_registers( writer: &mut BufWriter, ucontext: &ucontext_t, ) -> Result<(), std::io::Error> { @@ -39,11 +43,12 @@ fn dump_registers( Ok(()) } +/// Write the contens of all important registers #[cfg(all( any(target_os = "linux", target_os = "android"), target_arch = "aarch64" ))] -fn dump_registers( +pub fn dump_registers( writer: &mut BufWriter, ucontext: &ucontext_t, ) -> Result<(), std::io::Error> { @@ -62,8 +67,9 @@ fn dump_registers( Ok(()) } +/// Write the contens of all important registers #[cfg(all(target_vendor = "apple", target_arch = "aarch64"))] -fn dump_registers( +pub fn dump_registers( writer: &mut BufWriter, ucontext: &ucontext_t, ) -> Result<(), std::io::Error> { @@ -85,9 +91,10 @@ fn dump_registers( Ok(()) } +/// Write the contens of all important registers #[allow(clippy::unnecessary_wraps, clippy::similar_names)] #[cfg(all(target_vendor = "apple", target_arch = "x86_64"))] -fn dump_registers( +pub fn dump_registers( writer: &mut BufWriter, ucontext: &ucontext_t, ) -> Result<(), std::io::Error> { diff --git a/libafl/src/bolts/os/unix_signals.rs b/libafl/src/bolts/os/unix_signals.rs index 1bf1b89d01..a8b0b1d435 100644 --- a/libafl/src/bolts/os/unix_signals.rs +++ b/libafl/src/bolts/os/unix_signals.rs @@ -3,9 +3,7 @@ use alloc::vec::Vec; use core::{ cell::UnsafeCell, fmt::{self, Display, Formatter}, - mem, - mem::MaybeUninit, - ptr, + mem, ptr, ptr::write_volatile, sync::atomic::{compiler_fence, Ordering}, }; @@ -244,11 +242,15 @@ pub unsafe fn setup_signal_handler(handler: &mut T) -> Res /// Function to get the current [`ucontext_t`] for this process. /// This calls the libc `getcontext` function under the hood. +/// It can be useful, for example for `dump_regs`. +/// Note that calling this method may, of course, alter the state. /// We wrap it here, as it seems to be (currently) /// not available on `MacOS` in the `libc` crate. #[cfg(unix)] +#[allow(clippy::inline_always)] // we assume that inlining will destroy less state +#[inline(always)] pub fn ucontext() -> Result { - let mut ucontext = unsafe { MaybeUninit::zeroed().assume_init() }; + let mut ucontext = unsafe { mem::zeroed() }; if unsafe { getcontext(&mut ucontext) } == 0 { Ok(ucontext) } else {