3 * Copyright 2016 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include <grpc/grpc.h>
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/log.h>
23 #include <gtest/gtest.h>
26 #include <sys/types.h>
28 #include "absl/flags/flag.h"
29 #include "src/core/lib/gpr/env.h"
30 #include "src/core/lib/iomgr/load_file.h"
31 #include "test/core/util/test_config.h"
32 #include "test/cpp/util/test_config.h"
34 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
36 extern bool leak_check;
38 ABSL_FLAG(std::string, file, "", "Use this file as test data");
39 ABSL_FLAG(std::string, directory, "", "Use this directory as test data");
41 class FuzzerCorpusTest : public ::testing::TestWithParam<std::string> {};
43 TEST_P(FuzzerCorpusTest, RunOneExample) {
44 // Need to call grpc_init() here to use a slice, but need to shut it
45 // down before calling LLVMFuzzerTestOneInput(), because most
46 // implementations of that function will initialize and shutdown gRPC
49 gpr_log(GPR_DEBUG, "Example file: %s", GetParam().c_str());
53 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
54 grpc_load_file(GetParam().c_str(), 0, &buffer)));
55 size_t length = GRPC_SLICE_LENGTH(buffer);
56 void* data = gpr_malloc(length);
57 memcpy(data, GPR_SLICE_START_PTR(buffer), length);
58 grpc_slice_unref(buffer);
60 LLVMFuzzerTestOneInput(static_cast<uint8_t*>(data), length);
64 class ExampleGenerator
65 : public ::testing::internal::ParamGeneratorInterface<std::string> {
67 ::testing::internal::ParamIteratorInterface<std::string>* Begin()
69 ::testing::internal::ParamIteratorInterface<std::string>* End()
73 void Materialize() const {
74 if (examples_.empty()) {
75 if (!absl::GetFlag(FLAGS_file).empty()) {
76 examples_.push_back(absl::GetFlag(FLAGS_file));
78 if (!absl::GetFlag(FLAGS_directory).empty()) {
79 char* test_srcdir = gpr_getenv("TEST_SRCDIR");
80 gpr_log(GPR_DEBUG, "test_srcdir=\"%s\"", test_srcdir);
81 std::string directory = absl::GetFlag(FLAGS_directory);
82 if (test_srcdir != nullptr) {
84 test_srcdir + std::string("/com_github_grpc_grpc/") + directory;
86 gpr_log(GPR_DEBUG, "Using corpus directory: %s", directory.c_str());
89 dp = opendir(directory.c_str());
92 while ((ep = readdir(dp)) != nullptr) {
93 if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0) {
94 examples_.push_back(directory + "/" + ep->d_name);
100 perror("Couldn't open the directory");
103 gpr_free(test_srcdir);
106 // Make sure we don't succeed without doing anything, which caused
107 // us to be blind to our fuzzers not running for 9 months.
108 GPR_ASSERT(!examples_.empty());
111 mutable std::vector<std::string> examples_;
114 class ExampleIterator
115 : public ::testing::internal::ParamIteratorInterface<std::string> {
117 ExampleIterator(const ExampleGenerator& base_,
118 std::vector<std::string>::const_iterator begin)
119 : base_(base_), begin_(begin), current_(begin) {}
121 const ExampleGenerator* BaseGenerator() const override { return &base_; }
123 void Advance() override { current_++; }
124 ExampleIterator* Clone() const override { return new ExampleIterator(*this); }
125 const std::string* Current() const override { return &*current_; }
127 bool Equals(const ParamIteratorInterface<std::string>& other) const override {
128 return &base_ == other.BaseGenerator() &&
129 current_ == dynamic_cast<const ExampleIterator*>(&other)->current_;
133 ExampleIterator(const ExampleIterator& other)
134 : base_(other.base_), begin_(other.begin_), current_(other.current_) {}
136 const ExampleGenerator& base_;
137 const std::vector<std::string>::const_iterator begin_;
138 std::vector<std::string>::const_iterator current_;
141 ::testing::internal::ParamIteratorInterface<std::string>*
142 ExampleGenerator::Begin() const {
144 return new ExampleIterator(*this, examples_.begin());
147 ::testing::internal::ParamIteratorInterface<std::string>*
148 ExampleGenerator::End() const {
150 return new ExampleIterator(*this, examples_.end());
153 INSTANTIATE_TEST_SUITE_P(
154 CorpusExamples, FuzzerCorpusTest,
155 ::testing::internal::ParamGenerator<std::string>(new ExampleGenerator));
157 int main(int argc, char** argv) {
158 grpc::testing::TestEnvironment env(argc, argv);
159 grpc::testing::InitTest(&argc, &argv, true);
160 ::testing::InitGoogleTest(&argc, argv);
162 return RUN_ALL_TESTS();