diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 98549b1aff..f9ded1e056 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -11,6 +11,9 @@ repository = "https://github.com/nervosnetwork/ckb" [package.metadata] cargo-fuzz = true +[features] +binary_fuzz_service_proto = [] + [dependencies] libfuzzer-sys = "0.4" ipnetwork = "0.18" @@ -61,7 +64,7 @@ debug = 1 overflow-checks = true [build] -rustflags = ["-C", "instrument-coverage"] +rustflags = ["-C", "instrument-coverage", "-C", "sanitizer=address"] [[bin]] name = "fuzz_compress" diff --git a/fuzz/fuzz_targets/fuzz_ckb_protocol_handler.rs b/fuzz/fuzz_targets/fuzz_ckb_protocol_handler.rs index 328ae89437..89465b6097 100644 --- a/fuzz/fuzz_targets/fuzz_ckb_protocol_handler.rs +++ b/fuzz/fuzz_targets/fuzz_ckb_protocol_handler.rs @@ -131,7 +131,7 @@ fn run(data: &[u8]) -> Result<(), ()> { let _r = proto.init(nc.clone()).await; proto.connected(nc.clone(), 0.into(), "").await; // - let bufs = data.get_bufs(0xFFFFFFFF, 7, 1000); + let bufs = data.get_bufs(0xFFFFFFFF, 15, 4000); for buf in bufs { proto.received(nc.clone(), 0.into(), Bytes::from(buf)).await; } diff --git a/fuzz/fuzz_targets/fuzz_service_proto.rs b/fuzz/fuzz_targets/fuzz_service_proto.rs index 6ee484a8d3..f7e61d99e0 100644 --- a/fuzz/fuzz_targets/fuzz_service_proto.rs +++ b/fuzz/fuzz_targets/fuzz_service_proto.rs @@ -2,15 +2,18 @@ use libfuzzer_sys::fuzz_target; // Note -// If you want to use this fuzz, need to replace tentacle and related dependencies in Cargo.toml. +// This bin depends on feature: binary_fuzz_service_proto and needs to replace tentacle +// https://github.com/joii2020/tentacle/tree/dev +// Mainly add pub attributes to some functions for testing calls // [replace] // "tentacle:0.4.2" = {path = '../../tentacle/tentacle'} // "tentacle-multiaddr:0.3.4" = {path = '../../tentacle/multiaddr'} // "tentacle-secio:0.5.7" = {path = '../../tentacle/secio'} +#[cfg(feature = "binary_fuzz_service_proto")] use ckb_network::{ virtual_p2p::{ - channel, Bytes, ProtocolContext, ProtocolId, ServiceContext, ServiceProtocol, + p2p::channel::mpsc::channel, Bytes, ProtocolContext, ProtocolId, ServiceContext, ServiceProtocol, SessionContext, }, NetworkState, @@ -24,12 +27,14 @@ use tokio::time::{sleep, Duration}; use ckb_fuzz::BufManager; +#[cfg(feature = "binary_fuzz_service_proto")] struct ServiceProtoTest { data: BufManager, service_protocol: Box, _channel_id: usize, } +#[cfg(feature = "binary_fuzz_service_proto")] impl ServiceProtoTest { fn new(data: &[u8]) -> Result { let mut data = BufManager::new(&data); @@ -108,6 +113,7 @@ impl ServiceProtoTest { } } +#[cfg(feature = "binary_fuzz_service_proto")] fuzz_target!(|data: &[u8]| { let t = ServiceProtoTest::new(data); if t.is_err() { @@ -129,3 +135,6 @@ fuzz_target!(|data: &[u8]| { } }); }); + +#[cfg(not(feature = "binary_fuzz_service_proto"))] +fuzz_target!(|_data: &[u8]| { panic!("unsupport") }); diff --git a/network/src/virtual_p2p.rs b/network/src/virtual_p2p.rs index 77d7fbd7e4..e87128d450 100644 --- a/network/src/virtual_p2p.rs +++ b/network/src/virtual_p2p.rs @@ -1,9 +1,9 @@ use crate::network::NetworkState; use std::{sync::Arc, time::Duration}; +pub use p2p; pub use p2p::{ bytes::Bytes, - channel::mpsc::channel, context::{ProtocolContext, ServiceContext, SessionContext}, service::ServiceAsyncControl, traits::ServiceProtocol,