1# Copyright (c) 2019-2021 Yubico AB. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file.
4
5IMAGE		:= libfido2-coverage:1.9.1
6RUNNER		:= libfido2-runner
7PROFDATA	:= llvm-profdata-12
8COV		:= llvm-cov-12
9TARGETS		:= fuzz_assert fuzz_bio fuzz_cred fuzz_credman fuzz_hid \
10		   fuzz_largeblob fuzz_netlink fuzz_mgmt
11CORPORA		:= $(foreach f,${TARGETS},${f}/corpus)
12MINIFY		:= $(foreach f,${TARGETS},/minify/${f}/corpus)
13REMOTE		:= gs://libfido2-corpus.clusterfuzz-external.appspot.com
14.DEFAULT_GOAL	:= all
15
16all: ${TARGETS}
17
18build:
19	docker build -t ${IMAGE} - < Dockerfile
20
21run: build
22	-docker run -it -d --name ${RUNNER} ${IMAGE}
23	docker start ${RUNNER}
24
25sync: run
26	tar Ccf .. - src fuzz | docker exec -i ${RUNNER} tar Cxf /libfido2 -
27	docker exec ${RUNNER} make -C libfido2/build
28
29corpus: sync
30	docker exec ${RUNNER} /bin/sh -c 'cd /libfido2/fuzz && rm -rf ${TARGETS}'
31	docker exec ${RUNNER} tar Czxf /libfido2/fuzz /libfido2/fuzz/corpus.tgz
32
33${TARGETS}: corpus sync
34	docker exec -e LLVM_PROFILE_FILE=/profraw/$@ ${RUNNER} \
35		/bin/sh -c 'rm -f /profraw/$@ && /libfido2/build/fuzz/$@ \
36		-runs=1 /libfido2/fuzz/$@'
37
38${MINIFY}: /minify/%/corpus: %
39	docker exec ${RUNNER} /bin/sh -c 'rm -rf $@ && mkdir -p $@ && \
40		/libfido2/build/fuzz/$< -use_value_profile=1 -merge=1 $@ \
41		/libfido2/fuzz/$</corpus'
42
43corpus.tgz-: ${MINIFY}
44	docker exec -i ${RUNNER} tar Czcf /minify - ${TARGETS} > $@
45
46profdata: run
47	docker exec ${RUNNER} /bin/sh -c 'rm -f /$@ && ${PROFDATA} \
48		merge -sparse profraw/* -o $@'
49
50report.tgz: profdata
51	docker exec ${RUNNER} /bin/sh -c 'rm -rf /report && mkdir /report && \
52		${COV} show -format=html -tab-size=8 -instr-profile=/$< \
53		--show-branch-summary=false -output-dir=/report \
54		/libfido2/build/src/libfido2.so'
55	docker exec -i ${RUNNER} tar Czcf / - report > $@
56
57summary.txt: profdata
58	docker exec ${RUNNER} ${COV} report -use-color=false \
59		--show-branch-summary=false /libfido2/build/src/libfido2.so \
60		-instr-profile=/$< > $@
61
62functions.txt: profdata
63	docker exec ${RUNNER} /bin/sh -c '${COV} report -use-color=false \
64		-show-functions --show-branch-summary=false -instr-profile=/$< \
65		/libfido2/build/src/libfido2.so /libfido2/src/*.[ch]' > $@
66
67clean: run
68	docker exec ${RUNNER} /bin/sh -c 'rm -rf /profraw /profdata && \
69		make -C /libfido2/build clean'
70	-docker stop ${RUNNER}
71	rm -rf ${TARGETS}
72
73${CORPORA}:
74	-mkdir -p $@
75	gsutil -q -m rsync -d -r ${REMOTE}/libFuzzer/libfido2_$(@:/corpus=) $@
76
77corpus.tgz: ${CORPORA}
78	tar zcf $@ ${TARGETS}
79
80.PHONY: build run sync corpus ${TARGETS} ${CORPORA}
81.PHONY: report.tgz summary.txt functions.txt
82