Skip to content

Commit

Permalink
fuzz: Add logging to fuzz targets (#985)
Browse files Browse the repository at this point in the history
In order to make debugging fuzz test failures easier, it's useful to be
able to get logs from the proxy and libraries during a fuzz run.
However, because running the fuzz targets with a fuzzer will generate a
massive number of imputs, we probably don't want verbose logs by default
--- this would produce a huge amount of mostly useless data.

This branch adds a simple tracing setup to the fuzz targets. When
running without the `RUST_LOG` environment variable set, each run won't
log anything. This means the current behavior on cluster-fuzz is
unchanged and we won't output giant amounts of logs. However, when
running a minimized reproducer locally for debugging, we can enable any
amount of logging by setting `RUST_LOG`, which should make it easier to
diagnose fuzz failures.
  • Loading branch information
hawkw authored Apr 21, 2021
1 parent 90819e5 commit 826655d
Show file tree
Hide file tree
Showing 12 changed files with 1,542 additions and 10 deletions.
304 changes: 304 additions & 0 deletions linkerd/addr/fuzz/Cargo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "arbitrary"
version = "1.0.0"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "698b65a961a9d730fb45b6b0327e20207810c9f61ee421b082b27ba003f49e2b"

[[package]]
name = "bumpalo"
version = "3.6.1"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe"

[[package]]
name = "bytes"
version = "1.0.1"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040"

[[package]]
name = "cc"
version = "1.0.67"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd"

[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"

[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"

[[package]]
name = "http"
version = "0.2.4"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "527e8c9ac747e28542699a951517aa9a6945af506cd1f2e1b53a576c17b6cc11"
dependencies = [
"bytes",
"fnv",
"itoa",
]

[[package]]
name = "itoa"
version = "0.4.7"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"

[[package]]
name = "js-sys"
version = "0.3.50"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "2d99f9e3e84b8f67f846ef5b4cbbc3b1c29f6c759fcbce6f01aa0e73d932a24c"
dependencies = [
"wasm-bindgen",
]

[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"

[[package]]
name = "libc"
version = "0.2.93"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"

[[package]]
name = "libfuzzer-sys"
version = "0.4.0"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "86c975d637bc2a2f99440932b731491fc34c7f785d239e38af3addd3c2fd0e46"
dependencies = [
"arbitrary",
"cc",
]

[[package]]
name = "linkerd-addr"
version = "0.1.0"
dependencies = [
"http",
"linkerd-dns-name",
"thiserror",
]

[[package]]
name = "linkerd-addr-fuzz"
version = "0.0.0"
dependencies = [
"libfuzzer-sys",
"linkerd-addr",
]

[[package]]
name = "linkerd-dns-name"
version = "0.1.0"
dependencies = [
"thiserror",
"untrusted",
"webpki",
]

[[package]]
name = "log"
version = "0.4.14"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [
"cfg-if",
]

[[package]]
name = "once_cell"
version = "1.7.2"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"

[[package]]
name = "proc-macro2"
version = "1.0.26"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec"
dependencies = [
"unicode-xid",
]

[[package]]
name = "quote"
version = "1.0.9"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [
"proc-macro2",
]

[[package]]
name = "ring"
version = "0.16.20"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
dependencies = [
"cc",
"libc",
"once_cell",
"spin",
"untrusted",
"web-sys",
"winapi",
]

[[package]]
name = "spin"
version = "0.5.2"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"

[[package]]
name = "syn"
version = "1.0.69"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "48fe99c6bd8b1cc636890bcc071842de909d902c81ac7dab53ba33c421ab8ffb"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]

[[package]]
name = "thiserror"
version = "1.0.24"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e"
dependencies = [
"thiserror-impl",
]

[[package]]
name = "thiserror-impl"
version = "1.0.24"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"

[[package]]
name = "untrusted"
version = "0.7.1"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"

[[package]]
name = "wasm-bindgen"
version = "0.2.73"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "83240549659d187488f91f33c0f8547cbfef0b2088bc470c116d1d260ef623d9"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]

[[package]]
name = "wasm-bindgen-backend"
version = "0.2.73"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "ae70622411ca953215ca6d06d3ebeb1e915f0f6613e3b495122878d7ebec7dae"
dependencies = [
"bumpalo",
"lazy_static",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]

[[package]]
name = "wasm-bindgen-macro"
version = "0.2.73"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "3e734d91443f177bfdb41969de821e15c516931c3c3db3d318fa1b68975d0f6f"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]

[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.73"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "d53739ff08c8a68b0fdbcd54c372b8ab800b1449ab3c9d706503bc7dd1621b2c"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]

[[package]]
name = "wasm-bindgen-shared"
version = "0.2.73"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "d9a543ae66aa233d14bb765ed9af4a33e81b8b58d1584cf1b47ff8cd0b9e4489"

[[package]]
name = "web-sys"
version = "0.3.50"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "a905d57e488fec8861446d3393670fb50d27a262344013181c2cdf9fff5481be"
dependencies = [
"js-sys",
"wasm-bindgen",
]

[[package]]
name = "webpki"
version = "0.21.4"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "b8e38c0608262c46d4a56202ebabdeb094cef7e560ca7a226c6bf055188aa4ea"
dependencies = [
"ring",
"untrusted",
]

[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]

[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"

[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+/~https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
2 changes: 2 additions & 0 deletions linkerd/addr/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
tracing = "0.1.23"
tracing-subscriber = "0.2.17"

[dependencies.linkerd-addr]
path = ".."
Expand Down
3 changes: 2 additions & 1 deletion linkerd/addr/fuzz/fuzz_targets/fuzz_target_1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![no_main]
use libfuzzer_sys::fuzz_target;


fuzz_target!(|data: &[u8]| {
let _ = tracing_subscriber::fmt::try_init();
if let Ok(s) = std::str::from_utf8(data) {
tracing::info!(data = ?s, "running with input");
linkerd_addr::fuzz_logic::fuzz_addr_1(s);
}
});
2 changes: 2 additions & 0 deletions linkerd/app/inbound/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ hyper = { version = "0.14.2", features = ["http1", "http2"] }
http = "0.2"
linkerd-app-core = { path = "../../core" }
linkerd-app-test = { path = "../../test" }
tracing = "0.1"
tracing-subscriber = "0.2"

[dependencies.linkerd-app-inbound]
path = ".."
Expand Down
2 changes: 2 additions & 0 deletions linkerd/app/inbound/fuzz/fuzz_targets/fuzz_target_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use libfuzzer_sys::fuzz_target;
use linkerd_app_inbound::http::fuzz_logic::*;

fuzz_target!(|requests: Vec<HttpRequestSpec>| {
let _ = tracing_subscriber::fmt::try_init();
tracing::info!(?requests, "running with input");
if requests.len() == 0 {
return;
}
Expand Down
33 changes: 32 additions & 1 deletion linkerd/app/inbound/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,9 @@ pub mod fuzz_logic {
};
pub use linkerd_app_test as support;
use linkerd_app_test::*;
use std::{fmt, str};

#[derive(Debug, Arbitrary)]
#[derive(Arbitrary)]
pub struct HttpRequestSpec {
pub uri: Vec<u8>,
pub header_name: Vec<u8>,
Expand Down Expand Up @@ -378,4 +379,34 @@ pub mod fuzz_logic {
.push_http_server()
.into_inner()
}

impl fmt::Debug for HttpRequestSpec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Custom `Debug` impl that formats the URI, header name, and header
// value as strings if they are UTF-8, or falls back to raw bytes
// otherwise.
let mut dbg = f.debug_struct("HttpRequestSpec");
dbg.field("http_method", &self.http_method);

if let Ok(uri) = str::from_utf8(&self.uri[..]) {
dbg.field("uri", &uri);
} else {
dbg.field("uri", &self.uri);
}

if let Ok(name) = str::from_utf8(&self.header_name[..]) {
dbg.field("header_name", &name);
} else {
dbg.field("header_name", &self.header_name);
}

if let Ok(value) = str::from_utf8(&self.header_value[..]) {
dbg.field("header_value", &value);
} else {
dbg.field("header_value", &self.header_value);
}

dbg.finish()
}
}
}
2 changes: 2 additions & 0 deletions linkerd/dns/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ cargo-fuzz = true
[dependencies]
libfuzzer-sys = "0.4"
tokio = { version = "1", features = ["rt", "time", "io-util"] }
tracing = "0.1.23"
tracing-subscriber = "0.2.17"

[dependencies.linkerd-dns]
path = ".."
Expand Down
Loading

0 comments on commit 826655d

Please sign in to comment.