Skip to content

Commit

Permalink
initial oss-fuzz compatible version
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldreik committed Apr 27, 2019
1 parent 6cbd91a commit 20c01e1
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions fuzzing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build-*/
35 changes: 20 additions & 15 deletions fuzzing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
57 changes: 57 additions & 0 deletions fuzzing/build.sh
Original file line number Diff line number Diff line change
@@ -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

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 20c01e1

Please sign in to comment.