Skip to content

Commit f1940e9

Browse files
committed
Merge branch 'master' into modular_handshake
2 parents 4e6b25a + c9c9415 commit f1940e9

File tree

7 files changed

+906
-18
lines changed

7 files changed

+906
-18
lines changed

fuzz/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Fuzzing
2+
3+
Fuzz tests generate a ton of random parameter arguments to the program and then validate that none cause it to crash.
4+
5+
## How does it work?
6+
7+
Typically, Travis CI will run `travis-fuzz.sh` on one of the environments the automated tests are configured for.
8+
This is the most time-consuming component of the continuous integration workflow, so it is recommended that you detect
9+
issues locally, and Travis merely acts as a sanity check. Fuzzing is further only effective with
10+
a lot of CPU time, indicating that if crash scenarios are discovered on Travis with its low
11+
runtime constraints, the crash is caused relatively easily.
12+
13+
## How do I run fuzz tests locally?
14+
15+
You typically won't need to run the entire combination of different fuzzing tools. For local execution, `honggfuzz`
16+
should be more than sufficient.
17+
18+
### Setup
19+
20+
To install `honggfuzz`, simply run
21+
22+
```shell
23+
cargo update
24+
cargo install --force honggfuzz
25+
```
26+
27+
### Execution
28+
29+
To run the Hongg fuzzer, do
30+
31+
```shell
32+
export CPU_COUNT=1 # replace as needed
33+
export HFUZZ_BUILD_ARGS="--features honggfuzz_fuzz"
34+
export HFUZZ_RUN_ARGS="-n $CPU_COUNT --exit_upon_crash"
35+
36+
export TARGET="msg_ping_target" # replace with the target to be fuzzed
37+
cargo hfuzz run $TARGET
38+
```
39+
40+
To see a list of available fuzzing targets, run:
41+
42+
```shell
43+
ls ./src/bin/
44+
```
45+
46+
## A fuzz test failed on Travis, what do I do?
47+
48+
You're trying to create a PR, but need to find the underlying cause of that pesky fuzz failure blocking the merge?
49+
50+
Worry not, for this is easily traced.
51+
52+
If your Travis output log looks like this:
53+
54+
```
55+
Size:639 (i,b,hw,ed,ip,cmp): 0/0/0/0/0/1, Tot:0/0/0/2036/5/28604
56+
Seen a crash. Terminating all fuzzing threads
57+
58+
… # a lot of lines in between
59+
60+
<0x0000555555565559> [func:UNKNOWN file: line:0 module:/home/travis/build/rust-bitcoin/rust-lightning/fuzz/hfuzz_target/x86_64-unknown-linux-gnu/release/full_stack_target]
61+
<0x0000000000000000> [func:UNKNOWN file: line:0 module:UNKNOWN]
62+
=====================================================================
63+
2d3136383734090101010101010101010101010101010101010101010101
64+
010101010100040101010101010101010101010103010101010100010101
65+
0069d07c319a4961
66+
The command "if [ "$(rustup show | grep default | grep stable)" != "" ]; then cd fuzz && cargo test --verbose && ./travis-fuzz.sh; fi" exited with 1.
67+
```
68+
69+
Note that the penultimate stack trace line ends in `release/full_stack_target]`. That indicates that
70+
the failing target was `full_stack`. To reproduce the error locally, simply copy the hex,
71+
and run the following from the `fuzz` directory:
72+
73+
```shell
74+
export TARGET="full_stack" # adjust for your output
75+
export HEX="2d3136383734090101010101010101010101010101010101010101010101\
76+
010101010100040101010101010101010101010103010101010100010101\
77+
0069d07c319a4961" # adjust for your output
78+
79+
mkdir -p ./test_cases/$TARGET
80+
echo $HEX | xxd -r -p > ./test_cases/$TARGET/any_filename_works
81+
82+
export RUST_BACKTRACE=1
83+
cargo test
84+
```
85+
86+
This will reproduce the failing fuzz input and yield a usable stack trace.

lightning-net-tokio/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ mod tests {
507507
fn handle_channel_announcement(&self, _msg: &ChannelAnnouncement) -> Result<bool, LightningError> { Ok(false) }
508508
fn handle_channel_update(&self, _msg: &ChannelUpdate) -> Result<bool, LightningError> { Ok(false) }
509509
fn handle_htlc_fail_channel_update(&self, _update: &HTLCFailChannelUpdate) { }
510-
fn get_next_channel_announcements(&self, _starting_point: u64, _batch_amount: u8) -> Vec<(ChannelAnnouncement, ChannelUpdate, ChannelUpdate)> { Vec::new() }
510+
fn get_next_channel_announcements(&self, _starting_point: u64, _batch_amount: u8) -> Vec<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> { Vec::new() }
511511
fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<NodeAnnouncement> { Vec::new() }
512512
fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool { false }
513513
}

lightning/src/chain/chaininterface.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::marker::PhantomData;
2222
use std::ptr;
2323

2424
/// Used to give chain error details upstream
25+
#[derive(Clone)]
2526
pub enum ChainError {
2627
/// Client doesn't support UTXO lookup (but the chain hash matches our genesis block hash)
2728
NotSupported,

lightning/src/ln/msgs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ pub trait RoutingMessageHandler : Send + Sync {
601601
/// Gets a subset of the channel announcements and updates required to dump our routing table
602602
/// to a remote node, starting at the short_channel_id indicated by starting_point and
603603
/// including the batch_amount entries immediately higher in numerical value than starting_point.
604-
fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, ChannelUpdate, ChannelUpdate)>;
604+
fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)>;
605605
/// Gets a subset of the node announcements required to dump our routing table to a remote node,
606606
/// starting at the node *after* the provided publickey and including batch_amount entries
607607
/// immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.

lightning/src/ln/peer_handler.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
367367
InitSyncTracker::ChannelsSyncing(c) if c < 0xffff_ffff_ffff_ffff => {
368368
let steps = ((MSG_BUFF_SIZE - peer.pending_outbound_buffer.len() + 2) / 3) as u8;
369369
let all_messages = self.message_handler.route_handler.get_next_channel_announcements(c, steps);
370-
for &(ref announce, ref update_a, ref update_b) in all_messages.iter() {
370+
for &(ref announce, ref update_a_option, ref update_b_option) in all_messages.iter() {
371371
encode_and_send_msg!(announce);
372-
encode_and_send_msg!(update_a);
373-
encode_and_send_msg!(update_b);
372+
if let &Some(ref update_a) = update_a_option {
373+
encode_and_send_msg!(update_a);
374+
}
375+
if let &Some(ref update_b) = update_b_option {
376+
encode_and_send_msg!(update_b);
377+
}
374378
peer.sync_status = InitSyncTracker::ChannelsSyncing(announce.contents.short_channel_id + 1);
375379
}
376380
if all_messages.is_empty() || all_messages.len() != steps as usize {
@@ -1364,7 +1368,7 @@ mod tests {
13641368
Err(msgs::LightningError { err: "", action: msgs::ErrorAction::IgnoreError })
13651369
}
13661370
fn handle_htlc_fail_channel_update(&self, _update: &msgs::HTLCFailChannelUpdate) {}
1367-
fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, msgs::ChannelUpdate,msgs::ChannelUpdate)> {
1371+
fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(msgs::ChannelAnnouncement, Option<msgs::ChannelUpdate>, Option<msgs::ChannelUpdate>)> {
13681372
let mut chan_anns = Vec::new();
13691373
const TOTAL_UPDS: u64 = 100;
13701374
let end: u64 = min(starting_point + batch_amount as u64, TOTAL_UPDS - self.chan_anns_sent.load(Ordering::Acquire) as u64);
@@ -1373,7 +1377,7 @@ mod tests {
13731377
let chan_upd_2 = get_dummy_channel_update(i);
13741378
let chan_ann = get_dummy_channel_announcement(i);
13751379

1376-
chan_anns.push((chan_ann, chan_upd_1, chan_upd_2));
1380+
chan_anns.push((chan_ann, Some(chan_upd_1), Some(chan_upd_2)));
13771381
}
13781382

13791383
self.chan_anns_sent.fetch_add(chan_anns.len(), Ordering::AcqRel);

0 commit comments

Comments
 (0)