This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fuzzer for Pallet Bags List * Some small updates * Fuzzer for Pallet Bags List This PR adds a fuzzer for the `SortedListProvider` API exposed by pallet-bags-list. * Feature gate code NOT used by fuzz feature * Create Enum for list actions * fix some small mistakes * try and make CI happy * fmt * Do not insert before updating * clean up some misc. comments * marginally improve Node::sanity_check * Change ID_RANGE to 25_000 * comma * try improve correct feature gating so no unused code Co-authored-by: thiolliere <gui.thiolliere@gmail.com>
- Loading branch information
Showing
9 changed files
with
144 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
hfuzz_target | ||
hfuzz_workspace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "pallet-bags-list-fuzzer" | ||
version = "4.0.0-dev" | ||
authors = ["Parity Technologies <admin@parity.io>"] | ||
edition = "2018" | ||
license = "Apache-2.0" | ||
homepage = "https://substrate.dev" | ||
repository = "https://github.com/paritytech/substrate/" | ||
description = "Fuzzer for FRAME pallet bags list" | ||
readme = "README.md" | ||
publish = false | ||
|
||
[dependencies] | ||
honggfuzz = "0.5" | ||
rand = { version = "0.8", features = ["std", "small_rng"] } | ||
|
||
pallet-bags-list = { version = "4.0.0-dev", features = ["fuzz"], path = ".." } | ||
frame-election-provider-support = { version = "4.0.0-dev", path = "../../election-provider-support", features = ["runtime-benchmarks"] } | ||
|
||
[[bin]] | ||
name = "bags-list" | ||
path = "src/main.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2021 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! # Running | ||
//! Running this fuzzer can be done with `cargo hfuzz run bags-list`. `honggfuzz` CLI options can | ||
//! be used by setting `HFUZZ_RUN_ARGS`, such as `-n 4` to use 4 threads. | ||
//! | ||
//! # Debugging a panic | ||
//! Once a panic is found, it can be debugged with | ||
//! `cargo hfuzz run-debug fixed_point hfuzz_workspace/bags_list/*.fuzz`. | ||
//! | ||
//! # More information | ||
//! More information about `honggfuzz` can be found | ||
//! [here](https://docs.rs/honggfuzz/). | ||
|
||
use frame_election_provider_support::{SortedListProvider, VoteWeight}; | ||
use honggfuzz::fuzz; | ||
use pallet_bags_list::mock::{AccountId, BagsList, ExtBuilder}; | ||
use std::convert::From; | ||
|
||
const ID_RANGE: AccountId = 25_000; | ||
|
||
/// Actions of a `SortedListProvider` that we fuzz. | ||
enum Action { | ||
Insert, | ||
Update, | ||
Remove, | ||
} | ||
|
||
impl From<u32> for Action { | ||
fn from(v: u32) -> Self { | ||
let num_variants = Self::Remove as u32 + 1; | ||
match v % num_variants { | ||
_x if _x == Action::Insert as u32 => Action::Insert, | ||
_x if _x == Action::Update as u32 => Action::Update, | ||
_x if _x == Action::Remove as u32 => Action::Remove, | ||
_ => unreachable!(), | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
ExtBuilder::default().build_and_execute(|| loop { | ||
fuzz!(|data: (AccountId, VoteWeight, u32)| { | ||
let (account_id_seed, vote_weight, action_seed) = data; | ||
|
||
let id = account_id_seed % ID_RANGE; | ||
let action = Action::from(action_seed); | ||
|
||
match action { | ||
Action::Insert => { | ||
if BagsList::on_insert(id.clone(), vote_weight).is_err() { | ||
// this was a duplicate id, which is ok. We can just update it. | ||
BagsList::on_update(&id, vote_weight); | ||
} | ||
assert!(BagsList::contains(&id)); | ||
}, | ||
Action::Update => { | ||
let already_contains = BagsList::contains(&id); | ||
BagsList::on_update(&id, vote_weight); | ||
if already_contains { | ||
assert!(BagsList::contains(&id)); | ||
} | ||
}, | ||
Action::Remove => { | ||
BagsList::on_remove(&id); | ||
assert!(!BagsList::contains(&id)); | ||
}, | ||
} | ||
|
||
assert!(BagsList::sanity_check().is_ok()); | ||
}) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters