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

avoid DriverMode branching on byte driver #219

Merged
merged 1 commit into from
May 30, 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
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
Loading