ca5201ed0ace378e6f1fff61e2005854740270a9
[platform/upstream/grpc.git] / test / core / util / fuzzer_corpus_test.cc
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19 #include <dirent.h>
20 #include <grpc/grpc.h>
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/log.h>
23 #include <gtest/gtest.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27
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"
33
34 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
35 extern bool squelch;
36 extern bool leak_check;
37
38 ABSL_FLAG(std::string, file, "", "Use this file as test data");
39 ABSL_FLAG(std::string, directory, "", "Use this directory as test data");
40
41 class FuzzerCorpusTest : public ::testing::TestWithParam<std::string> {};
42
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
47   // internally.
48   grpc_init();
49   gpr_log(GPR_DEBUG, "Example file: %s", GetParam().c_str());
50   grpc_slice buffer;
51   squelch = false;
52   leak_check = false;
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);
59   grpc_shutdown();
60   LLVMFuzzerTestOneInput(static_cast<uint8_t*>(data), length);
61   gpr_free(data);
62 }
63
64 class ExampleGenerator
65     : public ::testing::internal::ParamGeneratorInterface<std::string> {
66  public:
67   ::testing::internal::ParamIteratorInterface<std::string>* Begin()
68       const override;
69   ::testing::internal::ParamIteratorInterface<std::string>* End()
70       const override;
71
72  private:
73   void Materialize() const {
74     if (examples_.empty()) {
75       if (!absl::GetFlag(FLAGS_file).empty()) {
76         examples_.push_back(absl::GetFlag(FLAGS_file));
77       }
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) {
83           directory =
84               test_srcdir + std::string("/com_github_grpc_grpc/") + directory;
85         }
86         gpr_log(GPR_DEBUG, "Using corpus directory: %s", directory.c_str());
87         DIR* dp;
88         struct dirent* ep;
89         dp = opendir(directory.c_str());
90
91         if (dp != nullptr) {
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);
95             }
96           }
97
98           (void)closedir(dp);
99         } else {
100           perror("Couldn't open the directory");
101           abort();
102         }
103         gpr_free(test_srcdir);
104       }
105     }
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());
109   }
110
111   mutable std::vector<std::string> examples_;
112 };
113
114 class ExampleIterator
115     : public ::testing::internal::ParamIteratorInterface<std::string> {
116  public:
117   ExampleIterator(const ExampleGenerator& base_,
118                   std::vector<std::string>::const_iterator begin)
119       : base_(base_), begin_(begin), current_(begin) {}
120
121   const ExampleGenerator* BaseGenerator() const override { return &base_; }
122
123   void Advance() override { current_++; }
124   ExampleIterator* Clone() const override { return new ExampleIterator(*this); }
125   const std::string* Current() const override { return &*current_; }
126
127   bool Equals(const ParamIteratorInterface<std::string>& other) const override {
128     return &base_ == other.BaseGenerator() &&
129            current_ == dynamic_cast<const ExampleIterator*>(&other)->current_;
130   }
131
132  private:
133   ExampleIterator(const ExampleIterator& other)
134       : base_(other.base_), begin_(other.begin_), current_(other.current_) {}
135
136   const ExampleGenerator& base_;
137   const std::vector<std::string>::const_iterator begin_;
138   std::vector<std::string>::const_iterator current_;
139 };
140
141 ::testing::internal::ParamIteratorInterface<std::string>*
142 ExampleGenerator::Begin() const {
143   Materialize();
144   return new ExampleIterator(*this, examples_.begin());
145 }
146
147 ::testing::internal::ParamIteratorInterface<std::string>*
148 ExampleGenerator::End() const {
149   Materialize();
150   return new ExampleIterator(*this, examples_.end());
151 }
152
153 INSTANTIATE_TEST_SUITE_P(
154     CorpusExamples, FuzzerCorpusTest,
155     ::testing::internal::ParamGenerator<std::string>(new ExampleGenerator));
156
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);
161
162   return RUN_ALL_TESTS();
163 }