fix rust mutator bindingsbuild

This commit is contained in:
Dominik Maier
2021-07-21 01:44:27 +02:00
parent 4d7c23e231
commit a3a86afd0d

View File

@ -53,7 +53,11 @@ pub trait RawCustomMutator {
1 1
} }
fn queue_new_entry(&mut self, filename_new_queue: &Path, _filename_orig_queue: Option<&Path>) -> bool { fn queue_new_entry(
&mut self,
filename_new_queue: &Path,
_filename_orig_queue: Option<&Path>,
) -> bool {
false false
} }
@ -86,7 +90,6 @@ pub mod wrappers {
use std::{ use std::{
any::Any, any::Any,
convert::TryInto,
ffi::{c_void, CStr, OsStr}, ffi::{c_void, CStr, OsStr},
mem::ManuallyDrop, mem::ManuallyDrop,
os::{raw::c_char, unix::ffi::OsStrExt}, os::{raw::c_char, unix::ffi::OsStrExt},
@ -178,6 +181,10 @@ pub mod wrappers {
} }
/// Internal function used in the macro /// Internal function used in the macro
/// # Safety
///
/// May dereference all passed-in pointers.
/// Should not be called manually, but will be called by `afl-fuzz`
pub unsafe fn afl_custom_fuzz_<M: RawCustomMutator>( pub unsafe fn afl_custom_fuzz_<M: RawCustomMutator>(
data: *mut c_void, data: *mut c_void,
buf: *mut u8, buf: *mut u8,
@ -201,13 +208,10 @@ pub mod wrappers {
} else { } else {
Some(slice::from_raw_parts(add_buf, add_buf_size)) Some(slice::from_raw_parts(add_buf, add_buf_size))
}; };
match context match context.mutator.fuzz(buff_slice, add_buff_slice, max_size) {
.mutator
.fuzz(buff_slice, add_buff_slice, max_size.try_into().unwrap())
{
Some(buffer) => { Some(buffer) => {
*out_buf = buffer.as_ptr(); *out_buf = buffer.as_ptr();
buffer.len().try_into().unwrap() buffer.len()
} }
None => { None => {
// return the input buffer with 0-length to let AFL skip this mutation attempt // return the input buffer with 0-length to let AFL skip this mutation attempt
@ -266,7 +270,7 @@ pub mod wrappers {
}; };
context context
.mutator .mutator
.queue_new_entry(filename_new_queue, filename_orig_queue); .queue_new_entry(filename_new_queue, filename_orig_queue)
}) { }) {
Ok(ret) => ret, Ok(ret) => ret,
Err(err) => panic_handler("afl_custom_queue_new_entry", err), Err(err) => panic_handler("afl_custom_queue_new_entry", err),
@ -544,8 +548,8 @@ pub trait CustomMutator {
&mut self, &mut self,
filename_new_queue: &Path, filename_new_queue: &Path,
filename_orig_queue: Option<&Path>, filename_orig_queue: Option<&Path>,
) -> Result<(), Self::Error> { ) -> Result<bool, Self::Error> {
Ok(()) Ok(false)
} }
fn queue_get(&mut self, filename: &Path) -> Result<bool, Self::Error> { fn queue_get(&mut self, filename: &Path) -> Result<bool, Self::Error> {
@ -619,11 +623,16 @@ where
} }
} }
fn queue_new_entry(&mut self, filename_new_queue: &Path, filename_orig_queue: Option<&Path>) -> bool { fn queue_new_entry(
&mut self,
filename_new_queue: &Path,
filename_orig_queue: Option<&Path>,
) -> bool {
match self.queue_new_entry(filename_new_queue, filename_orig_queue) { match self.queue_new_entry(filename_new_queue, filename_orig_queue) {
Ok(r) => r, Ok(r) => r,
Err(e) => { Err(e) => {
Self::handle_error(e); Self::handle_error(e);
false
} }
} }
} }
@ -698,8 +707,7 @@ mod default_mutator_describe {
fn truncate_str_unicode_safe(s: &str, max_len: usize) -> &str { fn truncate_str_unicode_safe(s: &str, max_len: usize) -> &str {
if s.len() <= max_len { if s.len() <= max_len {
s s
} else { } else if let Some((last_index, _)) = s
if let Some((last_index, _)) = s
.char_indices() .char_indices()
.take_while(|(index, _)| *index <= max_len) .take_while(|(index, _)| *index <= max_len)
.last() .last()
@ -708,7 +716,6 @@ fn truncate_str_unicode_safe(s: &str, max_len: usize) -> &str {
} else { } else {
"" ""
} }
}
} }
#[cfg(test)] #[cfg(test)]