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 1.5k
Add xcm fuzzer #3869
Merged
Merged
Add xcm fuzzer #3869
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
20380be
Extend xcm-simulator with a fuzzer for xcm exec
c6516db
Update cargo.toml in xcm-simulator-fuzzer
7a279b7
Add xcm-fuzzer to honggfuzz workflow
49a8506
Merge branch 'master' into pr/3869
shawntabrizi c8d8448
Update Cargo.lock
shawntabrizi 4176319
Update xcm/xcm-simulator/fuzzer/Cargo.toml so honggfuzz shows up on top
viniul 0ec390b
Update relay_chain.rs in xcm-fuzzer
4708734
Use MAX_XCM_DECODE_DEPTH instead of hardcoded decode limit in xcm-fuzzer
49a1368
Add comment on how to generate coverage report in xcm-fuzzer
ce5d9dc
fix warnings, fmt, and unused result
shawntabrizi 8da5693
Merge branch 'master' into pr/3869
shawntabrizi 04235ca
Merge branch 'master' into pr/3869
shawntabrizi 1b95b7c
fix compiler
shawntabrizi 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
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,32 @@ | ||
| [package] | ||
| name = "xcm-simulator-fuzzer" | ||
| version = "0.9.9" | ||
| authors = ["Parity Technologies <admin@parity.io>"] | ||
| description = "Examples of xcm-simulator usage." | ||
| edition = "2018" | ||
|
|
||
| [dependencies] | ||
| codec = { package = "parity-scale-codec", version = "2.0.0" } | ||
| scale-info = { version = "1.0", features = ["derive"] } | ||
|
|
||
| frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" } | ||
| frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" } | ||
| pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" } | ||
| sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" } | ||
| sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } | ||
| sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" } | ||
| sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" } | ||
|
|
||
| xcm = { path = "../../" } | ||
| xcm-simulator = { path = "../" } | ||
| xcm-executor = { path = "../../xcm-executor" } | ||
| xcm-builder = { path = "../../xcm-builder" } | ||
| pallet-xcm = { path = "../../pallet-xcm" } | ||
| polkadot-core-primitives = { path = "../../../core-primitives" } | ||
| polkadot-runtime-parachains = { path = "../../../runtime/parachains" } | ||
| polkadot-parachain = { path = "../../../parachain" } | ||
| honggfuzz = "0.5.54" | ||
|
|
||
| [[bin]] | ||
| path = "src/fuzz.rs" | ||
| name = "xcm-fuzzer" | ||
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,163 @@ | ||
| // Copyright 2021 Parity Technologies (UK) Ltd. | ||
| // This file is part of Polkadot. | ||
|
|
||
| // Polkadot is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Polkadot is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| mod parachain; | ||
| mod relay_chain; | ||
|
|
||
| use polkadot_parachain::primitives::Id as ParaId; | ||
| use sp_runtime::traits::AccountIdConversion; | ||
| use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain}; | ||
| use honggfuzz::fuzz; | ||
| use xcm_simulator::TestExt; | ||
| use codec::{Decode, DecodeLimit, Encode}; | ||
|
|
||
| use frame_support::assert_ok; | ||
| use xcm::latest::prelude::*; | ||
|
|
||
|
|
||
| pub const ALICE: sp_runtime::AccountId32 = sp_runtime::AccountId32::new([0u8; 32]); | ||
| pub const INITIAL_BALANCE: u128 = 1_000_000_000; | ||
|
|
||
| decl_test_parachain! { | ||
| pub struct ParaA { | ||
| Runtime = parachain::Runtime, | ||
| XcmpMessageHandler = parachain::MsgQueue, | ||
| DmpMessageHandler = parachain::MsgQueue, | ||
| new_ext = para_ext(1), | ||
| } | ||
| } | ||
|
|
||
| decl_test_parachain! { | ||
| pub struct ParaB { | ||
| Runtime = parachain::Runtime, | ||
| XcmpMessageHandler = parachain::MsgQueue, | ||
| DmpMessageHandler = parachain::MsgQueue, | ||
| new_ext = para_ext(2), | ||
| } | ||
| } | ||
|
|
||
| decl_test_relay_chain! { | ||
| pub struct Relay { | ||
| Runtime = relay_chain::Runtime, | ||
| XcmConfig = relay_chain::XcmConfig, | ||
| new_ext = relay_ext(), | ||
| } | ||
| } | ||
|
|
||
| decl_test_network! { | ||
| pub struct MockNet { | ||
| relay_chain = Relay, | ||
| parachains = vec![ | ||
| (1, ParaA), | ||
| (2, ParaB), | ||
| ], | ||
| } | ||
| } | ||
|
|
||
| pub fn para_account_id(id: u32) -> relay_chain::AccountId { | ||
| ParaId::from(id).into_account() | ||
| } | ||
|
|
||
| pub fn para_ext(para_id: u32) -> sp_io::TestExternalities { | ||
| use parachain::{MsgQueue, Runtime, System}; | ||
|
|
||
| let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap(); | ||
|
|
||
| pallet_balances::GenesisConfig::<Runtime> { balances: vec![(ALICE, INITIAL_BALANCE)] } | ||
| .assimilate_storage(&mut t) | ||
| .unwrap(); | ||
|
|
||
| let mut ext = sp_io::TestExternalities::new(t); | ||
| ext.execute_with(|| { | ||
| System::set_block_number(1); | ||
| MsgQueue::set_para_id(para_id.into()); | ||
| }); | ||
| ext | ||
| } | ||
|
|
||
| pub fn relay_ext() -> sp_io::TestExternalities { | ||
| use relay_chain::{Runtime, System}; | ||
|
|
||
| let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap(); | ||
|
|
||
| pallet_balances::GenesisConfig::<Runtime> { | ||
| balances: vec![(ALICE, INITIAL_BALANCE), (para_account_id(1), INITIAL_BALANCE)], | ||
| } | ||
| .assimilate_storage(&mut t) | ||
| .unwrap(); | ||
|
|
||
| let mut ext = sp_io::TestExternalities::new(t); | ||
| ext.execute_with(|| System::set_block_number(1)); | ||
| ext | ||
| } | ||
|
|
||
| pub type RelayChainPalletXcm = pallet_xcm::Pallet<relay_chain::Runtime>; | ||
| pub type ParachainPalletXcm = pallet_xcm::Pallet<parachain::Runtime>; | ||
|
|
||
| fn run_one_input(data: &[u8]) { | ||
| MockNet::reset(); | ||
| if let Ok(m) = Xcm::decode_all_with_depth_limit(8, data){ | ||
|
KiChjang marked this conversation as resolved.
Outdated
|
||
| #[cfg(not(fuzzing))] | ||
| { | ||
| println!("Executing message {:?}", m); | ||
| } | ||
| ParaA::execute_with(|| { | ||
| ParachainPalletXcm::send_xcm( | ||
| Here, | ||
| Parent.into(), | ||
| m, | ||
| ); | ||
| }); | ||
| Relay::execute_with(|| { | ||
| use relay_chain::{Event, System}; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| #[cfg(fuzzing)] | ||
| { | ||
| loop { | ||
| fuzz!(|data: &[u8]| { | ||
| run_one_input(data); | ||
| }); | ||
| } | ||
| } | ||
| #[cfg(not(fuzzing))] | ||
| { | ||
| use std::env; | ||
| use std::fs; | ||
| use std::fs::File; | ||
| use std::io::Read; | ||
| let args: Vec<_> = env::args().collect(); | ||
| let md = fs::metadata(&args[1]).unwrap(); | ||
| let all_files = match md.is_dir() { | ||
| true => fs::read_dir(&args[1]) | ||
| .unwrap() | ||
| .map(|x| x.unwrap().path().to_str().unwrap().to_string()) | ||
| .collect::<Vec<String>>(), | ||
| false => (&args[1..]).to_vec(), | ||
| }; | ||
| println!("All_files {:?}", all_files); | ||
| for argument in all_files { | ||
| println!("Now doing file {:?}", argument); | ||
| let mut buffer: Vec<u8> = Vec::new(); | ||
| let mut f = File::open(argument).unwrap(); | ||
| f.read_to_end(&mut buffer).unwrap(); | ||
| run_one_input(&buffer.as_slice()); | ||
| } | ||
| } | ||
|
drahnr marked this conversation as resolved.
Outdated
|
||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.