Skip to content

Commit

Permalink
Fuzz: drop workarounds for operations that may panic (#92)
Browse files Browse the repository at this point in the history
Fuzz: drop workarounds for operations that may panic now that rutenspitz compares panic behavior
  • Loading branch information
Shnatsel committed Jul 29, 2020
1 parent 5758549 commit 4c5fbbf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 55 deletions.
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false

[dependencies]
tinyvec = { path = "..", features = ["alloc", "nightly_slice_partition_dedup"] }
rutenspitz = "0.2"
rutenspitz = "0.2.1"
honggfuzz = "0.5.45"
arbitrary = { version = "0.4.5", features = ["derive"] }
better-panic = "0.2.0"
34 changes: 7 additions & 27 deletions fuzz/src/bin/arrayish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use honggfuzz::fuzz;
use std::{
fmt::Debug,
iter::FromIterator,
ops::{Bound, RangeBounds},
ops::RangeBounds,
};
use rutenspitz::arbitrary_stateful_operations;

Expand Down Expand Up @@ -47,37 +47,16 @@ arbitrary_stateful_operations! {

pre {
match self {
Self::insert { index, .. } | Self::split_off { at: index } if index > model.len() => {
// TODO: Should test that these identically panic
return;
}
Self::remove { index } | Self::swap_remove { index } if index >= model.len() => {
// TODO: Should test that these identically panic
return;
}
// We are comparing ArrayVec with a limited capacity against
// Vec to which you can push indefinitely. This is a behavior mismatch.
// To compensate we skip adding any elements if it would exceed capacity.
Self::insert { .. } | Self::push { .. } if model.len() == CAPACITY => {
return;
}
Self::resize { new_len, .. } if new_len > CAPACITY => {
return;
}
Self::drain { ref range } => {
// TODO: Should test that these identically panic
let start = match range.start_bound() {
Bound::Included(&n) => n,
Bound::Excluded(&n) => n + 1,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
// If it's already usize::max, doesn't really matter about adding 1
Bound::Included(&n) => n.checked_add(1).unwrap_or(n),
Bound::Excluded(&n) => n,
Bound::Unbounded => model.len(),
};
if start > end || end > model.len() {
return;
}
}

_ => {}
}
}
Expand All @@ -89,14 +68,15 @@ fn fuzz_cycle(data: &[u8]) -> Result<(), ()> {
let mut ring = Unstructured::new(&data);

let mut model = Vec::<u16>::default();
let mut tested: ArrayVec<[u16; 32]> = ArrayVec::new();
let mut tested: ArrayVec<[u16; CAPACITY]> = ArrayVec::new();

let mut _op_trace = String::new();
while let Ok(op) =
<op::Op<u16, ArbRange<usize>> as Arbitrary>::arbitrary(&mut ring)
{
#[cfg(fuzzing_debug)]
_op_trace.push_str(&format!("{}\n", op.to_string()));
// println!("Operations trace:\n{}", _op_trace);
op.execute_and_compare(&mut model, &mut tested);
}

Expand Down
30 changes: 3 additions & 27 deletions fuzz/src/bin/tinyvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use honggfuzz::fuzz;
use std::{
fmt::Debug,
iter::FromIterator,
ops::{Bound, RangeBounds},
ops::RangeBounds,
};
use rutenspitz::arbitrary_stateful_operations;

Expand Down Expand Up @@ -47,35 +47,10 @@ arbitrary_stateful_operations! {

pre {
match self {
Self::insert { index, .. } | Self::split_off { at: index } if index > model.len() => {
// TODO: Should test that these identically panic
return;
}
Self::remove { index } | Self::swap_remove { index } if index >= model.len() => {
// TODO: Should test that these identically panic
return;
}
// Arbitrary limit to avoid allocating too large a buffer
Self::resize { new_len, .. } if new_len > 4 * CAPACITY => {
return;
}
Self::drain { ref range } => {
// TODO: Should test that these identically panic
let start = match range.start_bound() {
Bound::Included(&n) => n,
Bound::Excluded(&n) => n + 1,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
// If it's already usize::max, doesn't really matter about adding 1
Bound::Included(&n) => n.checked_add(1).unwrap_or(n),
Bound::Excluded(&n) => n,
Bound::Unbounded => model.len(),
};
if start > end || end > model.len() {
return;
}
}
_ => {}
}
}
Expand All @@ -87,14 +62,15 @@ fn fuzz_cycle(data: &[u8]) -> Result<(), ()> {
let mut ring = Unstructured::new(&data);

let mut model = Vec::<u16>::default();
let mut tested: TinyVec<[u16; 32]> = TinyVec::new();
let mut tested: TinyVec<[u16; CAPACITY]> = TinyVec::new();

let mut _op_trace = String::new();
while let Ok(op) =
<op::Op<u16, ArbRange<usize>> as Arbitrary>::arbitrary(&mut ring)
{
#[cfg(fuzzing_debug)]
_op_trace.push_str(&format!("{}\n", op.to_string()));
// println!("Operations trace:\n{}", _op_trace);
op.execute_and_compare(&mut model, &mut tested);
}

Expand Down

0 comments on commit 4c5fbbf

Please sign in to comment.