forked from google/flatbuffers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatbuffers_codegen_fuzzer.cc
More file actions
156 lines (137 loc) · 5.62 KB
/
flatbuffers_codegen_fuzzer.cc
File metadata and controls
156 lines (137 loc) · 5.62 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stddef.h>
#include <stdint.h>
#include <string>
#include "flatbuffers/idl.h" // For Parser and generation functions
#include "test_init.h"
#include <memory>
#include <flatbuffers/flatc.h>
#include "idl_gen_cpp.h" // For C++ generator
#include <idl_gen_csharp.h>
#include <iostream>
#include "flatbuffers/code_generator.h"
#include <idl_gen_binary.h>
#include <idl_gen_dart.h>
#include <idl_gen_fbs.h>
#include <idl_gen_go.h>
#include <idl_gen_java.h>
#include <idl_gen_json_schema.h>
#include <idl_gen_kotlin.h>
#include <idl_gen_kotlin.h>
#include <idl_gen_lobster.h>
#include <bfbs_gen_lua.h>
#include <bfbs_gen_nim.h>
#include <idl_gen_python.h>
#include <idl_gen_php.h>
#include <idl_gen_rust.h>
#include <idl_gen_text.h>
#include <idl_gen_swift.h>
#include <idl_gen_ts.h>
static constexpr size_t kMinInputLength = 1;
static constexpr size_t kMaxInputLength = 16384;
static constexpr uint8_t flags_strict_json = 0x80;
static constexpr uint8_t flags_skip_unexpected_fields_in_json = 0x40;
static constexpr uint8_t flags_allow_non_utf8 = 0x20;
// Utility for test run.
OneTimeTestInit OneTimeTestInit::one_time_init_;
static const char* g_program_name = nullptr;
static void Warn(const flatbuffers::FlatCompiler* flatc,
const std::string& warn, bool show_exe_name) {
(void)flatc;
if (show_exe_name) {
printf("%s: ", g_program_name);
}
fprintf(stderr, "\nwarning:\n %s\n\n", warn.c_str());
}
static void Error(const flatbuffers::FlatCompiler* flatc,
const std::string& err, bool usage, bool show_exe_name) {
if (show_exe_name) {
printf("%s: ", g_program_name);
}
if (usage && flatc) {
fprintf(stderr, "%s\n", flatc->GetShortUsageString(g_program_name).c_str());
}
fprintf(stderr, "\nerror:\n %s\n\n", err.c_str());
exit(1);
}
namespace flatbuffers {
void LogCompilerWarn(const std::string& warn) {
Warn(static_cast<const flatbuffers::FlatCompiler*>(nullptr), warn, true);
}
void LogCompilerError(const std::string& err) {
Error(static_cast<const flatbuffers::FlatCompiler*>(nullptr), err, false,
true);
}
} // namespace flatbuffers
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
// Reserve one byte for Parser flags and one byte for repetition counter.
if (size < 3) return 0;
const uint8_t flags = data[0];
(void)data[1]; // reserved
data += 2;
size -= 2; // bypass
const std::string original(reinterpret_cast<const char*>(data), size);
auto input = std::string(original.c_str()); // until '\0'
if (input.size() < kMinInputLength || input.size() > kMaxInputLength)
return 0;
flatbuffers::IDLOptions opts;
opts.strict_json = (flags & flags_strict_json);
opts.skip_unexpected_fields_in_json =
(flags & flags_skip_unexpected_fields_in_json);
opts.allow_non_utf8 = (flags & flags_allow_non_utf8);
flatbuffers::Parser parser(opts);
// Guarantee 0-termination in the input.
auto parse_input = input.c_str();
// Parse the input schema
if (parser.Parse(parse_input)) {
parser.Serialize();
const uint8_t* buf = parser.builder_.GetBufferPointer();
flatbuffers::Verifier verifier(buf,
parser.builder_.GetSize());
TEST_EQ(true, reflection::VerifySchemaBuffer(verifier));
auto root = flatbuffers::GetRoot<flatbuffers::Table>(buf);
if (verifier.VerifyTableStart(buf) && root->VerifyTableStart(verifier)) {
if (parser.root_struct_def_) {
std::string json_output;
flatbuffers::GenText(parser, parser.builder_.GetBufferPointer(),
&json_output);
}
}
std::string temp_filename = "fuzzer_generated";
const std::string flatbuffers_version(flatbuffers::FLATBUFFERS_VERSION());
std::vector<std::unique_ptr<flatbuffers::CodeGenerator>> generators;
generators.emplace_back(flatbuffers::NewBinaryCodeGenerator());
generators.emplace_back(flatbuffers::NewCppCodeGenerator());
generators.emplace_back(flatbuffers::NewCSharpCodeGenerator());
generators.emplace_back(flatbuffers::NewDartCodeGenerator());
generators.emplace_back(flatbuffers::NewFBSCodeGenerator());
generators.emplace_back(flatbuffers::NewGoCodeGenerator());
generators.emplace_back(flatbuffers::NewJavaCodeGenerator());
generators.emplace_back(flatbuffers::NewJsonSchemaCodeGenerator());
generators.emplace_back(flatbuffers::NewKotlinCodeGenerator());
generators.emplace_back(flatbuffers::NewKotlinKMPCodeGenerator());
generators.emplace_back(flatbuffers::NewLobsterCodeGenerator());
generators.emplace_back(flatbuffers::NewLuaBfbsGenerator(flatbuffers_version));
generators.emplace_back(flatbuffers::NewNimBfbsGenerator(flatbuffers_version));
generators.emplace_back(flatbuffers::NewPythonCodeGenerator());
generators.emplace_back(flatbuffers::NewPhpCodeGenerator());
generators.emplace_back(flatbuffers::NewRustCodeGenerator());
generators.emplace_back(flatbuffers::NewTextCodeGenerator());
generators.emplace_back(flatbuffers::NewSwiftCodeGenerator());
generators.emplace_back(flatbuffers::NewTsCodeGenerator());
for (auto& gen : generators) {
auto p = gen.get();
std::string temp_path = "/tmp/";
auto status = p->GenerateCode(parser, temp_path, "fuzzer_generated");
if (status != flatbuffers::CodeGenerator::Status::OK) {
TEST_OUTPUT_LINE("GenerateCode failed %d", status);
}
// test gRPC code generation
auto grpc_status =
p->GenerateGrpcCode(parser, temp_path, "fuzzer_generated");
if (grpc_status != flatbuffers::CodeGenerator::Status::OK) {
TEST_OUTPUT_LINE("GenerateGrpcCode failed %d", grpc_status);
}
}
}
return 0;
}