Skip to content

Add a simple fuzz test for jsoncpp. #943

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

Merged
merged 26 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
38dd623
Add a simple fuzz test for jsoncpp.
Google-Autofuzz Jun 12, 2019
6cbae34
Updated header and fixed the bug
Google-Autofuzz Jun 24, 2019
3f5b2ec
Updated fuzz.h
Google-Autofuzz Jun 24, 2019
0ac1ef1
Update fuzz.cpp
Google-Autofuzz Jun 24, 2019
882a0b0
Update fuzz.h
Google-Autofuzz Jun 24, 2019
e297668
Update fuzz.h
Google-Autofuzz Jun 24, 2019
54f5ef4
Update fuzz.cpp
Google-Autofuzz Jun 24, 2019
95b1aee
Update fuzz.h
Google-Autofuzz Jun 24, 2019
b09f814
Update fuzz.cpp
Google-Autofuzz Jun 24, 2019
c45134d
Update fuzz.h
Google-Autofuzz Jun 24, 2019
2884e69
Update fuzz.cpp
Google-Autofuzz Jun 24, 2019
5972b88
fix llvm
Google-Autofuzz Jun 24, 2019
43ce3c7
added llvm
Google-Autofuzz Jun 24, 2019
a778bbe
Added include fuzz.cpp
Google-Autofuzz Jun 24, 2019
9f0cd22
Update main.cpp
Google-Autofuzz Jun 24, 2019
73cf116
Update main.cpp
Google-Autofuzz Jun 24, 2019
ebc2047
Update main.cpp
Google-Autofuzz Jun 24, 2019
a845c63
Update CMakeLists.txt
Google-Autofuzz Jun 24, 2019
7101a85
Update CMakeLists.txt
Google-Autofuzz Jun 24, 2019
d715e73
Update jsontest.cpp
Google-Autofuzz Jun 24, 2019
aca048c
Update jsontest.cpp
Google-Autofuzz Jun 24, 2019
a32b530
Update jsontest.cpp
Google-Autofuzz Jun 24, 2019
7b3f027
added fuzz.cpp to macro in main.cpp
Google-Autofuzz Jun 26, 2019
fe99a87
Merge branch 'add_autofuzz_fuzzer' of https://github.com/Google-Autof…
Google-Autofuzz Jun 26, 2019
e16638a
Update main.cpp
Google-Autofuzz Jun 26, 2019
0a35653
Add fuzz.cpp to jsoncpp_test
Google-Autofuzz Jun 26, 2019
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ datadiode <jochen.neubeck@vodafone.de>
David Seifert <soap@gentoo.org>
David West <david-west@idexx.com>
dawesc <chris.dawes@eftlab.co.uk>
Devin Jeanpierre <jeanpierreda@google.com>
Dmitry Marakasov <amdmi3@amdmi3.ru>
dominicpezzuto <dom@dompezzuto.com>
Don Milham <dmilham@gmail.com>
Expand Down
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ jsoncpp_test = executable(
'jsoncpp_test',
[ 'src/test_lib_json/jsontest.cpp',
'src/test_lib_json/jsontest.h',
'src/test_lib_json/main.cpp'],
'src/test_lib_json/main.cpp',
'src/test_lib_json/fuzz.cpp'],
include_directories : jsoncpp_include_directories,
link_with : jsoncpp_lib,
install : false,
Expand Down
2 changes: 2 additions & 0 deletions src/test_lib_json/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
add_executable( jsoncpp_test
jsontest.cpp
jsontest.h
fuzz.cpp
fuzz.h
main.cpp
)

Expand Down
49 changes: 49 additions & 0 deletions src/test_lib_json/fuzz.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2007-2019 The JsonCpp Authors
// Distributed under MIT license, or public domain if desired and
// 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 <string>

namespace Json {
class Exception;
}

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

if (size < sizeof(uint32_t)) {
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);

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.
return 0;
}
14 changes: 14 additions & 0 deletions src/test_lib_json/fuzz.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2007-2010 The JsonCpp Authors
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE

#ifndef FUZZ_H_INCLUDED
#define FUZZ_H_INCLUDED

#include <cstddef>
#include <stdint.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);

#endif // ifndef FUZZ_H_INCLUDED
15 changes: 15 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma warning(disable : 4996)
#endif

#include "fuzz.h"
#include "jsontest.h"
#include <cmath>
#include <cstring>
Expand Down Expand Up @@ -2528,6 +2529,18 @@ JSONTEST_FIXTURE(RValueTest, moveConstruction) {
JSONTEST_ASSERT_EQUAL(Json::stringValue, moved["key"].type());
}

struct FuzzTest : JsonTest::TestCase {};

// Build and run the fuzz test without any fuzzer, so that it's guaranteed not
// go out of date, even if it's never run as an actual fuzz test.
JSONTEST_FIXTURE(FuzzTest, fuzzDoesntCrash) {
const std::string example = "{}";
JSONTEST_ASSERT_EQUAL(
0,
LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t*>(example.c_str()),
example.size()));
}

int main(int argc, const char* argv[]) {
JsonTest::Runner runner;
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, checkNormalizeFloatingPointStr);
Expand Down Expand Up @@ -2607,6 +2620,8 @@ int main(int argc, const char* argv[]) {

JSONTEST_REGISTER_FIXTURE(runner, RValueTest, moveConstruction);

JSONTEST_REGISTER_FIXTURE(runner, FuzzTest, fuzzDoesntCrash);

return runner.runCommandLine(argc, argv);
}

Expand Down