Skip to content

Commit 85c1d03

Browse files
authored
Rename libafl_bolts::rands::Rand::zero_upto to below_or_zero. (#2911)
Hi LibAFL! I was playing with the [`Rand`] trait when I realized that the documentation of [`Rand::zero_upto`] did not match what I was expected: https://github.com/AFLplusplus/LibAFL/blob/fd6271fa356f3addda6db33f37db7e42a2c99bbc/libafl_bolts/src/rands/mod.rs#L139-L142 When using the following RNGs, [`Rand::zero_upto`] never returns the upper bound `n` as it would have been expected according to the documentation: - `RomuDuoJrRand` - `RomuTrioRand` - `Sfc64Rand` - `XkcdRand` - `XorShift64Rand` - `Xoshiro256PlusPlusRand` The default implementation of [`Rand::zero_upto`] is to use [`fast_bound_usize`], which excludes the given upper bound, thus I believe here that the default implementation of [`Rand::zero_upto`] is wrong. As discussed here: #2911 (comment), we believe that renaming the method would be better than changing the actual implementation. [`Rand`]: https://github.com/AFLplusplus/LibAFL/blob/fd6271fa356f3addda6db33f37db7e42a2c99bbc/libafl_bolts/src/rands/mod.rs#L108 [`Rand::zero_upto`]: https://github.com/AFLplusplus/LibAFL/blob/fd6271fa356f3addda6db33f37db7e42a2c99bbc/libafl_bolts/src/rands/mod.rs#L139-L142 [`fast_bound_usize`]: https://github.com/AFLplusplus/LibAFL/blob/fd6271fa356f3addda6db33f37db7e42a2c99bbc/libafl_bolts/src/rands/mod.rs#L100-L103
1 parent 5c5f6af commit 85c1d03

File tree

5 files changed

+6
-6
lines changed

5 files changed

+6
-6
lines changed

libafl/src/mutators/mopt_mutator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ where
608608
{
609609
/// Compute the number of iterations used to apply stacked mutations
610610
fn iterations(&self, state: &mut S, _: &I) -> u64 {
611-
1 << (1 + state.rand_mut().zero_upto(self.max_stack_pow))
611+
1 << (1 + state.rand_mut().below_or_zero(self.max_stack_pow))
612612
}
613613

614614
/// Get the next mutation to apply

libafl/src/mutators/mutations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn buffer_set<T: Clone>(data: &mut [T], from: usize, len: usize, val: T) {
7373
pub fn rand_range<S: HasRand>(state: &mut S, upper: usize, max_len: NonZeroUsize) -> Range<usize> {
7474
let len = 1 + state.rand_mut().below(max_len);
7575
// sample from [1..upper + len]
76-
let mut offset2 = 1 + state.rand_mut().zero_upto(upper + len - 1);
76+
let mut offset2 = 1 + state.rand_mut().below_or_zero(upper + len - 1);
7777
let offset1 = offset2.saturating_sub(len);
7878
if offset2 > upper {
7979
offset2 = upper;

libafl/src/mutators/scheduled.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ where
145145
{
146146
/// Compute the number of iterations used to apply stacked mutations
147147
fn iterations(&self, state: &mut S, _: &I) -> u64 {
148-
1 << (1 + state.rand_mut().zero_upto(self.max_stack_pow))
148+
1 << (1 + state.rand_mut().below_or_zero(self.max_stack_pow))
149149
}
150150

151151
/// Get the next mutation to apply

libafl/src/mutators/tuneable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ where
132132
iters
133133
} else {
134134
// fall back to random
135-
1 << (1 + state.rand_mut().zero_upto(self.max_stack_pow))
135+
1 << (1 + state.rand_mut().below_or_zero(self.max_stack_pow))
136136
}
137137
} else {
138138
// We will sample using the mutation probabilities.

libafl_bolts/src/rands/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ pub trait Rand: Debug + Serialize + DeserializeOwned {
136136
fast_bound(self.next(), upper_bound_excl)
137137
}
138138

139-
/// Gets a value between [0, n]
140-
fn zero_upto(&mut self, n: usize) -> usize {
139+
/// Gets a value below the given one or zero
140+
fn below_or_zero(&mut self, n: usize) -> usize {
141141
fast_bound_usize(self.next(), n)
142142
}
143143

0 commit comments

Comments
 (0)