From 20c01e1acf330c8a28192f55b16efeebddb72ab0 Mon Sep 17 00:00:00 2001 From: Paul Dreik Date: Sat, 27 Apr 2019 09:25:19 +0200 Subject: [PATCH] initial oss-fuzz compatible version --- CMakeLists.txt | 5 ++ fuzzing/.gitignore | 1 + fuzzing/CMakeLists.txt | 35 +++++++----- fuzzing/build.sh | 57 +++++++++++++++++++ fuzzing/{fuzz_named_arg.cpp => named_arg.cpp} | 0 fuzzing/{fuzz_one_arg.cpp => one_arg.cpp} | 0 fuzzing/{fuzz_sprintf.cpp => sprintf.cpp} | 0 fuzzing/{fuzz_two_args.cpp => two_args.cpp} | 0 8 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 fuzzing/.gitignore create mode 100755 fuzzing/build.sh rename fuzzing/{fuzz_named_arg.cpp => named_arg.cpp} (100%) rename fuzzing/{fuzz_one_arg.cpp => one_arg.cpp} (100%) rename fuzzing/{fuzz_sprintf.cpp => sprintf.cpp} (100%) rename fuzzing/{fuzz_two_args.cpp => two_args.cpp} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index ee0142c5ee48..184b098a3d2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ option(FMT_WERROR "Halt the compilation with an error on compiler warnings." option(FMT_DOC "Generate the doc target." ${MASTER_PROJECT}) option(FMT_INSTALL "Generate the install target." ${MASTER_PROJECT}) option(FMT_TEST "Generate the test target." ${MASTER_PROJECT}) +option(FMT_FUZZ "Generate the fuzz target." ${MASTER_PROJECT}) project(FMT CXX) @@ -261,6 +262,10 @@ if (FMT_TEST) add_subdirectory(test) endif () +if (FMT_FUZZ) + add_subdirectory(fuzzing) +endif () + set(gitignore ${PROJECT_SOURCE_DIR}/.gitignore) if (MASTER_PROJECT AND EXISTS ${gitignore}) # Get the list of ignored files from .gitignore. diff --git a/fuzzing/.gitignore b/fuzzing/.gitignore new file mode 100644 index 000000000000..414487d53eb8 --- /dev/null +++ b/fuzzing/.gitignore @@ -0,0 +1 @@ +build-*/ diff --git a/fuzzing/CMakeLists.txt b/fuzzing/CMakeLists.txt index 85f1d43cbeaf..304c47a44971 100644 --- a/fuzzing/CMakeLists.txt +++ b/fuzzing/CMakeLists.txt @@ -4,37 +4,42 @@ # https://www.pauldreik.se/ -cmake_minimum_required(VERSION 3.10) +#cmake_minimum_required(VERSION 3.10) -project(fmt_fuzzers LANGUAGES CXX) +#project(fmt_fuzzers LANGUAGES CXX) -add_definitions(-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1) +#add_definitions(-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1) -add_subdirectory(fmt) +#add_subdirectory(fmt) # settings this links in a main. useful for reproducing, # kcov, gdb, afl, valgrind. # (note that libFuzzer can also reproduce, just pass it the files) -option(reproduce_mode "enables the reproduce mode, instead of libFuzzer" On) +option(FMT_FUZZ_LINKMAIN "enables the reproduce mode, instead of libFuzzer" On) + +#for oss-fuzz - insert $LIB_FUZZING_ENGINE into the link flags, but only for +#the fuzz targets, otherwise the cmake configuration step fails. +option(FMT_FUZZ_LDFLAGS "LDFLAGS for the fuzz targets" "") #find all fuzzers. -file(GLOB SOURCES "fuzz*.cpp") +file(GLOB SOURCES "*.cpp") -if(reproduce_mode) - set(prefix reproducer_) +if(FMT_FUZZ_LINKMAIN) add_definitions(-DIMPLEMENT_MAIN=1) -else() - # this assumes clang is used - string(APPEND CMAKE_CXX_FLAGS " -fsanitize=fuzzer") - set(prefix fuzzer_) endif() + + macro(implement_fuzzer sourcefile) get_filename_component(basename ${sourcefile} NAME_WE) - add_executable(${prefix}${basename} ${sourcefile}) - target_link_libraries(${prefix}${basename} PRIVATE fmt) - set_property(TARGET ${prefix}${basename} PROPERTY CXX_STANDARD 17) + set(name fuzzer_${basename}) + add_executable(${name} ${sourcefile}) + target_link_libraries(${name} PRIVATE fmt) +if(FMT_FUZZ_LDFLAGS) + target_link_libraries(${name} PRIVATE ${FMT_FUZZ_LDFLAGS}) +endif() + set_property(TARGET ${name} PROPERTY CXX_STANDARD 17) endmacro() foreach(X IN ITEMS ${SOURCES}) diff --git a/fuzzing/build.sh b/fuzzing/build.sh new file mode 100755 index 000000000000..f209792a8d02 --- /dev/null +++ b/fuzzing/build.sh @@ -0,0 +1,57 @@ +#!/bin/sh +# +# +set -e +me=$(basename $0) +root=$(readlink -f "$(dirname "$0")/..") + + +echo $me: root=$root + +here=$(pwd) + +#builds the fuzzers as one would do if using afl or just making +#binaries for reproducing. +builddir=$here/build-fuzzers-reproduce +mkdir -p $builddir +cd $builddir +CXX="ccache g++" CXXFLAGS="-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1" cmake \ +$root -GNinja -DCMAKE_BUILD_TYPE=Debug -DFMT_DOC=Off -DFMT_TEST=Off -DFMT_FUZZ=On +cmake --build $builddir + + + +#builds the fuzzers as oss-fuzz does +builddir=$here/build-fuzzers-ossfuzz +mkdir -p $builddir +cd $builddir +CXX="clang++" \ +CXXFLAGS="-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1 -fsanitize=fuzzer-no-link" \ +cmake $root -GNinja -DCMAKE_BUILD_TYPE=Debug \ +-DFMT_DOC=Off \ +-DFMT_TEST=Off \ +-DFMT_FUZZ=On \ +-DFMT_FUZZ_LINKMAIN=Off \ +-DFMT_FUZZ_LDFLAGS="-fsanitize=fuzzer" + +cmake --build $builddir + + +#builds fuzzers for local fuzzing with libfuzzer +builddir=$here/build-fuzzers-libfuzzer +mkdir -p $builddir +cd $builddir +CXX="clang++" \ +CXXFLAGS="-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1 -fsanitize=fuzzer-no-link,address,undefined" \ +cmake $root -GNinja -DCMAKE_BUILD_TYPE=Debug \ +-DFMT_DOC=Off \ +-DFMT_TEST=Off \ +-DFMT_FUZZ=On \ +-DFMT_FUZZ_LINKMAIN=Off \ +-DFMT_FUZZ_LDFLAGS="-fsanitize=fuzzer" + +cmake --build $builddir + + +echo $me: all good + diff --git a/fuzzing/fuzz_named_arg.cpp b/fuzzing/named_arg.cpp similarity index 100% rename from fuzzing/fuzz_named_arg.cpp rename to fuzzing/named_arg.cpp diff --git a/fuzzing/fuzz_one_arg.cpp b/fuzzing/one_arg.cpp similarity index 100% rename from fuzzing/fuzz_one_arg.cpp rename to fuzzing/one_arg.cpp diff --git a/fuzzing/fuzz_sprintf.cpp b/fuzzing/sprintf.cpp similarity index 100% rename from fuzzing/fuzz_sprintf.cpp rename to fuzzing/sprintf.cpp diff --git a/fuzzing/fuzz_two_args.cpp b/fuzzing/two_args.cpp similarity index 100% rename from fuzzing/fuzz_two_args.cpp rename to fuzzing/two_args.cpp