Skip to content

Commit

Permalink
Added MessagePack fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jun 7, 2018
1 parent 4ff6809 commit a9a730f
Show file tree
Hide file tree
Showing 41 changed files with 74 additions and 48 deletions.
11 changes: 7 additions & 4 deletions fuzzing/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ CXXFLAGS += -I../src
all: \
$(OUT)/json_fuzzer \
$(OUT)/json_fuzzer_seed_corpus.zip \
$(OUT)/json_fuzzer.options
$(OUT)/json_fuzzer.options \
$(OUT)/msgpack_fuzzer \
$(OUT)/msgpack_fuzzer_seed_corpus.zip \
$(OUT)/msgpack_fuzzer.options

$(OUT)/json_fuzzer: fuzzer.cpp $(shell find ../src -type f)
$(OUT)/%_fuzzer: %_fuzzer.cpp $(shell find ../src -type f)
$(CXX) $(CXXFLAGS) $< -o$@ $(LIB_FUZZING_ENGINE)

$(OUT)/json_fuzzer_seed_corpus.zip: seed_corpus/*
$(OUT)/%_fuzzer_seed_corpus.zip: %_seed_corpus/*
zip -j $@ $?

$(OUT)/json_fuzzer.options:
$(OUT)/%_fuzzer.options:
@echo "[libfuzzer]" > $@
@echo "max_len = 256" >> $@
@echo "timeout = 10" >> $@
27 changes: 0 additions & 27 deletions fuzzing/fuzzer.cpp

This file was deleted.

File renamed without changes.
11 changes: 11 additions & 0 deletions fuzzing/json_fuzzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <ArduinoJson.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
DynamicJsonDocument doc;
DeserializationError error = deserializeJson(doc, data, size);
if (!error) {
std::string json;
serializeJson(doc, json);
}
return 0;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions fuzzing/msgpack_corpus/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
11 changes: 11 additions & 0 deletions fuzzing/msgpack_fuzzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <ArduinoJson.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
DynamicJsonDocument doc;
DeserializationError error = deserializeMsgPack(doc, data, size);
if (!error) {
std::string json;
serializeMsgPack(doc, json);
}
return 0;
}
Binary file added fuzzing/msgpack_seed_corpus/array16
Binary file not shown.
Binary file added fuzzing/msgpack_seed_corpus/array32
Binary file not shown.
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/fixarray
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��hello�world
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/fixint_negative
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/fixint_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/fixmap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��one�two
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/fixstr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�hello world
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/float32
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�@H��
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/float64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�@ !���o
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/int16
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/int32
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ҷi�.
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/int64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�4Vx����
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/int8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
Binary file added fuzzing/msgpack_seed_corpus/map16
Binary file not shown.
Binary file added fuzzing/msgpack_seed_corpus/map32
Binary file not shown.
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/nil
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Binary file added fuzzing/msgpack_seed_corpus/str16
Binary file not shown.
Binary file added fuzzing/msgpack_seed_corpus/str32
Binary file not shown.
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/str8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�hello
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/uint16
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�09
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/uint32
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�4Vx
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/uint64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�4Vx����
1 change: 1 addition & 0 deletions fuzzing/msgpack_seed_corpus/uint8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
11 changes: 6 additions & 5 deletions scripts/oss-fuzz/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"

config.vm.synced_folder "E:\\Git\\Arduino\\libraries\\ArduinoJson", "/host/ArduinoJson"
config.vm.synced_folder "../..", "/host/ArduinoJson"
config.vm.synced_folder "E:\\Git\\oss-fuzz", "/host/oss-fuzz"

config.vm.network "forwarded_port", guest: 8001, host: 8001

config.vm.provider "virtualbox" do |v|
v.memory = 2048
v.cpus = 2
end

config.vm.provision "shell", privileged: false, inline: <<-SHELL
set -x
Expand All @@ -18,10 +23,6 @@ Vagrant.configure(2) do |config|
git clone https://github.com/google/fuzzer-test-suite.git FTS
./FTS/tutorial/install-deps.sh # Get deps
./FTS/tutorial/install-clang.sh # Get fresh clang binaries
# Get libFuzzer sources and build it
svn co http://llvm.org/svn/llvm-project/llvm/trunk/lib/Fuzzer
Fuzzer/build.sh
sudo mv libFuzzer.a /usr/local/lib/
echo "export PROJECT_NAME='arduinojson'" >> $HOME/.profile
echo "export CC='clang'" >> $HOME/.profile
Expand Down
30 changes: 18 additions & 12 deletions scripts/travis/fuzz.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
#!/bin/bash -eux

ROOT_DIR=$(dirname $0)/../../
INCLUDE_DIR=$ROOT_DIR/src/
FUZZING_DIR=$ROOT_DIR/fuzzing/
JSON_CORPUS_DIR=$FUZZING_DIR/my_corpus
JSON_SEED_CORPUS_DIR=$FUZZING_DIR/seed_corpus

CXX="clang++-$CLANG"
INCLUDE_DIR=${ROOT_DIR}/src/
FUZZING_DIR=${ROOT_DIR}/fuzzing/
CXXFLAGS="-g -fprofile-instr-generate -fcoverage-mapping -fsanitize=address,fuzzer"

$CXX $CXXFLAGS -o json_fuzzer -I$INCLUDE_DIR $FUZZING_DIR/fuzzer.cpp
fuzz() {
NAME="$1"
FUZZER="${NAME}_fuzzer"
FUZZER_CPP="${FUZZING_DIR}/${NAME}_fuzzer.cpp"
CORPUS_DIR="${FUZZING_DIR}/${NAME}_corpus"
SEED_CORPUS_DIR="${FUZZING_DIR}/${NAME}_seed_corpus"

clang++-${CLANG} ${CXXFLAGS} -o ${FUZZER} -I$INCLUDE_DIR ${FUZZER_CPP}

export ASAN_OPTIONS="detect_leaks=0"
export LLVM_PROFILE_FILE="json_fuzzer.profraw"
./json_fuzzer "$JSON_CORPUS_DIR" "$JSON_SEED_CORPUS_DIR" -max_total_time=60
export ASAN_OPTIONS="detect_leaks=0"
export LLVM_PROFILE_FILE="${FUZZER}.profraw"
./${FUZZER} "$CORPUS_DIR" "$SEED_CORPUS_DIR" -max_total_time=30

llvm-profdata-$CLANG merge -sparse json_fuzzer.profraw -o json_fuzzer.profdata
llvm-profdata-${CLANG} merge -sparse ${LLVM_PROFILE_FILE} -o ${FUZZER}.profdata
llvm-cov-${CLANG} report ./${FUZZER} -instr-profile=${FUZZER}.profdata
}

llvm-cov-$CLANG report ./json_fuzzer -instr-profile=json_fuzzer.profdata
fuzz json
fuzz msgpack

0 comments on commit a9a730f

Please sign in to comment.