]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Utilize Writer size hinting in message fuzz targets and check them
authorMatt Corallo <git@bluematt.me>
Fri, 14 Sep 2018 22:05:13 +0000 (18:05 -0400)
committerMatt Corallo <git@bluematt.me>
Sun, 16 Sep 2018 18:33:50 +0000 (14:33 -0400)
27 files changed:
fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs
fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs
fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs
fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs
fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs
fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs
fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs
fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs
fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs
fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs
fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs
fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs
fuzz/fuzz_targets/msg_targets/msg_init_target.rs
fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs
fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs
fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs
fuzz/fuzz_targets/msg_targets/msg_ping_target.rs
fuzz/fuzz_targets/msg_targets/msg_pong_target.rs
fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs
fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs
fuzz/fuzz_targets/msg_targets/msg_target_template.txt
fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs
fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs
fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs
fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs
fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs
fuzz/fuzz_targets/msg_targets/utils.rs

index ef3ae4cdf4e1f6848c6b2be4b3555970dd09188c..bd058b1ed867ad20872d5e355bd32f6078026ef9 100644 (file)
@@ -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]) {
index f32aad1a198b2b6c4a63535de4e5a0b771724562..233a4a77a5a828e3f96d62752c7d0e555eb44c08 100644 (file)
@@ -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]) {
index e814c4fae52028365ae0589b486d465a2c272d7a..fbaca76dba0feb7c002e1e9a06d03e60752c27b2 100644 (file)
@@ -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]) {
index 8532ac120504c04efb6ebe515eceb26871059400..debd89466cdb5d5d05a6c676b39e2f391e34b0bd 100644 (file)
@@ -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]) {
index 8968ee33ea0c1df0a13c0d302ec6c0f1bf394fa1..4f38e2f4861311a19fa915d3241d5ccb86862a9c 100644 (file)
@@ -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]) {
index 70d969209403c77f95a43751ea5d0258844fd87f..7d63131e7e39dfc402773fc7885210ce2c463335 100644 (file)
@@ -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]) {
index c70437b8409e3f9756137b84bc5b7cf24521bb68..93e72bd75ce7d49b536f7be83bd60f4561e44406 100644 (file)
@@ -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]) {
index 173f13d3fdeb6b9c40536fc91b425605eab4c60b..4b97b17efd59bb606531619b69a7cd2b12986062 100644 (file)
@@ -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]) {
index f565e80f9195973c9b46e188a28b1a83bcf719fd..5ccbd26468017a644c3388e6712ff60c7eef5e7f 100644 (file)
@@ -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]) {
index b9c67ae33504b27e7820cb0b17095d73200d2264..57713196ced38bf5f6cf0f5644435798a73d833d 100644 (file)
@@ -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]) {
index 499be0049dc4e0783056a6d8cd6aacb5cf2e982a..b1ad5b3a089c73e911b31cc8bcd9e5fb8dec225d 100644 (file)
@@ -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]) {
index 2f63bd0bd166f1dcdc5045058f6cb7ed968261cf..a3b33dc643565f46b8996bf4038da754fe88fde7 100644 (file)
@@ -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]) {
index 2013a540650763ed5bdb08cdfd017c49be6f64d5..f7eb0671fb43b28788643fc72457627d2de0a9fb 100644 (file)
@@ -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]) {
index 1a0c9e062f24492d81c71387900ac6fbcac3ffec..a847d0dad67a6a13b7667fe03fbda203b16f86a8 100644 (file)
@@ -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]) {
index 398dd2886bfd74698c0ca6320e517bf694e5cca4..5588429185fa599f4376f8ea30fe19d5ee43beda 100644 (file)
@@ -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]) {
index 01082d8b77ac4e4931c9bf2e9e9ddcbb82d9274d..36f6519a4dd05e4128d5b62d16a0e126771e8b92 100644 (file)
@@ -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]) {
index 4a792f60399c8eb8a7f25140098ae253d93cd503..c470e1bbf4626d9235b7e1a15c2b527dcf583821 100644 (file)
@@ -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]) {
index ae5d22a3309cd4e3f202fb691688b79b9d4aa3e3..ef1e4ada8f3568ee55d18483aee7fc2be2fcfd41 100644 (file)
@@ -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]) {
index 804e4ba4b2f194169bc37126c31dff32859a6bb0..1e38a5d045a60140bb3e61b1c2485f8103afb81b 100644 (file)
@@ -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]) {
index 4cc58f28d594845747bcbb28d462073599620a06..d64efcc9f176df15e80593d2f681be1cf9b7807a 100644 (file)
@@ -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]) {
index d3bda1aa33595038806eed0ff18ad60a67643556..50e43373899bd17cc558a2c5a16b6942baa9b76d 100644 (file)
@@ -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]) {
index cfccc21a3f472c8b763b0934786c363bff5336e9..45bc9d1b9832d4f92bfb317795afad97366b1fa5 100644 (file)
@@ -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]) {
index 90827f31c95ff7935eef112d046e820d8fc3b701..8432a57daadd69fa65bcbc380803c72b1c2c2b6b 100644 (file)
@@ -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]) {
index a045dea6038de130b5dd097478e33e34eb41fcbf..ad129e00243d7a6da2049d78b829d858df85f465 100644 (file)
@@ -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]) {
index 6f5ff3e47a98867da17d63f5bee4c24ccbae5aeb..f345711cd8a6f901108140646893d7db0597cfc2 100644 (file)
@@ -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]) {
index c5dd32356570bdeceba462839e8bc8a9ae9533d0..380b02a47068d5095a9c3185f78bf2df34d9ea13 100644 (file)
@@ -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]) {
index 52ea530cf9fed976438e30f5ad1beff30ac94af7..a5257ba0f80c1fea24f399018103da37f572c763 100644 (file)
@@ -1,5 +1,18 @@
 #![macro_use]
 
+use lightning::util::ser::Writer;
+pub struct VecWriter(pub Vec<u8>);
+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..]);
                        }
                }
        }