Skip to content

Commit 4de3cfe

Browse files
committed
Fix message targets to not have an internal loop
1 parent a73dea7 commit 4de3cfe

16 files changed

+19
-116
lines changed

fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::AcceptChannel, data, read_pos);
20-
}
16+
test_msg!(msgs::AcceptChannel, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::ClosingSigned, data, read_pos);
20-
}
16+
test_msg!(msgs::ClosingSigned, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::CommitmentSigned, data, read_pos);
20-
}
16+
test_msg!(msgs::CommitmentSigned, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::FundingCreated, data, read_pos);
20-
}
16+
test_msg!(msgs::FundingCreated, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::FundingLocked, data, read_pos);
20-
}
16+
test_msg!(msgs::FundingLocked, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::FundingSigned, data, read_pos);
20-
}
16+
test_msg!(msgs::FundingSigned, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::OpenChannel, data, read_pos);
20-
}
16+
test_msg!(msgs::OpenChannel, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::RevokeAndACK, data, read_pos);
20-
}
16+
test_msg!(msgs::RevokeAndACK, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::Shutdown, data, read_pos);
20-
}
16+
test_msg!(msgs::Shutdown, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_target_template.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::MSG_TARGET, data, read_pos);
20-
}
16+
test_msg!(msgs::MSG_TARGET, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::UpdateAddHTLC, data, read_pos);
20-
}
16+
test_msg!(msgs::UpdateAddHTLC, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::UpdateFailHTLC, data, read_pos);
20-
}
16+
test_msg!(msgs::UpdateFailHTLC, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::UpdateFailMalformedHTLC, data, read_pos);
20-
}
16+
test_msg!(msgs::UpdateFailMalformedHTLC, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::UpdateFee, data, read_pos);
20-
}
16+
test_msg!(msgs::UpdateFee, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ use lightning::util::reset_rng_state;
99
use lightning::ln::msgs::{MsgEncodable, MsgDecodable};
1010

1111
mod utils;
12-
use utils::slice_to_be16;
1312

1413
#[inline]
1514
pub fn do_test(data: &[u8]) {
1615
reset_rng_state();
17-
let mut read_pos = 0;
18-
loop {
19-
test_msg!(msgs::UpdateFulfillHTLC, data, read_pos);
20-
}
16+
test_msg!(msgs::UpdateFulfillHTLC, data);
2117
}
2218

2319
#[cfg(feature = "afl")]

fuzz/fuzz_targets/msg_targets/utils.rs

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,13 @@
11
#![macro_use]
22

3-
#[allow(dead_code)]
4-
#[inline]
5-
pub fn slice_to_be16(v: &[u8]) -> u16 {
6-
((v[0] as u16) << 8*1) |
7-
((v[1] as u16) << 8*0)
8-
}
9-
103
#[macro_export]
114
macro_rules! test_msg {
12-
($MsgType: path, $data: ident, $read_pos: ident) => {
13-
{
14-
let len = slice_to_be16(get_slice!($data, $read_pos, 2));
15-
let raw = get_slice!($data, $read_pos, len);
16-
let cb = decode_msg!($MsgType, raw).encode();
17-
assert_eq!(&raw[..cb.len()], &cb[..]);
18-
}
19-
}
20-
}
21-
22-
#[macro_export]
23-
macro_rules! decode_msg {
24-
($MsgType: path, $data: expr) => {
25-
match <($MsgType)>::decode($data) {
26-
Ok(msg) => msg,
27-
Err(e) => match e {
28-
msgs::DecodeError::UnknownRealmByte => return,
29-
msgs::DecodeError::BadPublicKey => return,
30-
msgs::DecodeError::BadSignature => return,
31-
msgs::DecodeError::ExtraAddressesPerType => return,
32-
msgs::DecodeError::WrongLength => return,
33-
}
34-
}
35-
}
36-
}
37-
38-
#[macro_export]
39-
macro_rules! get_slice {
40-
($data: ident, $read_pos: ident, $len: expr) => {
5+
($MsgType: path, $data: ident) => {
416
{
42-
let slice_len = $len as usize;
43-
if $data.len() < $read_pos + slice_len {
44-
return;
7+
if let Ok(msg) = <$MsgType as MsgDecodable>::decode($data){
8+
let enc = msg.encode();
9+
assert_eq!(&$data[..enc.len()], &enc[..]);
4510
}
46-
$read_pos += slice_len;
47-
&$data[$read_pos - slice_len..$read_pos]
4811
}
4912
}
5013
}

0 commit comments

Comments
 (0)