File: json_fuzzer.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (73 lines) | stat: -rw-r--r-- 2,497 bytes parent folder | download | duplicates (6)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// JsonCpp fuzzing wrapper to help with automated fuzz testing.

#include <stdint.h>
#include <array>
#include <climits>
#include <cstdio>
#include <iostream>
#include <memory>
#include "third_party/jsoncpp/source/include/json/json.h"

namespace {
// JsonCpp has a few different parsing options. The code below makes sure that
// the most intersting variants are tested.
enum { kBuilderConfigDefault = 0, kBuilderConfigStrict, kNumBuilderConfig };
}  // namespace

static const std::array<Json::CharReaderBuilder, kNumBuilderConfig>&
Initialize() {
  static std::array<Json::CharReaderBuilder, kNumBuilderConfig> builders{};

  Json::CharReaderBuilder::strictMode(
      &builders[kBuilderConfigStrict].settings_);

  return builders;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  const auto& reader_builders = Initialize();

  for (const auto& reader_builder : reader_builders) {
    // Parse Json.
    auto reader =
        std::unique_ptr<Json::CharReader>(reader_builder.newCharReader());
    Json::Value root;
    bool res = reader->parse(reinterpret_cast<const char*>(data),
                             reinterpret_cast<const char*>(data + size), &root,
                             nullptr /* errs */);
    if (!res) {
      continue;
    }

    // Write and re-read json.
    const Json::StreamWriterBuilder writer_builder;
    auto writer =
        std::unique_ptr<Json::StreamWriter>(writer_builder.newStreamWriter());
    std::stringstream out_stream;
    writer->write(root, &out_stream);
    std::string output_json = out_stream.str();

    Json::Value root_again;
    res = reader->parse(output_json.data(),
                        output_json.data() + output_json.length(), &root_again,
                        nullptr /* errs */);
    if (!res) {
      continue;
    }

    // Run equality test.
    // Note: This actually causes the Json::Value tree to be traversed and all
    // the values to be dereferenced (until two of them are found not equal),
    // which is great for detecting memory corruption bugs when compiled with
    // AddressSanitizer. The result of the comparison is ignored, as it is
    // expected that both the original and the re-read version will differ from
    // time to time (e.g. due to floating point accuracy loss).
    (void)(root == root_again);
  }

  return 0;
}