fuzz: add basic fuzz testing target.

fuzz-pack-headers.c provides a fuzzing entry point compatible with
libFuzzer (and possibly other fuzzing engines).

Signed-off-by: Josh Steadmon <steadmon@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Josh Steadmon 2018-10-12 17:58:40 -07:00 коммит произвёл Junio C Hamano
Родитель 2efbb7f521
Коммит 5e47215080
3 изменённых файлов: 48 добавлений и 0 удалений

2
.gitignore поставляемый
Просмотреть файл

@ -1,3 +1,5 @@
/fuzz_corpora
/fuzz-pack-headers
/GIT-BUILD-OPTIONS
/GIT-CFLAGS
/GIT-LDFLAGS

Просмотреть файл

@ -590,6 +590,8 @@ XDIFF_OBJS =
VCSSVN_OBJS =
GENERATED_H =
EXTRA_CPPFLAGS =
FUZZ_OBJS =
FUZZ_PROGRAMS =
LIB_OBJS =
PROGRAM_OBJS =
PROGRAMS =
@ -682,6 +684,13 @@ SCRIPTS = $(SCRIPT_SH_INS) \
ETAGS_TARGET = TAGS
FUZZ_OBJS += fuzz-pack-headers.o
# Always build fuzz objects even if not testing, to prevent bit-rot.
all:: $(FUZZ_OBJS)
FUZZ_PROGRAMS += $(patsubst %.o,%,$(FUZZ_OBJS))
# Empty...
EXTRA_PROGRAMS =
@ -2250,6 +2259,7 @@ TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST
OBJECTS := $(LIB_OBJS) $(BUILTIN_OBJS) $(PROGRAM_OBJS) $(TEST_OBJS) \
$(XDIFF_OBJS) \
$(VCSSVN_OBJS) \
$(FUZZ_OBJS) \
common-main.o \
git.o
ifndef NO_CURL
@ -2937,6 +2947,7 @@ clean: profile-clean coverage-clean cocciclean
$(RM) $(LIB_FILE) $(XDIFF_LIB) $(VCSSVN_LIB)
$(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) git$X
$(RM) $(TEST_PROGRAMS) $(NO_INSTALL)
$(RM) $(FUZZ_PROGRAMS)
$(RM) -r bin-wrappers $(dep_dirs)
$(RM) -r po/build/
$(RM) *.pyc *.pyo */*.pyc */*.pyo command-list.h $(ETAGS_TARGET) tags cscope*
@ -3061,3 +3072,24 @@ cover_db: coverage-report
cover_db_html: cover_db
cover -report html -outputdir cover_db_html cover_db
### Fuzz testing
#
# Building fuzz targets generally requires a special set of compiler flags that
# are not necessarily appropriate for general builds, and that vary greatly
# depending on the compiler version used.
#
# An example command to build against libFuzzer from LLVM 4.0.0:
#
# make CC=clang CXX=clang++ \
# CFLAGS="-fsanitize-coverage=trace-pc-guard -fsanitize=address" \
# LIB_FUZZING_ENGINE=/usr/lib/llvm-4.0/lib/libFuzzer.a \
# fuzz-all
#
.PHONY: fuzz-all
$(FUZZ_PROGRAMS): all
$(QUIET_LINK)$(CXX) $(CFLAGS) $(LIB_OBJS) $(BUILTIN_OBJS) \
$(XDIFF_OBJS) $(EXTLIBS) git.o $@.o $(LIB_FUZZING_ENGINE) -o $@
fuzz-all: $(FUZZ_PROGRAMS)

14
fuzz-pack-headers.c Normal file
Просмотреть файл

@ -0,0 +1,14 @@
#include "packfile.h"
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
enum object_type type;
unsigned long len;
unpack_object_header_buffer((const unsigned char *)data,
(unsigned long)size, &type, &len);
return 0;
}