From: Yuntai Kyong Date: Wed, 12 Sep 2018 10:00:10 +0000 (+0900) Subject: Migrate fuzz router/channel target to Readable X-Git-Tag: v0.0.12~317^2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=refs%2Fheads%2F2018-09-119-rebased;p=rust-lightning Migrate fuzz router/channel target to Readable and be more specific about DecodeError::InvalidValue --- diff --git a/fuzz/fuzz_targets/channel_target.rs b/fuzz/fuzz_targets/channel_target.rs index d2a4fbca9..7eed4fb55 100644 --- a/fuzz/fuzz_targets/channel_target.rs +++ b/fuzz/fuzz_targets/channel_target.rs @@ -10,11 +10,12 @@ use bitcoin::network::serialize::{serialize, BitcoinHash}; use lightning::ln::channel::{Channel, ChannelKeys}; use lightning::ln::channelmanager::{HTLCFailReason, PendingHTLCStatus}; use lightning::ln::msgs; -use lightning::ln::msgs::{MsgDecodable, ErrorAction}; +use lightning::ln::msgs::{ErrorAction}; use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget}; use lightning::chain::transaction::OutPoint; use lightning::util::reset_rng_state; use lightning::util::logger::Logger; +use lightning::util::ser::{Readable, Reader}; mod utils; @@ -119,8 +120,9 @@ pub fn do_test(data: &[u8]) { } macro_rules! decode_msg { - ($MsgType: path, $len: expr) => { - match <($MsgType)>::decode(get_slice!($len)) { + ($MsgType: path, $len: expr) => {{ + let mut reader = Reader::new(::std::io::Cursor::new(get_slice!($len))); + match <($MsgType)>::read(&mut reader) { Ok(msg) => msg, Err(e) => match e { msgs::DecodeError::UnknownRealmByte => return, @@ -131,11 +133,11 @@ pub fn do_test(data: &[u8]) { msgs::DecodeError::ExtraAddressesPerType => return, msgs::DecodeError::BadLengthDescriptor => return, msgs::DecodeError::ShortRead => panic!("We picked the length..."), - msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."), - msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."), + msgs::DecodeError::InvalidValue => panic!("Should not happen with p2p message decoding"), + msgs::DecodeError::Io(e) => panic!(format!("{}", e)), } } - } + }} } macro_rules! decode_msg_with_len16 { @@ -145,21 +147,7 @@ pub fn do_test(data: &[u8]) { Some(slice) => slice, None => return, }[$begin_len..$begin_len + 2]); - match <($MsgType)>::decode(get_slice!($begin_len as usize + 2 + (extra_len as usize)*$factor)) { - Ok(msg) => msg, - Err(e) => match e { - msgs::DecodeError::UnknownRealmByte => return, - msgs::DecodeError::UnknownRequiredFeature => return, - msgs::DecodeError::BadPublicKey => return, - msgs::DecodeError::BadSignature => return, - msgs::DecodeError::BadText => return, - msgs::DecodeError::ExtraAddressesPerType => return, - msgs::DecodeError::BadLengthDescriptor => return, - msgs::DecodeError::ShortRead => panic!("We picked the length..."), - msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."), - msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."), - } - } + decode_msg!($MsgType, $begin_len as usize + 2 + (extra_len as usize)*$factor) } } } diff --git a/fuzz/fuzz_targets/router_target.rs b/fuzz/fuzz_targets/router_target.rs index 4669f1f8c..fc3e9e9ba 100644 --- a/fuzz/fuzz_targets/router_target.rs +++ b/fuzz/fuzz_targets/router_target.rs @@ -8,10 +8,11 @@ use bitcoin::blockdata::script::{Script, Builder}; use lightning::chain::chaininterface::{ChainError,ChainWatchInterface, ChainListener}; use lightning::ln::channelmanager::ChannelDetails; use lightning::ln::msgs; -use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler}; +use lightning::ln::msgs::{RoutingMessageHandler}; use lightning::ln::router::{Router, RouteHint}; use lightning::util::reset_rng_state; use lightning::util::logger::Logger; +use lightning::util::ser::{Reader, Readable}; use secp256k1::key::PublicKey; use secp256k1::Secp256k1; @@ -119,8 +120,9 @@ pub fn do_test(data: &[u8]) { } macro_rules! decode_msg { - ($MsgType: path, $len: expr) => { - match <($MsgType)>::decode(get_slice!($len)) { + ($MsgType: path, $len: expr) => {{ + let mut reader = Reader::new(::std::io::Cursor::new(get_slice!($len))); + match <($MsgType)>::read(&mut reader) { Ok(msg) => msg, Err(e) => match e { msgs::DecodeError::UnknownRealmByte => return, @@ -131,11 +133,11 @@ pub fn do_test(data: &[u8]) { msgs::DecodeError::ExtraAddressesPerType => return, msgs::DecodeError::BadLengthDescriptor => return, msgs::DecodeError::ShortRead => panic!("We picked the length..."), - msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."), - msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."), + msgs::DecodeError::InvalidValue => panic!("Should not happen with p2p message decoding"), + msgs::DecodeError::Io(e) => panic!(format!("{}", e)), } } - } + }} } macro_rules! decode_msg_with_len16 { diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index f2f9fcbd1..fc942ab5a 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -46,7 +46,7 @@ pub enum DecodeError { BadLengthDescriptor, /// Error from std::io Io(::std::io::Error), - /// Invalid value found when decoding + /// 1 or 0 is not found for boolean value InvalidValue, } pub trait MsgDecodable: Sized { @@ -525,7 +525,7 @@ impl Error for DecodeError { DecodeError::ExtraAddressesPerType => "More than one address of a single type", DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly", DecodeError::Io(ref e) => e.description(), - DecodeError::InvalidValue => "Invalid value in the bytes", + DecodeError::InvalidValue => "0 or 1 is not found for boolean", } } } diff --git a/src/ln/peer_handler.rs b/src/ln/peer_handler.rs index b102f947b..4e710117b 100644 --- a/src/ln/peer_handler.rs +++ b/src/ln/peer_handler.rs @@ -364,7 +364,7 @@ impl PeerManager { }, msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }), msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }), - msgs::DecodeError::InvalidValue => return Err(PeerHandleError{ no_connection_possible: false }), + msgs::DecodeError::InvalidValue => panic!("should not happen with message decoding"), } } };