Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
a084e90f
Commit
a084e90f
authored
Dec 22, 2016
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
split AFL test in driver and test file
parent
048330b1
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
91 additions
and
29 deletions
+91
-29
.gitignore
.gitignore
+2
-0
Makefile
Makefile
+3
-7
test/Makefile
test/Makefile
+8
-0
test/src/fuzzer-driver_afl.cpp
test/src/fuzzer-driver_afl.cpp
+33
-0
test/src/fuzzer-parse_json.cpp
test/src/fuzzer-parse_json.cpp
+45
-22
No files found.
.gitignore
View file @
a084e90f
...
@@ -21,3 +21,5 @@ cmake-build-debug
...
@@ -21,3 +21,5 @@ cmake-build-debug
test/test-*
test/test-*
.svn
.svn
test/thirdparty/Fuzzer/libFuzzer.a
Makefile
View file @
a084e90f
...
@@ -49,14 +49,10 @@ doctest:
...
@@ -49,14 +49,10 @@ doctest:
fuzz_testing
:
fuzz_testing
:
rm
-fr
fuzz-testing
rm
-fr
fuzz-testing
mkdir
-p
fuzz-testing fuzz-testing/testcases fuzz-testing/out
mkdir
-p
fuzz-testing fuzz-testing/testcases fuzz-testing/out
$(MAKE)
fuzz
CXX
=
afl-clang++
$(MAKE)
parse_afl_fuzzer
-C
test
CXX
=
afl-clang++
mv
fuzz fuzz-testing
mv
test
/fuzzer parse_afl_fuzzer
find
test
/data/json_tests
-size
-5k
-name
*
json | xargs
-I
{}
cp
"{}"
fuzz-testing/testcases
find
test
/data/json_tests
-size
-5k
-name
*
json | xargs
-I
{}
cp
"{}"
fuzz-testing/testcases
@
echo
"Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzz"
@
echo
"Execute: afl-fuzz -i fuzz-testing/testcases -o fuzz-testing/out fuzz-testing/fuzzer"
# the fuzzer binary
fuzz
:
test/src/fuzz.cpp src/json.hpp
$(CXX)
-std
=
c++11
$(CXXFLAGS)
$(FLAGS)
$(CPPFLAGS)
-I
src
$<
$(LDFLAGS)
-o
$@
##########################################################################
##########################################################################
...
...
test/Makefile
View file @
a084e90f
...
@@ -78,3 +78,11 @@ TEST_PATTERN = "*"
...
@@ -78,3 +78,11 @@ TEST_PATTERN = "*"
TEST_PREFIX
=
""
TEST_PREFIX
=
""
check
:
$(TESTCASES)
check
:
$(TESTCASES)
@
cd
..
;
for
testcase
in
$(TESTCASES)
;
do
echo
"Executing
$$
testcase..."
;
$(TEST_PREFIX)
test
/
$$
testcase
$(TEST_PATTERN)
||
exit
1
;
done
@
cd
..
;
for
testcase
in
$(TESTCASES)
;
do
echo
"Executing
$$
testcase..."
;
$(TEST_PREFIX)
test
/
$$
testcase
$(TEST_PATTERN)
||
exit
1
;
done
##############################################################################
# fuzzer
##############################################################################
parse_afl_fuzzer
:
$(CXX)
$(CXXFLAGS)
$(CPPFLAGS)
src/fuzzer-driver_afl.cpp src/fuzzer-parse_json.cpp
-o
$@
test/src/fuzz.cpp
→
test/src/fuzz
er-driver_afl
.cpp
View file @
a084e90f
...
@@ -4,14 +4,18 @@
...
@@ -4,14 +4,18 @@
| | |__ | | | | | | version 2.0.9
| | |__ | | | | | | version 2.0.9
|_____|_____|_____|_|___| https://github.com/nlohmann/json
|_____|_____|_____|_|___| https://github.com/nlohmann/json
Run "make fuzz_testing" and follow the instructions.
This file implements a driver for American Fuzzy Lop (afl-fuzz). It relies on
an implementation of the `LLVMFuzzerTestOneInput` function which processes a
passed byte array.
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
*/
*/
#include <json.hpp>
#include <sstream>
#include <cstdint>
#include <iostream>
using
json
=
nlohmann
::
json
;
extern
"C"
int
LLVMFuzzerTestOneInput
(
const
uint8_t
*
data
,
size_t
size
)
;
int
main
()
int
main
()
{
{
...
@@ -19,15 +23,10 @@ int main()
...
@@ -19,15 +23,10 @@ int main()
while
(
__AFL_LOOP
(
1000
))
while
(
__AFL_LOOP
(
1000
))
{
{
#endif
#endif
try
// copy stdin to stringstream to pass it to fuzzer as byte array
{
std
::
stringstream
ss
;
json
j
(
std
::
cin
);
ss
<<
std
::
cin
.
rdbuf
();
std
::
cout
<<
j
<<
std
::
endl
;
LLVMFuzzerTestOneInput
(
reinterpret_cast
<
const
uint8_t
*>
(
ss
.
str
().
c_str
()),
ss
.
str
().
size
());
}
catch
(
std
::
invalid_argument
&
e
)
{
std
::
cout
<<
"Invalid argument in parsing"
<<
e
.
what
()
<<
'\n'
;
}
#ifdef __AFL_HAVE_MANUAL_CONTROL
#ifdef __AFL_HAVE_MANUAL_CONTROL
}
}
#endif
#endif
...
...
test/src/fuzzer-parse_json.cpp
View file @
a084e90f
// Copyright 2016 Google Inc.
/*
//
__ _____ _____ _____
// Licensed under the Apache License, Version 2.0 (the "License");
__| | __| | | | JSON for Modern C++ (fuzz test support)
// you may not use this file except in compliance with the License.
| | |__ | | | | | | version 2.0.9
// You may obtain a copy of the License at
|_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// http://www.apache.org/licenses/LICENSE-2.0
This file implements a parser test suitable for fuzz testing. Given a byte
//
array data, it performs the following steps:
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
- j1 = parse(data)
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- s1 = serialize(j1)
// See the License for the specific language governing permissions and
- j2 = parse(s1)
// limitations under the License.
- s2 = serialize(j2)
- assert(s1 == s2)
The provided function `LLVMFuzzerTestOneInput` can be used in different fuzzer
drivers.
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
*/
#include <iostream>
#include <iostream>
#include <sstream>
#include <sstream>
...
@@ -18,25 +25,41 @@
...
@@ -18,25 +25,41 @@
using
json
=
nlohmann
::
json
;
using
json
=
nlohmann
::
json
;
// see http://llvm.org/docs/LibFuzzer.html
extern
"C"
int
LLVMFuzzerTestOneInput
(
const
uint8_t
*
data
,
size_t
size
)
extern
"C"
int
LLVMFuzzerTestOneInput
(
const
uint8_t
*
data
,
size_t
size
)
{
{
try
try
{
{
std
::
stringstream
s
;
// step 1: parse input
s
<<
json
::
parse
(
data
,
data
+
size
);
json
j1
=
json
::
parse
(
data
,
data
+
size
);
try
try
{
{
auto
j
=
json
::
parse
(
s
.
str
());
// step 2: round trip
std
::
stringstream
s2
;
s2
<<
j
;
// first serialization
assert
(
s
.
str
()
==
s2
.
str
());
std
::
string
s1
=
j1
.
dump
();
assert
(
j
==
json
::
parse
(
s
.
str
()));
// parse serialization
json
j2
=
json
::
parse
(
s1
);
// second serialization
std
::
string
s2
=
j2
.
dump
();
// serializations must match
assert
(
s1
==
s2
);
}
}
catch
(
const
std
::
invalid_argument
&
)
catch
(
const
std
::
invalid_argument
&
)
{
{
assert
(
0
);
// parsing a JSON serialization must not fail
assert
(
false
);
}
}
}
catch
(
const
std
::
invalid_argument
&
)
{
// parse errors are ok, because input may be random bytes
}
}
catch
(
const
std
::
invalid_argument
&
)
{
}
// return 0 - non-zero return values are reserved for future use
return
0
;
return
0
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment