-
Notifications
You must be signed in to change notification settings - Fork 411
fuzz: Add LSPS message decoder fuzzing #3849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// This file is Copyright its original authors, visible in version control | ||
// history. | ||
// | ||
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE | ||
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. | ||
// You may not use this file except in accordance with one or both of these | ||
// licenses. | ||
|
||
// This file is auto-generated by gen_target.sh based on target_template.txt | ||
// To modify it, modify target_template.txt and run gen_target.sh instead. | ||
|
||
#![cfg_attr(feature = "libfuzzer_fuzz", no_main)] | ||
#![cfg_attr(rustfmt, rustfmt_skip)] | ||
|
||
#[cfg(not(fuzzing))] | ||
compile_error!("Fuzz targets need cfg=fuzzing"); | ||
|
||
#[cfg(not(hashes_fuzz))] | ||
compile_error!("Fuzz targets need cfg=hashes_fuzz"); | ||
|
||
#[cfg(not(secp256k1_fuzz))] | ||
compile_error!("Fuzz targets need cfg=secp256k1_fuzz"); | ||
|
||
extern crate lightning_fuzz; | ||
use lightning_fuzz::lsps_message::*; | ||
|
||
#[cfg(feature = "afl")] | ||
#[macro_use] extern crate afl; | ||
#[cfg(feature = "afl")] | ||
fn main() { | ||
fuzz!(|data| { | ||
lsps_message_run(data.as_ptr(), data.len()); | ||
}); | ||
} | ||
|
||
#[cfg(feature = "honggfuzz")] | ||
#[macro_use] extern crate honggfuzz; | ||
#[cfg(feature = "honggfuzz")] | ||
fn main() { | ||
loop { | ||
fuzz!(|data| { | ||
lsps_message_run(data.as_ptr(), data.len()); | ||
}); | ||
} | ||
} | ||
|
||
#[cfg(feature = "libfuzzer_fuzz")] | ||
#[macro_use] extern crate libfuzzer_sys; | ||
#[cfg(feature = "libfuzzer_fuzz")] | ||
fuzz_target!(|data: &[u8]| { | ||
lsps_message_run(data.as_ptr(), data.len()); | ||
}); | ||
|
||
#[cfg(feature = "stdin_fuzz")] | ||
fn main() { | ||
use std::io::Read; | ||
|
||
let mut data = Vec::with_capacity(8192); | ||
std::io::stdin().read_to_end(&mut data).unwrap(); | ||
lsps_message_run(data.as_ptr(), data.len()); | ||
} | ||
|
||
#[test] | ||
fn run_test_cases() { | ||
use std::fs; | ||
use std::io::Read; | ||
use lightning_fuzz::utils::test_logger::StringBuffer; | ||
|
||
use std::sync::{atomic, Arc}; | ||
{ | ||
let data: Vec<u8> = vec![0]; | ||
lsps_message_run(data.as_ptr(), data.len()); | ||
} | ||
let mut threads = Vec::new(); | ||
let threads_running = Arc::new(atomic::AtomicUsize::new(0)); | ||
if let Ok(tests) = fs::read_dir("test_cases/lsps_message") { | ||
for test in tests { | ||
let mut data: Vec<u8> = Vec::new(); | ||
let path = test.unwrap().path(); | ||
fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); | ||
threads_running.fetch_add(1, atomic::Ordering::AcqRel); | ||
|
||
let thread_count_ref = Arc::clone(&threads_running); | ||
let main_thread_ref = std::thread::current(); | ||
threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), | ||
std::thread::spawn(move || { | ||
let string_logger = StringBuffer::new(); | ||
|
||
let panic_logger = string_logger.clone(); | ||
let res = if ::std::panic::catch_unwind(move || { | ||
lsps_message_test(&data, panic_logger); | ||
}).is_err() { | ||
Some(string_logger.into_string()) | ||
} else { None }; | ||
thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); | ||
main_thread_ref.unpark(); | ||
res | ||
}) | ||
)); | ||
while threads_running.load(atomic::Ordering::Acquire) > 32 { | ||
std::thread::park(); | ||
} | ||
} | ||
} | ||
let mut failed_outputs = Vec::new(); | ||
for (test, thread) in threads.drain(..) { | ||
if let Some(output) = thread.join().unwrap() { | ||
println!("\nOutput of {}:\n{}\n", test, output); | ||
failed_outputs.push(test); | ||
} | ||
} | ||
if !failed_outputs.is_empty() { | ||
println!("Test cases which failed: "); | ||
for case in failed_outputs { | ||
println!("{}", case); | ||
} | ||
panic!(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use crate::utils::test_logger; | ||
|
||
use bitcoin::blockdata::constants::genesis_block; | ||
use bitcoin::hashes::{sha256, Hash}; | ||
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey}; | ||
use bitcoin::Network; | ||
|
||
use lightning::chain::Filter; | ||
use lightning::chain::{chainmonitor, BestBlock}; | ||
use lightning::ln::channelmanager::{ChainParameters, ChannelManager}; | ||
use lightning::ln::peer_handler::CustomMessageHandler; | ||
use lightning::ln::wire::CustomMessageReader; | ||
use lightning::onion_message::messenger::DefaultMessageRouter; | ||
use lightning::routing::gossip::NetworkGraph; | ||
use lightning::routing::router::DefaultRouter; | ||
use lightning::sign::KeysManager; | ||
use lightning::sign::NodeSigner; | ||
use lightning::util::config::UserConfig; | ||
use lightning::util::test_utils::{ | ||
TestBroadcaster, TestChainSource, TestFeeEstimator, TestLogger, TestScorer, | ||
}; | ||
use lightning_persister::fs_store::FilesystemStore; | ||
|
||
use lightning_liquidity::lsps0::ser::LSPS_MESSAGE_TYPE_ID; | ||
use lightning_liquidity::LiquidityManager; | ||
|
||
use core::time::Duration; | ||
|
||
type LockingWrapper<T> = std::sync::Mutex<T>; | ||
|
||
use std::sync::Arc; | ||
|
||
pub fn do_test(data: &[u8]) { | ||
let network = Network::Bitcoin; | ||
let tx_broadcaster = Arc::new(TestBroadcaster::new(network)); | ||
let fee_estimator = Arc::new(TestFeeEstimator::new(253)); | ||
let logger = Arc::new(TestLogger::with_id("node".into())); | ||
let genesis_block = genesis_block(network); | ||
let network_graph = Arc::new(NetworkGraph::new(network, logger.clone())); | ||
let scorer = Arc::new(LockingWrapper::new(TestScorer::new())); | ||
let now = Duration::from_secs(genesis_block.header.time as u64); | ||
let seed = sha256::Hash::hash(b"lsps-message-seed").to_byte_array(); | ||
let keys_manager = Arc::new(KeysManager::new(&seed, now.as_secs(), now.subsec_nanos())); | ||
let router = Arc::new(DefaultRouter::new( | ||
network_graph.clone(), | ||
logger.clone(), | ||
Arc::clone(&keys_manager), | ||
scorer.clone(), | ||
Default::default(), | ||
)); | ||
let msg_router = | ||
Arc::new(DefaultMessageRouter::new(network_graph.clone(), Arc::clone(&keys_manager))); | ||
let chain_source = Arc::new(TestChainSource::new(Network::Bitcoin)); | ||
let kv_store = Arc::new(FilesystemStore::new("persister".into())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use a test persister rather than something that could write to the fs in a fuzz target? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we should just be able to use the in-memory |
||
let chain_monitor = Arc::new(chainmonitor::ChainMonitor::new( | ||
Some(chain_source.clone()), | ||
tx_broadcaster.clone(), | ||
logger.clone(), | ||
fee_estimator.clone(), | ||
kv_store.clone(), | ||
keys_manager.clone(), | ||
keys_manager.get_peer_storage_key(), | ||
)); | ||
let best_block = BestBlock::from_network(network); | ||
let params = ChainParameters { network, best_block }; | ||
let manager = Arc::new(ChannelManager::new( | ||
fee_estimator.clone(), | ||
chain_monitor.clone(), | ||
tx_broadcaster.clone(), | ||
router.clone(), | ||
msg_router.clone(), | ||
logger.clone(), | ||
keys_manager.clone(), | ||
keys_manager.clone(), | ||
keys_manager.clone(), | ||
UserConfig::default(), | ||
params, | ||
genesis_block.header.time, | ||
)); | ||
|
||
let liquidity_manager = Arc::new(LiquidityManager::new( | ||
Arc::clone(&keys_manager), | ||
Arc::clone(&manager), | ||
None::<Arc<dyn Filter + Send + Sync>>, | ||
None, | ||
None, | ||
None, | ||
)); | ||
let mut reader = data; | ||
if let Ok(Some(msg)) = liquidity_manager.read(LSPS_MESSAGE_TYPE_ID, &mut reader) { | ||
let secp = Secp256k1::signing_only(); | ||
let sender_node_id = | ||
PublicKey::from_secret_key(&secp, &SecretKey::from_slice(&[1; 32]).unwrap()); | ||
let _ = liquidity_manager.handle_custom_message(msg, sender_node_id); | ||
} | ||
} | ||
|
||
pub fn lsps_message_test<Out: test_logger::Output>(data: &[u8], _out: Out) { | ||
do_test(data); | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn lsps_message_run(data: *const u8, datalen: usize) { | ||
do_test(unsafe { core::slice::from_raw_parts(data, datalen) }); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Given we're about to enable a lint enforcing to use
Arc::clone
vs.arc.clone()
in #3851, let's switch all of these toArc::clone
pls.