From 63cb37d34622c9150fc262867865465f122a4d0b Mon Sep 17 00:00:00 2001 From: Yuntai Kyong Date: Sat, 1 Sep 2018 16:21:39 +0900 Subject: [PATCH] Add fuzz targets for Writeable messages/migrate fuzz test --- fuzz/Cargo.toml | 4 +- fuzz/fuzz_targets/msg_targets/gen_target.sh | 3 ++ .../msg_targets/msg_accept_channel_target.rs | 2 - .../msg_announcement_signatures_target.rs | 2 - .../msg_channel_announcement_target.rs | 2 - .../msg_channel_reestablish_target.rs | 2 - .../msg_targets/msg_channel_update_target.rs | 2 - .../msg_targets/msg_closing_signed_target.rs | 2 - .../msg_commitment_signed_target.rs | 2 - .../msg_decoded_onion_error_packet_target.rs | 2 - .../msg_targets/msg_error_message_target.rs | 2 - .../msg_targets/msg_funding_created_target.rs | 2 - .../msg_targets/msg_funding_locked_target.rs | 2 - .../msg_targets/msg_funding_signed_target.rs | 2 - .../msg_targets/msg_init_target.rs | 2 - .../msg_node_announcement_target.rs | 2 - .../msg_targets/msg_onion_hop_data_target.rs | 2 - .../msg_targets/msg_open_channel_target.rs | 2 - .../{ => msg_targets}/msg_ping_target.rs | 7 ++- .../{ => msg_targets}/msg_pong_target.rs | 7 ++- .../msg_targets/msg_revoke_and_ack_target.rs | 2 - .../msg_targets/msg_shutdown_target.rs | 2 - .../msg_targets/msg_target_template.txt | 2 - .../msg_targets/msg_update_add_htlc_target.rs | 2 - .../msg_update_fail_htlc_target.rs | 2 - .../msg_update_fail_malformed_htlc_target.rs | 2 - .../msg_targets/msg_update_fee_target.rs | 2 - .../msg_update_fulfill_htlc_target.rs | 2 - fuzz/fuzz_targets/msg_targets/utils.rs | 52 +++++++++++++++---- 29 files changed, 53 insertions(+), 68 deletions(-) rename fuzz/fuzz_targets/{ => msg_targets}/msg_ping_target.rs (84%) rename fuzz/fuzz_targets/{ => msg_targets}/msg_pong_target.rs (84%) diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 3cd249ea8..01311f206 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -54,11 +54,11 @@ path = "fuzz_targets/chanmon_deser_target.rs" # message fuzz targets [[bin]] name = "msg_ping_target" -path = "fuzz_targets/msg_ping_target.rs" +path = "fuzz_targets/msg_targets/msg_ping_target.rs" [[bin]] name = "msg_pong_target" -path = "fuzz_targets/msg_pong_target.rs" +path = "fuzz_targets/msg_targets/msg_pong_target.rs" [[bin]] name = "msg_error_message_target" diff --git a/fuzz/fuzz_targets/msg_targets/gen_target.sh b/fuzz/fuzz_targets/msg_targets/gen_target.sh index 32f071e85..cfd100f3d 100755 --- a/fuzz/fuzz_targets/msg_targets/gen_target.sh +++ b/fuzz/fuzz_targets/msg_targets/gen_target.sh @@ -31,3 +31,6 @@ GEN_TEST NodeAnnouncement test_msg_exact "" GEN_TEST UpdateAddHTLC test_msg_hole ", 85, 33" GEN_TEST ErrorMessage test_msg_hole ", 32, 2" GEN_TEST OnionHopData test_msg_hole ", 1+8+8+4, 12" + +GEN_TEST Ping test_msg_simple "" +GEN_TEST Pong test_msg_simple "" 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 d7f0a8813..ef3ae4cdf 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 33c52b33e..f32aad1a1 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 1252cfe79..e814c4fae 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 38f916045..8532ac120 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 c488fb6da..8968ee33e 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 504e1e376..70d969209 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 701dd1fb9..c70437b84 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 56a5fb9f6..173f13d3f 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 @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 97e15c3c4..f565e80f9 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 7289267c2..b9c67ae33 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 bfafdf489..499be0049 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 6a2b6ac3a..2f63bd0bd 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] diff --git a/fuzz/fuzz_targets/msg_targets/msg_init_target.rs b/fuzz/fuzz_targets/msg_targets/msg_init_target.rs index 8a5ee76f5..2013a5406 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_init_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_init_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 54b9cb684..1a0c9e062 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 70849c189..398dd2886 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 @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 737bc2f0c..01082d8b7 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] diff --git a/fuzz/fuzz_targets/msg_ping_target.rs b/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs similarity index 84% rename from fuzz/fuzz_targets/msg_ping_target.rs rename to fuzz/fuzz_targets/msg_targets/msg_ping_target.rs index c9fb34189..4a792f603 100644 --- a/fuzz/fuzz_targets/msg_ping_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs @@ -3,16 +3,15 @@ extern crate lightning; +use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable, Ping}; +mod utils; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - if let Ok(msg) = Ping::decode(data) { - let _ = msg.encode(); - } + test_msg_simple!(msgs::Ping, data); } #[cfg(feature = "afl")] diff --git a/fuzz/fuzz_targets/msg_pong_target.rs b/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs similarity index 84% rename from fuzz/fuzz_targets/msg_pong_target.rs rename to fuzz/fuzz_targets/msg_targets/msg_pong_target.rs index d4572d330..ae5d22a33 100644 --- a/fuzz/fuzz_targets/msg_pong_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs @@ -3,16 +3,15 @@ extern crate lightning; +use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable, Pong}; +mod utils; #[inline] pub fn do_test(data: &[u8]) { reset_rng_state(); - if let Ok(msg) = Pong::decode(data) { - let _ = msg.encode(); - } + test_msg_simple!(msgs::Pong, 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 6086d279c..804e4ba4b 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 @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] diff --git a/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs b/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs index 29243dab6..4cc58f28d 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] diff --git a/fuzz/fuzz_targets/msg_targets/msg_target_template.txt b/fuzz/fuzz_targets/msg_targets/msg_target_template.txt index 6053c041c..d3bda1aa3 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_target_template.txt +++ b/fuzz/fuzz_targets/msg_targets/msg_target_template.txt @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 64806f20f..cfccc21a3 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 @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 27eaf123e..90827f31c 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 @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 eb6dc94aa..a045dea60 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 @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 eec1d2624..6f5ff3e47 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] 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 3dd87b30c..c5dd32356 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 @@ -6,8 +6,6 @@ extern crate lightning; use lightning::ln::msgs; use lightning::util::reset_rng_state; -use lightning::ln::msgs::{MsgEncodable, MsgDecodable}; - mod utils; #[inline] diff --git a/fuzz/fuzz_targets/msg_targets/utils.rs b/fuzz/fuzz_targets/msg_targets/utils.rs index 64bfd8932..c1bcd8f36 100644 --- a/fuzz/fuzz_targets/msg_targets/utils.rs +++ b/fuzz/fuzz_targets/msg_targets/utils.rs @@ -4,9 +4,29 @@ macro_rules! test_msg { ($MsgType: path, $data: ident) => { { - if let Ok(msg) = <$MsgType as MsgDecodable>::decode($data){ - let enc = msg.encode(); - assert_eq!(&$data[..enc.len()], &enc[..]); + use lightning::util::ser::{Writer, Reader, Writeable, Readable}; + let mut r = Reader::new(::std::io::Cursor::new($data)); + if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) { + let p = r.get_ref().position() as usize; + let mut w = Writer::new(::std::io::Cursor::new(vec![])); + msg.write(&mut w).unwrap(); + + let buf = w.into_inner().into_inner(); + assert_eq!(buf.len(), p); + assert_eq!(&r.into_inner().into_inner()[..p], &buf[..p]); + } + } + } +} + +#[macro_export] +macro_rules! test_msg_simple { + ($MsgType: path, $data: ident) => { + { + use lightning::util::ser::{Writer, Reader, Writeable, Readable}; + let mut r = Reader::new(::std::io::Cursor::new($data)); + if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) { + msg.write(&mut Writer::new(::std::io::Cursor::new(vec![]))).unwrap(); } } } @@ -16,9 +36,14 @@ macro_rules! test_msg { macro_rules! test_msg_exact { ($MsgType: path, $data: ident) => { { - if let Ok(msg) = <$MsgType as MsgDecodable>::decode($data){ - let enc = msg.encode(); - assert_eq!(&$data[..], &enc[..]); + use lightning::util::ser::{Writer, Reader, Writeable, Readable}; + let mut r = Reader::new(::std::io::Cursor::new($data)); + if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) { + let mut w = Writer::new(::std::io::Cursor::new(vec![])); + msg.write(&mut w).unwrap(); + + let buf = w.into_inner().into_inner(); + assert_eq!(&r.into_inner().into_inner()[..], &buf[..]); } } } @@ -28,10 +53,17 @@ macro_rules! test_msg_exact { macro_rules! test_msg_hole { ($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => { { - if let Ok(msg) = <$MsgType as MsgDecodable>::decode($data){ - let enc = msg.encode(); - assert_eq!(&$data[..$hole], &enc[..$hole]); - assert_eq!(&$data[$hole + $hole_len..enc.len()], &enc[$hole + $hole_len..]); + use lightning::util::ser::{Writer, Reader, Writeable, Readable}; + let mut r = Reader::new(::std::io::Cursor::new($data)); + if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) { + let mut w = Writer::new(::std::io::Cursor::new(vec![])); + msg.write(&mut w).unwrap(); + let p = w.get_ref().position() as usize; + + let buf = w.into_inner().into_inner(); + assert_eq!(buf.len(),p); + assert_eq!(&r.get_ref().get_ref()[..$hole], &buf[..$hole]); + assert_eq!(&r.get_ref().get_ref()[$hole+$hole_len..p], &buf[$hole+$hole_len..]); } } } -- 2.39.5