fuzzer_corpus_test.cc
Go to the documentation of this file.
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 <stdbool.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 
24 #include <gtest/gtest.h>
25 
26 #include "absl/flags/flag.h"
27 
28 #include <grpc/grpc.h>
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 
32 #include "src/core/lib/gpr/env.h"
36 
37 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
38 extern bool squelch;
39 
40 ABSL_FLAG(std::string, file, "", "Use this file as test data");
41 ABSL_FLAG(std::string, directory, "", "Use this directory as test data");
42 
43 class FuzzerCorpusTest : public ::testing::TestWithParam<std::string> {};
44 
45 TEST_P(FuzzerCorpusTest, RunOneExample) {
46  // Need to call grpc_init() here to use a slice, but need to shut it
47  // down before calling LLVMFuzzerTestOneInput(), because most
48  // implementations of that function will initialize and shutdown gRPC
49  // internally.
50  gpr_log(GPR_INFO, "Example file: %s", GetParam().c_str());
52  squelch = false;
53  GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
54  grpc_load_file(GetParam().c_str(), 0, &buffer)));
56  void* data = gpr_malloc(length);
57  if (length > 0) {
58  memcpy(data, GPR_SLICE_START_PTR(buffer), length);
59  }
61  LLVMFuzzerTestOneInput(static_cast<uint8_t*>(data), length);
62  gpr_free(data);
63 }
64 
66  : public ::testing::internal::ParamGeneratorInterface<std::string> {
67  public:
69  const override;
71  const override;
72 
73  private:
74  void Materialize() const {
75  if (examples_.empty()) {
76  if (!absl::GetFlag(FLAGS_file).empty()) {
77  examples_.push_back(absl::GetFlag(FLAGS_file));
78  }
79  if (!absl::GetFlag(FLAGS_directory).empty()) {
80  char* test_srcdir = gpr_getenv("TEST_SRCDIR");
81  gpr_log(GPR_DEBUG, "test_srcdir=\"%s\"", test_srcdir);
82  std::string directory = absl::GetFlag(FLAGS_directory);
83  if (test_srcdir != nullptr) {
84  directory =
85  test_srcdir + std::string("/com_github_grpc_grpc/") + directory;
86  }
87  gpr_log(GPR_DEBUG, "Using corpus directory: %s", directory.c_str());
88  DIR* dp;
89  struct dirent* ep;
90  dp = opendir(directory.c_str());
91 
92  if (dp != nullptr) {
93  while ((ep = readdir(dp)) != nullptr) {
94  if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0) {
95  examples_.push_back(directory + "/" + ep->d_name);
96  }
97  }
98 
99  (void)closedir(dp);
100  } else {
101  perror("Couldn't open the directory");
102  abort();
103  }
104  gpr_free(test_srcdir);
105  }
106  }
107  // Make sure we don't succeed without doing anything, which caused
108  // us to be blind to our fuzzers not running for 9 months.
109  GPR_ASSERT(!examples_.empty());
110  // Get a consistent ordering of examples so problems don't just show up on
111  // CI
112  std::sort(examples_.begin(), examples_.end());
113  }
114 
115  mutable std::vector<std::string> examples_;
116 };
117 
119  : public ::testing::internal::ParamIteratorInterface<std::string> {
120  public:
122  std::vector<std::string>::const_iterator begin)
124 
125  const ExampleGenerator* BaseGenerator() const override { return &base_; }
126 
127  void Advance() override { current_++; }
128  ExampleIterator* Clone() const override { return new ExampleIterator(*this); }
129  const std::string* Current() const override { return &*current_; }
130 
131  bool Equals(const ParamIteratorInterface<std::string>& other) const override {
132  return &base_ == other.BaseGenerator() &&
133  current_ == dynamic_cast<const ExampleIterator*>(&other)->current_;
134  }
135 
136  private:
138  : base_(other.base_), begin_(other.begin_), current_(other.current_) {}
139 
141  const std::vector<std::string>::const_iterator begin_;
142  std::vector<std::string>::const_iterator current_;
143 };
144 
147  Materialize();
148  return new ExampleIterator(*this, examples_.begin());
149 }
150 
153  Materialize();
154  return new ExampleIterator(*this, examples_.end());
155 }
156 
158  CorpusExamples, FuzzerCorpusTest,
160 
161 int main(int argc, char** argv) {
162  grpc::testing::TestEnvironment env(&argc, argv);
163  grpc::testing::InitTest(&argc, &argv, true);
164  ::testing::InitGoogleTest(&argc, argv);
165 
166  return RUN_ALL_TESTS();
167 }
ExampleGenerator::examples_
std::vector< std::string > examples_
Definition: fuzzer_corpus_test.cc:115
testing::internal::ParamGenerator
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-param-util.h:86
grpc::testing::InitTest
void InitTest(int *argc, char ***argv, bool remove_flags)
Definition: test_config_cc.cc:28
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
log.h
grpc_load_file
grpc_error_handle grpc_load_file(const char *filename, int add_null_terminator, grpc_slice *output)
Definition: load_file.cc:33
generate.env
env
Definition: generate.py:37
load_file.h
begin
char * begin
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1007
main
int main(int argc, char **argv)
Definition: fuzzer_corpus_test.cc:161
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
ExampleIterator::Clone
ExampleIterator * Clone() const override
Definition: fuzzer_corpus_test.cc:128
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
ABSL_FLAG
ABSL_FLAG(std::string, file, "", "Use this file as test data")
env.h
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
ExampleGenerator::Begin
::testing::internal::ParamIteratorInterface< std::string > * Begin() const override
Definition: fuzzer_corpus_test.cc:146
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
testing::TestWithParam
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1883
ExampleIterator::Advance
void Advance() override
Definition: fuzzer_corpus_test.cc:127
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
gpr_getenv
char * gpr_getenv(const char *name)
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
ExampleIterator
Definition: fuzzer_corpus_test.cc:118
testing::internal::ParamIteratorInterface< std::string >
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition: sockaddr_utils_fuzzer_test.cc:29
ExampleIterator::Equals
bool Equals(const ParamIteratorInterface< std::string > &other) const override
Definition: fuzzer_corpus_test.cc:131
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
TEST_P
TEST_P(FuzzerCorpusTest, RunOneExample)
Definition: fuzzer_corpus_test.cc:45
squelch
bool squelch
Definition: sockaddr_utils_fuzzer_test.cc:27
google_benchmark.example.empty
def empty(state)
Definition: example.py:31
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
test_config.h
testing::internal::ParamGeneratorInterface
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-param-util.h:85
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(CorpusExamples, FuzzerCorpusTest, ::testing::internal::ParamGenerator< std::string >(new ExampleGenerator))
regress.directory
directory
Definition: regress/regress.py:17
FuzzerCorpusTest
Definition: fuzzer_corpus_test.cc:43
ExampleGenerator::Materialize
void Materialize() const
Definition: fuzzer_corpus_test.cc:74
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
ExampleGenerator::End
::testing::internal::ParamIteratorInterface< std::string > * End() const override
Definition: fuzzer_corpus_test.cc:152
test_config.h
ExampleGenerator
Definition: fuzzer_corpus_test.cc:65
ExampleIterator::begin_
const std::vector< std::string >::const_iterator begin_
Definition: fuzzer_corpus_test.cc:141
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
ExampleIterator::BaseGenerator
const ExampleGenerator * BaseGenerator() const override
Definition: fuzzer_corpus_test.cc:125
ExampleIterator::Current
const std::string * Current() const override
Definition: fuzzer_corpus_test.cc:129
ExampleIterator::ExampleIterator
ExampleIterator(const ExampleGenerator &base_, std::vector< std::string >::const_iterator begin)
Definition: fuzzer_corpus_test.cc:121
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
ExampleIterator::current_
std::vector< std::string >::const_iterator current_
Definition: fuzzer_corpus_test.cc:142
ExampleIterator::base_
const ExampleGenerator & base_
Definition: fuzzer_corpus_test.cc:140
ExampleIterator::ExampleIterator
ExampleIterator(const ExampleIterator &other)
Definition: fuzzer_corpus_test.cc:137


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:25