fuzz_target.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "bloaty.h"
  15. #include "bloaty.pb.h"
  16. #include "strarr.h"
  17. #include "absl/strings/string_view.h"
  18. using absl::string_view;
  19. namespace bloaty {
  20. class StringPieceInputFile : public InputFile {
  21. public:
  22. StringPieceInputFile(string_view data)
  23. : InputFile("fake_StringPieceInputFile_file") {
  24. data_ = data;
  25. }
  26. };
  27. class StringPieceInputFileFactory : public InputFileFactory {
  28. public:
  29. StringPieceInputFileFactory(string_view data) : data_(data) {}
  30. private:
  31. string_view data_;
  32. std::unique_ptr<InputFile> OpenFile(
  33. const std::string& /* filename */) const override {
  34. return std::unique_ptr<InputFile>(new StringPieceInputFile(data_));
  35. }
  36. };
  37. void RunBloaty(const InputFileFactory& factory,
  38. const std::string& data_source) {
  39. bloaty::RollupOutput output;
  40. bloaty::Options options;
  41. std::string error;
  42. options.add_data_source(data_source);
  43. options.add_filename("dummy_filename");
  44. bloaty::BloatyMain(options, factory, &output, &error);
  45. }
  46. } // namespace bloaty
  47. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  48. const char *data2 = reinterpret_cast<const char*>(data);
  49. bloaty::StringPieceInputFileFactory factory(string_view(data2, size));
  50. // Try all of the data sources.
  51. RunBloaty(factory, "segments");
  52. RunBloaty(factory, "sections");
  53. RunBloaty(factory, "symbols");
  54. RunBloaty(factory, "compileunits");
  55. RunBloaty(factory, "inlines");
  56. RunBloaty(factory, "armembers");
  57. return 0;
  58. }