blob: 6646cd6d084d7f70df53f0934a66dc283e975355 [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
12static constexpr uint8_t flags_strict_json = 0x01;
13static constexpr uint8_t flags_skip_unexpected_fields_in_json = 0x02;
14static constexpr uint8_t flags_allow_non_utf8 = 0x04;
15// static constexpr uint8_t flags_flag_3 = 0x08;
16// static constexpr uint8_t flags_flag_4 = 0x10;
17// static constexpr uint8_t flags_flag_5 = 0x20;
18// static constexpr uint8_t flags_flag_6 = 0x40;
19// static constexpr uint8_t flags_flag_7 = 0x80;
20
21// Utility for test run.
22OneTimeTestInit OneTimeTestInit::one_time_init_;
23
24extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
25 // Reserve one byte for Parser flags and one byte for repetition counter.
26 if (size < 3) return 0;
27 const uint8_t flags = data[0];
28 // normalize to ascii alphabet
Austin Schuh272c6132020-11-14 16:37:52 -080029 const int extra_rep_number =
30 std::max(5, (data[1] < '0' ? (data[1] - '0') : 0));
Austin Schuhe89fa2d2019-08-14 20:24:23 -070031 data += 2;
32 size -= 2; // bypass
33
34 const std::string original(reinterpret_cast<const char *>(data), size);
35 auto input = std::string(original.c_str()); // until '\0'
36 if (input.empty()) return 0;
37
38 flatbuffers::IDLOptions opts;
39 opts.strict_json = (flags & flags_strict_json);
40 opts.skip_unexpected_fields_in_json =
41 (flags & flags_skip_unexpected_fields_in_json);
42 opts.allow_non_utf8 = (flags & flags_allow_non_utf8);
43
44 flatbuffers::Parser parser(opts);
45
46 // Guarantee 0-termination in the input.
47 auto parse_input = input.c_str();
48
49 // The fuzzer can adjust the number repetition if a side-effects have found.
50 // Each test should pass at least two times to ensure that the parser doesn't
51 // have any hidden-states or locale-depended effects.
52 for (auto cnt = 0; cnt < (extra_rep_number + 2); cnt++) {
53 // Each even run (0,2,4..) will test locale independed code.
54 auto use_locale = !!OneTimeTestInit::test_locale() && (0 == (cnt % 2));
55 // Set new locale.
56 if (use_locale) {
57 FLATBUFFERS_ASSERT(setlocale(LC_ALL, OneTimeTestInit::test_locale()));
58 }
59
60 // Check Parser.
61 parser.Parse(parse_input);
62
63 // Restore locale.
64 if (use_locale) { FLATBUFFERS_ASSERT(setlocale(LC_ALL, "C")); }
65 }
66
67 return 0;
68}