Skip to content

Commit 7fefa00

Browse files
authored
Merge pull request #1939 from TheBlueMatt/2022-01-fuzz-hashbrown
Misc fuzzing tweaks
2 parents ce6bcf6 + f9bafa6 commit 7fefa00

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

fuzz/Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ libfuzzer_fuzz = ["libfuzzer-sys"]
1818
stdin_fuzz = []
1919

2020
[dependencies]
21-
afl = { version = "0.4", optional = true }
22-
lightning = { path = "../lightning", features = ["regex"] }
21+
lightning = { path = "../lightning", features = ["regex", "hashbrown"] }
2322
lightning-rapid-gossip-sync = { path = "../lightning-rapid-gossip-sync" }
2423
bitcoin = { version = "0.29.0", features = ["secp-lowmemory"] }
2524
hex = "0.3"
25+
hashbrown = "0.8"
26+
27+
afl = { version = "0.12", optional = true }
2628
honggfuzz = { version = "0.5", optional = true, default-features = false }
2729
libfuzzer-sys = { version = "0.4", optional = true }
2830

@@ -36,6 +38,8 @@ members = ["."]
3638
[profile.release]
3739
lto = true
3840
codegen-units = 1
41+
debug-assertions = true
42+
overflow-checks = true
3943

4044
# When testing a large fuzz corpus, -O1 offers a nice speedup
4145
[profile.dev]

fuzz/src/chanmon_consistency.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use bitcoin::secp256k1::Secp256k1;
6161

6262
use std::mem;
6363
use std::cmp::{self, Ordering};
64-
use std::collections::{HashSet, hash_map, HashMap};
64+
use hashbrown::{HashSet, hash_map, HashMap};
6565
use std::sync::{Arc,Mutex};
6666
use std::sync::atomic;
6767
use std::io::Cursor;

fuzz/src/full_stack.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use bitcoin::secp256k1::ecdsa::RecoverableSignature;
5858
use bitcoin::secp256k1::Secp256k1;
5959

6060
use std::cell::RefCell;
61-
use std::collections::{HashMap, hash_map};
61+
use hashbrown::{HashMap, hash_map};
6262
use std::convert::TryInto;
6363
use std::cmp;
6464
use std::sync::{Arc, Mutex};
@@ -632,7 +632,9 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
632632
// It's possible the channel has been closed in the mean time, but any other
633633
// failure may be a bug.
634634
if let APIError::ChannelUnavailable { err } = e {
635-
assert_eq!(err, "No such channel");
635+
if !err.starts_with("Can't find a peer matching the passed counterparty node_id ") {
636+
assert_eq!(err, "No such channel");
637+
}
636638
} else { panic!(); }
637639
}
638640
pending_funding_signatures.insert(funding_output, tx);

fuzz/src/router.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use bitcoin::blockdata::constants::genesis_block;
2929
use crate::utils::test_logger;
3030

3131
use std::convert::TryInto;
32-
use std::collections::HashSet;
32+
use hashbrown::HashSet;
3333
use std::sync::Arc;
3434
use std::sync::atomic::{AtomicUsize, Ordering};
3535

lightning/src/chain/onchaintx.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,8 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
476476
// remove it once it reaches the confirmation threshold, or to generate a new claim if the
477477
// transaction is reorged out.
478478
let mut all_inputs_have_confirmed_spend = true;
479-
for outpoint in &request_outpoints {
480-
if let Some(first_claim_txid_height) = self.claimable_outpoints.get(outpoint) {
479+
for outpoint in request_outpoints.iter() {
480+
if let Some(first_claim_txid_height) = self.claimable_outpoints.get(*outpoint) {
481481
// We check for outpoint spends within claims individually rather than as a set
482482
// since requests can have outpoints split off.
483483
if !self.onchain_events_awaiting_threshold_conf.iter()
@@ -811,7 +811,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
811811
for outpoint in request.outpoints() {
812812
log_debug!(logger, "Removing claim tracking for {} due to maturation of claim package {}.",
813813
outpoint, log_bytes!(package_id));
814-
self.claimable_outpoints.remove(&outpoint);
814+
self.claimable_outpoints.remove(outpoint);
815815
#[cfg(anchors)]
816816
self.pending_claim_events.remove(&package_id);
817817
}
@@ -820,7 +820,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
820820
OnchainEvent::ContentiousOutpoint { package } => {
821821
log_debug!(logger, "Removing claim tracking due to maturation of claim tx for outpoints:");
822822
log_debug!(logger, " {:?}", package.outpoints());
823-
self.claimable_outpoints.remove(&package.outpoints()[0]);
823+
self.claimable_outpoints.remove(package.outpoints()[0]);
824824
}
825825
}
826826
} else {
@@ -898,7 +898,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
898898
//- resurect outpoint back in its claimable set and regenerate tx
899899
match entry.event {
900900
OnchainEvent::ContentiousOutpoint { package } => {
901-
if let Some(ancestor_claimable_txid) = self.claimable_outpoints.get(&package.outpoints()[0]) {
901+
if let Some(ancestor_claimable_txid) = self.claimable_outpoints.get(package.outpoints()[0]) {
902902
if let Some(request) = self.pending_claim_requests.get_mut(&ancestor_claimable_txid.0) {
903903
request.merge_package(package);
904904
// Using a HashMap guarantee us than if we have multiple outpoints getting

lightning/src/ln/channelmanager.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2102,7 +2102,7 @@ where
21022102
// short_channel_id is non-0 in any ::Forward.
21032103
if let &PendingHTLCRouting::Forward { ref short_channel_id, .. } = routing {
21042104
if let Some((err, mut code, chan_update)) = loop {
2105-
let id_option = self.short_to_chan_info.read().unwrap().get(&short_channel_id).cloned();
2105+
let id_option = self.short_to_chan_info.read().unwrap().get(short_channel_id).cloned();
21062106
let forwarding_chan_info_opt = match id_option {
21072107
None => { // unknown_next_peer
21082108
// Note that this is likely a timing oracle for detecting whether an scid is a
@@ -2552,7 +2552,7 @@ where
25522552
let per_peer_state = self.per_peer_state.read().unwrap();
25532553
let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
25542554
if let None = peer_state_mutex_opt {
2555-
return Err(APIError::APIMisuseError { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })
2555+
return Err(APIError::ChannelUnavailable { err: format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id) })
25562556
}
25572557

25582558
let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
@@ -7116,7 +7116,7 @@ where
71167116
}
71177117
}
71187118

7119-
for (ref funding_txo, ref mut monitor) in args.channel_monitors.iter_mut() {
7119+
for (funding_txo, monitor) in args.channel_monitors.iter_mut() {
71207120
if !funding_txo_set.contains(funding_txo) {
71217121
log_info!(args.logger, "Broadcasting latest holder commitment transaction for closed channel {}", log_bytes!(funding_txo.to_channel_id()));
71227122
monitor.broadcast_latest_holder_commitment_txn(&args.tx_broadcaster, &args.logger);

0 commit comments

Comments
 (0)