diff options
author | Dominik Maier <domenukk@gmail.com> | 2021-02-27 15:52:36 +0100 |
---|---|---|
committer | Dominik Maier <domenukk@gmail.com> | 2021-02-27 15:52:36 +0100 |
commit | c219502f0fe654927c3f74f9791f786f0740f344 (patch) | |
tree | f6960facbd01bf0573199a9320aeb94a32653f1b | |
parent | a5da9ce42cab1eab93cf80ca744944ae26e6ab58 (diff) | |
download | afl++-c219502f0fe654927c3f74f9791f786f0740f344.tar.gz |
some rust cleanup
-rw-r--r-- | custom_mutators/rust/Cargo.toml | 3 | ||||
-rw-r--r-- | custom_mutators/rust/README.md | 6 | ||||
-rw-r--r-- | custom_mutators/rust/custom_mutator/src/lib.rs | 39 | ||||
-rw-r--r-- | custom_mutators/rust/example/Cargo.toml | 6 | ||||
-rw-r--r-- | custom_mutators/rust/example/src/example_mutator.rs (renamed from custom_mutators/rust/example/src/lib.rs) | 7 | ||||
-rw-r--r-- | custom_mutators/rust/example_lain/Cargo.toml | 4 | ||||
-rw-r--r-- | custom_mutators/rust/example_lain/src/lain_mutator.rs (renamed from custom_mutators/rust/example_lain/src/lib.rs) | 3 |
7 files changed, 35 insertions, 33 deletions
diff --git a/custom_mutators/rust/Cargo.toml b/custom_mutators/rust/Cargo.toml index 003bc58a..e36d24b5 100644 --- a/custom_mutators/rust/Cargo.toml +++ b/custom_mutators/rust/Cargo.toml @@ -3,5 +3,6 @@ members = [ "custom_mutator-sys", "custom_mutator", "example", - "example_lain", + # Lain needs a nightly toolchain + # "example_lain", ] \ No newline at end of file diff --git a/custom_mutators/rust/README.md b/custom_mutators/rust/README.md index 119ac790..e2cc38b4 100644 --- a/custom_mutators/rust/README.md +++ b/custom_mutators/rust/README.md @@ -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. diff --git a/custom_mutators/rust/custom_mutator/src/lib.rs b/custom_mutators/rust/custom_mutator/src/lib.rs index 0fb007ab..b82af250 100644 --- a/custom_mutators/rust/custom_mutator/src/lib.rs +++ b/custom_mutators/rust/custom_mutator/src/lib.rs @@ -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, { diff --git a/custom_mutators/rust/example/Cargo.toml b/custom_mutators/rust/example/Cargo.toml index 0c89b200..070d23b1 100644 --- a/custom_mutators/rust/example/Cargo.toml +++ b/custom_mutators/rust/example/Cargo.toml @@ -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"] \ No newline at end of file diff --git a/custom_mutators/rust/example/src/lib.rs b/custom_mutators/rust/example/src/example_mutator.rs index 4f9345c0..9b9d4997 100644 --- a/custom_mutators/rust/example/src/lib.rs +++ b/custom_mutators/rust/example/src/example_mutator.rs @@ -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(), }) diff --git a/custom_mutators/rust/example_lain/Cargo.toml b/custom_mutators/rust/example_lain/Cargo.toml index 1f68c7e0..29d606a4 100644 --- a/custom_mutators/rust/example_lain/Cargo.toml +++ b/custom_mutators/rust/example_lain/Cargo.toml @@ -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"] \ No newline at end of file diff --git a/custom_mutators/rust/example_lain/src/lib.rs b/custom_mutators/rust/example_lain/src/lain_mutator.rs index 3336e861..22e5fe73 100644 --- a/custom_mutators/rust/example_lain/src/lib.rs +++ b/custom_mutators/rust/example_lain/src/lain_mutator.rs @@ -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(), |