From 4de3cfe665c0ea09266f2592f5a487bb71f0e93b Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sat, 16 Jun 2018 23:11:37 -0400 Subject: [PATCH] Fix message targets to not have an internal loop --- .../msg_targets/msg_accept_channel_target.rs | 6 +-- .../msg_targets/msg_closing_signed_target.rs | 6 +-- .../msg_commitment_signed_target.rs | 6 +-- .../msg_targets/msg_funding_created_target.rs | 6 +-- .../msg_targets/msg_funding_locked_target.rs | 6 +-- .../msg_targets/msg_funding_signed_target.rs | 6 +-- .../msg_targets/msg_open_channel_target.rs | 6 +-- .../msg_targets/msg_revoke_and_ack_target.rs | 6 +-- .../msg_targets/msg_shutdown_target.rs | 6 +-- .../msg_targets/msg_target_template.txt | 6 +-- .../msg_targets/msg_update_add_htlc_target.rs | 6 +-- .../msg_update_fail_htlc_target.rs | 6 +-- .../msg_update_fail_malformed_htlc_target.rs | 6 +-- .../msg_targets/msg_update_fee_target.rs | 6 +-- .../msg_update_fulfill_htlc_target.rs | 6 +-- fuzz/fuzz_targets/msg_targets/utils.rs | 45 ++----------------- 16 files changed, 19 insertions(+), 116 deletions(-) diff --git a/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs b/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs index 146358cb8..a0e9cc4ef 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::AcceptChannel, data, read_pos); - } + test_msg!(msgs::AcceptChannel, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs index 1c32ab6ed..621ffe2b1 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::ClosingSigned, data, read_pos); - } + test_msg!(msgs::ClosingSigned, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs index 494ce54c2..e73696c8d 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::CommitmentSigned, data, read_pos); - } + test_msg!(msgs::CommitmentSigned, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs index 2420e921e..f7d1a58a5 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::FundingCreated, data, read_pos); - } + test_msg!(msgs::FundingCreated, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs index edf7ba9eb..00fdef3f8 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::FundingLocked, data, read_pos); - } + test_msg!(msgs::FundingLocked, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs index a17b9ba80..9b66c88fe 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::FundingSigned, data, read_pos); - } + test_msg!(msgs::FundingSigned, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs b/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs index f4c8803cb..8d7664f8a 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::OpenChannel, data, read_pos); - } + test_msg!(msgs::OpenChannel, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs b/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs index 725903af7..de7e93141 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::RevokeAndACK, data, read_pos); - } + test_msg!(msgs::RevokeAndACK, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs b/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs index 855b4aa7f..57e245676 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::Shutdown, data, read_pos); - } + test_msg!(msgs::Shutdown, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_target_template.txt b/fuzz/fuzz_targets/msg_targets/msg_target_template.txt index 05abd7236..55a9761c9 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_target_template.txt +++ b/fuzz/fuzz_targets/msg_targets/msg_target_template.txt @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::MSG_TARGET, data, read_pos); - } + test_msg!(msgs::MSG_TARGET, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs index a6489cb41..2a2c80701 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::UpdateAddHTLC, data, read_pos); - } + test_msg!(msgs::UpdateAddHTLC, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs index d389b36ca..aca62f7bb 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::UpdateFailHTLC, data, read_pos); - } + test_msg!(msgs::UpdateFailHTLC, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs index 4ea37a5b1..e91bb3bdf 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::UpdateFailMalformedHTLC, data, read_pos); - } + test_msg!(msgs::UpdateFailMalformedHTLC, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs index 4025d9490..e7488306b 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::UpdateFee, data, read_pos); - } + test_msg!(msgs::UpdateFee, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs index edddfb54c..cd449908a 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs @@ -9,15 +9,11 @@ use lightning::util::reset_rng_state; use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; mod utils; -use utils::slice_to_be16; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; - loop { - test_msg!(msgs::UpdateFulfillHTLC, data, read_pos); - } + test_msg!(msgs::UpdateFulfillHTLC, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_targets/utils.rs b/fuzz/fuzz_targets/msg_targets/utils.rs index cfe5a10e9..4d355585d 100644 --- a/fuzz/fuzz_targets/msg_targets/utils.rs +++ b/fuzz/fuzz_targets/msg_targets/utils.rs @@ -1,50 +1,13 @@ #![macro_use] -#[allow(dead_code)] -#[inline] -pub fn slice_to_be16(v: &[u8]) -> u16 { - ((v[0] as u16) << 8*1) | - ((v[1] as u16) << 8*0) -} - #[macro_export] macro_rules! test_msg { - ($MsgType: path, $data: ident, $read_pos: ident) => { - { - let len = slice_to_be16(get_slice!($data, $read_pos, 2)); - let raw = get_slice!($data, $read_pos, len); - let cb = decode_msg!($MsgType, raw).encode(); - assert_eq!(&raw[..cb.len()], &cb[..]); - } - } -} - -#[macro_export] -macro_rules! decode_msg { - ($MsgType: path, $data: expr) => { - match <($MsgType)>::decode($data) { - Ok(msg) => msg, - Err(e) => match e { - msgs::DecodeError::UnknownRealmByte => return, - msgs::DecodeError::BadPublicKey => return, - msgs::DecodeError::BadSignature => return, - msgs::DecodeError::ExtraAddressesPerType => return, - msgs::DecodeError::WrongLength => return, - } - } - } -} - -#[macro_export] -macro_rules! get_slice { - ($data: ident, $read_pos: ident, $len: expr) => { + ($MsgType: path, $data: ident) => { { - let slice_len = $len as usize; - if $data.len() < $read_pos + slice_len { - return; + if let Ok(msg) = <$MsgType as MsgDecodable>::decode($data){ + let enc = msg.encode(); + assert_eq!(&$data[..enc.len()], &enc[..]); } - $read_pos += slice_len; - &$data[$read_pos - slice_len..$read_pos] } } } -- 2.39.5