Skip to content

Commit

Permalink
Format desyscall (#2316)
Browse files Browse the repository at this point in the history
* f

* clippy and stuff

* no apple
  • Loading branch information
tokatoka authored Jun 16, 2024
1 parent 888079a commit 51db18e
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 47 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"utils/deexit",
"utils/libafl_benches",
"utils/gramatron/construct_automata",
"utils/desyscall",
]
default-members = [
"libafl",
Expand Down
1 change: 0 additions & 1 deletion libafl_bolts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// For `std::simd`
#![cfg_attr(nightly, feature(portable_simd))]
// For `core::error`
#![cfg_attr(nightly, feature(error_in_core))]
#![warn(clippy::cargo)]
#![allow(ambiguous_glob_reexports)]
#![deny(clippy::cargo_common_metadata)]
Expand Down
3 changes: 2 additions & 1 deletion libafl_qemu/libafl_qemu_sys/src/x86_64_stub_bindings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* 1.80.0-nightly */
/* 1.81.0-nightly */
/* automatically generated by rust-bindgen 0.69.4 */

#[repr(C)]
Expand Down Expand Up @@ -2341,6 +2341,7 @@ pub type DeviceReset = ::std::option::Option<unsafe extern "C" fn(dev: *mut Devi
#[derive(Debug, Copy, Clone)]
pub struct DeviceClass {
pub parent_class: ObjectClass,
#[doc = " @categories: device categories device belongs to"]
pub categories: [::std::os::raw::c_ulong; 1usize],
#[doc = " @fw_name: name used to identify device to firmware interfaces"]
pub fw_name: *const ::std::os::raw::c_char,
Expand Down
5 changes: 2 additions & 3 deletions libafl_qemu/runtime/libafl_qemu_stub_bindings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* 1.80.0-nightly */
/* 1.81.0-nightly */
/* automatically generated by rust-bindgen 0.69.4 */

pub const _STDINT_H: u32 = 1;
Expand Down Expand Up @@ -36,7 +36,7 @@ pub const __STDC_IEC_60559_COMPLEX__: u32 = 201404;
pub const __STDC_ISO_10646__: u32 = 201706;
pub const __GNU_LIBRARY__: u32 = 6;
pub const __GLIBC__: u32 = 2;
pub const __GLIBC_MINOR__: u32 = 39;
pub const __GLIBC_MINOR__: u32 = 38;
pub const _SYS_CDEFS_H: u32 = 1;
pub const __glibc_c99_flexarr_available: u32 = 1;
pub const __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI: u32 = 0;
Expand All @@ -60,7 +60,6 @@ pub const _BITS_TIME64_H: u32 = 1;
pub const _BITS_WCHAR_H: u32 = 1;
pub const _BITS_STDINT_INTN_H: u32 = 1;
pub const _BITS_STDINT_UINTN_H: u32 = 1;
pub const _BITS_STDINT_LEAST_H: u32 = 1;
pub const INT8_MIN: i32 = -128;
pub const INT16_MIN: i32 = -32768;
pub const INT32_MIN: i32 = -2147483648;
Expand Down
5 changes: 5 additions & 0 deletions utils/desyscall/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
name = "desyscall"
version = "0.1.0"
edition = "2021"
description = "DeSyscall: Hooks syscalls for reduces overhead during in-process fuzzing"
repository = "https://github.com/AFLplusplus/LibAFL/"
license = "MIT OR Apache-2.0"
categories = ["development-tools::testing", "emulators", "embedded", "os", "no-std"]
keywords = ["fuzzing", "libafl", "ldpreload"]

[dependencies]
meminterval = "0.3"
Expand Down
5 changes: 5 additions & 0 deletions utils/desyscall/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
use std::env;

fn main() {
if cfg!(not(target_os = "linux")) {
println!("Not supported!");
return;
}

let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir = out_dir.to_string_lossy().to_string();

Expand Down
11 changes: 8 additions & 3 deletions utils/desyscall/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ extern "C" {
fn __libafl_raw_read(fd: c_int, buf: Pointer, count: size_t) -> ssize_t;
}

/// # Safety
/// Call to functions using syscalls
#[allow(clippy::cast_possible_wrap)]
#[no_mangle]
pub unsafe fn write(fd: c_int, buf: Pointer, count: size_t) -> ssize_t {
pub unsafe extern "C" fn write(fd: c_int, buf: Pointer, count: size_t) -> ssize_t {
let ctx = Context::get();

if ctx.enabled && (fd == 1 || fd == 2) {
Expand All @@ -20,11 +23,13 @@ pub unsafe fn write(fd: c_int, buf: Pointer, count: size_t) -> ssize_t {
}
}

/// # Safety
/// Call to functions using syscalls
#[no_mangle]
pub unsafe fn read(fd: c_int, buf: Pointer, count: size_t) -> ssize_t {
pub unsafe extern "C" fn read(fd: c_int, buf: Pointer, count: size_t) -> ssize_t {
let ctx = Context::get();

if ctx.enabled && fd >= 0 && fd <= 2 {
if ctx.enabled && (0..=2).contains(&fd) {
0
} else {
__libafl_raw_read(fd, buf, count)
Expand Down
16 changes: 13 additions & 3 deletions utils/desyscall/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{mem::MaybeUninit, sync::Once};

use libc::{c_int, c_void};
use meminterval::IntervalTree;
use std::{mem::MaybeUninit, sync::Once};

pub mod file;
pub mod mmap;
Expand All @@ -20,7 +21,14 @@ pub struct Context {
exit_hook: Option<Box<dyn FnMut(i32)>>,
}

impl Default for Context {
fn default() -> Self {
Self::new()
}
}

impl Context {
#[must_use]
pub fn new() -> Self {
Self {
enabled: false,
Expand Down Expand Up @@ -73,13 +81,15 @@ extern "C" {
}

// void _exit(int status);
/// # Safety
/// Call to function using syscalls
#[no_mangle]
pub unsafe fn _exit(status: c_int) {
pub unsafe extern "C" fn _exit(status: c_int) {
let ctx = Context::get();

if ctx.enabled {
if let Some(hook) = &mut ctx.exit_hook {
(hook)(status as i32);
(hook)(status);
}
}

Expand Down
Loading

0 comments on commit 51db18e

Please sign in to comment.