Skip to content

Commit 78ac11e

Browse files
committed
Add a fuzzer to check that IndexedMap is equivalent to BTreeMap
1 parent 738b392 commit 78ac11e

File tree

6 files changed

+366
-0
lines changed

6 files changed

+366
-0
lines changed

fuzz/src/bin/gen_target.sh

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ GEN_TEST peer_crypt
1414
GEN_TEST process_network_graph
1515
GEN_TEST router
1616
GEN_TEST zbase32
17+
GEN_TEST indexedmap
1718

1819
GEN_TEST msg_accept_channel msg_targets::
1920
GEN_TEST msg_announcement_signatures msg_targets::

fuzz/src/bin/indexedmap_target.rs

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
// This file is auto-generated by gen_target.sh based on target_template.txt
11+
// To modify it, modify target_template.txt and run gen_target.sh instead.
12+
13+
#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
14+
15+
#[cfg(not(fuzzing))]
16+
compile_error!("Fuzz targets need cfg=fuzzing");
17+
18+
extern crate lightning_fuzz;
19+
use lightning_fuzz::indexedmap::*;
20+
21+
#[cfg(feature = "afl")]
22+
#[macro_use] extern crate afl;
23+
#[cfg(feature = "afl")]
24+
fn main() {
25+
fuzz!(|data| {
26+
indexedmap_run(data.as_ptr(), data.len());
27+
});
28+
}
29+
30+
#[cfg(feature = "honggfuzz")]
31+
#[macro_use] extern crate honggfuzz;
32+
#[cfg(feature = "honggfuzz")]
33+
fn main() {
34+
loop {
35+
fuzz!(|data| {
36+
indexedmap_run(data.as_ptr(), data.len());
37+
});
38+
}
39+
}
40+
41+
#[cfg(feature = "libfuzzer_fuzz")]
42+
#[macro_use] extern crate libfuzzer_sys;
43+
#[cfg(feature = "libfuzzer_fuzz")]
44+
fuzz_target!(|data: &[u8]| {
45+
indexedmap_run(data.as_ptr(), data.len());
46+
});
47+
48+
#[cfg(feature = "stdin_fuzz")]
49+
fn main() {
50+
use std::io::Read;
51+
52+
let mut data = Vec::with_capacity(8192);
53+
std::io::stdin().read_to_end(&mut data).unwrap();
54+
indexedmap_run(data.as_ptr(), data.len());
55+
}
56+
57+
#[test]
58+
fn run_test_cases() {
59+
use std::fs;
60+
use std::io::Read;
61+
use lightning_fuzz::utils::test_logger::StringBuffer;
62+
63+
use std::sync::{atomic, Arc};
64+
{
65+
let data: Vec<u8> = vec![0];
66+
indexedmap_run(data.as_ptr(), data.len());
67+
}
68+
let mut threads = Vec::new();
69+
let threads_running = Arc::new(atomic::AtomicUsize::new(0));
70+
if let Ok(tests) = fs::read_dir("test_cases/indexedmap") {
71+
for test in tests {
72+
let mut data: Vec<u8> = Vec::new();
73+
let path = test.unwrap().path();
74+
fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
75+
threads_running.fetch_add(1, atomic::Ordering::AcqRel);
76+
77+
let thread_count_ref = Arc::clone(&threads_running);
78+
let main_thread_ref = std::thread::current();
79+
threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
80+
std::thread::spawn(move || {
81+
let string_logger = StringBuffer::new();
82+
83+
let panic_logger = string_logger.clone();
84+
let res = if ::std::panic::catch_unwind(move || {
85+
indexedmap_test(&data, panic_logger);
86+
}).is_err() {
87+
Some(string_logger.into_string())
88+
} else { None };
89+
thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
90+
main_thread_ref.unpark();
91+
res
92+
})
93+
));
94+
while threads_running.load(atomic::Ordering::Acquire) > 32 {
95+
std::thread::park();
96+
}
97+
}
98+
}
99+
let mut failed_outputs = Vec::new();
100+
for (test, thread) in threads.drain(..) {
101+
if let Some(output) = thread.join().unwrap() {
102+
println!("\nOutput of {}:\n{}\n", test, output);
103+
failed_outputs.push(test);
104+
}
105+
}
106+
if !failed_outputs.is_empty() {
107+
println!("Test cases which failed: ");
108+
for case in failed_outputs {
109+
println!("{}", case);
110+
}
111+
panic!();
112+
}
113+
}
+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
// This file is auto-generated by gen_target.sh based on target_template.txt
11+
// To modify it, modify target_template.txt and run gen_target.sh instead.
12+
13+
#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
14+
15+
#[cfg(not(fuzzing))]
16+
compile_error!("Fuzz targets need cfg=fuzzing");
17+
18+
extern crate lightning_fuzz;
19+
use lightning_fuzz::msg_targets::msg_channel_details::*;
20+
21+
#[cfg(feature = "afl")]
22+
#[macro_use] extern crate afl;
23+
#[cfg(feature = "afl")]
24+
fn main() {
25+
fuzz!(|data| {
26+
msg_channel_details_run(data.as_ptr(), data.len());
27+
});
28+
}
29+
30+
#[cfg(feature = "honggfuzz")]
31+
#[macro_use] extern crate honggfuzz;
32+
#[cfg(feature = "honggfuzz")]
33+
fn main() {
34+
loop {
35+
fuzz!(|data| {
36+
msg_channel_details_run(data.as_ptr(), data.len());
37+
});
38+
}
39+
}
40+
41+
#[cfg(feature = "libfuzzer_fuzz")]
42+
#[macro_use] extern crate libfuzzer_sys;
43+
#[cfg(feature = "libfuzzer_fuzz")]
44+
fuzz_target!(|data: &[u8]| {
45+
msg_channel_details_run(data.as_ptr(), data.len());
46+
});
47+
48+
#[cfg(feature = "stdin_fuzz")]
49+
fn main() {
50+
use std::io::Read;
51+
52+
let mut data = Vec::with_capacity(8192);
53+
std::io::stdin().read_to_end(&mut data).unwrap();
54+
msg_channel_details_run(data.as_ptr(), data.len());
55+
}
56+
57+
#[test]
58+
fn run_test_cases() {
59+
use std::fs;
60+
use std::io::Read;
61+
use lightning_fuzz::utils::test_logger::StringBuffer;
62+
63+
use std::sync::{atomic, Arc};
64+
{
65+
let data: Vec<u8> = vec![0];
66+
msg_channel_details_run(data.as_ptr(), data.len());
67+
}
68+
let mut threads = Vec::new();
69+
let threads_running = Arc::new(atomic::AtomicUsize::new(0));
70+
if let Ok(tests) = fs::read_dir("test_cases/msg_channel_details") {
71+
for test in tests {
72+
let mut data: Vec<u8> = Vec::new();
73+
let path = test.unwrap().path();
74+
fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
75+
threads_running.fetch_add(1, atomic::Ordering::AcqRel);
76+
77+
let thread_count_ref = Arc::clone(&threads_running);
78+
let main_thread_ref = std::thread::current();
79+
threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
80+
std::thread::spawn(move || {
81+
let string_logger = StringBuffer::new();
82+
83+
let panic_logger = string_logger.clone();
84+
let res = if ::std::panic::catch_unwind(move || {
85+
msg_channel_details_test(&data, panic_logger);
86+
}).is_err() {
87+
Some(string_logger.into_string())
88+
} else { None };
89+
thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
90+
main_thread_ref.unpark();
91+
res
92+
})
93+
));
94+
while threads_running.load(atomic::Ordering::Acquire) > 32 {
95+
std::thread::park();
96+
}
97+
}
98+
}
99+
let mut failed_outputs = Vec::new();
100+
for (test, thread) in threads.drain(..) {
101+
if let Some(output) = thread.join().unwrap() {
102+
println!("\nOutput of {}:\n{}\n", test, output);
103+
failed_outputs.push(test);
104+
}
105+
}
106+
if !failed_outputs.is_empty() {
107+
println!("Test cases which failed: ");
108+
for case in failed_outputs {
109+
println!("{}", case);
110+
}
111+
panic!();
112+
}
113+
}

fuzz/src/indexedmap.rs

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
use lightning::util::indexed_map::{IndexedMap, self};
11+
use std::collections::{BTreeMap, btree_map};
12+
use hashbrown::HashSet;
13+
14+
use crate::utils::test_logger;
15+
16+
fn check_eq(btree: &BTreeMap<u8, u8>, indexed: &IndexedMap<u8, u8>) {
17+
assert_eq!(btree.len(), indexed.len());
18+
assert_eq!(btree.is_empty(), indexed.is_empty());
19+
20+
let mut btree_clone = btree.clone();
21+
assert!(btree_clone == *btree);
22+
let mut indexed_clone = indexed.clone();
23+
assert!(indexed_clone == *indexed);
24+
25+
for k in 0..=255 {
26+
assert_eq!(btree.contains_key(&k), indexed.contains_key(&k));
27+
assert_eq!(btree.get(&k), indexed.get(&k));
28+
29+
let btree_entry = btree_clone.entry(k);
30+
let indexed_entry = indexed_clone.entry(k);
31+
match btree_entry {
32+
btree_map::Entry::Occupied(mut bo) => {
33+
if let indexed_map::Entry::Occupied(mut io) = indexed_entry {
34+
assert_eq!(bo.get(), io.get());
35+
assert_eq!(bo.get_mut(), io.get_mut());
36+
} else { panic!(); }
37+
},
38+
btree_map::Entry::Vacant(_) => {
39+
if let indexed_map::Entry::Vacant(_) = indexed_entry {
40+
} else { panic!(); }
41+
}
42+
}
43+
}
44+
45+
const STRIDE: u8 = 16;
46+
for k in 0..=255/STRIDE {
47+
let lower_bound = k * STRIDE;
48+
let upper_bound = lower_bound + (STRIDE - 1);
49+
let mut btree_iter = btree.range(lower_bound..=upper_bound);
50+
let mut indexed_iter = indexed.range(lower_bound..=upper_bound);
51+
loop {
52+
let b_v = btree_iter.next();
53+
let i_v = indexed_iter.next();
54+
assert_eq!(b_v, i_v);
55+
if b_v.is_none() { break; }
56+
}
57+
}
58+
59+
let mut key_set = HashSet::with_capacity(256);
60+
for k in indexed.unordered_keys() {
61+
assert!(key_set.insert(*k));
62+
assert!(btree.contains_key(k));
63+
}
64+
assert_eq!(key_set.len(), btree.len());
65+
66+
key_set.clear();
67+
for (k, v) in indexed.unordered_iter() {
68+
assert!(key_set.insert(*k));
69+
assert_eq!(btree.get(k).unwrap(), v);
70+
}
71+
assert_eq!(key_set.len(), btree.len());
72+
73+
key_set.clear();
74+
for (k, v) in indexed_clone.unordered_iter_mut() {
75+
assert!(key_set.insert(*k));
76+
assert_eq!(btree.get(k).unwrap(), v);
77+
}
78+
assert_eq!(key_set.len(), btree.len());
79+
}
80+
81+
#[inline]
82+
pub fn do_test(data: &[u8]) {
83+
if data.len() % 2 != 0 { return; }
84+
let mut btree = BTreeMap::new();
85+
let mut indexed = IndexedMap::new();
86+
87+
// Read in k-v pairs from the input and insert them into the maps then check that the maps are
88+
// equivalent in every way we can read them.
89+
for tuple in data.windows(2) {
90+
let prev_value_b = btree.insert(tuple[0], tuple[1]);
91+
let prev_value_i = indexed.insert(tuple[0], tuple[1]);
92+
assert_eq!(prev_value_b, prev_value_i);
93+
}
94+
check_eq(&btree, &indexed);
95+
96+
// Now, modify the maps in all the ways we have to do so, checking that the maps remain
97+
// equivalent as we go.
98+
for (k, v) in indexed.unordered_iter_mut() {
99+
*v = *k;
100+
*btree.get_mut(k).unwrap() = *k;
101+
}
102+
check_eq(&btree, &indexed);
103+
104+
for k in 0..=255 {
105+
match btree.entry(k) {
106+
btree_map::Entry::Occupied(mut bo) => {
107+
if let indexed_map::Entry::Occupied(mut io) = indexed.entry(k) {
108+
if k < 64 {
109+
*io.get_mut() ^= 0xff;
110+
*bo.get_mut() ^= 0xff;
111+
} else if k < 128 {
112+
*io.into_mut() ^= 0xff;
113+
*bo.get_mut() ^= 0xff;
114+
} else {
115+
assert_eq!(bo.remove_entry(), io.remove_entry());
116+
}
117+
} else { panic!(); }
118+
},
119+
btree_map::Entry::Vacant(bv) => {
120+
if let indexed_map::Entry::Vacant(iv) = indexed.entry(k) {
121+
bv.insert(k);
122+
iv.insert(k);
123+
} else { panic!(); }
124+
},
125+
}
126+
}
127+
check_eq(&btree, &indexed);
128+
}
129+
130+
pub fn indexedmap_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
131+
do_test(data);
132+
}
133+
134+
#[no_mangle]
135+
pub extern "C" fn indexedmap_run(data: *const u8, datalen: usize) {
136+
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
137+
}

fuzz/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod utils;
1717
pub mod chanmon_deser;
1818
pub mod chanmon_consistency;
1919
pub mod full_stack;
20+
pub mod indexedmap;
2021
pub mod onion_message;
2122
pub mod peer_crypt;
2223
pub mod process_network_graph;

fuzz/targets.h

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ void peer_crypt_run(const unsigned char* data, size_t data_len);
77
void process_network_graph_run(const unsigned char* data, size_t data_len);
88
void router_run(const unsigned char* data, size_t data_len);
99
void zbase32_run(const unsigned char* data, size_t data_len);
10+
void indexedmap_run(const unsigned char* data, size_t data_len);
1011
void msg_accept_channel_run(const unsigned char* data, size_t data_len);
1112
void msg_announcement_signatures_run(const unsigned char* data, size_t data_len);
1213
void msg_channel_reestablish_run(const unsigned char* data, size_t data_len);

0 commit comments

Comments
 (0)