Skip to content

Commit

Permalink
Rand below should take a NonZero parameter (#2519)
Browse files Browse the repository at this point in the history
* Rand below should take a NonZero parameter

* More

* more

* More

* fix build

* bit of clippy

* more clippy

* more clippy

* More clippy

* More more

* more nonzero

* fix multipart

* Cleanup, more unsafe

* fix

* fix unicode

* clippy, fmt

* more

* More safer and more better

* MaxStackPow

* fix merge fails

* make random_slize_size faster

* fix

* more

* fix
  • Loading branch information
domenukk authored Oct 4, 2024
1 parent 15157e0 commit 4fc136c
Show file tree
Hide file tree
Showing 66 changed files with 771 additions and 313 deletions.
2 changes: 1 addition & 1 deletion docs/listings/baby_fuzzer/listing-04/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn main() {

/* ANCHOR: generator */
// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
2 changes: 1 addition & 1 deletion docs/listings/baby_fuzzer/listing-05/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn main() {
/* ANCHOR_END: executor_with_observer */

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
2 changes: 1 addition & 1 deletion docs/listings/baby_fuzzer/listing-06/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn main() {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/baby/baby_fuzzer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn main() {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/baby/baby_fuzzer_minimizing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn main() -> Result<(), Error> {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/baby/baby_fuzzer_swap_differential/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub fn main() {
);

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn main() {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub fn main() {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn main() {
let mut executor = MyExecutor { shmem_id }.into_executor(tuple_list!(observer, bt_observer));

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn main() {
.unwrap();

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(3);
let mut generator = RandPrintablesGenerator::new(3).unwrap();

// Generate 8 initial inputs
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn main() {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub fn main() {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub fn main() {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
3 changes: 2 additions & 1 deletion fuzzers/forkserver/forkserver_libafl_cc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ pub fn main() {

// Setup a mutational stage with a basic bytes mutator
let mutator =
StdScheduledMutator::with_max_stack_pow(havoc_mutations().merge(tokens_mutations()), 6);
StdScheduledMutator::with_max_stack_pow(havoc_mutations().merge(tokens_mutations()), 6)
.unwrap();
let mut stages = tuple_list!(StdMutationalStage::new(mutator));

fuzzer
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/forkserver/forkserver_simple/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {
let mut compile_command = Command::new(afl_cc_path);
compile_command
.args(["src/program.c", "-o"])
.arg(&format!("{}/target/release/program", &cwd));
.arg(format!("{cwd}/target/release/program"));

if let Ok(llvm_config) = env::var("LLVM_CONFIG") {
if !llvm_config.is_empty() {
Expand Down
3 changes: 2 additions & 1 deletion fuzzers/forkserver/forkserver_simple/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ pub fn main() {

// Setup a mutational stage with a basic bytes mutator
let mutator =
StdScheduledMutator::with_max_stack_pow(havoc_mutations().merge(tokens_mutations()), 6);
StdScheduledMutator::with_max_stack_pow(havoc_mutations().merge(tokens_mutations()), 6)
.unwrap();
let mut stages = tuple_list!(StdMutationalStage::new(mutator));

fuzzer
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/fuzz_anything/baby_fuzzer_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn fuzz() {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/fuzz_anything/baby_no_std/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/fuzz_anything/libafl_atheris/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pub extern "C" fn LLVMFuzzerRunDriver(
if state.must_load_initial_inputs() {
if input_dirs.is_empty() {
// Generator of printable bytearrays of max size 32
let mut generator = RandBytesGenerator::new(32);
let mut generator = RandBytesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
10 changes: 7 additions & 3 deletions fuzzers/fuzz_anything/push_harness/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! [`Klo-routines`](https://github.com/andreafioraldi/klo-routines/) based fuzzer.
//! The target loops and the harness pulls inputs out of `LibAFL`, instead of being called by `LibAFL`.
use std::path::PathBuf;
use std::{path::PathBuf, ptr::addr_of_mut};

use klo_routines::{yield_, KloRoutine};
use libafl::{
Expand Down Expand Up @@ -39,8 +39,12 @@ fn input_generator() {
ExitKind::Ok
};

let signals_ptr = addr_of_mut!(SIGNALS);
let signals_len = unsafe { *signals_ptr }.len();

// Create an observation channel using the signals map
let observer = unsafe { StdMapObserver::new("signals", &mut SIGNALS) };
let observer =
unsafe { StdMapObserver::from_mut_ptr("signals", addr_of_mut!(SIGNALS) as _, signals_len) };

// Feedback to rate the interestingness of an input
let mut feedback = MaxMapFeedback::new(&observer);
Expand Down Expand Up @@ -89,7 +93,7 @@ fn input_generator() {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = RandPrintablesGenerator::new(32);
let mut generator = RandPrintablesGenerator::new(32).unwrap();

// Generate 8 initial inputs
state
Expand Down
3 changes: 2 additions & 1 deletion fuzzers/inprocess/fuzzbench_text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ fn fuzz_text(
GrimoireRandomDeleteMutator::new(),
),
3,
);
)
.unwrap();
let grimoire = StdMutationalStage::transforming(grimoire_mutator);

// A minimization+queue policy to get testcasess from the corpus
Expand Down
24 changes: 13 additions & 11 deletions fuzzers/structure_aware/baby_fuzzer_custom_input/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,25 @@ impl CustomInput {
}

/// A generator for [`CustomInput`] used in this example
pub struct CustomInputGenerator {
pub max_len: usize,
pub struct CustomInputGenerator<S: HasRand> {
pub bytes_generator: RandBytesGenerator<S>,
}

impl CustomInputGenerator {
impl<S: HasRand> CustomInputGenerator<S> {
/// Creates a new [`CustomInputGenerator`]
pub fn new(max_len: usize) -> Self {
Self { max_len }
pub fn new(max_len: usize) -> Result<Self, Error> {
Ok(Self {
bytes_generator: RandBytesGenerator::new(max_len)?,
})
}
}

impl<S> Generator<CustomInput, S> for CustomInputGenerator
impl<S> Generator<CustomInput, S> for CustomInputGenerator<S>
where
S: HasRand,
{
fn generate(&mut self, state: &mut S) -> Result<CustomInput, Error> {
let mut generator = RandBytesGenerator::new(self.max_len);
let generator = &mut self.bytes_generator;

let byte_array = generator.generate(state).unwrap().target_bytes().into();
let optional_byte_array = state
Expand All @@ -103,10 +105,10 @@ where
S: HasRand,
{
/// Creates a new [`ToggleOptionalByteArrayMutator`]
pub fn new(length: usize) -> Self {
Self {
generator: RandBytesGenerator::new(length),
}
pub fn new(length: usize) -> Result<Self, Error> {
Ok(Self {
generator: RandBytesGenerator::new(length)?,
})
}
}

Expand Down
19 changes: 13 additions & 6 deletions fuzzers/structure_aware/baby_fuzzer_custom_input/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ mod input;

#[cfg(windows)]
use std::ptr::write_volatile;
use std::{path::PathBuf, ptr::write};
use std::{
path::PathBuf,
ptr::{addr_of_mut, write},
};

use input::{
CustomInput, CustomInputGenerator, ToggleBooleanMutator, ToggleOptionalByteArrayMutator,
Expand Down Expand Up @@ -37,8 +40,9 @@ use {
};

/// Coverage map with explicit assignments due to the lack of instrumentation
static mut SIGNALS: [u8; 16] = [0; 16];
static mut SIGNALS_PTR: *mut u8 = unsafe { SIGNALS.as_mut_ptr() };
const SIGNALS_LEN: usize = 16;
static mut SIGNALS: [u8; SIGNALS_LEN] = [0; 16];
static mut SIGNALS_PTR: *mut u8 = addr_of_mut!(SIGNALS) as _;

/// Assign a signal to the signals map
fn signals_set(idx: usize) {
Expand Down Expand Up @@ -78,7 +82,7 @@ pub fn main() {
};

// Create an observation channel using the signals map
let observer = unsafe { StdMapObserver::from_mut_ptr("signals", SIGNALS_PTR, SIGNALS.len()) };
let observer = unsafe { StdMapObserver::from_mut_ptr("signals", SIGNALS_PTR, SIGNALS_LEN) };

// Feedback to rate the interestingness of an input
let mut feedback = MaxMapFeedback::new(&observer);
Expand Down Expand Up @@ -127,7 +131,8 @@ pub fn main() {
.expect("Failed to create the Executor");

// Generator of printable bytearrays of max size 32
let mut generator = CustomInputGenerator::new(1);
let mut generator =
CustomInputGenerator::new(1).expect("Failed to create our custom input generator");

// Generate 8 initial inputs
state
Expand Down Expand Up @@ -178,7 +183,9 @@ pub fn main() {
// Then, mutators for the optional byte array, these return MutationResult::Skipped if the part is not present
.merge(optional_mapped_mutators)
// A custom mutator that sets the optional byte array to None if present, and generates a random byte array of length 1 if it is not
.prepend(ToggleOptionalByteArrayMutator::new(1))
.prepend(
ToggleOptionalByteArrayMutator::new(1).expect("Failed to create bytearray mutator"),
)
// Finally, a custom mutator that toggles the boolean part of the input
.prepend(ToggleBooleanMutator);

Expand Down
11 changes: 7 additions & 4 deletions fuzzers/structure_aware/baby_fuzzer_gramatron/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
fs,
io::{BufReader, Read},
path::{Path, PathBuf},
ptr::addr_of_mut,
};

use libafl::{
Expand All @@ -27,8 +28,9 @@ use libafl::{
use libafl_bolts::{rands::StdRand, tuples::tuple_list};

/// Coverage map with explicit assignments due to the lack of instrumentation
static mut SIGNALS: [u8; 16] = [0; 16];
static mut SIGNALS_PTR: *mut u8 = unsafe { SIGNALS.as_mut_ptr() };
const SIGNALS_LEN: usize = 16;
static mut SIGNALS: [u8; SIGNALS_LEN] = [0; SIGNALS_LEN];
static mut SIGNALS_PTR: *mut u8 = addr_of_mut!(SIGNALS) as _;
/*
/// Assign a signal to the signals map
fn signals_set(idx: usize) {
Expand Down Expand Up @@ -58,7 +60,7 @@ pub fn main() {
};

// Create an observation channel using the signals map
let observer = unsafe { StdMapObserver::from_mut_ptr("signals", SIGNALS_PTR, SIGNALS.len()) };
let observer = unsafe { StdMapObserver::from_mut_ptr("signals", SIGNALS_PTR, SIGNALS_LEN) };

// Feedback to rate the interestingness of an input
let mut feedback = MaxMapFeedback::new(&observer);
Expand Down Expand Up @@ -154,7 +156,8 @@ pub fn main() {
GramatronRecursionMutator::new()
),
2,
);
)
.unwrap();
let mut stages = tuple_list!(StdMutationalStage::new(mutator));

fuzzer
Expand Down
5 changes: 3 additions & 2 deletions fuzzers/structure_aware/baby_fuzzer_grimoire/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub fn main() {
.expect("Failed to create the Executor");

// Setup a mutational stage with a basic bytes mutator
let mutator = StdScheduledMutator::with_max_stack_pow(havoc_mutations(), 2);
let mutator = StdScheduledMutator::with_max_stack_pow(havoc_mutations(), 2).unwrap();
let grimoire_mutator = StdScheduledMutator::with_max_stack_pow(
tuple_list!(
GrimoireExtensionMutator::new(),
Expand All @@ -150,7 +150,8 @@ pub fn main() {
GrimoireRandomDeleteMutator::new(),
),
3,
);
)
.unwrap();
let mut stages = tuple_list!(
generalization,
StdMutationalStage::new(mutator),
Expand Down
3 changes: 2 additions & 1 deletion fuzzers/structure_aware/baby_fuzzer_nautilus/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ pub fn main() {
NautilusSpliceMutator::new(&context),
),
2,
);
)
.unwrap();
let mut stages = tuple_list!(StdMutationalStage::new(mutator));

fuzzer
Expand Down
2 changes: 1 addition & 1 deletion fuzzers/structure_aware/baby_fuzzer_tokens/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn main() {
.expect("Failed to create the Executor");

// Setup a mutational stage with a basic bytes mutator
let mutator = StdScheduledMutator::with_max_stack_pow(encoded_mutations(), 2);
let mutator = StdScheduledMutator::with_max_stack_pow(encoded_mutations(), 2).unwrap();
let mut stages = tuple_list!(StdMutationalStage::new(mutator));

println!("Decoder {:?} ...", &encoder_decoder);
Expand Down
Loading

0 comments on commit 4fc136c

Please sign in to comment.