Skip to content

Commit a73dea7

Browse files
committed
Add ping/pong fuzz targets
1 parent e3effa4 commit a73dea7

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

fuzz/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ name = "full_stack_target"
4343
path = "fuzz_targets/full_stack_target.rs"
4444

4545
# message fuzz targets
46+
[[bin]]
47+
name = "msg_ping_target"
48+
path = "fuzz_targets/msg_ping_target.rs"
49+
50+
[[bin]]
51+
name = "msg_pong_target"
52+
path = "fuzz_targets/msg_pong_target.rs"
53+
4654
[[bin]]
4755
name = "msg_accept_channel_target"
4856
path = "fuzz_targets/msg_targets/msg_accept_channel_target.rs"

fuzz/fuzz_targets/msg_ping_target.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// This file is auto-generated by gen_target.sh based on msg_target_template.txt
2+
// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
3+
4+
extern crate lightning;
5+
6+
use lightning::util::reset_rng_state;
7+
8+
use lightning::ln::msgs::{MsgEncodable, MsgDecodable, Ping};
9+
10+
#[inline]
11+
pub fn do_test(data: &[u8]) {
12+
reset_rng_state();
13+
if let Ok(msg) = Ping::decode(data) {
14+
let _ = msg.encode();
15+
}
16+
}
17+
18+
#[cfg(feature = "afl")]
19+
extern crate afl;
20+
#[cfg(feature = "afl")]
21+
fn main() {
22+
afl::read_stdio_bytes(|data| {
23+
do_test(&data);
24+
});
25+
}
26+
27+
#[cfg(feature = "honggfuzz")]
28+
#[macro_use] extern crate honggfuzz;
29+
#[cfg(feature = "honggfuzz")]
30+
fn main() {
31+
loop {
32+
fuzz!(|data| {
33+
do_test(data);
34+
});
35+
}
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
41+
let mut b = 0;
42+
for (idx, c) in hex.as_bytes().iter().enumerate() {
43+
b <<= 4;
44+
match *c {
45+
b'A'...b'F' => b |= c - b'A' + 10,
46+
b'a'...b'f' => b |= c - b'a' + 10,
47+
b'0'...b'9' => b |= c - b'0',
48+
_ => panic!("Bad hex"),
49+
}
50+
if (idx & 1) == 1 {
51+
out.push(b);
52+
b = 0;
53+
}
54+
}
55+
}
56+
57+
#[test]
58+
fn duplicate_crash() {
59+
let mut a = Vec::new();
60+
extend_vec_from_hex("00", &mut a);
61+
super::do_test(&a);
62+
}
63+
}

fuzz/fuzz_targets/msg_pong_target.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// This file is auto-generated by gen_target.sh based on msg_target_template.txt
2+
// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
3+
4+
extern crate lightning;
5+
6+
use lightning::util::reset_rng_state;
7+
8+
use lightning::ln::msgs::{MsgEncodable, MsgDecodable, Pong};
9+
10+
#[inline]
11+
pub fn do_test(data: &[u8]) {
12+
reset_rng_state();
13+
if let Ok(msg) = Pong::decode(data) {
14+
let _ = msg.encode();
15+
}
16+
}
17+
18+
#[cfg(feature = "afl")]
19+
extern crate afl;
20+
#[cfg(feature = "afl")]
21+
fn main() {
22+
afl::read_stdio_bytes(|data| {
23+
do_test(&data);
24+
});
25+
}
26+
27+
#[cfg(feature = "honggfuzz")]
28+
#[macro_use] extern crate honggfuzz;
29+
#[cfg(feature = "honggfuzz")]
30+
fn main() {
31+
loop {
32+
fuzz!(|data| {
33+
do_test(data);
34+
});
35+
}
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
41+
let mut b = 0;
42+
for (idx, c) in hex.as_bytes().iter().enumerate() {
43+
b <<= 4;
44+
match *c {
45+
b'A'...b'F' => b |= c - b'A' + 10,
46+
b'a'...b'f' => b |= c - b'a' + 10,
47+
b'0'...b'9' => b |= c - b'0',
48+
_ => panic!("Bad hex"),
49+
}
50+
if (idx & 1) == 1 {
51+
out.push(b);
52+
b = 0;
53+
}
54+
}
55+
}
56+
57+
#[test]
58+
fn duplicate_crash() {
59+
let mut a = Vec::new();
60+
extend_vec_from_hex("00", &mut a);
61+
super::do_test(&a);
62+
}
63+
}

0 commit comments

Comments
 (0)