This repository was 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.7k
Fuzzer for Pallet Bags List #9851
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
30890a3
Fuzzer for Pallet Bags List
emostov 727b416
Some small updates
emostov ab1989f
Fuzzer for Pallet Bags List
emostov 7d43123
Feature gate code NOT used by fuzz feature
emostov 29f244b
Create Enum for list actions
emostov 72c3c98
Create Enum for list actions
emostov 6da84f8
fix some small mistakes
emostov d8eeae4
try and make CI happy
emostov 206f97d
fmt
emostov 7111f38
Do not insert before updating
emostov af8411f
clean up some misc. comments
emostov 7cca905
marginally improve Node::sanity_check
emostov 5590d94
Change ID_RANGE to 25_000
emostov aa8a523
comma
emostov 1da7aa9
Merge remote-tracking branch 'origin' into zeke-bags-list-fuzzer
emostov 2a8cd5d
try improve correct feature gating so no unused code
emostov c3469e5
Merge remote-tracking branch 'origin/master' into zeke-bags-list-fuzzer
gui1117 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 <[email protected]>"] | ||
| 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be a good time for you to go and re-read
sanity_checkand make sure it is checking everything there is to check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found one small check to add 7cca905