Skip to content

Commit

Permalink
fuzz_service_proto needs to be enabled via macro
Browse files Browse the repository at this point in the history
  • Loading branch information
joii2020 committed Jun 28, 2024
1 parent cf33b9d commit 6d70ef7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
5 changes: 4 additions & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/fuzz_ckb_protocol_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 11 additions & 2 deletions fuzz/fuzz_targets/fuzz_service_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<dyn ServiceProtocol>,
_channel_id: usize,
}

#[cfg(feature = "binary_fuzz_service_proto")]
impl ServiceProtoTest {
fn new(data: &[u8]) -> Result<Self, ()> {
let mut data = BufManager::new(&data);
Expand Down Expand Up @@ -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() {
Expand All @@ -129,3 +135,6 @@ fuzz_target!(|data: &[u8]| {
}
});
});

#[cfg(not(feature = "binary_fuzz_service_proto"))]
fuzz_target!(|_data: &[u8]| { panic!("unsupport") });
2 changes: 1 addition & 1 deletion network/src/virtual_p2p.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 6d70ef7

Please sign in to comment.