...
1// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4#include <stddef.h>
5#include <stdint.h>
6#include <clocale>
7#include <string>
8
9#include "flatbuffers/idl.h"
10#include "test_init.h"
11
12static constexpr size_t kMinInputLength = 1;
13static constexpr size_t kMaxInputLength = 16384;
14
15static constexpr uint8_t flags_strict_json = 0x80;
16static constexpr uint8_t flags_skip_unexpected_fields_in_json = 0x40;
17static constexpr uint8_t flags_allow_non_utf8 = 0x20;
18
19// Utility for test run.
20OneTimeTestInit OneTimeTestInit::one_time_init_;
21
22extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
23 // Reserve one byte for Parser flags and one byte for repetition counter.
24 if (size < 3) return 0;
25 const uint8_t flags = data[0];
26 (void)data[1]; // reserved
27 data += 2;
28 size -= 2; // bypass
29
30 const std::string original(reinterpret_cast<const char *>(data), size);
31 auto input = std::string(original.c_str()); // until '\0'
32 if (input.size() < kMinInputLength || input.size() > kMaxInputLength)
33 return 0;
34
35 flatbuffers::IDLOptions opts;
36 opts.strict_json = (flags & flags_strict_json);
37 opts.skip_unexpected_fields_in_json =
38 (flags & flags_skip_unexpected_fields_in_json);
39 opts.allow_non_utf8 = (flags & flags_allow_non_utf8);
40
41 flatbuffers::Parser parser(opts);
42
43 // Guarantee 0-termination in the input.
44 auto parse_input = input.c_str();
45
46 // Check Parser.
47 parser.Parse(parse_input);
48 // TODO:
49 // Need to add additional checks for inputs passed Parse(parse_input) successfully:
50 // 1. Serialization to bfbs.
51 // 2. Generation of a default object.
52 // 3. Verification of the object using reflection.
53 // 3. Printing to json.
54 return 0;
55}
View as plain text