File: flatbuffers_annotator_fuzzer.cc

package info (click to toggle)
golang-github-google-flatbuffers 24.12.23-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,704 kB
  • sloc: cpp: 53,217; python: 6,900; cs: 5,566; java: 4,370; php: 1,460; javascript: 1,061; xml: 1,016; sh: 886; makefile: 13
file content (53 lines) | stat: -rw-r--r-- 1,698 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#include <filesystem>
#include <string>

#include "binary_annotator.h"
#include "test_init.h"

static std::filesystem::path exe_path_;
static const uint8_t *schema_bfbs_;
static size_t schema_bfbs_length_;

bool TestFileExists(std::filesystem::path file_path) {
  if (file_path.has_filename() && std::filesystem::exists(file_path))
    return true;

  TEST_OUTPUT_LINE("@DEBUG: file '%s' not found", file_path.string().c_str());
  for (const auto &entry :
       std::filesystem::directory_iterator(file_path.parent_path())) {
    TEST_OUTPUT_LINE("@DEBUG: parent path entry: '%s'",
                     entry.path().string().c_str());
  }
  return false;
}

std::string LoadBinarySchema(const char *file_name) {
  const auto file_path = exe_path_.parent_path() / file_name;
  TEST_EQ(true, TestFileExists(file_path));
  std::string schemafile;
  TEST_EQ(true,
          flatbuffers::LoadFile(file_path.string().c_str(), true, &schemafile));

  flatbuffers::Verifier verifier(
      reinterpret_cast<const uint8_t *>(schemafile.c_str()), schemafile.size());
  TEST_EQ(true, reflection::VerifySchemaBuffer(verifier));
  return schemafile;
}

extern "C" int LLVMFuzzerInitialize(int *, char ***argv) {
  exe_path_ = (*argv)[0];
  static const std::string schema_file =
      LoadBinarySchema("annotated_binary.bfbs");
  schema_bfbs_ = reinterpret_cast<const uint8_t *>(schema_file.c_str());
  schema_bfbs_length_ = schema_file.size();
  return 0;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  flatbuffers::BinaryAnnotator annotator(schema_bfbs_, schema_bfbs_length_,
                                         data, size, false);

  annotator.Annotate();
  return 0;
}