Skip to content

Commit

Permalink
Type check fuzzers on CI and fix another bug (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul authored Jun 7, 2024
1 parent a9b8f2a commit 6347db6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
run: rustup update stable
- name: Check formatting with cargo fmt
run: cargo fmt --all -- --check
- name: Check fuzzer formatting with cargo fmt
run: cargo fmt --manifest-path fuzz/Cargo.toml --all --check
clippy:
name: clippy
runs-on: ubuntu-latest
Expand All @@ -38,7 +40,11 @@ jobs:
- uses: actions/checkout@v3
- name: Get latest version of stable Rust
run: rustup update stable
- name: Get latest version of nightly Rust
run: rustup update nightly
- name: Run tests
run: cargo test --release
- name: Check all examples, binaries, etc
run: cargo check --all-targets
- name: Check fuzzer targets
run: cargo +nightly check --manifest-path fuzz/Cargo.toml --all-targets
23 changes: 18 additions & 5 deletions fuzz/fuzz_targets/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@ use milhouse::builder::Builder;

fuzz_target!(|data: &[u8]| {
// We use the first byte as 'depth'
if data.len() < 1 {return}
if data.len() < 1 {
return;
}
let depth = data[0];

let data = &data[1..];
let length = data.len();
if length > 256 {return}
if length > 256 {
return;
}

let mut builder = Builder::<u8>::new(depth as usize, 0);
let Ok(mut builder) = Builder::<u8>::new(depth as usize, 0) else {
return;
};
for i in 0..length {
builder.push(data[i]);
if builder.push(data[i]).is_err() {
assert!(i > (1 << depth));
}
}

let Ok((arc_tree, depth, len)) = builder.finish() else {return};
let Ok((arc_tree, tree_depth, len)) = builder.finish() else {
return;
};
assert_eq!(tree_depth, depth as usize);
assert!(len.as_usize() <= length);
assert_eq!(arc_tree.compute_len(), len.as_usize());
});
20 changes: 14 additions & 6 deletions fuzz/fuzz_targets/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@

use libfuzzer_sys::fuzz_target;
use milhouse::List;
use typenum::{Unsigned, U16, U32};
use typenum::U16;

fuzz_target!(|data: &[u8]| {
let length = data.len();
if length > 256 {return}
if length > 256 {
return;
}

// Create list using .push()
let mut list1 = List::<u8, U16>::empty();
for i in 0..length {
list1.push(data[i]);
if list1.push(data[i]).is_err() {
assert!(i >= 16);
}
}
list1.apply_updates();

list1.apply_updates().unwrap();

// Create list using iterator
let list2 = List::<u8, U16>::try_from_iter(data.iter().copied());
let Ok(list2) = List::<u8, U16>::try_from_iter(data.iter().copied()) else {
return;
};

assert_eq!(list1, list2);
});
6 changes: 6 additions & 0 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ impl<T: Value, N: Unsigned, U: UpdateMap<T>> List<T, N, U> {

let (tree, depth, length) = builder.finish()?;

// Check the length to cover the case where the capacity implied by packing_depth is
// greater than N. E.g. the builder might pack up to 32 u8s, even if N is < 32.
if length.as_usize() > N::to_usize() {
return Err(Error::BuilderFull);
}

Ok(Self::from_parts(tree, depth, length))
}

Expand Down

0 comments on commit 6347db6

Please sign in to comment.