Skip to content

Commit c91a56e

Browse files
authored
Add a very simple fuzz test for HttpTemplate::Parse (#56)
This commit includes both a simple fuzz test as well as the infrastructure needed to make the fuzz test run. To run the fuzz test, use a command like: $bazel run --config=asan-libfuzzer //test:http_template_fuzz_test_run \ -- --clean --timeout_secs=30 As for setting up the fuzz testing framework, I followed the directions from https://github.com/bazelbuild/rules_fuzzing/blob/master/README.md. At the time of this commit, the latest version of the fuzz testing library was release 0.1.1 (https://github.com/bazelbuild/rules_fuzzing/releases/tag/v0.1.1), so that’s where I pointed the bazel rules.
1 parent c76472d commit c91a56e

File tree

10 files changed

+61
-0
lines changed

10 files changed

+61
-0
lines changed

.bazelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Force the use of Clang for C++ builds.
2+
build --action_env=CC=clang-10
3+
build --action_env=CXX=clang++-10
4+
5+
# Define the --config=asan-libfuzzer configuration.
6+
build:asan-libfuzzer --@rules_fuzzing//fuzzing:cc_engine=@rules_fuzzing//fuzzing/engines:libfuzzer
7+
build:asan-libfuzzer --@rules_fuzzing//fuzzing:cc_engine_instrumentation=libfuzzer
8+
build:asan-libfuzzer --@rules_fuzzing//fuzzing:cc_engine_sanitizer=asan

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ following commands build and test sources:
1919
$ bazel build //...
2020
$ bazel test //...
2121
```
22+
## Toolchain
23+
24+
The Bazel build system defaults to using clang 10 to enable reproducible builds.
25+
2226

2327
# Contribution
2428
See [CONTRIBUTING.md](CONTRIBUTING.md).

WORKSPACE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ load(
2323
"protobuf_repositories",
2424
)
2525

26+
# See
27+
# https://github.com/bazelbuild/rules_fuzzing/blob/master/README.md#configuring-the-workspace.
28+
# The fuzzing rules must be first because if they are not, bazel will
29+
# pull in incompatible versions of absl and rules_python.
30+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
31+
32+
http_archive(
33+
name = "rules_fuzzing",
34+
sha256 = "a5734cb42b1b69395c57e0bbd32ade394d5c3d6afbfe782b24816a96da24660d",
35+
strip_prefix = "rules_fuzzing-0.1.1",
36+
urls = ["https://github.com/bazelbuild/rules_fuzzing/archive/v0.1.1.zip"],
37+
)
38+
39+
load("@rules_fuzzing//fuzzing:repositories.bzl", "rules_fuzzing_dependencies")
40+
41+
rules_fuzzing_dependencies()
42+
43+
load("@rules_fuzzing//fuzzing:init.bzl", "rules_fuzzing_init")
44+
45+
rules_fuzzing_init()
46+
2647
absl_repositories()
2748

2849
protobuf_repositories()

test/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
################################################################################
1616
#
1717
load("@com_google_protobuf//:protobuf.bzl", "py_proto_library")
18+
load("@rules_fuzzing//fuzzing:cc_defs.bzl", "cc_fuzz_test")
1819

1920
package(default_visibility = ["//visibility:public"])
2021

@@ -31,6 +32,17 @@ cc_test(
3132
],
3233
)
3334

35+
cc_fuzz_test(
36+
name = "http_template_fuzz_test",
37+
srcs = [
38+
"http_template_fuzz_test.cc",
39+
],
40+
corpus = glob(["http_template_fuzz_test_corpus/**"]),
41+
deps = [
42+
"//src:http_template",
43+
],
44+
)
45+
3446
cc_test(
3547
name = "path_matcher_test",
3648
size = "small",

test/http_template_fuzz_test.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "grpc_transcoding/http_template.h"
2+
3+
#include <cstdint>
4+
#include <cstddef>
5+
#include <string>
6+
7+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
8+
std::string path((const char*)data, size);
9+
google::grpc::transcoding::HttpTemplate::Parse(path);
10+
return 0;
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/foo
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/foo/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/{root=**}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/foo/*
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/{root=*}

0 commit comments

Comments
 (0)