diff --git a/test/core/compression/message_compress_corpus/dummy b/test/core/compression/message_compress_corpus/dummy new file mode 100644 index 00000000000..c1b0730e013 --- /dev/null +++ b/test/core/compression/message_compress_corpus/dummy @@ -0,0 +1 @@ +x \ No newline at end of file diff --git a/test/core/compression/message_decompress_corpus/dummy b/test/core/compression/message_decompress_corpus/dummy new file mode 100644 index 00000000000..c1b0730e013 --- /dev/null +++ b/test/core/compression/message_decompress_corpus/dummy @@ -0,0 +1 @@ +x \ No newline at end of file diff --git a/test/core/compression/stream_compression_corpus/dummy b/test/core/compression/stream_compression_corpus/dummy new file mode 100644 index 00000000000..c1b0730e013 --- /dev/null +++ b/test/core/compression/stream_compression_corpus/dummy @@ -0,0 +1 @@ +x \ No newline at end of file diff --git a/test/core/compression/stream_decompression_corpus/dummy b/test/core/compression/stream_decompression_corpus/dummy new file mode 100644 index 00000000000..c1b0730e013 --- /dev/null +++ b/test/core/compression/stream_decompression_corpus/dummy @@ -0,0 +1 @@ +x \ No newline at end of file diff --git a/test/core/slice/b64_decode_fuzzer.cc b/test/core/slice/b64_decode_fuzzer.cc index 96ca91af1b8..3bcd86a7d3a 100644 --- a/test/core/slice/b64_decode_fuzzer.cc +++ b/test/core/slice/b64_decode_fuzzer.cc @@ -19,6 +19,8 @@ #include #include +#include + #include "src/core/lib/slice/b64.h" bool squelch = true; @@ -26,9 +28,11 @@ bool leak_check = true; extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { if (size < 1) return 0; + grpc_init(); const bool url_safe = static_cast(0x100) < data[0]; grpc_slice res = grpc_base64_decode_with_len( reinterpret_cast(data + 1), size - 1, url_safe); grpc_slice_unref(res); + grpc_shutdown_blocking(); return 0; } diff --git a/test/core/util/fuzzer_corpus_test.cc b/test/core/util/fuzzer_corpus_test.cc index 60529689e06..233a2d5ee73 100644 --- a/test/core/util/fuzzer_corpus_test.cc +++ b/test/core/util/fuzzer_corpus_test.cc @@ -26,6 +26,8 @@ #include #include +#include + #include "src/core/lib/gpr/env.h" #include "src/core/lib/iomgr/load_file.h" #include "test/core/util/test_config.h" @@ -48,15 +50,24 @@ DEFINE_string(directory, "", "Use this directory as test data"); class FuzzerCorpusTest : public ::testing::TestWithParam {}; TEST_P(FuzzerCorpusTest, RunOneExample) { + // Need to call grpc_init() here to use a slice, but need to shut it + // down before calling LLVMFuzzerTestOneInput(), because most + // implementations of that function will initialize and shutdown gRPC + // internally. + grpc_init(); gpr_log(GPR_DEBUG, "Example file: %s", GetParam().c_str()); grpc_slice buffer; squelch = false; leak_check = false; GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", grpc_load_file(GetParam().c_str(), 0, &buffer))); - LLVMFuzzerTestOneInput(GRPC_SLICE_START_PTR(buffer), - GRPC_SLICE_LENGTH(buffer)); + size_t length = GRPC_SLICE_LENGTH(buffer); + void* data = gpr_malloc(length); + memcpy(data, GPR_SLICE_START_PTR(buffer), length); grpc_slice_unref(buffer); + grpc_shutdown_blocking(); + LLVMFuzzerTestOneInput(static_cast(data), length); + gpr_free(data); } class ExampleGenerator @@ -72,19 +83,21 @@ class ExampleGenerator if (!FLAGS_file.empty()) examples_.push_back(FLAGS_file); if (!FLAGS_directory.empty()) { char* test_srcdir = gpr_getenv("TEST_SRCDIR"); + gpr_log(GPR_DEBUG, "test_srcdir=\"%s\"", test_srcdir); + std::string directory = FLAGS_directory; if (test_srcdir != nullptr) { - FLAGS_directory = test_srcdir + - std::string("/com_github_grpc_grpc/") + - FLAGS_directory; + directory = + test_srcdir + std::string("/com_github_grpc_grpc/") + directory; } + gpr_log(GPR_DEBUG, "Using corpus directory: %s", directory.c_str()); DIR* dp; struct dirent* ep; - dp = opendir(FLAGS_directory.c_str()); + dp = opendir(directory.c_str()); if (dp != nullptr) { while ((ep = readdir(dp)) != nullptr) { - if (ep->d_type == DT_REG) { - examples_.push_back(FLAGS_directory + "/" + ep->d_name); + if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0) { + examples_.push_back(directory + "/" + ep->d_name); } } @@ -96,6 +109,9 @@ class ExampleGenerator gpr_free(test_srcdir); } } + // Make sure we don't succeed without doing anything, which caused + // us to be blind to our fuzzers not running for 9 months. + GPR_ASSERT(!examples_.empty()); } mutable std::vector examples_; @@ -146,8 +162,8 @@ INSTANTIATE_TEST_SUITE_P( int main(int argc, char** argv) { grpc::testing::TestEnvironment env(argc, argv); - ::testing::InitGoogleTest(&argc, argv); grpc::testing::InitTest(&argc, &argv, true); + ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }