Debian Codegraph @

test/core/util/fuzzer_corpus_test.cc

raw
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 <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <algorithm>
26 #include <string>
27 #include <vector>
28
29 #include "absl/flags/flag.h"
30 #include "absl/strings/string_view.h"
31 #include "absl/types/optional.h"
32 #include "gtest/gtest.h"
33
34 #include <grpc/slice.h>
35 #include <grpc/support/alloc.h>
36 #include <grpc/support/log.h>
37
38 #include "src/core/lib/gprpp/env.h"
39 #include "src/core/lib/iomgr/error.h"
40 #include "src/core/lib/iomgr/load_file.h"
41 #include "test/core/util/test_config.h"
42 #include "test/cpp/util/test_config.h"
43
44 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
45 extern bool squelch;
46
47 ABSL_FLAG(std::string, file, "", "Use this file as test data");
48 ABSL_FLAG(std::string, directory, "", "Use this directory as test data");
49
50 class FuzzerCorpusTest : public ::testing::TestWithParam<std::string> {};
51
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 gpr_log(GPR_INFO, "Example file: %s", GetParam().c_str());
58 grpc_slice buffer;
59 squelch = false;
60 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
61 grpc_load_file(GetParam().c_str(), 0, &buffer)));
62 size_t length = GRPC_SLICE_LENGTH(buffer);
63 void* data = gpr_malloc(length);
64 if (length > 0) {
65 memcpy(data, GPR_SLICE_START_PTR(buffer), length);
66 }
67 grpc_slice_unref(buffer);
68 LLVMFuzzerTestOneInput(static_cast<uint8_t*>(data), length);
69 gpr_free(data);
70 }
71
72 class ExampleGenerator
73 : public ::testing::internal::ParamGeneratorInterface<std::string> {
74 public:
75 ::testing::internal::ParamIteratorInterface<std::string>* Begin()
76 const override;
77 ::testing::internal::ParamIteratorInterface<std::string>* End()
78 const override;
79
80 private:
81 void Materialize() const {
82 if (examples_.empty()) {
83 if (!absl::GetFlag(FLAGS_file).empty()) {
84 examples_.push_back(absl::GetFlag(FLAGS_file));
85 }
86 if (!absl::GetFlag(FLAGS_directory).empty()) {
87 auto test_srcdir = grpc_core::GetEnv("TEST_SRCDIR");
88 gpr_log(GPR_DEBUG, "test_srcdir=\"%s\"",
89 test_srcdir.has_value() ? test_srcdir->c_str() : "(null)");
90 std::string directory = absl::GetFlag(FLAGS_directory);
91 if (test_srcdir.has_value()) {
92 directory =
93 *test_srcdir + std::string("/com_github_grpc_grpc/") + directory;
94 }
95 gpr_log(GPR_DEBUG, "Using corpus directory: %s", directory.c_str());
96 DIR* dp;
97 struct dirent* ep;
98 dp = opendir(directory.c_str());
99
100 if (dp != nullptr) {
101 while ((ep = readdir(dp)) != nullptr) {
102 if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0) {
103 examples_.push_back(directory + "/" + ep->d_name);
104 }
105 }
106
107 (void)closedir(dp);
108 } else {
109 perror("Couldn't open the directory");
110 abort();
111 }
112 }
113 }
114 // Make sure we don't succeed without doing anything, which caused
115 // us to be blind to our fuzzers not running for 9 months.
116 GPR_ASSERT(!examples_.empty());
117 // Get a consistent ordering of examples so problems don't just show up on
118 // CI
119 std::sort(examples_.begin(), examples_.end());
120 }
121
122 mutable std::vector<std::string> examples_;
123 };
124
125 class ExampleIterator
126 : public ::testing::internal::ParamIteratorInterface<std::string> {
127 public:
128 ExampleIterator(const ExampleGenerator& base_,
129 std::vector<std::string>::const_iterator begin)
130 : base_(base_), begin_(begin), current_(begin) {}
131
132 const ExampleGenerator* BaseGenerator() const override { return &base_; }
133
134 void Advance() override { current_++; }
135 ExampleIterator* Clone() const override { return new ExampleIterator(*this); }
136 const std::string* Current() const override { return &*current_; }
137
138 bool Equals(const ParamIteratorInterface<std::string>& other) const override {
139 return &base_ == other.BaseGenerator() &&
140 current_ == dynamic_cast<const ExampleIterator*>(&other)->current_;
141 }
142
143 private:
144 ExampleIterator(const ExampleIterator& other)
145 : base_(other.base_), begin_(other.begin_), current_(other.current_) {}
146
147 const ExampleGenerator& base_;
148 const std::vector<std::string>::const_iterator begin_;
149 std::vector<std::string>::const_iterator current_;
150 };
151
152 ::testing::internal::ParamIteratorInterface<std::string>*
153 ExampleGenerator::Begin() const {
154 Materialize();
155 return new ExampleIterator(*this, examples_.begin());
156 }
157
158 ::testing::internal::ParamIteratorInterface<std::string>*
159 ExampleGenerator::End() const {
160 Materialize();
161 return new ExampleIterator(*this, examples_.end());
162 }
163
164 INSTANTIATE_TEST_SUITE_P(
165 CorpusExamples, FuzzerCorpusTest,
166 ::testing::internal::ParamGenerator<std::string>(new ExampleGenerator));
167
168 int main(int argc, char** argv) {
169 grpc::testing::TestEnvironment env(&argc, argv);
170 grpc::testing::InitTest(&argc, &argv, true);
171 ::testing::InitGoogleTest(&argc, argv);
172
173 return RUN_ALL_TESTS();
174 }