Skip to content

Commit 34bf3b8

Browse files
committed
Cleanup fuzz test naming and add equivalence AMT test
Signed-off-by: Jakub Sztandera <[email protected]>
1 parent 536ef06 commit 34bf3b8

File tree

6 files changed

+81
-13
lines changed

6 files changed

+81
-13
lines changed

fvm/src/system_actor.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use anyhow::Context;
22
use cid::Cid;
3-
use serde::{Deserialize, Serialize};
4-
53
use fvm_shared::address::Address;
64
use fvm_shared::blockstore::{Blockstore, CborStore};
75
use fvm_shared::encoding::Cbor;
6+
use serde::{Deserialize, Serialize};
87

98
use crate::kernel::{ClassifyResult, Result};
109
use crate::state_tree::{ActorState, StateTree};

ipld/amt/fuzz/Cargo.toml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@ libfuzzer-sys = "0.3"
1313
arbitrary = { version = "0.4", features = ["derive"] }
1414
ahash = "0.6"
1515

16-
[dependencies.fvm_ipld_amt]
17-
path = ".."
16+
cid = { version = "0.8.2", default-features = false, features = ["serde-codec"] }
1817

19-
[dependencies.fvm_shared]
20-
path = "../../../shared"
18+
fvm_ipld_amt = {path = ".."}
19+
fvm_shared = {path = "../../../shared"}
2120

2221
# Prevent this from interfering with workspaces
2322
[workspace]
2423
members = ["."]
2524

2625
[[bin]]
27-
name = "amt_fuzz"
28-
path = "fuzz_targets/amt_fuzz.rs"
26+
name = "simple"
27+
path = "fuzz_targets/simple.rs"
28+
test = false
29+
doc = false
30+
31+
[[bin]]
32+
name = "equivalence"
33+
path = "fuzz_targets/equivalence.rs"
2934
test = false
3035
doc = false
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2019-2022 ChainSafe Systems
2+
// SPDX-License-Identifier: Apache-2.0, MIT
3+
4+
#![no_main]
5+
use arbitrary::Arbitrary;
6+
use fvm_ipld_amt::{Amt, MAX_INDEX};
7+
use libfuzzer_sys::fuzz_target;
8+
use cid::Cid;
9+
10+
11+
#[derive(Debug, Arbitrary)]
12+
struct Operation {
13+
idx: u64,
14+
method: Method,
15+
}
16+
17+
#[derive(Debug, Arbitrary)]
18+
enum Method {
19+
Insert(u64),
20+
Remove,
21+
Get,
22+
}
23+
24+
fn execute(ops: Vec<Operation>) -> (Cid, ahash::AHashMap<u64, u64>) {
25+
let db = fvm_shared::blockstore::MemoryBlockstore::default();
26+
let mut amt = Amt::new(&db);
27+
let mut elements = ahash::AHashMap::new();
28+
29+
for (i, Operation { idx, method }) in ops.into_iter().enumerate() {
30+
if idx > MAX_INDEX {
31+
continue;
32+
}
33+
34+
match method {
35+
Method::Insert(v) => {
36+
elements.insert(idx, v);
37+
amt.set(idx, v).unwrap();
38+
}
39+
Method::Remove => {
40+
let el = elements.remove(&idx);
41+
let amt_deleted = amt.delete(idx).unwrap();
42+
assert_eq!(amt_deleted, el, "step {}", i);
43+
}
44+
Method::Get => {
45+
let ev = elements.get(&idx);
46+
let av = amt.get(idx).unwrap();
47+
assert_eq!(av, ev, "step {}", i);
48+
}
49+
}
50+
}
51+
(amt.flush().unwrap(), elements)
52+
}
53+
54+
fuzz_target!(|ops: Vec<Operation>| {
55+
let (res_cid, m) = execute(ops);
56+
57+
let simplfied_ops = m.iter().map(|(k ,v)| {
58+
Operation{idx: *k, method: Method::Insert(*v)}
59+
}).collect();
60+
61+
let (simplified_cid, _) = execute(simplfied_ops);
62+
63+
assert_eq!(res_cid, simplified_cid)
64+
});

ipld/hamt/fuzz/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ cargo-fuzz = true
1212
libfuzzer-sys = "0.3"
1313
arbitrary = { version = "0.4", features = ["derive"] }
1414
ahash = "0.6"
15-
fvm_shared = { path = "../../../shared" }
1615

17-
[dependencies.fvm_ipld_hamt]
18-
path = ".."
16+
fvm_ipld_hamt = {path = ".."}
17+
fvm_shared = {path = "../../../shared"}
18+
1919

2020
# Prevent this from interfering with workspaces
2121
[workspace]
2222
members = ["."]
2323

2424
[[bin]]
25-
name = "hamt_fuzz"
26-
path = "fuzz_targets/hamt_fuzz.rs"
25+
name = "simple"
26+
path = "fuzz_targets/simple.rs"
2727
test = false
2828
doc = false

0 commit comments

Comments
 (0)