Skip to content

Commit

Permalink
Implement hamt fuzzer (#872)
Browse files Browse the repository at this point in the history
* Implement hamt fuzzer

* Change author
  • Loading branch information
austinabell authored Nov 30, 2020
1 parent 36154ff commit 8193a4b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ipld/hamt/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

target
corpus
artifacts
Cargo.lock
29 changes: 29 additions & 0 deletions ipld/hamt/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

[package]
name = "ipld_hamt-fuzz"
version = "0.0.0"
authors = ["ChainSafe Systems <info@chainsafe.io>"]
publish = false
edition = "2018"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.3"
arbitrary = { version = "0.4", features = ["derive"] }
ahash = "0.5"
db = { path = "../../../node/db" }

[dependencies.ipld_hamt]
path = ".."

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[[bin]]
name = "hamt_fuzz"
path = "fuzz_targets/hamt_fuzz.rs"
test = false
doc = false
49 changes: 49 additions & 0 deletions ipld/hamt/fuzz/fuzz_targets/hamt_fuzz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![no_main]
use arbitrary::Arbitrary;
use ipld_hamt::Hamt;
use libfuzzer_sys::fuzz_target;

#[derive(Debug, Arbitrary)]
struct Operation {
key: u64,
method: Method,
}

#[derive(Debug, Arbitrary)]
enum Method {
Insert(u64),
Remove,
Get,
}

fuzz_target!(|data: (u8, Vec<Operation>)| {
let (flush_rate, operations) = data;
let db = db::MemoryDB::default();
let mut hamt = Hamt::<_, _, _>::new_with_bit_width(&db, 5);
let mut elements = ahash::AHashMap::new();

let flush_rate = (flush_rate as usize).saturating_add(5);
for (i, Operation { key, method }) in operations.into_iter().enumerate() {
if i % flush_rate == 0 {
// Periodic flushing of Hamt to fuzz blockstore usage also
hamt.flush().unwrap();
}

match method {
Method::Insert(v) => {
elements.insert(key, v);
hamt.set(key, v).unwrap();
}
Method::Remove => {
let el = elements.remove(&key);
let hamt_deleted = hamt.delete(&key).unwrap().map(|(_, v)| v);
assert_eq!(hamt_deleted, el);
}
Method::Get => {
let ev = elements.get(&key);
let av = hamt.get(&key).unwrap();
assert_eq!(av, ev);
}
}
}
});

0 comments on commit 8193a4b

Please sign in to comment.