Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libafl_bolts: more rands improvements #2096

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions libafl/src/corpus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ impl From<CorpusId> for usize {
#[macro_export]
macro_rules! random_corpus_id {
($corpus:expr, $rand:expr) => {{
let cnt = $corpus.count() as u64;
let nth = $rand.below(cnt) as usize;
let cnt = $corpus.count();
let nth = $rand.below(cnt);
$corpus.nth(nth)
}};
}
Expand All @@ -78,8 +78,8 @@ macro_rules! random_corpus_id {
#[macro_export]
macro_rules! random_corpus_id_with_disabled {
($corpus:expr, $rand:expr) => {{
let cnt = $corpus.count_all() as u64;
let nth = $rand.below(cnt) as usize;
let cnt = $corpus.count_all();
let nth = $rand.below(cnt);
$corpus.nth_from_all(nth)
}};
}
Expand Down
4 changes: 2 additions & 2 deletions libafl/src/generators/gramatron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ where
.last()
.map_or(self.automaton.init_state, |last| {
let triggers = &self.automaton.pda[last.state];
let idx = state.rand_mut().below(triggers.len() as u64) as usize;
let idx = state.rand_mut().below(triggers.len());
triggers[idx].dest
});

while current_state != final_state {
let triggers = &self.automaton.pda[current_state];
let idx = state.rand_mut().below(triggers.len() as u64) as usize;
let idx = state.rand_mut().below(triggers.len());
let trigger = &triggers[idx];
input
.terminals_mut()
Expand Down
4 changes: 2 additions & 2 deletions libafl/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where
S: HasRand,
{
fn generate(&mut self, state: &mut S) -> Result<BytesInput, Error> {
let mut size = state.rand_mut().below(self.max_size as u64);
let mut size = state.rand_mut().below(self.max_size);
if size == 0 {
size = 1;
}
Expand Down Expand Up @@ -141,7 +141,7 @@ where
S: HasRand,
{
fn generate(&mut self, state: &mut S) -> Result<BytesInput, Error> {
let mut size = state.rand_mut().below(self.max_size as u64);
let mut size = state.rand_mut().below(self.max_size);
if size == 0 {
size = 1;
}
Expand Down
28 changes: 14 additions & 14 deletions libafl/src/mutators/encoded_mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ impl<S: HasRand> Mutator<EncodedInput, S> for EncodedDeleteMutator {
return Ok(MutationResult::Skipped);
}

let off = state.rand_mut().below(size as u64) as usize;
let len = state.rand_mut().below((size - off) as u64) as usize;
let off = state.rand_mut().below(size);
let len = state.rand_mut().below(size - off);
input.codes_mut().drain(off..off + len);

Ok(MutationResult::Mutated)
Expand Down Expand Up @@ -198,8 +198,8 @@ where
if size == 0 {
return Ok(MutationResult::Skipped);
}
let off = state.rand_mut().below((size + 1) as u64) as usize;
let mut len = 1 + state.rand_mut().below(min(16, size as u64)) as usize;
let off = state.rand_mut().below(size + 1);
let mut len = 1 + state.rand_mut().below(min(16, size));

if size + len > max_size {
if max_size > size {
Expand All @@ -212,7 +212,7 @@ where
let from = if size == len {
0
} else {
state.rand_mut().below((size - len) as u64) as usize
state.rand_mut().below(size - len)
};

input.codes_mut().resize(size + len, 0);
Expand Down Expand Up @@ -254,9 +254,9 @@ impl<S: HasRand> Mutator<EncodedInput, S> for EncodedCopyMutator {
return Ok(MutationResult::Skipped);
}

let from = state.rand_mut().below(size as u64) as usize;
let to = state.rand_mut().below(size as u64) as usize;
let len = 1 + state.rand_mut().below((size - max(from, to)) as u64) as usize;
let from = state.rand_mut().below(size);
let to = state.rand_mut().below(size);
let len = 1 + state.rand_mut().below(size - max(from, to));

unsafe {
buffer_self_copy(input.codes_mut(), from, to, len);
Expand Down Expand Up @@ -310,9 +310,9 @@ where
}

let max_size = state.max_size();
let from = state.rand_mut().below(other_size as u64) as usize;
let to = state.rand_mut().below(size as u64) as usize;
let mut len = 1 + state.rand_mut().below((other_size - from) as u64) as usize;
let from = state.rand_mut().below(other_size);
let to = state.rand_mut().below(size);
let mut len = 1 + state.rand_mut().below(other_size - from);

if size + len > max_size {
if max_size > size {
Expand Down Expand Up @@ -383,9 +383,9 @@ where
return Ok(MutationResult::Skipped);
}

let from = state.rand_mut().below(other_size as u64) as usize;
let len = state.rand_mut().below(min(other_size - from, size) as u64) as usize;
let to = state.rand_mut().below((size - len) as u64) as usize;
let from = state.rand_mut().below(other_size);
let len = state.rand_mut().below(min(other_size - from, size));
let to = state.rand_mut().below(size - len);

let other_testcase = state.corpus().get_from_all(idx)?.borrow_mut();
// no need to load the input again, it'll already be present at this point.
Expand Down
10 changes: 5 additions & 5 deletions libafl/src/mutators/gramatron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
Error, HasMetadata,
};

const RECUR_THRESHOLD: u64 = 5;
const RECUR_THRESHOLD: usize = 5;

/// A random mutator for grammar fuzzing
#[derive(Debug)]
Expand All @@ -41,7 +41,7 @@ where
input: &mut GramatronInput,
) -> Result<MutationResult, Error> {
if !input.terminals().is_empty() {
let size = state.rand_mut().below(input.terminals().len() as u64 + 1) as usize;
let size = state.rand_mut().below(input.terminals().len() + 1);
input.terminals_mut().truncate(size);
}
if self.generator.append_generated_terminals(input, state) > 0 {
Expand Down Expand Up @@ -119,7 +119,7 @@ where

let idx = random_corpus_id!(state.corpus(), state.rand_mut());

let insert_at = state.rand_mut().below(input.terminals().len() as u64) as usize;
let insert_at = state.rand_mut().below(input.terminals().len());

let rand_num = state.rand_mut().next();

Expand Down Expand Up @@ -212,11 +212,11 @@ where
let chosen_nums = self.counters.get(&chosen).unwrap().0;

#[allow(clippy::cast_sign_loss, clippy::pedantic)]
let mut first = state.rand_mut().below(chosen_nums as u64 - 1) as i64;
let mut first = state.rand_mut().below(chosen_nums - 1) as i64;
#[allow(clippy::cast_sign_loss, clippy::pedantic)]
let mut second = state
.rand_mut()
.between(first as u64 + 1, chosen_nums as u64 - 1) as i64;
.between(first as usize + 1, chosen_nums - 1) as i64;

let mut idx_1 = 0;
let mut idx_2 = 0;
Expand Down
14 changes: 6 additions & 8 deletions libafl/src/mutators/grimoire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ where
}
};

let token_find = state.rand_mut().below(tokens_len as u64) as usize;
let mut token_replace = state.rand_mut().below(tokens_len as u64) as usize;
let token_find = state.rand_mut().below(tokens_len);
let mut token_replace = state.rand_mut().below(tokens_len);
if token_find == token_replace {
token_replace = state.rand_mut().below(tokens_len as u64) as usize;
token_replace = state.rand_mut().below(tokens_len);
}

let stop_at_first = state.rand_mut().coinflip(0.5);
Expand All @@ -268,7 +268,7 @@ where
let mut mutated = MutationResult::Skipped;

let gen = generalised_meta.generalized_mut();
let rand_idx = fast_bound(rand_idx, gen.len() as u64) as usize;
let rand_idx = fast_bound(rand_idx, gen.len());

'first: for item in &mut gen[..rand_idx] {
if let GeneralizedItem::Bytes(bytes) = item {
Expand Down Expand Up @@ -360,10 +360,8 @@ where
{
self.gap_indices.push(i);
}
let min_idx =
self.gap_indices[state.rand_mut().below(self.gap_indices.len() as u64) as usize];
let max_idx =
self.gap_indices[state.rand_mut().below(self.gap_indices.len() as u64) as usize];
let min_idx = self.gap_indices[state.rand_mut().below(self.gap_indices.len())];
let max_idx = self.gap_indices[state.rand_mut().below(self.gap_indices.len())];

let (min_idx, max_idx) = (min(min_idx, max_idx), max(min_idx, max_idx));

Expand Down
4 changes: 2 additions & 2 deletions libafl/src/mutators/mopt_mutator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ where
mode: MOptMode,
finds_before: usize,
mutations: MT,
max_stack_pow: u64,
max_stack_pow: usize,
phantom: PhantomData<(I, S)>,
}

Expand Down Expand Up @@ -521,7 +521,7 @@ where
pub fn new(
state: &mut S,
mutations: MT,
max_stack_pow: u64,
max_stack_pow: usize,
swarm_num: usize,
) -> Result<Self, Error> {
if !state.has_metadata::<MOpt>() {
Expand Down
10 changes: 5 additions & 5 deletions libafl/src/mutators/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ where
if input.parts().is_empty() {
Ok(MutationResult::Skipped)
} else {
let selected = state.rand_mut().below(input.parts().len() as u64) as usize;
let selected = state.rand_mut().below(input.parts().len());
let mutated = input.part_mut(selected).unwrap();
self.mutate(state, mutated)
}
Expand Down Expand Up @@ -153,7 +153,7 @@ where
.map(|(idx, part)| (idx, part.bytes().len()));

if let Some((part_idx, size)) = maybe_size {
let target = state.rand_mut().below(size as u64) as usize;
let target = state.rand_mut().below(size);
let range = rand_range(state, other_size, min(other_size, size - target));

let [part, chosen] = match part_idx.cmp(&choice) {
Expand Down Expand Up @@ -195,7 +195,7 @@ where
drop(other_testcase);
let size = part.bytes().len();

let target = state.rand_mut().below(size as u64) as usize;
let target = state.rand_mut().below(size);
let range = rand_range(state, other_size, min(other_size, size - target));

let other_testcase = state.corpus().get(idx)?.borrow_mut();
Expand Down Expand Up @@ -257,7 +257,7 @@ where
.map(|(idx, part)| (idx, part.bytes().len()));

if let Some((part_idx, size)) = maybe_size {
let target = state.rand_mut().below(size as u64) as usize;
let target = state.rand_mut().below(size);
let range = rand_range(state, other_size, min(other_size, size - target));

let [part, chosen] = match part_idx.cmp(&choice) {
Expand Down Expand Up @@ -299,7 +299,7 @@ where
drop(other_testcase);
let size = part.bytes().len();

let target = state.rand_mut().below(size as u64) as usize;
let target = state.rand_mut().below(size);
let range = rand_range(state, other_size, min(other_size, size - target));

let other_testcase = state.corpus().get(idx)?.borrow_mut();
Expand Down
32 changes: 16 additions & 16 deletions libafl/src/mutators/mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ pub fn buffer_set<T: Clone>(data: &mut [T], from: usize, len: usize, val: T) {
/// This problem corresponds to: <https://oeis.org/A059036>
#[inline]
pub fn rand_range<S: HasRand>(state: &mut S, upper: usize, max_len: usize) -> Range<usize> {
let len = 1 + state.rand_mut().below(max_len as u64) as usize;
let len = 1 + state.rand_mut().below(max_len);
// sample from [1..upper + len]
let mut offset2 = 1 + state.rand_mut().below((upper + len - 1) as u64) as usize;
let mut offset2 = 1 + state.rand_mut().below(upper + len - 1);
let offset1 = offset2.saturating_sub(len);
if offset2 > upper {
offset2 = upper;
Expand All @@ -77,7 +77,7 @@ pub fn rand_range<S: HasRand>(state: &mut S, upper: usize, max_len: usize) -> Ra
}

/// The max value that will be added or subtracted during add mutations
pub const ARITH_MAX: u64 = 35;
pub const ARITH_MAX: usize = 35;

/// Interesting 8-bit values from AFL
pub const INTERESTING_8: [i8; 9] = [-128, -1, 0, 1, 16, 32, 64, 100, 127];
Expand Down Expand Up @@ -413,8 +413,8 @@ macro_rules! interesting_mutator_impl {
Ok(MutationResult::Skipped)
} else {
let bytes = input.bytes_mut();
let upper_bound = (bytes.len() + 1 - size_of::<$size>()) as u64;
let idx = state.rand_mut().below(upper_bound) as usize;
let upper_bound = (bytes.len() + 1 - size_of::<$size>());
let idx = state.rand_mut().below(upper_bound);
let val = *state.rand_mut().choose(&$interesting) as $size;
let new_bytes = match state.rand_mut().choose(&[0, 1]) {
0 => val.to_be_bytes(),
Expand Down Expand Up @@ -548,8 +548,8 @@ where
return Ok(MutationResult::Skipped);
}

let mut amount = 1 + state.rand_mut().below(16) as usize;
let offset = state.rand_mut().below(size as u64 + 1) as usize;
let mut amount = 1 + state.rand_mut().below(16);
let offset = state.rand_mut().below(size + 1);

if size + amount > max_size {
if max_size > size {
Expand All @@ -559,7 +559,7 @@ where
}
}

let val = input.bytes()[state.rand_mut().below(size as u64) as usize];
let val = input.bytes()[state.rand_mut().below(size)];

input.bytes_mut().resize(size + amount, 0);
unsafe {
Expand Down Expand Up @@ -602,8 +602,8 @@ where
return Ok(MutationResult::Skipped);
}

let mut amount = 1 + state.rand_mut().below(16) as usize;
let offset = state.rand_mut().below(size as u64 + 1) as usize;
let mut amount = 1 + state.rand_mut().below(16);
let offset = state.rand_mut().below(size + 1);

if size + amount > max_size {
if max_size > size {
Expand Down Expand Up @@ -733,7 +733,7 @@ where
return Ok(MutationResult::Skipped);
}

let target = state.rand_mut().below(size as u64) as usize;
let target = state.rand_mut().below(size);
let range = rand_range(state, size, size - target);

unsafe {
Expand Down Expand Up @@ -776,7 +776,7 @@ where
return Ok(MutationResult::Skipped);
}

let target = state.rand_mut().below(size as u64) as usize;
let target = state.rand_mut().below(size);
// make sure that the sampled range is both in bounds and of an acceptable size
let max_insert_len = min(size - target, state.max_size() - size);
let range = rand_range(state, size, min(16, max_insert_len));
Expand Down Expand Up @@ -1091,7 +1091,7 @@ where
}

let range = rand_range(state, other_size, min(other_size, max_size - size));
let target = state.rand_mut().below(size as u64) as usize;
let target = state.rand_mut().below(size);

let other_testcase = state.corpus().get_from_all(idx)?.borrow_mut();
// No need to load the input again, it'll still be cached.
Expand Down Expand Up @@ -1172,7 +1172,7 @@ where
return Ok(MutationResult::Skipped);
}

let target = state.rand_mut().below(size as u64) as usize;
let target = state.rand_mut().below(size);
let range = rand_range(state, other_size, min(other_size, size - target));

let other_testcase = state.corpus().get_from_all(idx)?.borrow_mut();
Expand Down Expand Up @@ -1243,13 +1243,13 @@ where
let (f, l) = locate_diffs(input.bytes(), other.bytes());

if f != l && f >= 0 && l >= 2 {
(f as u64, l as u64)
(f as usize, l as usize)
} else {
return Ok(MutationResult::Skipped);
}
};

let split_at = state.rand_mut().between(first_diff, last_diff) as usize;
let split_at = state.rand_mut().between(first_diff, last_diff);

let other_testcase = state.corpus().get_from_all(idx)?.borrow_mut();
// Input will already be loaded.
Expand Down
Loading
Loading