Skip to content

Commit 32883f4

Browse files
committed
[fuzz] Reduce size of payments_sent in full_stack_target
Slightly reducing the amount of data we read to do things.
1 parent d0465b6 commit 32883f4

File tree

1 file changed

+32
-41
lines changed

1 file changed

+32
-41
lines changed

fuzz/src/full_stack.rs

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -76,35 +76,21 @@ pub fn slice_to_be16(v: &[u8]) -> u16 {
7676
((v[1] as u16) << 8*0)
7777
}
7878

79+
#[inline]
80+
pub fn be16_to_array(u: u16) -> [u8; 2] {
81+
let mut v = [0; 2];
82+
v[0] = ((u >> 8*1) & 0xff) as u8;
83+
v[1] = ((u >> 8*0) & 0xff) as u8;
84+
v
85+
}
86+
7987
#[inline]
8088
pub fn slice_to_be24(v: &[u8]) -> u32 {
8189
((v[0] as u32) << 8*2) |
8290
((v[1] as u32) << 8*1) |
8391
((v[2] as u32) << 8*0)
8492
}
8593

86-
#[inline]
87-
pub fn slice_to_be32(v: &[u8]) -> u32 {
88-
((v[0] as u32) << 8*3) |
89-
((v[1] as u32) << 8*2) |
90-
((v[2] as u32) << 8*1) |
91-
((v[3] as u32) << 8*0)
92-
}
93-
94-
#[inline]
95-
pub fn be64_to_array(u: u64) -> [u8; 8] {
96-
let mut v = [0; 8];
97-
v[0] = ((u >> 8*7) & 0xff) as u8;
98-
v[1] = ((u >> 8*6) & 0xff) as u8;
99-
v[2] = ((u >> 8*5) & 0xff) as u8;
100-
v[3] = ((u >> 8*4) & 0xff) as u8;
101-
v[4] = ((u >> 8*3) & 0xff) as u8;
102-
v[5] = ((u >> 8*2) & 0xff) as u8;
103-
v[6] = ((u >> 8*1) & 0xff) as u8;
104-
v[7] = ((u >> 8*0) & 0xff) as u8;
105-
v
106-
}
107-
10894
struct InputData {
10995
data: Vec<u8>,
11096
read_pos: AtomicUsize,
@@ -467,6 +453,17 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
467453
}
468454
}
469455

456+
macro_rules! get_bytes {
457+
($len: expr) => { {
458+
let mut res = [0; $len];
459+
match input.get_slice($len as usize) {
460+
Some(slice) => res.copy_from_slice(slice),
461+
None => return,
462+
}
463+
res
464+
} }
465+
}
466+
470467
macro_rules! get_pubkey {
471468
() => {
472469
match PublicKey::from_slice(get_slice!(33)) {
@@ -513,7 +510,7 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
513510

514511
let mut should_forward = false;
515512
let mut payments_received: Vec<PaymentHash> = Vec::new();
516-
let mut payments_sent = 0;
513+
let mut payments_sent: u16 = 0;
517514
let mut pending_funding_generation: Vec<(ChannelId, PublicKey, u64, ScriptBuf)> = Vec::new();
518515
let mut pending_funding_signatures = HashMap::new();
519516

@@ -563,36 +560,30 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
563560
let params = RouteParameters::from_payment_params_and_value(
564561
payment_params, final_value_msat);
565562
let mut payment_hash = PaymentHash([0; 32]);
566-
payment_hash.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
563+
payment_hash.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
567564
payment_hash.0 = Sha256::hash(&payment_hash.0[..]).to_byte_array();
568565
payments_sent += 1;
569-
match channelmanager.send_payment(payment_hash,
570-
RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), params,
571-
Retry::Attempts(0))
572-
{
573-
Ok(_) => {},
574-
Err(_) => return,
575-
}
566+
let _ = channelmanager.send_payment(
567+
payment_hash, RecipientOnionFields::spontaneous_empty(),
568+
PaymentId(payment_hash.0), params, Retry::Attempts(2)
569+
);
576570
},
577571
15 => {
578572
let final_value_msat = slice_to_be24(get_slice!(3)) as u64;
579573
let payment_params = PaymentParameters::from_node_id(get_pubkey!(), 42);
580574
let params = RouteParameters::from_payment_params_and_value(
581575
payment_params, final_value_msat);
582576
let mut payment_hash = PaymentHash([0; 32]);
583-
payment_hash.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
577+
payment_hash.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
584578
payment_hash.0 = Sha256::hash(&payment_hash.0[..]).to_byte_array();
585579
payments_sent += 1;
586580
let mut payment_secret = PaymentSecret([0; 32]);
587-
payment_secret.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
581+
payment_secret.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
588582
payments_sent += 1;
589-
match channelmanager.send_payment(payment_hash,
590-
RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0),
591-
params, Retry::Attempts(0))
592-
{
593-
Ok(_) => {},
594-
Err(_) => return,
595-
}
583+
let _ = channelmanager.send_payment(
584+
payment_hash, RecipientOnionFields::secret_only(payment_secret),
585+
PaymentId(payment_hash.0), params, Retry::Attempts(2)
586+
);
596587
},
597588
5 => {
598589
let peer_id = get_slice!(1)[0];
@@ -685,7 +676,7 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
685676
}
686677
},
687678
12 => {
688-
let txlen = slice_to_be16(get_slice!(2));
679+
let txlen = u16::from_be_bytes(get_bytes!(2));
689680
if txlen == 0 {
690681
loss_detector.connect_block(&[]);
691682
} else {

0 commit comments

Comments
 (0)