Merge pull request #116 from AdamKorcz/fuzz1

Add fuzzer with oss-fuzz build script
This commit is contained in:
Tristan Penman 2020-12-17 09:39:17 +11:00 committed by GitHub
commit f544cd020a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

43
tests/fuzzing/fuzzer.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <stdexcept>
#include <unistd.h>
#include <document.h>
#include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/utils/rapidjson_utils.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_parser.hpp>
using valijson::Schema;
using valijson::SchemaParser;
using valijson::adapters::RapidJsonAdapter;
extern "C" int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
if(size<3) return 0;
char input_file[256];
sprintf(input_file, "/tmp/libfuzzer.json");
FILE *fp = fopen(input_file, "wb");
if (!fp)
return 0;
fwrite(data, size, 1, fp);
fclose(fp);
rapidjson::Document schemaDocument;
if (!valijson::utils::loadDocument(input_file, schemaDocument)) {
return 1;
}
Schema schema;
SchemaParser parser;
RapidJsonAdapter schemaDocumentAdapter(schemaDocument);
try {
parser.populateSchema(schemaDocumentAdapter, schema);
} catch (std::exception &e) {
unlink(input_file);
return 1;
}
unlink(input_file);
return 1;
}

32
tests/fuzzing/oss-fuzz-build.sh Executable file
View File

@ -0,0 +1,32 @@
#!/bin/bash -eu
# This line causes an abort which breaks fuzzing:
sed -i '27d' include/valijson/utils/rapidjson_utils.hpp
mkdir build
cd build
cmake -Dvalijson_BUILD_EXAMPLES=FALSE \
-Dvalijson_EXCLUDE_BOOST=TRUE \
..
make -j$(nproc)
cd ../tests/fuzzing
find ../.. -name "*.o" -exec ar rcs fuzz_lib.a {} \;
$CXX $CXXFLAGS -DVALIJSON_USE_EXCEPTIONS=1 \
-I/src/valijson/thirdparty/rapidjson-1.1.0/include \
-I/src/valijson/thirdparty/rapidjson-1.1.0/include/rapidjson \
-I/src/valijson/include \
-I/src/valijson/include/valijson \
-I/src/valijson/include/valijson/adapters \
-c fuzzer.cpp -o fuzzer.o
$CXX $CXXFLAGS $LIB_FUZZING_ENGINE \
-DVALIJSON_USE_EXCEPTIONS=1 \
-rdynamic fuzzer.o \
-o $OUT/fuzzer fuzz_lib.a
zip $OUT/fuzzer_seed_corpus.zip \
$SRC/valijson/doc/schema/draft-03.json