Repository navigation
Expand file tree
/
Copy pathMakefile.fuzz.am
More file actions
200 lines (177 loc) · 8.86 KB
/
Copy pathMakefile.fuzz.am
File metadata and controls
200 lines (177 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# Makefile.fuzz.am - LLVM libFuzzer target definitions.
#
# Included from the top-level Makefile.am rather than inlined there, since
# this content is self-contained and is only built when configured with
# --enable-fuzzing. automake processes an included file exactly as if its
# text were pasted in place, so variables, conditionals and rules here behave
# identically to being written directly in Makefile.am.
#
# All fuzz binaries link the same module set as grub-fstest (libgrubmods.a +
# libgrubkern.a + libgrubgcry.a) and reuse the generated grub_fstest_init.c,
# which calls every GRUB_MOD_INIT so that the filesystems, partition maps and
# other parsers are registered.
#
# grub_malloc()/grub_free() wrap the libc allocator in the EMU build, so the
# sanitizers observe every GRUB allocation with no further shims.
if COND_FUZZING
# Helper variables. The names deliberately avoid automake's per-target
# suffixes (_LDADD, _LDFLAGS, _CPPFLAGS) so that automake does not read them
# as belonging to a program called "fuzz".
fuzz_libs = \
libgrubmods.a \
libgrubkern.a \
libgrubgcry.a \
grub-core/lib/gnulib/libgnu.a \
$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)
# 'make all' builds libgnu.a through SUBDIRS but a direct 'make fuzz-fs-ext2'
# does not; gnulib's 'all' also generates the headers that libgnu.a needs.
grub-core/lib/gnulib/libgnu.a:
$(MAKE) $(AM_MAKEFLAGS) -C grub-core/lib/gnulib all
fuzz_defs = $(AM_CPPFLAGS) $(CPPFLAGS_PROGRAM) -DFUZZING
# The two --wrap options redirect GRUB's error and debug output to the no-op
# definitions in fuzz/grub_fuzzing_platform.c. A fuzz target would otherwise
# spend much of its time formatting messages nobody reads.
fuzz_lflags = $(AM_LDFLAGS) $(LDFLAGS_PROGRAM) $(FUZZING_LDFLAGS) \
-Wl,--wrap=grub_err_printf \
-Wl,--wrap=grub_real_dprintf \
$(coverage_ldflags)
# -fsanitize=function (pulled in by -fsanitize=undefined) is unsound for
# GRUB's fshelp design: a filesystem calls callbacks such as get_block()
# through a function pointer whose grub_fshelp_node argument is an opaque
# struct in fshelp.c but a complete, filesystem-specific struct in the module.
# That is valid C, but the check reports it as a call "through pointer to
# incorrect function type". Disable just that sub-sanitizer. It is appended
# after the -fsanitize flags carried in AM_CFLAGS and is harmless in
# non-sanitized builds.
no_function_san = -fno-sanitize=function
# ── Optional LLVM source-based coverage ──────────────────────────────────────
# Set COVERAGE=1 to additionally instrument the fuzz binary and the libraries
# it links with LLVM source-based coverage, so that the file actually under
# test (ext2.c, say) shows up in a report rather than just the harness.
#
# This is separate from the coverage instrumentation libFuzzer uses to guide
# its own exploration, which is always on.
#
# The flags change how every object is compiled, so run 'make clean' when
# turning COVERAGE on or off.
COVERAGE =
COVERAGE_DIR = fuzz/coverage
LLVM_PROFDATA = llvm-profdata
LLVM_COV = llvm-cov
coverage_cflags = $(if $(COVERAGE),-fprofile-instr-generate -fcoverage-mapping)
coverage_ldflags = $(if $(COVERAGE),-fprofile-instr-generate)
fuzz_cflags = $(AM_CFLAGS) $(coverage_cflags) $(no_function_san)
# The code under test lives in these libraries, so they need the same
# treatment.
libgrubmods_a_CFLAGS += $(coverage_cflags) $(no_function_san)
libgrubkern_a_CFLAGS += $(coverage_cflags) $(no_function_san)
libgrubgcry_a_CFLAGS += $(coverage_cflags) $(no_function_san)
# Sources compiled into every fuzz binary: the platform shim plus the EMU
# glue that lets GRUB read from the host filesystem.
fuzz_common_srcs = \
fuzz/grub_fuzzing_platform.c \
grub-core/disk/host.c \
grub-core/kern/emu/hostfs.c
# ── Fuzzer run targets ───────────────────────────────────────────────────────
# Build the binary, then run it against its corpus directory. A fuzzer runs
# until interrupted; extra libFuzzer flags may be passed via FUZZ_FLAGS:
#
# make fuzz-fs-ext2
# make fuzz-fs-ext2 FUZZ_FLAGS="-runs=100000"
# make fuzz-fs-ext2 FUZZ_FLAGS="-max_total_time=300 -jobs=4"
#
# The corpus directory starts empty, which works but makes the fuzzer spend a
# long time rediscovering the file format. Seeding it with a few small,
# valid images of the format under test is strongly recommended; see the
# fuzz-get-corpus target below.
FUZZ_FLAGS =
# ── Seed corpus ──────────────────────────────────────────────────────────────
# The seed corpus is kept in a separate repository so that this one does not
# have to carry a large and continually growing collection of binary inputs.
#
# Fetching it is opt-in. Nothing depends on this rule, so building or running
# a fuzzer never touches the network; ask for the corpus explicitly with:
#
# make fuzz-get-corpus
#
# Point it elsewhere, e.g. at a local mirror or a work-in-progress branch, by
# overriding either variable:
#
# make fuzz-get-corpus FUZZ_CORPUS_REPO=/path/to/clone
# make fuzz-get-corpus FUZZ_CORPUS_BRANCH=my-branch
#
# The corpus is merged into fuzz/corpus/ rather than replacing it, so inputs
# a local run has discovered are kept.
FUZZ_CORPUS_REPO ?= https://gitlab.freedesktop.org/gnu-grub/grub-fuzz-corpus.git
FUZZ_CORPUS_BRANCH ?= main
.PHONY: fuzz-get-corpus
fuzz-get-corpus:
@echo "Fetching fuzz corpus from $(FUZZ_CORPUS_REPO) (branch $(FUZZ_CORPUS_BRANCH))"
$(AM_V_GEN)set -e; \
tmpdir=`mktemp -d`; \
trap 'rm -rf "$$tmpdir"' EXIT; \
git clone --depth=1 --filter=blob:none --no-checkout \
-b $(FUZZ_CORPUS_BRANCH) $(FUZZ_CORPUS_REPO) $$tmpdir; \
git -C $$tmpdir sparse-checkout set corpus; \
git -C $$tmpdir checkout; \
mkdir -p fuzz/corpus; \
cp -r $$tmpdir/corpus/. fuzz/corpus/
noinst_PROGRAMS += fuzz_fs_ext2
fuzz_fs_ext2_SOURCES = fuzz/fuzz_fs_ext2.c $(fuzz_common_srcs)
nodist_fuzz_fs_ext2_SOURCES = grub_fstest_init.c
fuzz_fs_ext2_CFLAGS = $(fuzz_cflags)
fuzz_fs_ext2_LDADD = $(fuzz_libs)
fuzz_fs_ext2_CPPFLAGS = $(fuzz_defs)
fuzz_fs_ext2_LDFLAGS = $(fuzz_lflags)
# With COVERAGE=1 the run writes an LLVM .profraw file per process into
# $(COVERAGE_DIR); %p expands to the pid so parallel runs do not collide.
ext2_prof = $(if $(COVERAGE),LLVM_PROFILE_FILE="$(COVERAGE_DIR)/ext2-%p.profraw")
.PHONY: fuzz-fs-ext2
fuzz-fs-ext2: fuzz_fs_ext2
@mkdir -p fuzz/corpus/fs-ext2 $(if $(COVERAGE),$(COVERAGE_DIR))
$(ext2_prof) ./fuzz_fs_ext2 fuzz/corpus/fs-ext2/ -max_len=65536 \
-dict=$(srcdir)/fuzz/dict/ext2.dict $(FUZZ_FLAGS)
# ── Coverage reports ─────────────────────────────────────────────────────────
# Both targets expect the fuzzer to have been built and run with COVERAGE=1:
#
# make clean
# make fuzz-fs-ext2 COVERAGE=1 FUZZ_FLAGS="-runs=0" # replay the corpus
# make coverage # text, to stdout
# make coverage-html # HTML report
#
# -runs=0 replays the existing corpus without generating new inputs, which is
# usually what you want when measuring what the corpus reaches.
#
# Third-party sources carried in-tree are filtered out; they are not what the
# fuzzer is measuring.
coverage_ignore_re = grub-core/lib/(gnulib|libgcrypt-grub|libgcrypt_wrap|minilzo|xzembed|zstd)
.PHONY: coverage coverage-html
coverage:
@ls $(COVERAGE_DIR)/*.profraw >/dev/null 2>&1 || \
{ echo "No .profraw files in $(COVERAGE_DIR)/; run a fuzzer with COVERAGE=1 first."; exit 1; }
$(LLVM_PROFDATA) merge -sparse $(COVERAGE_DIR)/*.profraw \
-o $(COVERAGE_DIR)/coverage.profdata
$(LLVM_COV) report ./fuzz_fs_ext2 \
-instr-profile=$(COVERAGE_DIR)/coverage.profdata \
--ignore-filename-regex='$(coverage_ignore_re)'
coverage-html:
@ls $(COVERAGE_DIR)/*.profraw >/dev/null 2>&1 || \
{ echo "No .profraw files in $(COVERAGE_DIR)/; run a fuzzer with COVERAGE=1 first."; exit 1; }
$(LLVM_PROFDATA) merge -sparse $(COVERAGE_DIR)/*.profraw \
-o $(COVERAGE_DIR)/coverage.profdata
$(LLVM_COV) show ./fuzz_fs_ext2 \
-instr-profile=$(COVERAGE_DIR)/coverage.profdata \
--format=html \
--ignore-filename-regex='$(coverage_ignore_re)' \
--output-dir=$(COVERAGE_DIR)/html
@echo "HTML coverage report: $(COVERAGE_DIR)/html/index.html"
clean-local:
-rm -rf $(COVERAGE_DIR)
endif # COND_FUZZING
# Distributed unconditionally so that a fuzzer can be built from an unpacked
# tarball without the tarball having been created with --enable-fuzzing. The
# .c sources are distributed automatically via _SOURCES, but the header and
# the dictionary must be listed explicitly.
EXTRA_DIST += \
fuzz/dict/ext2.dict \
fuzz/grub_fuzzing_platform.h