Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ libfuzzer-sys = "0.4"
arbitrary = { version = "1.3.1", features = ["derive"] }
tempfile.workspace = true
bitvec.workspace = true
nomt-core = { path = "../core" }

[dependencies.nomt]
path = "../nomt"
Expand Down
43 changes: 36 additions & 7 deletions fuzz/fuzz_targets/api_surface.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#![no_main]

use std::collections::{HashMap, HashSet};
use std::collections::{BTreeMap, HashMap, HashSet};

use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;

use nomt::{
hasher::Blake3Hasher, trie::KeyPath, KeyReadWrite, Nomt, Options, SessionParams, Value,
WitnessMode,
hasher::{Blake3Hasher, ValueHasher},
trie::{KeyPath, ValueHash},
KeyReadWrite, Nomt, Options, SessionParams, Value, WitnessMode,
};
use nomt_core::update::build_trie;

fuzz_target!(|run: Run| {
let db = open_db(run.commit_concurrency);
let mut model = BTreeMap::<KeyPath, ValueHash>::new();

for call in run.calls.calls {
match call {
Expand All @@ -33,7 +36,26 @@ fuzz_target!(|run: Run| {
session.warm_up(key_path);
}
SessionCall::CommitAndProve { keys } => {
for (key_path, key_rw) in &keys {
match key_rw {
KeyReadWrite::Write(Some(value))
| KeyReadWrite::ReadThenWrite(_, Some(value)) => {
model.insert(*key_path, Blake3Hasher::hash_value(value));
}
KeyReadWrite::Write(None)
| KeyReadWrite::ReadThenWrite(_, None) => {
model.remove(key_path);
}
KeyReadWrite::Read(_) => {}
}
}
let _ = session.finish(keys).unwrap().commit(&db).unwrap();
let expected_root = build_trie::<Blake3Hasher>(
0,
model.iter().map(|(k, v)| (*k, *v)),
|_| {},
);
assert_eq!(db.root().into_inner(), expected_root);
break;
}
SessionCall::Drop => {
Expand Down Expand Up @@ -81,10 +103,17 @@ impl Context {
input: &mut arbitrary::Unstructured<'_>,
) -> arbitrary::Result<Option<Value>> {
if input.arbitrary()? {
let mut size = *input.choose(&[0, 4096])?;
if input.arbitrary()? {
size += 1;
}
// Inline leaf values, both sides of the overflow threshold (MAX_LEAF_VALUE_SIZE = 1332)
// and overflow values spanning several pages.
let size = match input.int_in_range(0..=6)? {
0 => 0,
1 => input.int_in_range(1..=32)?,
2 => input.int_in_range(33..=1331)?,
3 => input.int_in_range(1332..=1333)?,
4 => input.int_in_range(1334..=4096)?,
5 => input.int_in_range(4097..=8192)?,
_ => input.int_in_range(8193..=32 * 1024)?,
};
let mut buf = vec![0; size];
input.fill_buffer(&mut buf)?;
Ok(Some(buf.into()))
Expand Down
Loading