some rust cleanup

This commit is contained in:
Dominik Maier
2021-02-27 15:52:36 +01:00
parent a5da9ce42c
commit c219502f0f
7 changed files with 35 additions and 33 deletions

View File

@ -3,5 +3,6 @@ members = [
"custom_mutator-sys",
"custom_mutator",
"example",
"example_lain",
# Lain needs a nightly toolchain
# "example_lain",
]

View File

@ -1,9 +1,11 @@
# Rust Custom Mutators
Bindings to create custom mutators in Rust.
These bindings are documented with rustdoc. To view the documentation run
```cargo doc -p custom_mutator --open```.
A minimal example can be found in `example`.
A minimal example can be found in `example`. Build it using `cargo build --example example_mutator`.
An example using [lain](https://github.com/microsoft/lain) can be found in `example_lain`.
An example using [lain](https://github.com/microsoft/lain) for structured fuzzing can be found in `example_lain`.
Since lain requires a nightly rust toolchain, you need to set one up before you can play with it.

View File

@ -23,7 +23,7 @@
//! The state is passed to [`CustomMutator::init`], when the feature is activated.
//!
//! _This is completely unsafe and uses automatically generated types extracted from the AFL++ source._
use std::{ffi::CStr, fmt::Debug, os::raw::c_uint};
use std::{ffi::CStr, fmt::Debug};
#[cfg(feature = "afl_internals")]
#[doc(hidden)]
@ -37,7 +37,7 @@ pub trait RawCustomMutator {
where
Self: Sized;
#[cfg(not(feature = "afl_internals"))]
fn init(seed: c_uint) -> Self
fn init(seed: u32) -> Self
where
Self: Sized;
@ -87,7 +87,7 @@ pub mod wrappers {
convert::TryInto,
ffi::{c_void, CStr},
mem::ManuallyDrop,
os::raw::{c_char, c_uint},
os::raw::c_char,
panic::catch_unwind,
process::abort,
ptr::null,
@ -112,13 +112,13 @@ pub mod wrappers {
}
#[cfg(feature = "afl_internals")]
fn new(afl: &'static afl_state, seed: c_uint) -> Box<Self> {
fn new(afl: &'static afl_state, seed: u32) -> Box<Self> {
Box::new(Self {
mutator: M::init(afl, seed),
})
}
#[cfg(not(feature = "afl_internals"))]
fn new(seed: c_uint) -> Box<Self> {
fn new(seed: u32) -> Box<Self> {
Box::new(Self {
mutator: M::init(seed),
})
@ -143,7 +143,7 @@ pub mod wrappers {
/// Internal function used in the macro
#[cfg(not(feature = "afl_internals"))]
pub fn afl_custom_init_<M: RawCustomMutator>(seed: c_uint) -> *const c_void {
pub fn afl_custom_init_<M: RawCustomMutator>(seed: u32) -> *const c_void {
match catch_unwind(|| FFIContext::<M>::new(seed).into_ptr()) {
Ok(ret) => ret,
Err(err) => panic_handler("afl_custom_init", err),
@ -154,7 +154,7 @@ pub mod wrappers {
#[cfg(feature = "afl_internals")]
pub fn afl_custom_init_<M: RawCustomMutator>(
afl: Option<&'static afl_state>,
seed: c_uint,
seed: u32,
) -> *const c_void {
match catch_unwind(|| {
let afl = afl.expect("mutator func called with NULL afl");
@ -328,16 +328,15 @@ pub mod wrappers {
/// # #[cfg(feature = "afl_internals")]
/// # use custom_mutator::afl_state;
/// # use custom_mutator::CustomMutator;
/// # use std::os::raw::c_uint;
/// struct MyMutator;
/// impl CustomMutator for MyMutator {
/// /// ...
/// # type Error = ();
/// # #[cfg(feature = "afl_internals")]
/// # fn init(_afl_state: &afl_state, _seed: c_uint) -> Result<Self,()> {unimplemented!()}
/// # fn init(_afl_state: &afl_state, _seed: u32) -> Result<Self,()> {unimplemented!()}
/// # #[cfg(not(feature = "afl_internals"))]
/// # fn init(_seed: c_uint) -> Result<Self,()> {unimplemented!()}
/// # fn fuzz<'b,'s:'b>(&'s mut self, _buffer: &'b mut [u8], _add_buff: Option<&[u8]>, _max_size: usize) -> Result<Option<&'b [u8]>,()> {unimplemented!()}
/// # fn init(_seed: u32) -> Result<Self, Self::Error> {unimplemented!()}
/// # fn fuzz<'b,'s:'b>(&'s mut self, _buffer: &'b mut [u8], _add_buff: Option<&[u8]>, _max_size: usize) -> Result<Option<&'b [u8]>, Self::Error> {unimplemented!()}
/// }
/// export_mutator!(MyMutator);
/// ```
@ -350,7 +349,7 @@ macro_rules! export_mutator {
afl: ::std::option::Option<&'static $crate::afl_state>,
seed: ::std::os::raw::c_uint,
) -> *const ::std::os::raw::c_void {
$crate::wrappers::afl_custom_init_::<$mutator_type>(afl, seed)
$crate::wrappers::afl_custom_init_::<$mutator_type>(afl, seed as u32)
}
#[cfg(not(feature = "afl_internals"))]
@ -359,7 +358,7 @@ macro_rules! export_mutator {
_afl: *const ::std::os::raw::c_void,
seed: ::std::os::raw::c_uint,
) -> *const ::std::os::raw::c_void {
$crate::wrappers::afl_custom_init_::<$mutator_type>(seed)
$crate::wrappers::afl_custom_init_::<$mutator_type>(seed as u32)
}
#[no_mangle]
@ -442,8 +441,6 @@ macro_rules! export_mutator {
#[cfg(test)]
/// this sanity test is supposed to just find out whether an empty mutator being exported by the macro compiles
mod sanity_test {
use std::os::raw::c_uint;
#[cfg(feature = "afl_internals")]
use super::afl_state;
@ -453,12 +450,12 @@ mod sanity_test {
impl RawCustomMutator for ExampleMutator {
#[cfg(feature = "afl_internals")]
fn init(_afl: &afl_state, _seed: c_uint) -> Self {
fn init(_afl: &afl_state, _seed: u32) -> Self {
unimplemented!()
}
#[cfg(not(feature = "afl_internals"))]
fn init(_seed: c_uint) -> Self {
fn init(_seed: u32) -> Self {
unimplemented!()
}
@ -497,12 +494,12 @@ pub trait CustomMutator {
}
#[cfg(feature = "afl_internals")]
fn init(afl: &'static afl_state, seed: c_uint) -> Result<Self, Self::Error>
fn init(afl: &'static afl_state, seed: u32) -> Result<Self, Self::Error>
where
Self: Sized;
#[cfg(not(feature = "afl_internals"))]
fn init(seed: c_uint) -> Result<Self, Self::Error>
fn init(seed: u32) -> Result<Self, Self::Error>
where
Self: Sized;
@ -544,7 +541,7 @@ where
M::Error: Debug,
{
#[cfg(feature = "afl_internals")]
fn init(afl: &'static afl_state, seed: c_uint) -> Self
fn init(afl: &'static afl_state, seed: u32) -> Self
where
Self: Sized,
{
@ -558,7 +555,7 @@ where
}
#[cfg(not(feature = "afl_internals"))]
fn init(seed: c_uint) -> Self
fn init(seed: u32) -> Self
where
Self: Sized,
{

View File

@ -1,5 +1,5 @@
[package]
name = "example"
name = "example_mutator"
version = "0.1.0"
authors = ["Julius Hohnerlein <julihoh@users.noreply.github.com>"]
edition = "2018"
@ -9,5 +9,7 @@ edition = "2018"
[dependencies]
custom_mutator = { path = "../custom_mutator" }
[lib]
[[example]]
name = "example_mutator"
path = "./src/example_mutator.rs"
crate-type = ["cdylib"]

View File

@ -1,14 +1,13 @@
#![allow(unused_variables)]
use custom_mutator::{export_mutator, CustomMutator};
use std::os::raw::c_uint;
struct ExampleMutator;
impl CustomMutator for ExampleMutator {
type Error = ();
fn init(seed: c_uint) -> Result<Self, ()> {
fn init(seed: u32) -> Result<Self, Self::Error> {
Ok(Self)
}
@ -17,7 +16,7 @@ impl CustomMutator for ExampleMutator {
buffer: &'b mut [u8],
add_buff: Option<&[u8]>,
max_size: usize,
) -> Result<Option<&'b [u8]>, ()> {
) -> Result<Option<&'b [u8]>, Self::Error> {
buffer.reverse();
Ok(Some(buffer))
}
@ -30,7 +29,7 @@ struct OwnBufferExampleMutator {
impl CustomMutator for OwnBufferExampleMutator {
type Error = ();
fn init(seed: c_uint) -> Result<Self, ()> {
fn init(seed: u32) -> Result<Self, Self::Error> {
Ok(Self {
own_buffer: Vec::new(),
})

View File

@ -10,5 +10,7 @@ edition = "2018"
custom_mutator = { path = "../custom_mutator" }
lain="0.5"
[lib]
[[example]]
name = "example_lain"
path = "./src/lain_mutator.rs"
crate-type = ["cdylib"]

View File

@ -4,7 +4,6 @@ use lain::{
prelude::*,
rand::{rngs::StdRng, SeedableRng},
};
use std::os::raw::c_uint;
#[derive(Debug, Mutatable, NewFuzzed, BinarySerialize)]
struct MyStruct {
@ -31,7 +30,7 @@ struct LainMutator {
impl CustomMutator for LainMutator {
type Error = ();
fn init(seed: c_uint) -> Result<Self, ()> {
fn init(seed: u32) -> Result<Self, ()> {
Ok(Self {
mutator: Mutator::new(StdRng::seed_from_u64(seed as u64)),
buffer: Vec::new(),