• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdbool.h>
20 
21 #include <dirent.h>
22 #include <gflags/gflags.h>
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 #include <gtest/gtest.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 
29 #include <grpc/grpc.h>
30 
31 #include "src/core/lib/gpr/env.h"
32 #include "src/core/lib/iomgr/load_file.h"
33 #include "test/core/util/test_config.h"
34 #include "test/cpp/util/test_config.h"
35 
36 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
37 extern bool squelch;
38 extern bool leak_check;
39 
40 // In some distros, gflags is in the namespace google, and in some others,
41 // in gflags. This hack is enabling us to find both.
42 namespace google {}
43 namespace gflags {}
44 using namespace google;
45 using namespace gflags;
46 
47 DEFINE_string(file, "", "Use this file as test data");
48 DEFINE_string(directory, "", "Use this directory as test data");
49 
50 class FuzzerCorpusTest : public ::testing::TestWithParam<std::string> {};
51 
TEST_P(FuzzerCorpusTest,RunOneExample)52 TEST_P(FuzzerCorpusTest, RunOneExample) {
53   // Need to call grpc_init() here to use a slice, but need to shut it
54   // down before calling LLVMFuzzerTestOneInput(), because most
55   // implementations of that function will initialize and shutdown gRPC
56   // internally.
57   grpc_init();
58   gpr_log(GPR_DEBUG, "Example file: %s", GetParam().c_str());
59   grpc_slice buffer;
60   squelch = false;
61   leak_check = false;
62   GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
63                                grpc_load_file(GetParam().c_str(), 0, &buffer)));
64   size_t length = GRPC_SLICE_LENGTH(buffer);
65   void* data = gpr_malloc(length);
66   memcpy(data, GPR_SLICE_START_PTR(buffer), length);
67   grpc_slice_unref(buffer);
68   grpc_shutdown_blocking();
69   LLVMFuzzerTestOneInput(static_cast<uint8_t*>(data), length);
70   gpr_free(data);
71 }
72 
73 class ExampleGenerator
74     : public ::testing::internal::ParamGeneratorInterface<std::string> {
75  public:
76   virtual ::testing::internal::ParamIteratorInterface<std::string>* Begin()
77       const;
78   virtual ::testing::internal::ParamIteratorInterface<std::string>* End() const;
79 
80  private:
Materialize() const81   void Materialize() const {
82     if (examples_.empty()) {
83       if (!FLAGS_file.empty()) examples_.push_back(FLAGS_file);
84       if (!FLAGS_directory.empty()) {
85         char* test_srcdir = gpr_getenv("TEST_SRCDIR");
86         gpr_log(GPR_DEBUG, "test_srcdir=\"%s\"", test_srcdir);
87         std::string directory = FLAGS_directory;
88         if (test_srcdir != nullptr) {
89           directory =
90               test_srcdir + std::string("/com_github_grpc_grpc/") + directory;
91         }
92         gpr_log(GPR_DEBUG, "Using corpus directory: %s", directory.c_str());
93         DIR* dp;
94         struct dirent* ep;
95         dp = opendir(directory.c_str());
96 
97         if (dp != nullptr) {
98           while ((ep = readdir(dp)) != nullptr) {
99             if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0) {
100               examples_.push_back(directory + "/" + ep->d_name);
101             }
102           }
103 
104           (void)closedir(dp);
105         } else {
106           perror("Couldn't open the directory");
107           abort();
108         }
109         gpr_free(test_srcdir);
110       }
111     }
112     // Make sure we don't succeed without doing anything, which caused
113     // us to be blind to our fuzzers not running for 9 months.
114     GPR_ASSERT(!examples_.empty());
115   }
116 
117   mutable std::vector<std::string> examples_;
118 };
119 
120 class ExampleIterator
121     : public ::testing::internal::ParamIteratorInterface<std::string> {
122  public:
ExampleIterator(const ExampleGenerator & base_,std::vector<std::string>::const_iterator begin)123   ExampleIterator(const ExampleGenerator& base_,
124                   std::vector<std::string>::const_iterator begin)
125       : base_(base_), begin_(begin), current_(begin) {}
126 
BaseGenerator() const127   virtual const ExampleGenerator* BaseGenerator() const { return &base_; }
128 
Advance()129   virtual void Advance() { current_++; }
Clone() const130   virtual ExampleIterator* Clone() const { return new ExampleIterator(*this); }
Current() const131   virtual const std::string* Current() const { return &*current_; }
132 
Equals(const ParamIteratorInterface<std::string> & other) const133   virtual bool Equals(const ParamIteratorInterface<std::string>& other) const {
134     return &base_ == other.BaseGenerator() &&
135            current_ == dynamic_cast<const ExampleIterator*>(&other)->current_;
136   }
137 
138  private:
ExampleIterator(const ExampleIterator & other)139   ExampleIterator(const ExampleIterator& other)
140       : base_(other.base_), begin_(other.begin_), current_(other.current_) {}
141 
142   const ExampleGenerator& base_;
143   const std::vector<std::string>::const_iterator begin_;
144   std::vector<std::string>::const_iterator current_;
145 };
146 
147 ::testing::internal::ParamIteratorInterface<std::string>*
Begin() const148 ExampleGenerator::Begin() const {
149   Materialize();
150   return new ExampleIterator(*this, examples_.begin());
151 }
152 
153 ::testing::internal::ParamIteratorInterface<std::string>*
End() const154 ExampleGenerator::End() const {
155   Materialize();
156   return new ExampleIterator(*this, examples_.end());
157 }
158 
159 INSTANTIATE_TEST_SUITE_P(
160     CorpusExamples, FuzzerCorpusTest,
161     ::testing::internal::ParamGenerator<std::string>(new ExampleGenerator));
162 
main(int argc,char ** argv)163 int main(int argc, char** argv) {
164   grpc::testing::TestEnvironment env(argc, argv);
165   grpc::testing::InitTest(&argc, &argv, true);
166   ::testing::InitGoogleTest(&argc, argv);
167 
168   return RUN_ALL_TESTS();
169 }
170