Skip to content

Commit 64138b7

Browse files
authored
Merge pull request libgit2#4728 from pks-t/pks/fuzzers
Fuzzers
2 parents 0cf7546 + 835d604 commit 64138b7

File tree

325 files changed

+629
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

325 files changed

+629
-29
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ msvc/Release/
3131
.*.swp
3232
tags
3333
mkmf.log
34+
*.profdata
35+
*.profraw

CMakeLists.txt

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,36 @@ INCLUDE(EnableWarnings)
3838

3939
# Build options
4040
#
41-
OPTION( SONAME "Set the (SO)VERSION of the target" ON )
42-
OPTION( BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON )
43-
OPTION( THREADSAFE "Build libgit2 as threadsafe" ON )
44-
OPTION( BUILD_CLAR "Build Tests using the Clar suite" ON )
45-
OPTION( BUILD_EXAMPLES "Build library usage example apps" OFF )
46-
OPTION( TAGS "Generate tags" OFF )
47-
OPTION( PROFILE "Generate profiling information" OFF )
48-
OPTION( ENABLE_TRACE "Enables tracing support" OFF )
49-
OPTION( LIBGIT2_FILENAME "Name of the produced binary" OFF )
50-
51-
SET(SHA1_BACKEND "CollisionDetection" CACHE STRING "Backend to use for SHA1. One of Generic, OpenSSL, Win32, CommonCrypto, mbedTLS, CollisionDetection. ")
52-
OPTION( USE_SSH "Link with libssh to enable SSH support" ON )
53-
OPTION( USE_HTTPS "Enable HTTPS support. Can be set to a specific backend" ON )
54-
OPTION( USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF )
55-
OPTION( VALGRIND "Configure build for valgrind" OFF )
56-
OPTION( CURL "Use curl for HTTP if available" ON)
57-
OPTION( USE_EXT_HTTP_PARSER "Use system HTTP_Parser if available" ON)
58-
OPTION( DEBUG_POOL "Enable debug pool allocator" OFF )
59-
OPTION( ENABLE_WERROR "Enable compilation with -Werror" OFF )
60-
OPTION( USE_BUNDLED_ZLIB "Use the bundled version of zlib" OFF )
41+
OPTION(SONAME "Set the (SO)VERSION of the target" ON)
42+
OPTION(BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
43+
OPTION(THREADSAFE "Build libgit2 as threadsafe" ON)
44+
OPTION(BUILD_CLAR "Build Tests using the Clar suite" ON)
45+
OPTION(BUILD_EXAMPLES "Build library usage example apps" OFF)
46+
OPTION(BUILD_FUZZERS "Build the fuzz targets" OFF)
47+
OPTION(TAGS "Generate tags" OFF)
48+
OPTION(PROFILE "Generate profiling information" OFF)
49+
OPTION(ENABLE_TRACE "Enables tracing support" OFF)
50+
OPTION(LIBGIT2_FILENAME "Name of the produced binary" OFF)
51+
52+
SET(SHA1_BACKEND "CollisionDetection" CACHE STRING
53+
"Backend to use for SHA1. One of Generic, OpenSSL, Win32, CommonCrypto, mbedTLS, CollisionDetection.")
54+
OPTION(USE_SSH "Link with libssh to enable SSH support" ON)
55+
OPTION(USE_HTTPS "Enable HTTPS support. Can be set to a specific backend" ON)
56+
OPTION(USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF)
57+
OPTION(USE_STANDALONE_FUZZERS "Enable standalone fuzzers (compatible with gcc)" OFF)
58+
OPTION(VALGRIND "Configure build for valgrind" OFF)
59+
OPTION(CURL "Use curl for HTTP if available" ON)
60+
OPTION(USE_EXT_HTTP_PARSER "Use system HTTP_Parser if available" ON)
61+
OPTION(DEBUG_POOL "Enable debug pool allocator" OFF)
62+
OPTION(ENABLE_WERROR "Enable compilation with -Werror" OFF)
63+
OPTION(USE_BUNDLED_ZLIB "Use the bundled version of zlib" OFF)
6164

6265
IF (UNIX AND NOT APPLE)
63-
OPTION( ENABLE_REPRODUCIBLE_BUILDS "Enable reproducible builds" OFF )
66+
OPTION(ENABLE_REPRODUCIBLE_BUILDS "Enable reproducible builds" OFF)
6467
ENDIF()
6568

6669
IF (APPLE)
67-
OPTION( USE_ICONV "Link with and use iconv library" ON )
70+
OPTION(USE_ICONV "Link with and use iconv library" ON)
6871
ENDIF()
6972

7073
IF(MSVC)
@@ -74,27 +77,27 @@ IF(MSVC)
7477
#
7578
# If you are writing a CLR program and want to link to libgit2, you'll want
7679
# to turn this on by invoking CMake with the "-DSTDCALL=ON" argument.
77-
OPTION( STDCALL "Build libgit2 with the __stdcall convention" OFF )
80+
OPTION(STDCALL "Build libgit2 with the __stdcall convention" OFF)
7881

7982
# This option must match the settings used in your program, in particular if you
8083
# are linking statically
81-
OPTION( STATIC_CRT "Link the static CRT libraries" ON )
84+
OPTION(STATIC_CRT "Link the static CRT libraries" ON)
8285

8386
# If you want to embed a copy of libssh2 into libgit2, pass a
8487
# path to libssh2
85-
OPTION( EMBED_SSH_PATH "Path to libssh2 to embed (Windows)" OFF )
88+
OPTION(EMBED_SSH_PATH "Path to libssh2 to embed (Windows)" OFF)
8689
ENDIF()
8790

8891

8992
IF(WIN32)
9093
# By default, libgit2 is built with WinHTTP. To use the built-in
9194
# HTTP transport, invoke CMake with the "-DWINHTTP=OFF" argument.
92-
OPTION( WINHTTP "Use Win32 WinHTTP routines" ON )
95+
OPTION(WINHTTP "Use Win32 WinHTTP routines" ON)
9396
ENDIF()
9497

9598
IF(MSVC)
9699
# Enable MSVC CRTDBG memory leak reporting when in debug mode.
97-
OPTION(MSVC_CRTDBG "Enable CRTDBG memory leak reporting" OFF)
100+
OPTION(MSVC_CRTDBG "Enable CRTDBG memory leak reporting" OFF)
98101
ENDIF()
99102

100103
FILE(STRINGS "${libgit2_SOURCE_DIR}/include/git2/version.h" GIT2_HEADER REGEX "^#define LIBGIT2_VERSION \"[^\"]*\"$")
@@ -245,6 +248,14 @@ ELSE()
245248
# that uses CMAKE_CONFIGURATION_TYPES and not CMAKE_BUILD_TYPE
246249
ENDIF()
247250

251+
IF(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS)
252+
# The actual sanitizer link target will be added when linking the fuzz
253+
# targets.
254+
SET(CMAKE_REQUIRED_FLAGS "-fsanitize=fuzzer-no-link")
255+
ADD_C_FLAG(-fsanitize=fuzzer-no-link)
256+
UNSET(CMAKE_REQUIRED_FLAGS)
257+
ENDIF ()
258+
248259
ADD_SUBDIRECTORY(src)
249260

250261
# Tests
@@ -282,6 +293,18 @@ IF (BUILD_EXAMPLES)
282293
ADD_SUBDIRECTORY(examples)
283294
ENDIF ()
284295

296+
IF(BUILD_FUZZERS)
297+
IF(NOT USE_STANDALONE_FUZZERS)
298+
IF(BUILD_EXAMPLES)
299+
MESSAGE(FATAL_ERROR "Cannot build the fuzzer targets and the examples together")
300+
ENDIF()
301+
IF(BUILD_CLAR)
302+
MESSAGE(FATAL_ERROR "Cannot build the fuzzer targets and the tests together")
303+
ENDIF()
304+
ENDIF()
305+
ADD_SUBDIRECTORY(fuzzers)
306+
ENDIF()
307+
285308
IF(CMAKE_VERSION VERSION_GREATER 3)
286309
FEATURE_SUMMARY(WHAT ENABLED_FEATURES DESCRIPTION "Enabled features:")
287310
FEATURE_SUMMARY(WHAT DISABLED_FEATURES DESCRIPTION "Disabled features:")

ci/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ echo "## Configuring build environment"
2929
echo "##############################################################################"
3030

3131
echo cmake ${SOURCE_DIR} -DBUILD_EXAMPLES=ON ${CMAKE_OPTIONS}
32-
cmake ${SOURCE_DIR} -DBUILD_EXAMPLES=ON ${CMAKE_OPTIONS}
32+
cmake ${SOURCE_DIR} -DBUILD_EXAMPLES=ON -DBUILD_FUZZERS=ON -DUSE_STANDALONE_FUZZERS=ON ${CMAKE_OPTIONS}
3333

3434
echo ""
3535
echo "##############################################################################"

ci/test.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ if [ -z "$SKIP_SSH_TESTS" ]; then
184184
unset GITTEST_REMOTE_SSH_FINGERPRINT
185185
fi
186186

187+
if [ -z "$SKIP_FUZZERS" ]; then
188+
echo ""
189+
echo "##############################################################################"
190+
echo "## Running fuzzers"
191+
echo "##############################################################################"
192+
193+
for fuzzer in fuzzers/*_fuzzer; do
194+
"${fuzzer}" "${SOURCE_DIR}/fuzzers/corpora/$(basename "${fuzzer%_fuzzer}")" || die $?
195+
done
196+
fi
197+
187198
echo "Success."
188199
cleanup
189200
exit 0

cmake/Modules/AddCFlagIfSupported.cmake

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@
55

66
INCLUDE(CheckCCompilerFlag)
77

8+
MACRO(ADD_C_FLAG _FLAG)
9+
STRING(TOUPPER ${_FLAG} UPCASE)
10+
STRING(REGEX REPLACE "^-" "" UPCASE_PRETTY ${UPCASE})
11+
CHECK_C_COMPILER_FLAG(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED)
12+
13+
IF(IS_${UPCASE_PRETTY}_SUPPORTED)
14+
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FLAG}")
15+
ELSE()
16+
MESSAGE(FATAL_ERROR "Required flag ${_FLAG} is not supported")
17+
ENDIF()
18+
ENDMACRO()
19+
820
MACRO(ADD_C_FLAG_IF_SUPPORTED _FLAG)
921
STRING(TOUPPER ${_FLAG} UPCASE)
10-
STRING(REGEX REPLACE "^-" "" UPCASE_PRETTY ${UPCASE})
22+
STRING(REGEX REPLACE "^-" "" UPCASE_PRETTY ${UPCASE})
1123
CHECK_C_COMPILER_FLAG(${_FLAG} IS_${UPCASE_PRETTY}_SUPPORTED)
1224

1325
IF(IS_${UPCASE_PRETTY}_SUPPORTED)

docs/fuzzing.md

Lines changed: 71 additions & 0 deletions

fuzzers/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
LINK_DIRECTORIES(${LIBGIT2_LIBDIRS})
2+
INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
3+
4+
IF(BUILD_FUZZERS AND NOT USE_STANDALONE_FUZZERS)
5+
ADD_C_FLAG(-fsanitize=fuzzer)
6+
ENDIF ()
7+
8+
FILE(GLOB SRC_FUZZ RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *_fuzzer.c)
9+
FOREACH(fuzz_target_src ${SRC_FUZZ})
10+
STRING(REPLACE ".c" "" fuzz_target_name ${fuzz_target_src})
11+
SET(${fuzz_target_name}_SOURCES ${fuzz_target_src} ${LIBGIT2_OBJECTS})
12+
IF(USE_STANDALONE_FUZZERS)
13+
LIST(APPEND ${fuzz_target_name}_SOURCES "standalone_driver.c")
14+
ENDIF()
15+
ADD_EXECUTABLE(${fuzz_target_name} ${${fuzz_target_name}_SOURCES})
16+
SET_TARGET_PROPERTIES(${fuzz_target_name} PROPERTIES C_STANDARD 90)
17+
TARGET_LINK_LIBRARIES(${fuzz_target_name} ${LIBGIT2_LIBS})
18+
ENDFOREACH()
632 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�PACK
Binary file not shown.

0 commit comments

Comments
 (0)