fuzzer_corpus_test.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include <dirent.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <gtest/gtest.h>
  23. #include "absl/flags/flag.h"
  24. #include <grpc/grpc.h>
  25. #include <grpc/support/alloc.h>
  26. #include <grpc/support/log.h>
  27. #include "src/core/lib/gpr/env.h"
  28. #include "src/core/lib/iomgr/load_file.h"
  29. #include "test/core/util/test_config.h"
  30. #include "test/cpp/util/test_config.h"
  31. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
  32. extern bool squelch;
  33. ABSL_FLAG(std::string, file, "", "Use this file as test data");
  34. ABSL_FLAG(std::string, directory, "", "Use this directory as test data");
  35. class FuzzerCorpusTest : public ::testing::TestWithParam<std::string> {};
  36. TEST_P(FuzzerCorpusTest, RunOneExample) {
  37. // Need to call grpc_init() here to use a slice, but need to shut it
  38. // down before calling LLVMFuzzerTestOneInput(), because most
  39. // implementations of that function will initialize and shutdown gRPC
  40. // internally.
  41. gpr_log(GPR_INFO, "Example file: %s", GetParam().c_str());
  42. grpc_slice buffer;
  43. squelch = false;
  44. GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
  45. grpc_load_file(GetParam().c_str(), 0, &buffer)));
  46. size_t length = GRPC_SLICE_LENGTH(buffer);
  47. void* data = gpr_malloc(length);
  48. if (length > 0) {
  49. memcpy(data, GPR_SLICE_START_PTR(buffer), length);
  50. }
  51. grpc_slice_unref(buffer);
  52. LLVMFuzzerTestOneInput(static_cast<uint8_t*>(data), length);
  53. gpr_free(data);
  54. }
  55. class ExampleGenerator
  56. : public ::testing::internal::ParamGeneratorInterface<std::string> {
  57. public:
  58. ::testing::internal::ParamIteratorInterface<std::string>* Begin()
  59. const override;
  60. ::testing::internal::ParamIteratorInterface<std::string>* End()
  61. const override;
  62. private:
  63. void Materialize() const {
  64. if (examples_.empty()) {
  65. if (!absl::GetFlag(FLAGS_file).empty()) {
  66. examples_.push_back(absl::GetFlag(FLAGS_file));
  67. }
  68. if (!absl::GetFlag(FLAGS_directory).empty()) {
  69. char* test_srcdir = gpr_getenv("TEST_SRCDIR");
  70. gpr_log(GPR_DEBUG, "test_srcdir=\"%s\"", test_srcdir);
  71. std::string directory = absl::GetFlag(FLAGS_directory);
  72. if (test_srcdir != nullptr) {
  73. directory =
  74. test_srcdir + std::string("/com_github_grpc_grpc/") + directory;
  75. }
  76. gpr_log(GPR_DEBUG, "Using corpus directory: %s", directory.c_str());
  77. DIR* dp;
  78. struct dirent* ep;
  79. dp = opendir(directory.c_str());
  80. if (dp != nullptr) {
  81. while ((ep = readdir(dp)) != nullptr) {
  82. if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0) {
  83. examples_.push_back(directory + "/" + ep->d_name);
  84. }
  85. }
  86. (void)closedir(dp);
  87. } else {
  88. perror("Couldn't open the directory");
  89. abort();
  90. }
  91. gpr_free(test_srcdir);
  92. }
  93. }
  94. // Make sure we don't succeed without doing anything, which caused
  95. // us to be blind to our fuzzers not running for 9 months.
  96. GPR_ASSERT(!examples_.empty());
  97. // Get a consistent ordering of examples so problems don't just show up on
  98. // CI
  99. std::sort(examples_.begin(), examples_.end());
  100. }
  101. mutable std::vector<std::string> examples_;
  102. };
  103. class ExampleIterator
  104. : public ::testing::internal::ParamIteratorInterface<std::string> {
  105. public:
  106. ExampleIterator(const ExampleGenerator& base_,
  107. std::vector<std::string>::const_iterator begin)
  108. : base_(base_), begin_(begin), current_(begin) {}
  109. const ExampleGenerator* BaseGenerator() const override { return &base_; }
  110. void Advance() override { current_++; }
  111. ExampleIterator* Clone() const override { return new ExampleIterator(*this); }
  112. const std::string* Current() const override { return &*current_; }
  113. bool Equals(const ParamIteratorInterface<std::string>& other) const override {
  114. return &base_ == other.BaseGenerator() &&
  115. current_ == dynamic_cast<const ExampleIterator*>(&other)->current_;
  116. }
  117. private:
  118. ExampleIterator(const ExampleIterator& other)
  119. : base_(other.base_), begin_(other.begin_), current_(other.current_) {}
  120. const ExampleGenerator& base_;
  121. const std::vector<std::string>::const_iterator begin_;
  122. std::vector<std::string>::const_iterator current_;
  123. };
  124. ::testing::internal::ParamIteratorInterface<std::string>*
  125. ExampleGenerator::Begin() const {
  126. Materialize();
  127. return new ExampleIterator(*this, examples_.begin());
  128. }
  129. ::testing::internal::ParamIteratorInterface<std::string>*
  130. ExampleGenerator::End() const {
  131. Materialize();
  132. return new ExampleIterator(*this, examples_.end());
  133. }
  134. INSTANTIATE_TEST_SUITE_P(
  135. CorpusExamples, FuzzerCorpusTest,
  136. ::testing::internal::ParamGenerator<std::string>(new ExampleGenerator));
  137. int main(int argc, char** argv) {
  138. grpc::testing::TestEnvironment env(argc, argv);
  139. grpc::testing::InitTest(&argc, &argv, true);
  140. ::testing::InitGoogleTest(&argc, argv);
  141. return RUN_ALL_TESTS();
  142. }