Migrate fuzz router/channel target to Readable

and be more specific about DecodeError::InvalidValue
This commit is contained in:
Yuntai Kyong 2018-09-12 19:00:10 +09:00 committed by Matt Corallo
parent 3e89106163
commit 28a612f9f3
4 changed files with 20 additions and 30 deletions

View file

@ -10,11 +10,12 @@ use bitcoin::network::serialize::{serialize, BitcoinHash};
use lightning::ln::channel::{Channel, ChannelKeys}; use lightning::ln::channel::{Channel, ChannelKeys};
use lightning::ln::channelmanager::{HTLCFailReason, PendingHTLCStatus}; use lightning::ln::channelmanager::{HTLCFailReason, PendingHTLCStatus};
use lightning::ln::msgs; use lightning::ln::msgs;
use lightning::ln::msgs::{MsgDecodable, ErrorAction}; use lightning::ln::msgs::{ErrorAction};
use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget}; use lightning::chain::chaininterface::{FeeEstimator, ConfirmationTarget};
use lightning::chain::transaction::OutPoint; use lightning::chain::transaction::OutPoint;
use lightning::util::reset_rng_state; use lightning::util::reset_rng_state;
use lightning::util::logger::Logger; use lightning::util::logger::Logger;
use lightning::util::ser::{Readable, Reader};
mod utils; mod utils;
@ -119,8 +120,9 @@ pub fn do_test(data: &[u8]) {
} }
macro_rules! decode_msg { macro_rules! decode_msg {
($MsgType: path, $len: expr) => { ($MsgType: path, $len: expr) => {{
match <($MsgType)>::decode(get_slice!($len)) { let mut reader = Reader::new(::std::io::Cursor::new(get_slice!($len)));
match <($MsgType)>::read(&mut reader) {
Ok(msg) => msg, Ok(msg) => msg,
Err(e) => match e { Err(e) => match e {
msgs::DecodeError::UnknownRealmByte => return, msgs::DecodeError::UnknownRealmByte => return,
@ -131,11 +133,11 @@ pub fn do_test(data: &[u8]) {
msgs::DecodeError::ExtraAddressesPerType => return, msgs::DecodeError::ExtraAddressesPerType => return,
msgs::DecodeError::BadLengthDescriptor => return, msgs::DecodeError::BadLengthDescriptor => return,
msgs::DecodeError::ShortRead => panic!("We picked the length..."), msgs::DecodeError::ShortRead => panic!("We picked the length..."),
msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."), msgs::DecodeError::InvalidValue => panic!("Should not happen with p2p message decoding"),
msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."), msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
} }
} }
} }}
} }
macro_rules! decode_msg_with_len16 { macro_rules! decode_msg_with_len16 {
@ -145,21 +147,7 @@ pub fn do_test(data: &[u8]) {
Some(slice) => slice, Some(slice) => slice,
None => return, None => return,
}[$begin_len..$begin_len + 2]); }[$begin_len..$begin_len + 2]);
match <($MsgType)>::decode(get_slice!($begin_len as usize + 2 + (extra_len as usize)*$factor)) { decode_msg!($MsgType, $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..."),
}
}
} }
} }
} }

View file

@ -8,10 +8,11 @@ use bitcoin::blockdata::script::{Script, Builder};
use lightning::chain::chaininterface::{ChainError,ChainWatchInterface, ChainListener}; use lightning::chain::chaininterface::{ChainError,ChainWatchInterface, ChainListener};
use lightning::ln::channelmanager::ChannelDetails; use lightning::ln::channelmanager::ChannelDetails;
use lightning::ln::msgs; use lightning::ln::msgs;
use lightning::ln::msgs::{MsgDecodable, RoutingMessageHandler}; use lightning::ln::msgs::{RoutingMessageHandler};
use lightning::ln::router::{Router, RouteHint}; use lightning::ln::router::{Router, RouteHint};
use lightning::util::reset_rng_state; use lightning::util::reset_rng_state;
use lightning::util::logger::Logger; use lightning::util::logger::Logger;
use lightning::util::ser::{Reader, Readable};
use secp256k1::key::PublicKey; use secp256k1::key::PublicKey;
use secp256k1::Secp256k1; use secp256k1::Secp256k1;
@ -119,8 +120,9 @@ pub fn do_test(data: &[u8]) {
} }
macro_rules! decode_msg { macro_rules! decode_msg {
($MsgType: path, $len: expr) => { ($MsgType: path, $len: expr) => {{
match <($MsgType)>::decode(get_slice!($len)) { let mut reader = Reader::new(::std::io::Cursor::new(get_slice!($len)));
match <($MsgType)>::read(&mut reader) {
Ok(msg) => msg, Ok(msg) => msg,
Err(e) => match e { Err(e) => match e {
msgs::DecodeError::UnknownRealmByte => return, msgs::DecodeError::UnknownRealmByte => return,
@ -131,11 +133,11 @@ pub fn do_test(data: &[u8]) {
msgs::DecodeError::ExtraAddressesPerType => return, msgs::DecodeError::ExtraAddressesPerType => return,
msgs::DecodeError::BadLengthDescriptor => return, msgs::DecodeError::BadLengthDescriptor => return,
msgs::DecodeError::ShortRead => panic!("We picked the length..."), msgs::DecodeError::ShortRead => panic!("We picked the length..."),
msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."), msgs::DecodeError::InvalidValue => panic!("Should not happen with p2p message decoding"),
msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."), msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
} }
} }
} }}
} }
macro_rules! decode_msg_with_len16 { macro_rules! decode_msg_with_len16 {

View file

@ -46,7 +46,7 @@ pub enum DecodeError {
BadLengthDescriptor, BadLengthDescriptor,
/// Error from std::io /// Error from std::io
Io(::std::io::Error), Io(::std::io::Error),
/// Invalid value found when decoding /// 1 or 0 is not found for boolean value
InvalidValue, InvalidValue,
} }
pub trait MsgDecodable: Sized { pub trait MsgDecodable: Sized {
@ -525,7 +525,7 @@ impl Error for DecodeError {
DecodeError::ExtraAddressesPerType => "More than one address of a single type", 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::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
DecodeError::Io(ref e) => e.description(), DecodeError::Io(ref e) => e.description(),
DecodeError::InvalidValue => "Invalid value in the bytes", DecodeError::InvalidValue => "0 or 1 is not found for boolean",
} }
} }
} }

View file

@ -364,7 +364,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
}, },
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }), msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
msgs::DecodeError::Io(_) => 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"),
} }
} }
}; };