Skip to content

Commit 9c2b735

Browse files
authored
Merge pull request #408 from filecoin-project/feat/fuzz-cleanup
Cleanup fuzz test naming and add equivalence AMT test
2 parents 536ef06 + deacbfa commit 9c2b735

File tree

6 files changed

+87
-14
lines changed

6 files changed

+87
-14
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: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,26 @@ cargo-fuzz = true
1111
[dependencies]
1212
libfuzzer-sys = "0.3"
1313
arbitrary = { version = "0.4", features = ["derive"] }
14-
ahash = "0.6"
14+
ahash = "0.7.6"
15+
itertools = "0.10.3"
1516

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

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

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

2626
[[bin]]
27-
name = "amt_fuzz"
28-
path = "fuzz_targets/amt_fuzz.rs"
27+
name = "simple"
28+
path = "fuzz_targets/simple.rs"
29+
test = false
30+
doc = false
31+
32+
[[bin]]
33+
name = "equivalence"
34+
path = "fuzz_targets/equivalence.rs"
2935
test = false
3036
doc = false
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
use itertools::Itertools;
10+
11+
12+
#[derive(Debug, Arbitrary)]
13+
struct Operation {
14+
idx: u64,
15+
method: Method,
16+
}
17+
18+
#[derive(Debug, Arbitrary)]
19+
enum Method {
20+
Insert(u64),
21+
Remove,
22+
Get,
23+
}
24+
25+
fn execute(ops: Vec<Operation>) -> (Cid, ahash::AHashMap<u64, u64>) {
26+
let db = fvm_shared::blockstore::MemoryBlockstore::default();
27+
let mut amt = Amt::new(&db);
28+
let mut elements = ahash::AHashMap::new();
29+
30+
for (i, Operation { idx, method }) in ops.into_iter().enumerate() {
31+
if idx > MAX_INDEX {
32+
continue;
33+
}
34+
35+
match method {
36+
Method::Insert(v) => {
37+
elements.insert(idx, v);
38+
amt.set(idx, v).unwrap();
39+
}
40+
Method::Remove => {
41+
let el = elements.remove(&idx);
42+
let amt_deleted = amt.delete(idx).unwrap();
43+
assert_eq!(amt_deleted, el, "step {}", i);
44+
}
45+
Method::Get => {
46+
let ev = elements.get(&idx);
47+
let av = amt.get(idx).unwrap();
48+
assert_eq!(av, ev, "step {}", i);
49+
}
50+
}
51+
}
52+
(amt.flush().unwrap(), elements)
53+
}
54+
55+
// Verifies that AMT created by this order of operations results in the same CID as
56+
// AMT created by minimal number of operations required.
57+
// The aim is to verify lack of past memory in the AMT structures.
58+
// AMT with same elements should have the same CID, regardless of their past.
59+
fuzz_target!(|ops: Vec<Operation>| {
60+
let (res_cid, m) = execute(ops);
61+
62+
let simplified_ops = m.iter().sorted_by_key(|(_, v)| *v).map(|(k ,v)| {
63+
Operation{idx: *k, method: Method::Insert(*v)}
64+
}).collect();
65+
66+
let (simplified_cid, _) = execute(simplified_ops);
67+
assert_eq!(res_cid, simplified_cid)
68+
});

ipld/hamt/fuzz/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ cargo-fuzz = true
1111
[dependencies]
1212
libfuzzer-sys = "0.3"
1313
arbitrary = { version = "0.4", features = ["derive"] }
14-
ahash = "0.6"
14+
ahash = "0.7.6"
15+
16+
fvm_ipld_hamt = { path = ".." }
1517
fvm_shared = { path = "../../../shared" }
1618

17-
[dependencies.fvm_ipld_hamt]
18-
path = ".."
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)