Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 1145dd7

Browse files
authored
Add fuzzers for trace propagation parsers. (#319)
1 parent 2eef344 commit 1145dd7

File tree

15 files changed

+180
-0
lines changed

15 files changed

+180
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ endif()
2323

2424
project(opencensus-cpp VERSION 0.4.0 LANGUAGES CXX)
2525

26+
option(FUZZER "Either OFF or e.g. -fsanitize=fuzzer,address" OFF)
27+
2628
set(CMAKE_CXX_STANDARD 11)
2729
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2830

cmake/OpenCensusHelpers.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,16 @@ function(opencensus_lib NAME)
7070
add_library(${PROJECT_NAME}::${NAME} ALIAS ${_NAME})
7171
endif()
7272
endfunction()
73+
74+
# Helper function for fuzzing. Usage:
75+
#
76+
# opencensus_fuzzer(trace_some_fuzzer internal/some_fuzzer.cc dep1 dep2...)
77+
function(opencensus_fuzzer NAME SRC)
78+
if(FUZZER)
79+
set(_NAME "opencensus_${NAME}")
80+
add_executable(${_NAME} ${SRC})
81+
prepend_opencensus(DEPS "${ARGN}")
82+
target_link_libraries(${_NAME} "${DEPS}" ${FUZZER})
83+
target_compile_options(${_NAME} PRIVATE ${FUZZER})
84+
endif()
85+
endfunction()

opencensus/doc/fuzzing.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Fuzzing
2+
3+
To build the fuzzers and run them in test only mode:
4+
5+
```shell
6+
rm -rf .build
7+
tools/fuzz.sh
8+
```
9+
10+
To run the fuzzer and find bugs:
11+
12+
```shell
13+
mkdir /tmp/fuzz
14+
./.build/opencensus/trace/opencensus_trace_cloud_trace_context_fuzzer \
15+
/tmp/fuzz opencensus/trace/internal/cloud_trace_context_corpus
16+
```
17+
18+
The corpus directories are not meant to give complete coverage, but
19+
rather should only contain manually generated inputs that serve as a seed
20+
for the fuzzer, so it can more quickly find a useful search space.

opencensus/trace/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,18 @@ opencensus_benchmark(trace_with_span_benchmark
215215
internal/with_span_benchmark.cc
216216
trace
217217
trace_with_span)
218+
219+
opencensus_fuzzer(trace_cloud_trace_context_fuzzer
220+
internal/cloud_trace_context_fuzzer.cc
221+
trace_cloud_trace_context
222+
absl::strings)
223+
224+
opencensus_fuzzer(trace_grpc_trace_bin_fuzzer
225+
internal/grpc_trace_bin_fuzzer.cc
226+
trace_grpc_trace_bin
227+
absl::strings)
228+
229+
opencensus_fuzzer(trace_trace_context_fuzzer
230+
internal/trace_context_fuzzer.cc
231+
trace_trace_context
232+
absl::strings)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
12345678901234567890123456789012/18446744073709551616
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
12345678901234567890123456789012/12345;o=1
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "absl/strings/string_view.h"
16+
#include "opencensus/trace/propagation/cloud_trace_context.h"
17+
#include "opencensus/trace/span_context.h"
18+
19+
using ::opencensus::trace::SpanContext;
20+
using ::opencensus::trace::propagation::FromCloudTraceContextHeader;
21+
using ::opencensus::trace::propagation::ToCloudTraceContextHeader;
22+
23+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
24+
absl::string_view header(reinterpret_cast<const char *>(Data), Size);
25+
SpanContext ctx = FromCloudTraceContextHeader(header);
26+
if (ctx.IsValid()) {
27+
ToCloudTraceContextHeader(ctx);
28+
}
29+
return 0;
30+
}
29 Bytes
Binary file not shown.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "absl/strings/string_view.h"
16+
#include "opencensus/trace/propagation/grpc_trace_bin.h"
17+
#include "opencensus/trace/span_context.h"
18+
19+
using ::opencensus::trace::SpanContext;
20+
using ::opencensus::trace::propagation::FromGrpcTraceBinHeader;
21+
using ::opencensus::trace::propagation::ToGrpcTraceBinHeader;
22+
using ::opencensus::trace::propagation::kGrpcTraceBinHeaderLen;
23+
24+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
25+
absl::string_view header(reinterpret_cast<const char *>(Data), Size);
26+
uint8_t outbuf[kGrpcTraceBinHeaderLen];
27+
SpanContext ctx = FromGrpcTraceBinHeader(header);
28+
if (ctx.IsValid()) {
29+
ToGrpcTraceBinHeader(ctx, outbuf);
30+
}
31+
return 0;
32+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
00-404142434445464748494a4b4c4d4e4f-6162636465666768-0`

0 commit comments

Comments
 (0)