From 19b92448c579982fcfcd52e28448323576472883 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 14 Sep 2018 18:05:13 -0400 Subject: [PATCH] Utilize Writer size hinting in message fuzz targets and check them --- .../msg_targets/msg_accept_channel_target.rs | 1 + .../msg_announcement_signatures_target.rs | 1 + .../msg_channel_announcement_target.rs | 1 + .../msg_channel_reestablish_target.rs | 1 + .../msg_targets/msg_channel_update_target.rs | 1 + .../msg_targets/msg_closing_signed_target.rs | 1 + .../msg_commitment_signed_target.rs | 1 + .../msg_decoded_onion_error_packet_target.rs | 1 + .../msg_targets/msg_error_message_target.rs | 1 + .../msg_targets/msg_funding_created_target.rs | 1 + .../msg_targets/msg_funding_locked_target.rs | 1 + .../msg_targets/msg_funding_signed_target.rs | 1 + .../msg_targets/msg_init_target.rs | 1 + .../msg_node_announcement_target.rs | 1 + .../msg_targets/msg_onion_hop_data_target.rs | 1 + .../msg_targets/msg_open_channel_target.rs | 1 + .../msg_targets/msg_ping_target.rs | 1 + .../msg_targets/msg_pong_target.rs | 1 + .../msg_targets/msg_revoke_and_ack_target.rs | 1 + .../msg_targets/msg_shutdown_target.rs | 1 + .../msg_targets/msg_target_template.txt | 1 + .../msg_targets/msg_update_add_htlc_target.rs | 1 + .../msg_update_fail_htlc_target.rs | 1 + .../msg_update_fail_malformed_htlc_target.rs | 1 + .../msg_targets/msg_update_fee_target.rs | 1 + .../msg_update_fulfill_htlc_target.rs | 1 + fuzz/fuzz_targets/msg_targets/utils.rs | 39 ++++++++++++------- 27 files changed, 51 insertions(+), 14 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 ef3ae4cdf..bd058b1ed 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs b/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs index f32aad1a1..233a4a77a 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs b/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs index e814c4fae..fbaca76db 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs b/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs index 8532ac120..debd89466 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs b/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs index 8968ee33e..4f38e2f48 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 70d969209..7d63131e7 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 c70437b84..93e72bd75 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs b/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs index 173f13d3f..4b97b17ef 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs b/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs index f565e80f9..5ccbd2646 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 b9c67ae33..57713196c 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 499be0049..b1ad5b3a0 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 2f63bd0bd..a3b33dc64 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_init_target.rs b/fuzz/fuzz_targets/msg_targets/msg_init_target.rs index 2013a5406..f7eb0671f 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_init_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_init_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs b/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs index 1a0c9e062..a847d0dad 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs b/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs index 398dd2886..558842918 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 01082d8b7..36f6519a4 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs b/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs index 4a792f603..c470e1bbf 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs b/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs index ae5d22a33..ef1e4ada8 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 804e4ba4b..1e38a5d04 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 @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs b/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs index 4cc58f28d..d64efcc9f 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/msg_target_template.txt b/fuzz/fuzz_targets/msg_targets/msg_target_template.txt index d3bda1aa3..50e433738 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_target_template.txt +++ b/fuzz/fuzz_targets/msg_targets/msg_target_template.txt @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 cfccc21a3..45bc9d1b9 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 @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 90827f31c..8432a57da 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 @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 a045dea60..ad129e002 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 @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 6f5ff3e47..f345711cd 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { 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 c5dd32356..380b02a47 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 @@ -7,6 +7,7 @@ use lightning::ln::msgs; use lightning::util::reset_rng_state; mod utils; +use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { diff --git a/fuzz/fuzz_targets/msg_targets/utils.rs b/fuzz/fuzz_targets/msg_targets/utils.rs index 52ea530cf..a5257ba0f 100644 --- a/fuzz/fuzz_targets/msg_targets/utils.rs +++ b/fuzz/fuzz_targets/msg_targets/utils.rs @@ -1,5 +1,18 @@ #![macro_use] +use lightning::util::ser::Writer; +pub struct VecWriter(pub Vec); +impl Writer for VecWriter { + fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> { + assert!(self.0.capacity() >= self.0.len() + buf.len()); + self.0.extend_from_slice(buf); + Ok(()) + } + fn size_hint(&mut self, size: usize) { + self.0.reserve_exact(size); + } +} + #[macro_export] macro_rules! test_msg { ($MsgType: path, $data: ident) => { @@ -8,12 +21,11 @@ macro_rules! test_msg { let mut r = ::std::io::Cursor::new($data); if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) { let p = r.position() as usize; - let mut w = ::std::io::Cursor::new(vec![]); + let mut w = VecWriter(Vec::new()); msg.write(&mut w).unwrap(); - let buf = w.into_inner(); - assert_eq!(buf.len(), p); - assert_eq!(&r.into_inner()[..p], &buf[..p]); + assert_eq!(w.0.len(), p); + assert_eq!(&r.into_inner()[..p], &w.0[..p]); } } } @@ -26,7 +38,8 @@ macro_rules! test_msg_simple { use lightning::util::ser::{Writeable, Readable}; let mut r = ::std::io::Cursor::new($data); if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) { - msg.write(&mut ::std::io::Cursor::new(vec![])).unwrap(); + let mut w = VecWriter(Vec::new()); + msg.write(&mut w).unwrap(); } } } @@ -39,11 +52,10 @@ macro_rules! test_msg_exact { use lightning::util::ser::{Writeable, Readable}; let mut r = ::std::io::Cursor::new($data); if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) { - let mut w = ::std::io::Cursor::new(vec![]); + let mut w = VecWriter(Vec::new()); msg.write(&mut w).unwrap(); - let buf = w.into_inner(); - assert_eq!(&r.into_inner()[..], &buf[..]); + assert_eq!(&r.into_inner()[..], &w.0[..]); } } } @@ -56,14 +68,13 @@ macro_rules! test_msg_hole { use lightning::util::ser::{Writeable, Readable}; let mut r = ::std::io::Cursor::new($data); if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) { - let mut w = ::std::io::Cursor::new(vec![]); + let mut w = VecWriter(Vec::new()); msg.write(&mut w).unwrap(); - let p = w.position() as usize; + let p = w.0.len() as usize; - let buf = w.into_inner(); - assert_eq!(buf.len(),p); - assert_eq!(&r.get_ref()[..$hole], &buf[..$hole]); - assert_eq!(&r.get_ref()[$hole+$hole_len..p], &buf[$hole+$hole_len..]); + assert_eq!(w.0.len(), p); + assert_eq!(&r.get_ref()[..$hole], &w.0[..$hole]); + assert_eq!(&r.get_ref()[$hole+$hole_len..p], &w.0[$hole+$hole_len..]); } } } -- 2.39.5