blob: 0b74f2ceb14078f7e28da4ecb349aaf278e891f6 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001// 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
James Kuszmaul8e62b022022-03-22 09:33:25 -070012static constexpr size_t kMinInputLength = 1;
13static constexpr size_t kMaxInputLength = 16384;
14
Austin Schuh58b9b472020-11-25 19:12:44 -080015static 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;
Austin Schuhe89fa2d2019-08-14 20:24:23 -070018
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];
James Kuszmaul8e62b022022-03-22 09:33:25 -070026 (void)data[1]; // reserved
Austin Schuhe89fa2d2019-08-14 20:24:23 -070027 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'
James Kuszmaul8e62b022022-03-22 09:33:25 -070032 if (input.size() < kMinInputLength || input.size() > kMaxInputLength)
33 return 0;
Austin Schuhe89fa2d2019-08-14 20:24:23 -070034
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
James Kuszmaul8e62b022022-03-22 09:33:25 -070046 // 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.
Austin Schuhe89fa2d2019-08-14 20:24:23 -070054 return 0;
55}