-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_parse.cpp
More file actions
59 lines (48 loc) · 2.18 KB
/
Copy pathfuzz_parse.cpp
File metadata and controls
59 lines (48 loc) · 2.18 KB
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
// Copyright 2025 ByteDance Ltd. and/or its affiliates. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
#include "fuzz_util.h"
#include <cstddef>
#include <cstdint>
#include <string>
using ByteDance::pjson;
using ByteDance::pJsonParser;
namespace {
// DOM parsing and serialization invariants.
// Exercises one parser configuration and checks successful
// values across both compact and pretty serialization modes.
void exerciseParser(const uint8_t* data, size_t size, size_t variantOffset) {
const pJsonParser::Options options =
pjson_fuzz::parseOptionsVariant(data, size, variantOffset);
pJsonParser::Error error;
pjson value = pJsonParser(options).parse(pjson_fuzz::bytes(data, size), size, error);
if (!error.ok)
return;
// Compact output must be a valid, value-preserving representation.
const std::string compact = value.toString();
pJsonParser::Options compactOptions = options;
compactOptions.maxInputBytes = compact.size();
pJsonParser::Error compactError;
pjson reparsed = pJsonParser(compactOptions).parse(compact, compactError);
pjson_fuzz::require(compactError.ok);
pjson_fuzz::require(reparsed == value);
// Pretty printing may change whitespace, but never the represented JSON value.
const std::string pretty = value.toString(pjson::SerializeOptions::prettyPrinted());
pJsonParser::Options prettyOptions = options;
prettyOptions.maxInputBytes = pretty.size();
pJsonParser::Error prettyError;
pjson prettyParsed = pJsonParser(prettyOptions).parse(pretty, prettyError);
pjson_fuzz::require(prettyError.ok);
pjson_fuzz::require(prettyParsed == value);
}
} // namespace
// libFuzzer entry point.
// Bounds each test case and exercises the same bytes under several
// duplicate-policy/resource-budget variants.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size > pjson_fuzz::kMaxInputBytes)
return 0;
exerciseParser(data, size, 0U);
exerciseParser(data, size, 4U);
exerciseParser(data, size, 8U);
return 0;
}