Skip to content

Commit d1bb0cb

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 e64342a commit d1bb0cb

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,
@@ -462,6 +448,17 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
462448
}
463449
}
464450

451+
macro_rules! get_bytes {
452+
($len: expr) => { {
453+
let mut res = [0; $len];
454+
match input.get_slice($len as usize) {
455+
Some(slice) => res.copy_from_slice(slice),
456+
None => return,
457+
}
458+
res
459+
} }
460+
}
461+
465462
macro_rules! get_pubkey {
466463
() => {
467464
match PublicKey::from_slice(get_slice!(33)) {
@@ -508,7 +505,7 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
508505

509506
let mut should_forward = false;
510507
let mut payments_received: Vec<PaymentHash> = Vec::new();
511-
let mut payments_sent = 0;
508+
let mut payments_sent: u16 = 0;
512509
let mut pending_funding_generation: Vec<(ChannelId, PublicKey, u64, ScriptBuf)> = Vec::new();
513510
let mut pending_funding_signatures = HashMap::new();
514511

@@ -558,36 +555,30 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
558555
let params = RouteParameters::from_payment_params_and_value(
559556
payment_params, final_value_msat);
560557
let mut payment_hash = PaymentHash([0; 32]);
561-
payment_hash.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
558+
payment_hash.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
562559
payment_hash.0 = Sha256::hash(&payment_hash.0[..]).to_byte_array();
563560
payments_sent += 1;
564-
match channelmanager.send_payment(payment_hash,
565-
RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), params,
566-
Retry::Attempts(0))
567-
{
568-
Ok(_) => {},
569-
Err(_) => return,
570-
}
561+
let _ = channelmanager.send_payment(
562+
payment_hash, RecipientOnionFields::spontaneous_empty(),
563+
PaymentId(payment_hash.0), params, Retry::Attempts(2)
564+
);
571565
},
572566
15 => {
573567
let final_value_msat = slice_to_be24(get_slice!(3)) as u64;
574568
let payment_params = PaymentParameters::from_node_id(get_pubkey!(), 42);
575569
let params = RouteParameters::from_payment_params_and_value(
576570
payment_params, final_value_msat);
577571
let mut payment_hash = PaymentHash([0; 32]);
578-
payment_hash.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
572+
payment_hash.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
579573
payment_hash.0 = Sha256::hash(&payment_hash.0[..]).to_byte_array();
580574
payments_sent += 1;
581575
let mut payment_secret = PaymentSecret([0; 32]);
582-
payment_secret.0[0..8].copy_from_slice(&be64_to_array(payments_sent));
576+
payment_secret.0[0..2].copy_from_slice(&be16_to_array(payments_sent));
583577
payments_sent += 1;
584-
match channelmanager.send_payment(payment_hash,
585-
RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0),
586-
params, Retry::Attempts(0))
587-
{
588-
Ok(_) => {},
589-
Err(_) => return,
590-
}
578+
let _ = channelmanager.send_payment(
579+
payment_hash, RecipientOnionFields::secret_only(payment_secret),
580+
PaymentId(payment_hash.0), params, Retry::Attempts(2)
581+
);
591582
},
592583
5 => {
593584
let peer_id = get_slice!(1)[0];
@@ -680,7 +671,7 @@ pub fn do_test(mut data: &[u8], logger: &Arc<dyn Logger>) {
680671
}
681672
},
682673
12 => {
683-
let txlen = slice_to_be16(get_slice!(2));
674+
let txlen = u16::from_be_bytes(get_bytes!(2));
684675
if txlen == 0 {
685676
loss_detector.connect_block(&[]);
686677
} else {

0 commit comments

Comments
 (0)