Skip to content

Modified fuzz.cpp and Added a dict #994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 21 additions & 37 deletions src/test_lib_json/fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,31 @@
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE

#include "fuzz.h"

#include <cstdint>
#include <json/config.h>
#include <json/json.h>
#include <memory>
#include <stdint.h>
#include <sstream>
#include <string>

namespace Json {
class Exception;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
Json::CharReaderBuilder builder;

if (size < sizeof(uint32_t)) {
#include "fuzz.h"
#include "json/config.h"
#include "json/features.h"
#include "json/reader.h"
#include "json/value.h"
#include "json/writer.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use #include "json/json.h" instead of "json/features.h", "json/reader.h", "json/value.h", "json/writer.h"


extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
const std::string json_string(reinterpret_cast<const char *>(data), size);
Json::Reader reader(Json::Features::strictMode());
Json::Value value;
const bool success = reader.parse(json_string, value, false);
if (!success) {
return 0;
}

uint32_t hash_settings = *(const uint32_t*)data;
data += sizeof(uint32_t);

builder.settings_["failIfExtra"] = hash_settings & (1 << 0);
builder.settings_["allowComments_"] = hash_settings & (1 << 1);
builder.settings_["strictRoot_"] = hash_settings & (1 << 2);
builder.settings_["allowDroppedNullPlaceholders_"] = hash_settings & (1 << 3);
builder.settings_["allowNumericKeys_"] = hash_settings & (1 << 4);
builder.settings_["allowSingleQuotes_"] = hash_settings & (1 << 5);
builder.settings_["failIfExtra_"] = hash_settings & (1 << 6);
builder.settings_["rejectDupKeys_"] = hash_settings & (1 << 7);
builder.settings_["allowSpecialFloats_"] = hash_settings & (1 << 8);
// Write with StyledWriter
Json::StyledWriter styled_writer;
styled_writer.write(value);

std::unique_ptr<Json::CharReader> reader(builder.newCharReader());

Json::Value root;
const char* data_str = reinterpret_cast<const char*>(data);
try {
reader->parse(data_str, data_str + size, &root, nullptr);
} catch (Json::Exception const&) {
}
// Whether it succeeded or not doesn't matter.
// Write with StyledStreamWriter
Json::StyledStreamWriter styled_stream_writer;
JSONCPP_OSTRINGSTREAM sstream;
styled_stream_writer.write(sstream, value);
return 0;
}
54 changes: 54 additions & 0 deletions src/test_lib_json/fuzz.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# AFL dictionary for JSON
# -----------------------
#
# Just the very basics.
#
# Inspired by a dictionary by Jakub Wilk <jwilk@jwilk.net>
#
# https://github.com/rc0r/afl-fuzz/blob/master/dictionaries/json.dict
#

"0"
",0"
":0"
"0:"
"-1.2e+3"

"true"
"false"
"null"

"\"\""
",\"\""
":\"\""
"\"\":"

"{}"
",{}"
":{}"
"{\"\":0}"
"{{}}"

"[]"
",[]"
":[]"
"[0]"
"[[]]"

"''"
"\\"
"\\b"
"\\f"
"\\n"
"\\r"
"\\t"
"\\u0000"
"\\x00"
"\\0"
"\\uD800\\uDC00"
"\\uDBFF\\uDFFF"

"\"\":0"
"//"
"/**/"