/src/flatbuffers/tests/fuzzer/flatbuffers_annotator_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | |
2 | | #include <filesystem> |
3 | | #include <string> |
4 | | |
5 | | #include "binary_annotator.h" |
6 | | #include "test_init.h" |
7 | | |
8 | | static std::filesystem::path exe_path_; |
9 | | static const uint8_t *schema_bfbs_; |
10 | | static size_t schema_bfbs_length_; |
11 | | |
12 | 2 | bool TestFileExists(std::filesystem::path file_path) { |
13 | 2 | if (file_path.has_filename() && std::filesystem::exists(file_path)) |
14 | 2 | return true; |
15 | | |
16 | 0 | TEST_OUTPUT_LINE("@DEBUG: file '%s' not found", file_path.string().c_str()); |
17 | 0 | for (const auto &entry : |
18 | 0 | std::filesystem::directory_iterator(file_path.parent_path())) { |
19 | 0 | TEST_OUTPUT_LINE("@DEBUG: parent path entry: '%s'", |
20 | 0 | entry.path().string().c_str()); |
21 | 0 | } |
22 | 0 | return false; |
23 | 2 | } |
24 | | |
25 | 2 | std::string LoadBinarySchema(const char *file_name) { |
26 | 2 | const auto file_path = exe_path_.parent_path() / file_name; |
27 | 2 | TEST_EQ(true, TestFileExists(file_path)); |
28 | 2 | std::string schemafile; |
29 | 2 | TEST_EQ(true, |
30 | 2 | flatbuffers::LoadFile(file_path.string().c_str(), true, &schemafile)); |
31 | | |
32 | 2 | flatbuffers::Verifier verifier( |
33 | 2 | reinterpret_cast<const uint8_t *>(schemafile.c_str()), schemafile.size()); |
34 | 2 | TEST_EQ(true, reflection::VerifySchemaBuffer(verifier)); |
35 | 2 | return schemafile; |
36 | 2 | } |
37 | | |
38 | 6 | extern "C" int LLVMFuzzerInitialize(int *, char ***argv) { |
39 | 6 | exe_path_ = (*argv)[0]; |
40 | 6 | static const std::string schema_file = |
41 | 6 | LoadBinarySchema("annotated_binary.bfbs"); |
42 | 6 | schema_bfbs_ = reinterpret_cast<const uint8_t *>(schema_file.c_str()); |
43 | 6 | schema_bfbs_length_ = schema_file.size(); |
44 | 6 | return 0; |
45 | 6 | } |
46 | | |
47 | 10.6k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
48 | 10.6k | flatbuffers::BinaryAnnotator annotator(schema_bfbs_, schema_bfbs_length_, |
49 | 10.6k | data, size, false); |
50 | | |
51 | 10.6k | annotator.Annotate(); |
52 | 10.6k | return 0; |
53 | 10.6k | } |