xref: /freebsd/contrib/libfido2/fuzz/Makefile (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
1# Copyright (c) 2019-2023 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# SPDX-License-Identifier: BSD-2-Clause
5
6IMAGE		:= libfido2-coverage:1.14.0
7RUNNER		:= libfido2-runner
8PROFDATA	:= llvm-profdata
9COV		:= llvm-cov
10TARGETS		:= fuzz_assert fuzz_bio fuzz_cred fuzz_credman fuzz_hid \
11		   fuzz_largeblob fuzz_netlink fuzz_mgmt fuzz_pcsc
12CORPORA		:= $(foreach f,${TARGETS},${f}/corpus)
13MINIFY		:= $(foreach f,${TARGETS},/minify/${f}/corpus)
14REMOTE		:= gs://libfido2-corpus.clusterfuzz-external.appspot.com
15.DEFAULT_GOAL	:= all
16
17all: ${TARGETS}
18
19build:
20	docker build -t ${IMAGE} - < Dockerfile
21
22run: build
23	-docker run -it -d --name ${RUNNER} ${IMAGE}
24	docker start ${RUNNER}
25
26sync: run
27	tar Ccf .. - src fuzz | docker exec -i ${RUNNER} tar Cxf /libfido2 -
28	docker exec ${RUNNER} make -C /libfido2/build
29
30corpus: sync
31	docker exec ${RUNNER} /bin/sh -c 'cd /libfido2/fuzz && rm -rf ${TARGETS}'
32	docker exec ${RUNNER} tar Czxf /libfido2/fuzz /libfido2/fuzz/corpus.tgz
33
34${TARGETS}: corpus sync
35	docker exec -e LLVM_PROFILE_FILE=/profraw/$@ ${RUNNER} \
36		/bin/sh -c 'rm -f /profraw/$@ && /libfido2/build/fuzz/$@ \
37		-runs=1 /libfido2/fuzz/$@'
38
39${MINIFY}: /minify/%/corpus: %
40	docker exec ${RUNNER} /bin/sh -c 'rm -rf $@ && mkdir -p $@ && \
41		/libfido2/build/fuzz/$< -use_value_profile=1 -merge=1 $@ \
42		/libfido2/fuzz/$</corpus'
43
44corpus.tgz-: ${MINIFY}
45	docker exec -i ${RUNNER} tar Czcf /minify - ${TARGETS} > $@
46
47profdata: run
48	docker exec ${RUNNER} /bin/sh -c 'rm -f /$@ && ${PROFDATA} \
49		merge -sparse /profraw/* -o /$@'
50
51report.tgz: profdata
52	docker exec ${RUNNER} /bin/sh -c 'rm -rf /report && mkdir /report && \
53		${COV} show -format=html -tab-size=8 -instr-profile=/$< \
54		-ignore-filename-regex=pcsclite.h --show-branch-summary=false \
55		-output-dir=/report /libfido2/build/src/libfido2.so'
56	docker exec -i ${RUNNER} tar Czcf / - report > $@
57
58summary.txt: profdata
59	docker exec ${RUNNER} ${COV} report -use-color=false \
60		-ignore-filename-regex=pcsclite.h --show-branch-summary=false \
61		/libfido2/build/src/libfido2.so -instr-profile=/$< > $@
62
63functions.txt: profdata
64	docker exec ${RUNNER} /bin/sh -c '${COV} report -use-color=false \
65		-ignore-filename-regex=pcsclite.h -show-functions \
66		--show-branch-summary=false -instr-profile=/$< \
67		/libfido2/build/src/libfido2.so /libfido2/src/*.[ch]' > $@
68
69clean: run
70	docker exec ${RUNNER} /bin/sh -c 'rm -rf /profraw /profdata && \
71		make -C /libfido2/build clean'
72	-docker stop ${RUNNER}
73	rm -rf ${TARGETS}
74
75${CORPORA}:
76	-mkdir -p $@
77	gsutil -q -m rsync -d -r ${REMOTE}/libFuzzer/libfido2_$(@:/corpus=) $@
78
79fetch-oss-fuzz: ${CORPORA}
80	find ${TARGETS} -type f -size +8192c -print0 | xargs -0 rm
81
82fetch-franz:
83	ssh franz tar -C corpus -cf- . | tar -xf-
84
85corpus.tgz:
86	tar zcf $@ ${TARGETS}
87
88.PHONY: build run sync corpus ${TARGETS} ${CORPORA}
89.PHONY: report.tgz summary.txt functions.txt
90.PHONY: fetch-oss-fuzz fetch-franz corpus.tgz
91