-
Notifications
You must be signed in to change notification settings - Fork 471
feat(fuzz): add fuzzing harness for LocalChain with CI job
#2239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
erickcestari
wants to merge
3
commits into
bitcoindevkit:master
Choose a base branch
from
erickcestari:fuzz-bdk
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ Cargo.lock | |
| *.sqlite* | ||
|
|
||
| crates/electrum/target | ||
| fuzz/target | ||
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,4 @@ | ||
| target | ||
| corpus | ||
| artifacts | ||
| coverage |
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,37 @@ | ||
| [package] | ||
| name = "bdk_chain-fuzz" | ||
| version = "0.0.0" | ||
| publish = false | ||
| edition = "2021" | ||
|
|
||
| [package.metadata] | ||
| cargo-fuzz = true | ||
|
|
||
| [workspace] | ||
| members = ["."] | ||
|
|
||
| [dependencies] | ||
| libfuzzer-sys = { version = "0.4", optional = true } | ||
| arbitrary = { version = "1.4.1", features = ["derive"] } | ||
| honggfuzz = { version = "0.5.61", optional = true } | ||
| afl = { version = "0.18.2", optional = true } | ||
| bdk_chain = { path = "../crates/chain" } | ||
|
|
||
| [features] | ||
| afl_fuzz = ["afl"] | ||
| honggfuzz_fuzz = ["honggfuzz"] | ||
| libfuzzer_fuzz = ["libfuzzer-sys"] | ||
|
|
||
| [[bin]] | ||
| name = "local_chain_apply_update" | ||
| path = "fuzz_targets/local_chain_apply_update.rs" | ||
| test = false | ||
| doc = false | ||
| bench = false | ||
|
|
||
| [[bin]] | ||
| name = "local_chain_apply_update_header" | ||
| path = "fuzz_targets/local_chain_apply_update_header.rs" | ||
| test = false | ||
| doc = false | ||
| bench = false |
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,202 @@ | ||
| #![cfg_attr(feature = "libfuzzer_fuzz", no_main)] | ||
|
|
||
| use bdk_chain::bitcoin::hashes::Hash; | ||
| use bdk_chain::bitcoin::BlockHash; | ||
| use bdk_chain::local_chain::{LocalChain, MissingGenesisError}; | ||
| use bdk_chain::BlockId; | ||
| use bdk_chain_fuzz::arbitrary::{self, Arbitrary, Unstructured}; | ||
| use bdk_chain_fuzz::checks::{assert_changeset_against_chains, assert_checkpoint_order}; | ||
|
|
||
| /// An operation to perform against the chain under test. | ||
| #[derive(Arbitrary, Debug, Clone, Copy)] | ||
| enum Op { | ||
| /// `apply_update` with an independently constructed chain as the update. | ||
| ApplyUpdate, | ||
| /// `insert_block` with an arbitrary height and hash. | ||
| InsertBlock, | ||
| /// `disconnect_from` an existing checkpoint or an arbitrary block id. | ||
| DisconnectFrom, | ||
| /// `apply_header` with a header that usually connects to an existing checkpoint. | ||
| ApplyHeader, | ||
| /// `apply_header_connected_to` with an arbitrarily picked connection point. | ||
| ApplyHeaderConnectedTo, | ||
| /// `apply_update` with an update derived by mutating the chain's own tip, so the | ||
| /// update shares `Arc` nodes with the original and exercises `merge_chains`' | ||
| /// `eq_ptr` fast path. | ||
| ApplyDerivedUpdate, | ||
| } | ||
|
|
||
| fn assert_chain(chain: &LocalChain) { | ||
| assert_checkpoint_order(chain); | ||
|
|
||
| let tip = chain.chain_tip(); | ||
| assert_eq!(tip, chain.tip().block_id()); | ||
| for cp in chain.iter_checkpoints() { | ||
| assert_eq!( | ||
| chain.is_block_in_chain(cp.block_id(), tip), | ||
| Some(true), | ||
| "every checkpoint must be in the chain of its own tip" | ||
| ); | ||
| let mut flipped = cp.hash().to_byte_array(); | ||
| flipped[0] ^= 1; | ||
| let wrong = BlockId { | ||
| height: cp.height(), | ||
| hash: BlockHash::from_byte_array(flipped), | ||
| }; | ||
| assert_eq!( | ||
| chain.is_block_in_chain(wrong, tip), | ||
| Some(false), | ||
| "a conflicting hash at an occupied height must not be in chain" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| fn do_test(data: &[u8]) { | ||
| let mut u = Unstructured::new(data); | ||
|
|
||
| let op_count = match u.int_in_range(1..=16) { | ||
| Ok(count) => count, | ||
| Err(_) => return, | ||
| }; | ||
|
|
||
| let mut chain: Option<LocalChain> = None; | ||
| for _ in 0..op_count { | ||
| if chain.is_none() { | ||
| match arbitrary::blockhash_chain(&mut u) { | ||
| Ok(Some(initial)) => chain = Some(initial), | ||
| Ok(None) => continue, | ||
| Err(_) => break, | ||
| } | ||
| continue; | ||
| } | ||
| let chain = chain.as_mut().expect("initialized above"); | ||
|
|
||
| let op = match Op::arbitrary(&mut u) { | ||
| Ok(op) => op, | ||
| Err(_) => break, | ||
| }; | ||
| let pre = chain.clone(); | ||
| match op { | ||
| Op::ApplyUpdate => { | ||
| let update = match arbitrary::blockhash_chain(&mut u) { | ||
| Ok(Some(update)) => update, | ||
| Ok(None) => continue, | ||
| Err(_) => break, | ||
| }; | ||
| let result = chain.apply_update(update.tip()); | ||
| assert_changeset_against_chains(pre, chain, &result); | ||
| } | ||
| Op::InsertBlock => { | ||
| let (height, hash) = match u32::arbitrary(&mut u) | ||
| .and_then(|height| arbitrary::hash(&mut u).map(|hash| (height, hash))) | ||
| { | ||
| Ok(block) => block, | ||
| Err(_) => break, | ||
| }; | ||
| let result = chain.insert_block(height, hash); | ||
| assert_changeset_against_chains(pre, chain, &result); | ||
| match &result { | ||
| Ok(_) => { | ||
| assert_eq!(chain.get(height).map(|cp| cp.hash()), Some(hash)); | ||
| } | ||
| Err(err) => { | ||
| assert_eq!( | ||
| chain.get(err.height).map(|cp| cp.hash()), | ||
| Some(err.original_hash), | ||
| "insert conflict must report the existing checkpoint" | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| Op::DisconnectFrom => { | ||
| let block_id = match arbitrary::block_id(&mut u, chain, &[]) { | ||
| Ok(block_id) => block_id, | ||
| Err(_) => break, | ||
| }; | ||
| let result = chain.disconnect_from(block_id); | ||
| assert_changeset_against_chains(pre, chain, &result); | ||
| match &result { | ||
| Ok(changeset) if !changeset.blocks.is_empty() => { | ||
| assert!(chain.tip().height() < block_id.height); | ||
| } | ||
| Ok(_) => {} | ||
| Err(MissingGenesisError) => { | ||
| assert_eq!(block_id.height, 0); | ||
| assert_eq!(block_id.hash, chain.genesis_hash()); | ||
| } | ||
| } | ||
| } | ||
| Op::ApplyHeader => { | ||
| let (header, height) = match arbitrary::connectable_header(&mut u, chain) { | ||
| Ok(header) => header, | ||
| Err(_) => break, | ||
| }; | ||
| let result = chain.apply_header(&header, height); | ||
| assert_changeset_against_chains(pre, chain, &result); | ||
| if result.is_ok() { | ||
| assert_eq!( | ||
| chain.get(height).map(|cp| cp.hash()), | ||
| Some(header.block_hash()) | ||
| ); | ||
| } | ||
| } | ||
| Op::ApplyHeaderConnectedTo => { | ||
| let params: arbitrary::Result<_> = (|| { | ||
| let (header, height) = arbitrary::connectable_header(&mut u, chain)?; | ||
| let connected_to = arbitrary::block_id(&mut u, chain, &[])?; | ||
| Ok((header, height, connected_to)) | ||
| })(); | ||
| let (header, height, connected_to) = match params { | ||
| Ok(params) => params, | ||
| Err(_) => break, | ||
| }; | ||
| let result = chain.apply_header_connected_to(&header, height, connected_to); | ||
| assert_changeset_against_chains(pre, chain, &result); | ||
| if result.is_ok() { | ||
| assert_eq!( | ||
| chain.get(height).map(|cp| cp.hash()), | ||
| Some(header.block_hash()) | ||
| ); | ||
| } | ||
| } | ||
| Op::ApplyDerivedUpdate => { | ||
| let params: arbitrary::Result<_> = (|| { | ||
| let insert = bool::arbitrary(&mut u)?; | ||
| let height = u32::arbitrary(&mut u)?; | ||
| let hash = arbitrary::hash(&mut u)?; | ||
| Ok((insert, height, hash)) | ||
| })(); | ||
| let (insert, height, hash) = match params { | ||
| Ok(params) => params, | ||
| Err(_) => break, | ||
| }; | ||
| let (update_tip, height) = if insert { | ||
| // Height 0 would panic (genesis is immutable in `CheckPoint::insert`). | ||
| let height = height.max(1); | ||
| (chain.tip().insert(height, hash), height) | ||
| } else { | ||
| let height = match chain.tip().height().checked_add(1 + height % 4) { | ||
| Some(height) => height, | ||
| None => continue, | ||
| }; | ||
| match chain.tip().extend([(height, hash)]) { | ||
| Ok(tip) => (tip, height), | ||
| Err(_) => continue, | ||
| } | ||
| }; | ||
| let result = chain.apply_update(update_tip); | ||
| assert_changeset_against_chains(pre, chain, &result); | ||
| if result.is_ok() { | ||
| assert_eq!(chain.get(height).map(|cp| cp.hash()), Some(hash)); | ||
| } | ||
| } | ||
| } | ||
| assert_chain(chain); | ||
| } | ||
|
|
||
| if let Some(chain) = chain { | ||
| assert_chain(&chain); | ||
| } | ||
| } | ||
|
|
||
| bdk_chain_fuzz::fuzz_main!(do_test); | ||
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.
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.
@erickcestari we probably should also add coverage for the changeset operations/APIs.