Skip to content

Commit

Permalink
avoid DriverMode branching on byte driver
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft committed May 30, 2024
1 parent 04e6a5b commit 0da7b1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
5 changes: 5 additions & 0 deletions bin/cargo-bolero/src/libfuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ pub(crate) fn reduce(selection: &Selection, reduce: &reduce::Args) -> Result<()>

let inputs = write_control_file(&mut control_file, &corpus_dir)?;

// no point in shrinking an empty corpus
if inputs.is_empty() {
return Ok(());
}

let mut cmd = test_target.command();

let mut args = vec![
Expand Down
37 changes: 19 additions & 18 deletions lib/bolero-generator/src/driver/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,32 @@ impl<'a> FillBytes for ByteSliceDriver<'a> {

#[inline]
fn peek_bytes(&mut self, offset: usize, bytes: &mut [u8]) -> Option<()> {
match self.mode {
DriverMode::Direct => {
if (offset + bytes.len()) > self.input.len() {
None
} else {
bytes.copy_from_slice(&self.input[offset..(offset + bytes.len())]);
Some(())
}
match self.input.len().checked_sub(offset) {
None | Some(0) => {
// no bytes left so fill in zeros
bytes.fill(0);
}
DriverMode::Forced => {
if offset < self.input.len() {
let copy_len = core::cmp::min(bytes.len(), self.input.len() - offset);
bytes[..copy_len].copy_from_slice(&self.input[offset..(offset + copy_len)]);
bytes[copy_len..].fill(0);
} else {
bytes.fill(0);
}
Some(())
Some(remaining_len) if remaining_len >= bytes.len() => {
let input = &self.input[offset..];
let input = &input[..bytes.len()];
bytes.copy_from_slice(input);
}
Some(remaining_len) => {
let input = &self.input[offset..];
// we don't have enough bytes to fill the whole output
let (head, tail) = bytes.split_at_mut(remaining_len);
head.copy_from_slice(input);
tail.fill(0);
}
}

Some(())
}

#[inline]
fn consume_bytes(&mut self, consumed: usize) {
self.input = &self.input[core::cmp::min(consumed, self.input.len())..];
let consumed = consumed.min(self.input.len());
self.input = &self.input[consumed..];
}
}

Expand Down

0 comments on commit 0da7b1d

Please sign in to comment.