Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/libafl_asan/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ default = [
"hooks",
"host",
"libc",
"linux",
"syscalls",
"mimalloc",
"test",
"tracking",
Expand All @@ -50,8 +50,8 @@ host = ["dep:syscalls"]
initialize = []
## Enable use of the `libc` library to support creation of mappings, read/write, logging etc (more OS agnostic)
libc = ["dep:libc"]
## Enable the use of direct syscalls (supported by `rustix`) to interact with the operating system (Linux specific).
linux = ["dep:rustix", "dep:syscalls"]
## Enable the use of direct syscalls (supported by `rustix`) to interact with the operating system (Unix specific).
syscalls = ["dep:rustix", "dep:syscalls"]
## Enable the `baby_mimalloc` allocator
mimalloc = ["dep:baby-mimalloc"]
## Disable the magic used to support `no_std` environments for running unit and integration tests (we only run our tests on Linux right now).
Expand Down
2 changes: 1 addition & 1 deletion crates/libafl_asan/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
#[cfg(all(feature = "linux", not(target_os = "linux")))]
//#[cfg(all(feature = "syscalls", not(target_os = "linux")))]
println!("cargo:warning=The feature `linux` can only be used on Linux!");

println!("cargo:rerun-if-changed=cc/include/hooks.h");
Expand Down
2 changes: 1 addition & 1 deletion crates/libafl_asan/libafl_asan_fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ libafl_asan = { path = "../", default-features = false, features = [
"guest",
"host",
"libc",
"linux",
"syscalls",
"test",
"tracking",
] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use libafl_asan::{
backend::dlmalloc::DlmallocBackend,
frontend::{AllocatorFrontend, default::DefaultFrontend},
},
mmap::linux::LinuxMmap,
mmap::unix::MmapRegion,
shadow::{
Shadow,
guest::{DefaultShadowLayout, GuestShadow},
Expand All @@ -19,17 +19,17 @@ use libfuzzer_sys::fuzz_target;
use log::info;

type DF = DefaultFrontend<
DlmallocBackend<LinuxMmap>,
GuestShadow<LinuxMmap, DefaultShadowLayout>,
DlmallocBackend<MmapRegion>,
GuestShadow<MmapRegion, DefaultShadowLayout>,
GuestTracking,
>;

const PAGE_SIZE: usize = 4096;

static INIT_ONCE: LazyLock<Mutex<DF>> = LazyLock::new(|| {
env_logger::init();
let backend = DlmallocBackend::<LinuxMmap>::new(PAGE_SIZE);
let shadow = GuestShadow::<LinuxMmap, DefaultShadowLayout>::new().unwrap();
let backend = DlmallocBackend::<MmapRegion>::new(PAGE_SIZE);
let shadow = GuestShadow::<MmapRegion, DefaultShadowLayout>::new().unwrap();
let tracking = GuestTracking::new().unwrap();
let frontend = DF::new(
backend,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
use libafl_asan::{
GuestAddr,
allocator::frontend::{AllocatorFrontend, default::DefaultFrontend},
mmap::{Mmap, linux::LinuxMmap},
mmap::{Mmap, unix::MmapRegion},
shadow::{
Shadow,
guest::{DefaultShadowLayout, GuestShadow},
Expand Down Expand Up @@ -52,14 +52,14 @@ unsafe impl GlobalAlloc for MockBackend {
#[derive(Error, Debug, PartialEq)]
pub enum MockBackendError {}

type DF = DefaultFrontend<MockBackend, GuestShadow<LinuxMmap, DefaultShadowLayout>, GuestTracking>;
type DF = DefaultFrontend<MockBackend, GuestShadow<MmapRegion, DefaultShadowLayout>, GuestTracking>;

static MAP: LazyLock<LinuxMmap> = LazyLock::new(|| LinuxMmap::map(MAX_ADDR).unwrap());
static MAP: LazyLock<MmapRegion> = LazyLock::new(|| MmapRegion::map(MAX_ADDR).unwrap());

static INIT_ONCE: LazyLock<Mutex<DF>> = LazyLock::new(|| {
env_logger::init();
let backend = MockBackend::new();
let shadow = GuestShadow::<LinuxMmap, DefaultShadowLayout>::new().unwrap();
let shadow = GuestShadow::<MmapRegion, DefaultShadowLayout>::new().unwrap();
let tracking = GuestTracking::new().unwrap();
let frontend = DF::new(
backend,
Expand Down
8 changes: 4 additions & 4 deletions crates/libafl_asan/src/allocator/frontend/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ impl<B: GlobalAlloc + Send, S: Shadow, T: Tracking> AllocatorFrontend for Defaul

fn alloc(&mut self, len: usize, align: usize) -> Result<GuestAddr, Self::Error> {
debug!("alloc - len: {len:#x}, align: {align:#x}");
if align % size_of::<GuestAddr>() != 0 {
if !align.is_multiple_of(size_of::<GuestAddr>()) {
Err(DefaultFrontendError::InvalidAlignment(align))?;
}
let size = len + align;
let allocated_size = (self.red_zone_size * 2) + Self::align_up(size);
assert!(allocated_size % Self::ALLOC_ALIGN_SIZE == 0);
assert!(allocated_size.is_multiple_of(Self::ALLOC_ALIGN_SIZE));
let ptr = unsafe {
self.backend.alloc(
Layout::from_size_align(allocated_size, Self::ALLOC_ALIGN_SIZE)
Expand All @@ -84,7 +84,7 @@ impl<B: GlobalAlloc + Send, S: Shadow, T: Tracking> AllocatorFrontend for Defaul
} else {
rz + align - (rz % align)
};
assert!(align == 0 || data % align == 0);
assert!(align == 0 || data.is_multiple_of(align));
assert!(data + len <= orig + allocated_size);

self.allocations.insert(
Expand Down Expand Up @@ -171,7 +171,7 @@ impl<B: GlobalAlloc + Send, S: Shadow, T: Tracking> DefaultFrontend<B, S, T> {
red_zone_size: usize,
quarantine_size: usize,
) -> Result<DefaultFrontend<B, S, T>, DefaultFrontendError<S, T>> {
if red_zone_size % Self::ALLOC_ALIGN_SIZE != 0 {
if !red_zone_size.is_multiple_of(Self::ALLOC_ALIGN_SIZE) {
Err(DefaultFrontendError::InvalidRedZoneSize(red_zone_size))?;
}
Ok(DefaultFrontend::<B, S, T> {
Expand Down
10 changes: 5 additions & 5 deletions crates/libafl_asan/src/exit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
pub use crate::exit::libc::abort;
#[cfg(feature = "libc")]
pub use crate::exit::libc::exit;
#[cfg(all(feature = "linux", target_os = "linux", not(feature = "libc")))]
#[cfg(all(feature = "syscalls", target_os = "linux", not(feature = "libc")))]
pub use crate::exit::linux::abort;
#[cfg(all(feature = "linux", target_os = "linux", not(feature = "libc")))]
#[cfg(all(feature = "syscalls", target_os = "linux", not(feature = "libc")))]
pub use crate::exit::linux::exit;

#[cfg(feature = "libc")]
pub mod libc;

#[cfg(all(feature = "linux", target_os = "linux"))]
#[cfg(all(feature = "syscalls", target_os = "linux"))]
pub mod linux;

#[cfg(all(
not(feature = "libc"),
not(all(feature = "linux", target_os = "linux"))
not(all(feature = "syscalls", target_os = "linux"))
))]
pub fn abort() -> ! {
loop {}
}

#[cfg(all(
not(feature = "libc"),
not(all(feature = "linux", target_os = "linux"))
not(all(feature = "syscalls", target_os = "linux"))
))]
pub fn exit(_status: core::ffi::c_int) -> ! {
loop {}
Expand Down
2 changes: 1 addition & 1 deletion crates/libafl_asan/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::ffi::CStr;
#[cfg(feature = "libc")]
pub mod libc;

#[cfg(all(feature = "linux", target_os = "linux"))]
#[cfg(all(feature = "syscalls", target_os = "linux"))]
pub mod linux;

pub trait FileReader: Debug + Send + Sized {
Expand Down
2 changes: 1 addition & 1 deletion crates/libafl_asan/src/hooks/aligned_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub unsafe extern "C" fn aligned_alloc(alignment: size_t, size: size_t) -> *mut
n != 0 && (n & (n - 1)) == 0
}

if alignment % size_of::<GuestAddr>() != 0 {
if !alignment.is_multiple_of(size_of::<GuestAddr>()) {
asan_panic(
c"aligned_alloc - alignment is not a multiple of pointer size".as_ptr()
as *const c_char,
Expand Down
2 changes: 1 addition & 1 deletion crates/libafl_asan/src/hooks/memalign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub unsafe extern "C" fn memalign(align: size_t, size: size_t) -> *mut c_void {
n != 0 && (n & (n - 1)) == 0
}

if align % size_of::<GuestAddr>() != 0 {
if !align.is_multiple_of(size_of::<GuestAddr>()) {
asan_panic(
c"memalign - align is not a multiple of pointer size".as_ptr() as *const c_char,
);
Expand Down
2 changes: 1 addition & 1 deletion crates/libafl_asan/src/hooks/mmap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "libc")]
pub mod libc;

#[cfg(all(feature = "linux", target_os = "linux", not(feature = "libc")))]
#[cfg(all(feature = "syscalls", target_os = "linux", not(feature = "libc")))]
pub mod linux;
2 changes: 1 addition & 1 deletion crates/libafl_asan/src/hooks/munmap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "libc")]
pub mod libc;

#[cfg(all(feature = "linux", target_os = "linux", not(feature = "libc")))]
#[cfg(all(feature = "syscalls", target_os = "linux", not(feature = "libc")))]
pub mod linux;
2 changes: 1 addition & 1 deletion crates/libafl_asan/src/hooks/posix_memalign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub unsafe extern "C" fn posix_memalign(
n != 0 && (n & (n - 1)) == 0
}

if align % size_of::<GuestAddr>() != 0 {
if !align.is_multiple_of(size_of::<GuestAddr>()) {
asan_panic(
c"posix_memalign - align is not a multiple of pointer size".as_ptr()
as *const c_char,
Expand Down
2 changes: 1 addition & 1 deletion crates/libafl_asan/src/hooks/read/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "libc")]
pub mod libc;

#[cfg(all(feature = "linux", target_os = "linux", not(feature = "libc")))]
#[cfg(all(feature = "syscalls", target_os = "linux", not(feature = "libc")))]
pub mod linux;
2 changes: 1 addition & 1 deletion crates/libafl_asan/src/hooks/write/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "libc")]
pub mod libc;

#[cfg(all(feature = "linux", target_os = "linux", not(feature = "libc")))]
#[cfg(all(feature = "syscalls", target_os = "linux", not(feature = "libc")))]
pub mod linux;
2 changes: 1 addition & 1 deletion crates/libafl_asan/src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{GuestAddr, shadow::PoisonType};
#[cfg(feature = "libc")]
pub mod libc;

#[cfg(all(feature = "linux", target_os = "linux"))]
#[cfg(all(feature = "syscalls", target_os = "linux"))]
pub mod linux;

#[repr(usize)]
Expand Down
14 changes: 7 additions & 7 deletions crates/libafl_asan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ use core::mem::transmute;
use ::core::ffi::{c_char, c_void};
use nostd_printf::vsnprintf;

/*
* vsnprintf is only called from our C code, but we need to tell Rust that we
* still need it even though it isn't referenced from rust.
*/
#[used]
static LINK_VSNPRINTF: unsafe extern "C" fn() = unsafe { transmute(vsnprintf as *const ()) };

#[cfg(not(feature = "test"))]
unsafe extern "C" {
pub fn asan_load(addr: *const c_void, size: usize);
Expand All @@ -136,10 +143,3 @@ unsafe extern "C" {
pub fn asan_panic(msg: *const c_char) -> !;
pub fn asan_swap(enabled: bool);
}

/*
* vsnprintf is only called from our C code, but we need to tell Rust that we
* still need it even though it isn't referenced from rust.
*/
#[used]
static LINK_VSNPRINTF: unsafe extern "C" fn() = unsafe { transmute(vsnprintf as *const ()) };
2 changes: 1 addition & 1 deletion crates/libafl_asan/src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#[cfg(feature = "libc")]
pub mod libc;

#[cfg(all(feature = "linux", target_os = "linux"))]
#[cfg(all(feature = "syscalls", target_os = "linux"))]
pub mod linux;

use core::ffi::{CStr, c_char};
Expand Down
15 changes: 10 additions & 5 deletions crates/libafl_asan/src/maps/libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,16 @@ impl Function for FunctionRead {
#[derive(Debug)]
struct FunctionErrnoLocation;

#[cfg(target_os = "linux")]
impl Function for FunctionErrnoLocation {
type Func = unsafe extern "C" fn() -> *mut c_int;
const NAME: &'static CStr = c"__errno_location";
}
#[cfg(target_vendor = "apple")]
impl Function for FunctionErrnoLocation {
type Func = unsafe extern "C" fn() -> *mut c_int;
const NAME: &'static CStr = c"__error";
}

static OPEN_ADDR: AtomicGuestAddr = AtomicGuestAddr::new();
static CLOSE_ADDR: AtomicGuestAddr = AtomicGuestAddr::new();
Expand Down Expand Up @@ -88,8 +94,7 @@ impl<S: Symbols> LibcMapReader<S> {
Ok(f)
}

fn get_errno_location()
-> Result<<FunctionErrnoLocation as Function>::Func, LibcMapReaderError<S>> {
fn get_errno_location() -> Result<<FunctionErrnoLocation as Function>::Func, LibcMapReaderError<S>> {
let addr = GET_ERRNO_LOCATION_ADDR.try_get_or_insert_with(|| {
S::lookup(FunctionErrnoLocation::NAME)
.map_err(|e| LibcMapReaderError::FailedToFindSymbol(e))
Expand Down Expand Up @@ -123,7 +128,7 @@ impl<S: Symbols> MapReader for LibcMapReader<S> {
};
unsafe { asan_swap(true) };
if fd < 0 {
let errno = Self::errno().unwrap();
let errno = Self::errno();
return Err(LibcMapReaderError::FailedToOpen(errno));
}
Ok(LibcMapReader {
Expand All @@ -138,7 +143,7 @@ impl<S: Symbols> MapReader for LibcMapReader<S> {
let ret = unsafe { fn_read(self.fd, buf.as_mut_ptr() as *mut c_char, buf.len()) };
unsafe { asan_swap(true) };
if ret < 0 {
let errno = Self::errno().unwrap();
let errno = Self::errno();
return Err(LibcMapReaderError::FailedToRead(self.fd, errno));
}
Ok(ret as usize)
Expand All @@ -152,7 +157,7 @@ impl<S: Symbols> Drop for LibcMapReader<S> {
let ret = unsafe { fn_close(self.fd) };
unsafe { asan_swap(true) };
if ret < 0 {
let errno = Self::errno().unwrap();
let errno = Self::errno();
panic!("Failed to close: {}, Errno: {}", self.fd, errno);
}
trace!("Closed fd: {}", self.fd);
Expand Down
4 changes: 2 additions & 2 deletions crates/libafl_asan/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use crate::allocator::backend::dlmalloc::DlmallocBackend;

#[cfg(all(
feature = "global_allocator",
feature = "linux",
feature = "syscalls",
target_os = "linux",
not(feature = "libc")
))]
type Mmap = crate::mmap::linux::LinuxMmap;
type Mmap = crate::mmap::linux::MmapRegion;

#[cfg(all(feature = "global_allocator", feature = "libc",))]
type Mmap = crate::mmap::libc::LibcMmap<
Expand Down
Loading
Loading