Skip to content

Commit 4be89d5

Browse files
committedMar 8, 2020
Add fuzz test for server::accept
1 parent 90e9717 commit 4be89d5

File tree

8 files changed

+149
-0
lines changed

8 files changed

+149
-0
lines changed
 

‎fuzz/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
target
3+
corpus
4+
artifacts

‎fuzz/Cargo.toml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
[package]
3+
name = "async-h1-fuzz"
4+
version = "0.0.0"
5+
authors = ["Automatically generated"]
6+
publish = false
7+
edition = "2018"
8+
9+
[package.metadata]
10+
cargo-fuzz = true
11+
12+
[dependencies]
13+
async-std = "1.5.0"
14+
http-types = "1.0.0"
15+
libfuzzer-sys = "0.3"
16+
futures-io = "0.3"
17+
18+
[dependencies.async-h1]
19+
path = ".."
20+
21+
# Prevent this from interfering with workspaces
22+
[workspace]
23+
members = ["."]
24+
25+
[[bin]]
26+
name = "server_accept"
27+
path = "fuzz_targets/server_accept.rs"

‎fuzz/dicts/server_accept

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"HTTP/1.1"
2+
"GET"
3+
"POST"
4+
"PUT"
5+
"DELETE"
6+
"PATCH"
7+
"OPTIONS"
8+
"CONNECT"
9+
"HEAD"
10+
" /"
11+
"index.html"
12+
"?q="
13+
"content-type"
14+
"transfer-encoding"
15+
"chunked"
16+
"text/plain"
17+
"application/octet-stream"
18+
"application/json"
19+
"image/png"
20+
"audio/opus"
21+
"authorization"
22+
"cookie"
23+
"content-length"
24+
"host"
25+
"Basic"
26+
"accept-encoding"
27+
"gzip"
28+
"br"
29+
"\x0d\x0a"
30+
": "

‎fuzz/fuzz_targets/server_accept.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#![no_main]
2+
use libfuzzer_sys::fuzz_target;
3+
4+
use std::pin::Pin;
5+
use std::sync::{Arc, Mutex};
6+
use std::task::{Context, Poll};
7+
8+
use async_std::io::Cursor;
9+
use futures_io::{AsyncRead, AsyncWrite};
10+
11+
#[derive(Clone, Debug)]
12+
struct RwWrapper(Arc<Mutex<Cursor<Vec<u8>>>>);
13+
14+
impl RwWrapper {
15+
fn new(input: Vec<u8>) -> Self {
16+
Self(Arc::new(Mutex::new(Cursor::new(input))))
17+
}
18+
}
19+
20+
impl AsyncRead for RwWrapper {
21+
fn poll_read(
22+
mut self: Pin<&mut Self>,
23+
cx: &mut Context<'_>,
24+
buf: &mut [u8],
25+
) -> Poll<std::io::Result<usize>> {
26+
Pin::new(&mut *self.0.lock().unwrap()).poll_read(cx, buf)
27+
}
28+
}
29+
30+
impl AsyncWrite for RwWrapper {
31+
fn poll_write(
32+
self: Pin<&mut Self>,
33+
_cx: &mut Context<'_>,
34+
buf: &[u8],
35+
) -> Poll<std::io::Result<usize>> {
36+
Poll::Ready(Ok(buf.len()))
37+
}
38+
39+
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
40+
Poll::Ready(Ok(()))
41+
}
42+
43+
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
44+
Poll::Ready(Ok(()))
45+
}
46+
}
47+
48+
fuzz_target!(|request: &[u8]| {
49+
let stream = RwWrapper::new(request.to_vec());
50+
async_std::task::block_on(async_h1::accept("http://localhost", stream, |req| async {
51+
let mut res = http_types::Response::new(http_types::StatusCode::Ok);
52+
res.set_body(req);
53+
Ok(res)
54+
}))
55+
.ok();
56+
});
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
POST / HTTP/1.1
2+
content-type: text/plain
3+
transfer-encoding: chunked
4+
5+
5
6+
Hello
7+
a
8+
aaaaabbbbb
9+
0
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
POST / HTTP/1.1
2+
content-type: text/plain
3+
content-length: 10
4+
5+
aaaaabbbbb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
POST / HTTP/1.1
2+
content-length: 0
3+

‎run-fuzzer.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
set -e
3+
4+
TARGET_NAME="$1"
5+
if [ -z "$TARGET_NAME" ]; then
6+
echo "$0: target name required" >&2
7+
exit 1
8+
fi
9+
10+
mkdir -p "./fuzz/corpus/${TARGET_NAME}/"
11+
cargo +nightly fuzz run "${TARGET_NAME}" \
12+
"./fuzz/corpus/${TARGET_NAME}/" "./fuzz/init_corpus/${TARGET_NAME}/" -- \
13+
-dict="./fuzz/dicts/${TARGET_NAME}" \
14+
-timeout=3

0 commit comments

Comments
 (0)
Please sign in to comment.