Coverage Report

Created: 2024-09-08 06:18

/src/bloaty/tests/fuzz_target.cc
Line
Count
Source (jump to first uncovered line)
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
15
#include "bloaty.h"
16
#include "bloaty.pb.h"
17
#include "strarr.h"
18
19
#include "absl/strings/string_view.h"
20
21
using absl::string_view;
22
23
namespace bloaty {
24
25
class StringPieceInputFile : public InputFile {
26
 public:
27
  StringPieceInputFile(string_view data)
28
47.2k
      : InputFile("fake_StringPieceInputFile_file") {
29
47.2k
    data_ = data;
30
47.2k
  }
31
32
  bool TryOpen(absl::string_view /* filename */,
33
0
               std::unique_ptr<InputFile>& file) override {
34
0
    file.reset(new StringPieceInputFile(data_));
35
0
    return true;
36
0
  }
37
};
38
39
class StringPieceInputFileFactory : public InputFileFactory {
40
 public:
41
3.96k
  StringPieceInputFileFactory(string_view data) : data_(data) {}
42
 private:
43
  string_view data_;
44
  std::unique_ptr<InputFile> OpenFile(
45
47.2k
      const std::string& /* filename */) const override {
46
47.2k
    return std::unique_ptr<InputFile>(new StringPieceInputFile(data_));
47
47.2k
  }
48
};
49
50
void RunBloaty(const InputFileFactory& factory,
51
23.7k
               const std::string& data_source) {
52
23.7k
  bloaty::RollupOutput output;
53
23.7k
  bloaty::Options options;
54
23.7k
  std::string error;
55
23.7k
  options.add_data_source(data_source);
56
23.7k
  options.add_filename("dummy_filename");
57
23.7k
  bloaty::BloatyMain(options, factory, &output, &error);
58
23.7k
}
59
60
}  // namespace bloaty
61
62
3.96k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
63
3.96k
  const char *data2 = reinterpret_cast<const char*>(data);
64
3.96k
  bloaty::StringPieceInputFileFactory factory(string_view(data2, size));
65
66
  // Try all of the data sources.
67
3.96k
  RunBloaty(factory, "segments");
68
3.96k
  RunBloaty(factory, "sections");
69
3.96k
  RunBloaty(factory, "symbols");
70
3.96k
  RunBloaty(factory, "compileunits");
71
3.96k
  RunBloaty(factory, "inlines");
72
3.96k
  RunBloaty(factory, "armembers");
73
74
3.96k
  return 0;
75
3.96k
}