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 12 static constexpr uint8_t flags_strict_json = 0x01; 13 static constexpr uint8_t flags_skip_unexpected_fields_in_json = 0x02; 14 static 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. 22 OneTimeTestInit OneTimeTestInit::one_time_init_; 23 LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)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 29 const int extra_rep_number = data[1] >= '0' ? (data[1] - '0') : 0; 30 data += 2; 31 size -= 2; // bypass 32 33 const std::string original(reinterpret_cast<const char *>(data), size); 34 auto input = std::string(original.c_str()); // until '\0' 35 if (input.empty()) return 0; 36 37 flatbuffers::IDLOptions opts; 38 opts.strict_json = (flags & flags_strict_json); 39 opts.skip_unexpected_fields_in_json = 40 (flags & flags_skip_unexpected_fields_in_json); 41 opts.allow_non_utf8 = (flags & flags_allow_non_utf8); 42 43 flatbuffers::Parser parser(opts); 44 45 // Guarantee 0-termination in the input. 46 auto parse_input = input.c_str(); 47 48 // The fuzzer can adjust the number repetition if a side-effects have found. 49 // Each test should pass at least two times to ensure that the parser doesn't 50 // have any hidden-states or locale-depended effects. 51 for (auto cnt = 0; cnt < (extra_rep_number + 2); cnt++) { 52 // Each even run (0,2,4..) will test locale independed code. 53 auto use_locale = !!OneTimeTestInit::test_locale() && (0 == (cnt % 2)); 54 // Set new locale. 55 if (use_locale) { 56 FLATBUFFERS_ASSERT(setlocale(LC_ALL, OneTimeTestInit::test_locale())); 57 } 58 59 // Check Parser. 60 parser.Parse(parse_input); 61 62 // Restore locale. 63 if (use_locale) { FLATBUFFERS_ASSERT(setlocale(LC_ALL, "C")); } 64 } 65 66 return 0; 67 } 68