Skip to content

Commit

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

* Fix typo

* Change author
  • Loading branch information
austinabell authored Nov 30, 2020
1 parent 15c0e67 commit 36154ff
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ipld/amt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ criterion = "0.3.1"
ipld_blockstore = { path = "../blockstore", features = ["tracking"] }

[[bench]]
name = "amt_beckmark"
name = "amt_benchmark"
path = "benches/amt_benchmark.rs"
harness = false
5 changes: 5 additions & 0 deletions ipld/amt/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/amt/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

[package]
name = "ipld_amt-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_amt]
path = ".."

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

[[bin]]
name = "amt_fuzz"
path = "fuzz_targets/amt_fuzz.rs"
test = false
doc = false
52 changes: 52 additions & 0 deletions ipld/amt/fuzz/fuzz_targets/amt_fuzz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#![no_main]
use arbitrary::Arbitrary;
use ipld_amt::{Amt, MAX_INDEX};
use libfuzzer_sys::fuzz_target;

#[derive(Debug, Arbitrary)]
struct Operation {
idx: 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 amt = Amt::new(&db);
let mut elements = ahash::AHashMap::new();

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

match method {
Method::Insert(v) => {
elements.insert(idx, v);
amt.set(idx, v).unwrap();
}
Method::Remove => {
let el = elements.remove(&idx);
let amt_deleted = amt.delete(idx).unwrap();
assert_eq!(amt_deleted, el.is_some());
}
Method::Get => {
let ev = elements.get(&idx);
let av = amt.get(idx).unwrap();
assert_eq!(av, ev);
}
}
}
});
4 changes: 2 additions & 2 deletions ipld/amt/src/amt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ where
Ok(Self { root, block_store })
}

// Getter for height
/// Gets the height of the `Amt`.
pub fn height(&self) -> u64 {
self.root.height
}

// Getter for count
/// Gets count of elements added in the `Amt`.
pub fn count(&self) -> u64 {
self.root.count
}
Expand Down

0 comments on commit 36154ff

Please sign in to comment.