Skip to content

Commit 8b9a42b

Browse files
steadmongitster
authored andcommitted
fuzz: fix fuzz test build rules
When we originally added the fuzz tests in 5e47215 (fuzz: add basic fuzz testing target., 2018-10-12), we went to some trouble to create a Makefile rule that allowed linking the fuzz executables without pulling in common-main.o. This was necessary to prevent the fuzzing-engine-provided main() from clashing with Git's main(). However, since 19d7594 (common-main.c: move non-trace2 exit() behavior out of trace2.c, 2022-06-02), it has been necessary to link common-main.o due to moving the common_exit() function to that file. Ævar suggested a set of compiler flags to allow this in [1], but this was never reflected in the Makefile. Since we now must include common-main.o, there's no reason to pick and choose a subset of object files to link, so simplify the Makefile rule for the fuzzer executables to just use libgit.a. While we're at it, include the necessary linker flag to allow multiple definitions directly in the Makefile rule, rather than requiring it to be passed on the command-line each time. This means the Makefile rule as written is now more compiler-specific, but this was already the case for the fuzzers themselves anyway. [1] https://lore.kernel.org/git/[email protected]/ Signed-off-by: Josh Steadmon <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 564d025 commit 8b9a42b

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

Makefile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ SCRIPTS = $(SCRIPT_SH_GEN) \
749749

750750
ETAGS_TARGET = TAGS
751751

752+
FUZZ_OBJS += oss-fuzz/dummy-cmd-main.o
752753
FUZZ_OBJS += oss-fuzz/fuzz-commit-graph.o
753754
FUZZ_OBJS += oss-fuzz/fuzz-pack-headers.o
754755
FUZZ_OBJS += oss-fuzz/fuzz-pack-idx.o
@@ -758,7 +759,7 @@ fuzz-objs: $(FUZZ_OBJS)
758759
# Always build fuzz objects even if not testing, to prevent bit-rot.
759760
all:: $(FUZZ_OBJS)
760761

761-
FUZZ_PROGRAMS += $(patsubst %.o,%,$(FUZZ_OBJS))
762+
FUZZ_PROGRAMS += $(patsubst %.o,%,$(filter-out %dummy-cmd-main.o,$(FUZZ_OBJS)))
762763

763764
# Empty...
764765
EXTRA_PROGRAMS =
@@ -3838,15 +3839,16 @@ cover_db_html: cover_db
38383839
#
38393840
# make CC=clang CXX=clang++ \
38403841
# CFLAGS="-fsanitize=fuzzer-no-link,address" \
3841-
# LIB_FUZZING_ENGINE="-fsanitize=fuzzer" \
3842+
# LIB_FUZZING_ENGINE="-fsanitize=fuzzer,address" \
38423843
# fuzz-all
38433844
#
3844-
FUZZ_CXXFLAGS ?= $(CFLAGS)
3845+
FUZZ_CXXFLAGS ?= $(ALL_CFLAGS)
38453846

38463847
.PHONY: fuzz-all
38473848

3848-
$(FUZZ_PROGRAMS): all
3849-
$(QUIET_LINK)$(CXX) $(FUZZ_CXXFLAGS) $(LIB_OBJS) $(BUILTIN_OBJS) \
3850-
$(XDIFF_OBJS) $(EXTLIBS) git.o $@.o $(LIB_FUZZING_ENGINE) -o $@
3849+
$(FUZZ_PROGRAMS): %: %.o oss-fuzz/dummy-cmd-main.o $(GITLIBS) GIT-LDFLAGS
3850+
$(QUIET_LINK)$(CXX) $(FUZZ_CXXFLAGS) -o $@ $(ALL_LDFLAGS) \
3851+
-Wl,--allow-multiple-definition \
3852+
$(filter %.o,$^) $(filter %.a,$^) $(LIBS) $(LIB_FUZZING_ENGINE)
38513853

38523854
fuzz-all: $(FUZZ_PROGRAMS)

oss-fuzz/dummy-cmd-main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "git-compat-util.h"
2+
3+
/*
4+
* When linking the fuzzers, we link against common-main.o to pick up some
5+
* symbols. However, even though we ignore common-main:main(), we still need to
6+
* provide all the symbols it references. In the fuzzers' case, we need to
7+
* provide a dummy cmd_main() for the linker to be happy. It will never be
8+
* executed.
9+
*/
10+
11+
int cmd_main(int argc, const char **argv) {
12+
BUG("We should not execute cmd_main() from a fuzz target");
13+
return 1;
14+
}

0 commit comments

Comments
 (0)