Skip to content

Commit 1e8667a

Browse files
authored
libafl_bolts: more rands improvements (AFLplusplus#2096)
* rands: add missing inline directives See: - https://nnethercote.github.io/perf-book/inlining.html - https://users.rust-lang.org/t/enable-cross-crate-inlining-without-suggesting-inlining/55004/6 * rands: better fast_bound() signature
1 parent 0f42efa commit 1e8667a

File tree

18 files changed

+108
-106
lines changed

18 files changed

+108
-106
lines changed

libafl/src/corpus/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ impl From<CorpusId> for usize {
6767
#[macro_export]
6868
macro_rules! random_corpus_id {
6969
($corpus:expr, $rand:expr) => {{
70-
let cnt = $corpus.count() as u64;
71-
let nth = $rand.below(cnt) as usize;
70+
let cnt = $corpus.count();
71+
let nth = $rand.below(cnt);
7272
$corpus.nth(nth)
7373
}};
7474
}
@@ -78,8 +78,8 @@ macro_rules! random_corpus_id {
7878
#[macro_export]
7979
macro_rules! random_corpus_id_with_disabled {
8080
($corpus:expr, $rand:expr) => {{
81-
let cnt = $corpus.count_all() as u64;
82-
let nth = $rand.below(cnt) as usize;
81+
let cnt = $corpus.count_all();
82+
let nth = $rand.below(cnt);
8383
$corpus.nth_from_all(nth)
8484
}};
8585
}

libafl/src/generators/gramatron.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ where
7676
.last()
7777
.map_or(self.automaton.init_state, |last| {
7878
let triggers = &self.automaton.pda[last.state];
79-
let idx = state.rand_mut().below(triggers.len() as u64) as usize;
79+
let idx = state.rand_mut().below(triggers.len());
8080
triggers[idx].dest
8181
});
8282

8383
while current_state != final_state {
8484
let triggers = &self.automaton.pda[current_state];
85-
let idx = state.rand_mut().below(triggers.len() as u64) as usize;
85+
let idx = state.rand_mut().below(triggers.len());
8686
let trigger = &triggers[idx];
8787
input
8888
.terminals_mut()

libafl/src/generators/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ where
101101
S: HasRand,
102102
{
103103
fn generate(&mut self, state: &mut S) -> Result<BytesInput, Error> {
104-
let mut size = state.rand_mut().below(self.max_size as u64);
104+
let mut size = state.rand_mut().below(self.max_size);
105105
if size == 0 {
106106
size = 1;
107107
}
@@ -141,7 +141,7 @@ where
141141
S: HasRand,
142142
{
143143
fn generate(&mut self, state: &mut S) -> Result<BytesInput, Error> {
144-
let mut size = state.rand_mut().below(self.max_size as u64);
144+
let mut size = state.rand_mut().below(self.max_size);
145145
if size == 0 {
146146
size = 1;
147147
}

libafl/src/mutators/encoded_mutations.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ impl<S: HasRand> Mutator<EncodedInput, S> for EncodedDeleteMutator {
159159
return Ok(MutationResult::Skipped);
160160
}
161161

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

166166
Ok(MutationResult::Mutated)
@@ -198,8 +198,8 @@ where
198198
if size == 0 {
199199
return Ok(MutationResult::Skipped);
200200
}
201-
let off = state.rand_mut().below((size + 1) as u64) as usize;
202-
let mut len = 1 + state.rand_mut().below(min(16, size as u64)) as usize;
201+
let off = state.rand_mut().below(size + 1);
202+
let mut len = 1 + state.rand_mut().below(min(16, size));
203203

204204
if size + len > max_size {
205205
if max_size > size {
@@ -212,7 +212,7 @@ where
212212
let from = if size == len {
213213
0
214214
} else {
215-
state.rand_mut().below((size - len) as u64) as usize
215+
state.rand_mut().below(size - len)
216216
};
217217

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

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

261261
unsafe {
262262
buffer_self_copy(input.codes_mut(), from, to, len);
@@ -310,9 +310,9 @@ where
310310
}
311311

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

317317
if size + len > max_size {
318318
if max_size > size {
@@ -383,9 +383,9 @@ where
383383
return Ok(MutationResult::Skipped);
384384
}
385385

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

390390
let other_testcase = state.corpus().get_from_all(idx)?.borrow_mut();
391391
// no need to load the input again, it'll already be present at this point.

libafl/src/mutators/gramatron.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
Error, HasMetadata,
2121
};
2222

23-
const RECUR_THRESHOLD: u64 = 5;
23+
const RECUR_THRESHOLD: usize = 5;
2424

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

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

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

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

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

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

221221
let mut idx_1 = 0;
222222
let mut idx_2 = 0;

libafl/src/mutators/grimoire.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ where
252252
}
253253
};
254254

255-
let token_find = state.rand_mut().below(tokens_len as u64) as usize;
256-
let mut token_replace = state.rand_mut().below(tokens_len as u64) as usize;
255+
let token_find = state.rand_mut().below(tokens_len);
256+
let mut token_replace = state.rand_mut().below(tokens_len);
257257
if token_find == token_replace {
258-
token_replace = state.rand_mut().below(tokens_len as u64) as usize;
258+
token_replace = state.rand_mut().below(tokens_len);
259259
}
260260

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

270270
let gen = generalised_meta.generalized_mut();
271-
let rand_idx = fast_bound(rand_idx, gen.len() as u64) as usize;
271+
let rand_idx = fast_bound(rand_idx, gen.len());
272272

273273
'first: for item in &mut gen[..rand_idx] {
274274
if let GeneralizedItem::Bytes(bytes) = item {
@@ -360,10 +360,8 @@ where
360360
{
361361
self.gap_indices.push(i);
362362
}
363-
let min_idx =
364-
self.gap_indices[state.rand_mut().below(self.gap_indices.len() as u64) as usize];
365-
let max_idx =
366-
self.gap_indices[state.rand_mut().below(self.gap_indices.len() as u64) as usize];
363+
let min_idx = self.gap_indices[state.rand_mut().below(self.gap_indices.len())];
364+
let max_idx = self.gap_indices[state.rand_mut().below(self.gap_indices.len())];
367365

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

libafl/src/mutators/mopt_mutator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ where
368368
mode: MOptMode,
369369
finds_before: usize,
370370
mutations: MT,
371-
max_stack_pow: u64,
371+
max_stack_pow: usize,
372372
phantom: PhantomData<(I, S)>,
373373
}
374374

@@ -521,7 +521,7 @@ where
521521
pub fn new(
522522
state: &mut S,
523523
mutations: MT,
524-
max_stack_pow: u64,
524+
max_stack_pow: usize,
525525
swarm_num: usize,
526526
) -> Result<Self, Error> {
527527
if !state.has_metadata::<MOpt>() {

libafl/src/mutators/multi.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ where
4444
if input.parts().is_empty() {
4545
Ok(MutationResult::Skipped)
4646
} else {
47-
let selected = state.rand_mut().below(input.parts().len() as u64) as usize;
47+
let selected = state.rand_mut().below(input.parts().len());
4848
let mutated = input.part_mut(selected).unwrap();
4949
self.mutate(state, mutated)
5050
}
@@ -153,7 +153,7 @@ where
153153
.map(|(idx, part)| (idx, part.bytes().len()));
154154

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

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

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

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

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

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

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

305305
let other_testcase = state.corpus().get(idx)?.borrow_mut();

libafl/src/mutators/mutations.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ pub fn buffer_set<T: Clone>(data: &mut [T], from: usize, len: usize, val: T) {
6565
/// This problem corresponds to: <https://oeis.org/A059036>
6666
#[inline]
6767
pub fn rand_range<S: HasRand>(state: &mut S, upper: usize, max_len: usize) -> Range<usize> {
68-
let len = 1 + state.rand_mut().below(max_len as u64) as usize;
68+
let len = 1 + state.rand_mut().below(max_len);
6969
// sample from [1..upper + len]
70-
let mut offset2 = 1 + state.rand_mut().below((upper + len - 1) as u64) as usize;
70+
let mut offset2 = 1 + state.rand_mut().below(upper + len - 1);
7171
let offset1 = offset2.saturating_sub(len);
7272
if offset2 > upper {
7373
offset2 = upper;
@@ -77,7 +77,7 @@ pub fn rand_range<S: HasRand>(state: &mut S, upper: usize, max_len: usize) -> Ra
7777
}
7878

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

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

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

554554
if size + amount > max_size {
555555
if max_size > size {
@@ -559,7 +559,7 @@ where
559559
}
560560
}
561561

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

564564
input.bytes_mut().resize(size + amount, 0);
565565
unsafe {
@@ -602,8 +602,8 @@ where
602602
return Ok(MutationResult::Skipped);
603603
}
604604

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

608608
if size + amount > max_size {
609609
if max_size > size {
@@ -733,7 +733,7 @@ where
733733
return Ok(MutationResult::Skipped);
734734
}
735735

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

739739
unsafe {
@@ -776,7 +776,7 @@ where
776776
return Ok(MutationResult::Skipped);
777777
}
778778

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

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

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

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

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

12451245
if f != l && f >= 0 && l >= 2 {
1246-
(f as u64, l as u64)
1246+
(f as usize, l as usize)
12471247
} else {
12481248
return Ok(MutationResult::Skipped);
12491249
}
12501250
};
12511251

1252-
let split_at = state.rand_mut().between(first_diff, last_diff) as usize;
1252+
let split_at = state.rand_mut().between(first_diff, last_diff);
12531253

12541254
let other_testcase = state.corpus().get_from_all(idx)?.borrow_mut();
12551255
// Input will already be loaded.

0 commit comments

Comments
 (0)