Repository navigation
Expand file tree
/
Copy pathcov-analysis
More file actions
executable file
·5535 lines (5045 loc) · 215 KB
/
Copy pathcov-analysis
File metadata and controls
executable file
·5535 lines (5045 loc) · 215 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# File: cov-analysis
#
# Version: 1.3-dev
#
# Purpose: Generate LLVM source-based code coverage reports from AFL++ corpus.
# Replays queue/crash/hang files through a coverage-instrumented
# binary, merges .profraw profiles, and produces HTML/text/JSON
# reports via llvm-profdata and llvm-cov.
#
# Copyright (c) 2026 by Marc "vanHauser" Heuse (vh@thc.org)
#
# License: GNU Affero General Public License 3 (or any later version)
#
set -euo pipefail
VERSION="1.3-dev"
REPORT_MARKER_NAME=".cov-analysis-report"
# ── helpers ───────────────────────────────────────────────────────────────────
QUIET=0
VERBOSE=0
FUZZER_LAYOUT="" # set by cmd_report; one of: afl | flat
log() { test "$QUIET" -eq 0 && echo "[+] $*" || true; }
logv() { test "$VERBOSE" -eq 1 && echo "[*] $*" || true; }
err() { echo "[-] $*" >&2; }
# A finding that is not an error: the run continues and publishes.
logw() { test "$QUIET" -eq 0 && echo "[!] $*" >&2 || true; }
# Like log() but writes to stderr. Used by commands (e.g. search) whose stdout
# must stay machine-readable so it can be piped.
loge() { test "$QUIET" -eq 0 && echo "[+] $*" >&2 || true; }
logerr() { test "$QUIET" -eq 0 && echo "[-] $*" >&2 || true; }
# Use TMPDIR when provided, otherwise fallback to /tmp default.
tmp_base_dir() {
printf '%s' "${TMPDIR:-/tmp}"
}
# Identify the running copy, not just its version string. Two installations can
# carry the same VERSION and differ by months; the content hash and the path
# settle which one actually ran.
version_string() {
local self hash="unknown"
self="${BASH_SOURCE[0]}"
self="$(realpath -e -- "$self" 2>/dev/null || printf '%s' "${BASH_SOURCE[0]}")"
if test -f "$self" && command -v sha256sum >/dev/null 2>&1; then
hash="$(sha256sum -- "$self" 2>/dev/null | cut -c1-12)"
test -n "$hash" || hash="unknown"
fi
printf 'cov-analysis-%s (sha256:%s, %s)\n' "$VERSION" "$hash" "$self"
}
require_command() {
local cmd="$1" purpose="$2"
if ! command -v "$cmd" >/dev/null 2>&1; then
err "$purpose requires '$cmd', but it was not found on PATH."
return 1
fi
}
require_gnu_command() {
local cmd="$1" package="$2" purpose="$3" version
require_command "$cmd" "$purpose" || return 1
version="$("$cmd" --version 2>/dev/null || true)"
case "$version" in
*GNU*) ;;
*) err "$purpose requires GNU $cmd from $package."; return 1 ;;
esac
}
require_coreutils_command() {
local cmd="$1" purpose="$2" version
require_command "$cmd" "$purpose" || return 1
version="$("$cmd" --version 2>/dev/null || true)"
case "$version" in
*"GNU coreutils"*|*"uutils coreutils"*) ;;
*) err "$purpose requires '$cmd' from GNU coreutils or uutils coreutils."; return 1 ;;
esac
}
require_replay_commands() {
local purpose="$1"
require_gnu_command find "GNU findutils" "$purpose" || return 1
require_gnu_command xargs "GNU findutils" "$purpose" || return 1
require_coreutils_command timeout "$purpose" || return 1
local cmd
for cmd in mktemp realpath sort tr wc; do
require_command "$cmd" "$purpose" || return 1
done
}
# ── queue replay deadline ─────────────────────────────────────────────────────
# The coverage binary is not the fuzzed binary: no forkserver, -O1 with
# counters and a profile write per exec. So the fuzzer's own slowest execution
# is rounded up to a whole second and multiplied for headroom.
QUEUE_TIMEOUT_FACTOR=5
QUEUE_TIMEOUT_MIN=5
QUEUE_TIMEOUT_FALLBACK=60
BATCH_SIZE_DEFAULT=128
BATCH_DEADLINE_INPUTS=10
# Print "<max slowest_exec_ms> <instances>" over every fuzzer_stats an AFL++
# output directory holds (one per parallel instance). exec_timeout stands in
# where slowest_exec_ms is missing or zero. Returns 1 when there is no stats
# file to read.
afl_exec_ms() {
local dir="$1" p
local -a stats=()
test -f "$dir/fuzzer_stats" && stats+=("$dir/fuzzer_stats")
shopt -s nullglob
for p in "$dir"/*/fuzzer_stats; do stats+=("$p"); done
shopt -u nullglob
test "${#stats[@]}" -gt 0 || return 1
awk '
FNR == 1 { files++ }
$1 == "slowest_exec_ms" { slow[FILENAME] = $3 + 0 }
$1 == "exec_timeout" { tmout[FILENAME] = $3 + 0 }
END {
for (f in slow) {
v = slow[f]
if (v == 0 && f in tmout) v = tmout[f]
if (v > max) max = v
}
for (f in tmout) if (!(f in slow) && tmout[f] > max) max = tmout[f]
printf "%d %d\n", max + 0, files + 0
}
' "${stats[@]}"
}
# Print "<seconds> <ms> <instances>" for the queue replay deadline of an input
# directory. Falls back to a fixed default for layouts that carry no AFL++
# statistics, and for a stats file that holds no usable number.
derive_queue_timeout() {
local dir="$1" layout="$2" ms=0 instances=0 secs
if test "$layout" = "afl"; then
read -r ms instances < <(afl_exec_ms "$dir" || printf '0 0\n')
fi
if test "${ms:-0}" -gt 0; then
secs=$(( ((ms + 999) / 1000) * QUEUE_TIMEOUT_FACTOR ))
test "$secs" -lt "$QUEUE_TIMEOUT_MIN" && secs="$QUEUE_TIMEOUT_MIN"
else
secs="$QUEUE_TIMEOUT_FALLBACK"
ms=0
fi
printf '%d %d %d\n' "$secs" "$ms" "${instances:-0}"
}
run_with_timeout() {
local seconds="$1" rc=0 pid restore prev="${!-}"
shift
restore="$(trap -p INT TERM HUP)"
trap 'test "${!-}" = "$prev" || kill -KILL -- "-$!" "$!" 2>/dev/null; exit 130' INT
trap 'test "${!-}" = "$prev" || kill -KILL -- "-$!" "$!" 2>/dev/null; exit 143' TERM
trap 'test "${!-}" = "$prev" || kill -KILL -- "-$!" "$!" 2>/dev/null; exit 129' HUP
timeout --signal=TERM --kill-after=1s "${seconds}s" "$@" <&0 &
pid=$!
wait "$pid" || rc=$?
trap - INT TERM HUP
eval "$restore"
kill -KILL -- "-$pid" 2>/dev/null || true
return "$rc"
}
command_for_input() {
local cmd="$1" input="$2" quoted
printf -v quoted '%q' "$input"
printf '%s%s%s' "${cmd%%@@*}" "$quoted" "${cmd#*@@}"
}
run_input_command() {
local seconds="$1" cmd="$2" input="$3" rendered
case "$cmd" in
*@@*)
rendered="$(command_for_input "$cmd" "$input")"
run_with_timeout "$seconds" bash -c "$rendered"
;;
*)
run_with_timeout "$seconds" bash -c "$cmd" < "$input"
;;
esac
}
run_input_command_unbounded() {
local cmd="$1" input="$2" rendered
case "$cmd" in
*@@*)
rendered="$(command_for_input "$cmd" "$input")"
bash -c "$rendered"
;;
*)
bash -c "$cmd" < "$input"
;;
esac
}
number_inputs() {
local n=0 p
while IFS= read -r -d '' p; do
n=$((n + 1))
printf '%d\t%s\0' "$n" "$p"
done
}
replay_one_input() {
local cmd="$1" seconds="$2" dir="$3" record="$4" idx path status
idx="${record%%$'\t'*}"
path="${record#*$'\t'}"
export LLVM_PROFILE_FILE="$dir/${idx}-%p.profraw"
if test "$seconds" -gt 0; then
run_input_command "$seconds" "$cmd" "$path" >/dev/null 2>&1
else
run_input_command_unbounded "$cmd" "$path" >/dev/null 2>&1
fi
status=$?
record_slow_input "$status" "$path"
printf '%d\n' "$status"
}
replay_one_input_repeated() {
local record="$4"
export COV_REPEAT_TAG="${record%%$'\t'*}"
replay_one_input "$@"
}
# Replay one batch of inputs in a single process that profiles into a directory
# of its own. The outer deadline is the per-input deadline times the batch size,
# but at most <cap> per-input deadlines unless <cap> is 0. A driver binary gets
# no cap: it bounds each input with its own alarm (COV_INPUT_TIMEOUT), so the
# outer deadline only has to allow for every input of a slow but healthy batch.
# A batch that exits non-zero or is killed is discarded, counted in
# COV_BATCH_FALLBACK_LOG, and its inputs are replayed one per process, so a
# crashing or hanging input costs no other input its coverage.
replay_batch() {
local cmd="$1" seconds="$2" cap="$3" dir="$4" bdir status n=0 path deadline
shift 4
if ! bdir="$(mktemp -d "$dir/b.XXXXXX")"; then
printf '1 %d\n' "$#"
return 0
fi
export LLVM_PROFILE_FILE="$bdir/%p.profraw"
if test "$seconds" -gt 0; then
deadline="$#"
if test "$cap" -gt 0 && test "$deadline" -gt "$cap"; then
deadline="$cap"
fi
run_with_timeout "$((deadline * seconds))" \
bash -c "$cmd \"\$@\"" _ "$@" >/dev/null 2>&1
else
bash -c "$cmd \"\$@\"" _ "$@" >/dev/null 2>&1
fi
status=$?
if test "$status" -eq 0; then
printf '0 %d\n' "$#"
return 0
fi
test -z "${COV_BATCH_FALLBACK_LOG:-}" || printf '%d\n' "$#" >> "$COV_BATCH_FALLBACK_LOG"
rm -rf -- "$bdir"
if ! mkdir -- "$bdir"; then
printf '%d %d\n' "$status" "$#"
return 0
fi
for path in "$@"; do
n=$((n + 1))
replay_one_input "$cmd @@" "$seconds" "$bdir" "$n"$'\t'"$path"
done
}
# Name an input that hit its deadline. The driver appends to the same file when
# its own per-input alarm fires; lines are far below PIPE_BUF, so the O_APPEND
# writes of parallel workers do not interleave.
record_slow_input() {
local status="$1" path="$2"
test -n "${COV_TIMEOUT_LOG:-}" || return 0
case "$status" in
124|137) printf '%s\n' "$path" >> "$COV_TIMEOUT_LOG" ;;
esac
return 0
}
resolve_input_dir() {
local dir="$1" resolved
if ! resolved="$(realpath -e -- "$dir" 2>/dev/null)"; then
err "Could not resolve input directory: $dir"
return 1
fi
printf '%s\n' "$resolved"
}
# Sum one status record per replayed input into "TOTAL OK FAILED KILLED
# UNREADABLE", and report progress while doing it. Every record already passes
# through here, so the counter needs no second channel. With <expected> set it
# prints a progress line to stderr at most every 2 seconds: a replay that is
# merely slow keeps a rate, a wedged one drops to 0/s. In batch mode one record
# covers a clean batch, so progress advances in batch-sized steps.
replay_tally() {
local driver="$1" expected="${2:-0}" label="${3:-files}"
awk -v driver="$driver" -v expected="$expected" -v label="$label" \
-v quiet="${QUIET:-0}" '
function now( line, parts, t) {
t = 0
if ((getline line < "/proc/uptime") > 0) {
split(line, parts, " ")
t = parts[1] + 0
}
close("/proc/uptime")
return t
}
BEGIN { start = now(); last = start }
{
st = $1 + 0
n = (NF > 1 ? $2 + 0 : 1)
total += n
if (st == 0) ok += n
else if (st == 124 || st == 137) killed += n
else if (st == 2 && driver == 1) unreadable += n
else failed += n
if (quiet + 0 == 0 && expected + 0 > 0) {
t = now()
if (t - last >= 2) {
rate = (t > start) ? total / (t - start) : 0
left = (rate > 0) ? (expected - total) / rate : 0
if (left < 0) left = 0
printf "[+] %d %s: %d done, %.0f/s, ~%ds left\n",
expected, label, total, rate, left > "/dev/stderr"
fflush("/dev/stderr")
last = t
}
}
}
END { printf "%d %d %d %d %d\n", total+0, ok+0, failed+0, killed+0, unreadable+0 }
'
}
write_profile_manifest() {
local dir="$1" manifest="$2"
find "$dir" -type f -name '*.profraw' -print | sort > "$manifest"
}
_stab_macro_sites() {
local profdata="$1"
"$LLVM_COV" export "$COV_BINARY" --format=text "-instr-profile=$profdata" 2>/dev/null \
| python3 -c '
import json
import sys
try:
data = json.load(sys.stdin)
except Exception:
raise SystemExit(0)
sites = {}
for obj in data.get("data", []) or []:
for entry in obj.get("files", []) or []:
for exp in entry.get("expansions", []) or []:
names = exp.get("filenames") or []
src = exp.get("source_region") or []
if len(src) < 7:
continue
efid = src[6]
if not isinstance(efid, int) or efid < 0 or efid >= len(names):
continue
target = names[efid]
lines = set()
for region in exp.get("target_regions") or []:
if len(region) >= 6 and region[5] == efid:
lines.add(region[0])
for line in lines:
key = (target, line)
sites[key] = sites.get(key, 0) + 1
for (path, line), count in sorted(sites.items()):
print("%s:%d\t%d" % (path, line, count))
'
}
write_gap_inventory() {
local perfunc="$1" out="$2" verdicts="${3-}" tmpd rc=0
local have_v=0
test -n "$verdicts" && test -s "$verdicts" && have_v=1
tmpd="$(mktemp -d "$(tmp_base_dir)/cov-analysis-gaps.XXXXXX")" || return 1
if test -s "$perfunc"; then
awk -v verdicts="$verdicts" -v have_v="$have_v" \
-v ffile="$tmpd/files.tsv" -v gfile="$tmpd/funcs.tsv" '
BEGIN {
if (have_v == 1) {
while ((getline line < verdicts) > 0) {
n = split(line, v, "\t")
if (n >= 3) vstate[v[2] "\t" v[3]] = v[1]
}
close(verdicts)
}
}
substr($0, 1, 6) == "File \047" {
f = substr($0, 7)
sub(/\047:[ \t]*$/, "", f)
cur = f
racc = 0
next
}
{
if (cur == "" || NF < 10) next
if ($2 !~ /^[0-9]+$/ || $3 !~ /^[0-9]+$/) next
if ($5 !~ /^[0-9]+$/ || $6 !~ /^[0-9]+$/) next
regions = $2 + 0; rmiss = $3 + 0; rcover = $4
lines = $5 + 0; lmiss = $6 + 0
if ($1 == "TOTAL") {
key = (have_v == 1) ? racc : rmiss
print key "\t" rmiss "\t" racc "\t" lmiss "\t" regions "\t" rcover "\t" cur > ffile
next
}
st = "none"
if (have_v == 1 && ((cur "\t" $1) in vstate)) st = vstate[cur "\t" $1]
if (st != "unreachable") racc += rmiss
if (regions > 0 && rmiss == regions) {
print rmiss "\t" lmiss "\t" st "\t" $1 "\t" cur > gfile
}
}
' "$perfunc" || rc=1
fi
: > "$tmpd/files.tsv.sorted"
: > "$tmpd/funcs.tsv.sorted"
test -s "$tmpd/files.tsv" && sort -t "$(printf '\t')" -k1,1nr -k7,7 \
"$tmpd/files.tsv" > "$tmpd/files.tsv.sorted"
test -s "$tmpd/funcs.tsv" && sort -t "$(printf '\t')" -k1,1nr -k4,4 \
"$tmpd/funcs.tsv" > "$tmpd/funcs.tsv.sorted"
{
printf 'Coverage gaps\n'
printf '=============\n\n'
printf 'Ranked by ABSOLUTE uncovered regions, not by percentage: a large file at\n'
printf '4%% holds far more uncovered code than a small file at 0%%. Percentages are\n'
printf 'shown for context only. Numbers come from llvm-cov and match summary.txt.\n\n'
printf '== Files by uncovered regions ==\n\n'
if test "$have_v" -eq 1; then
printf '%10s %10s %10s %10s %8s\n' Uncovered Reachable Uncovered Total Region
printf '%10s %10s %10s %10s %8s %s\n' regions uncovered lines regions cover File
else
printf '%10s %10s %10s %8s\n' Uncovered Uncovered Total Region
printf '%10s %10s %10s %8s %s\n' regions lines regions cover File
fi
if test -s "$tmpd/files.tsv.sorted"; then
awk -F '\t' -v v="$have_v" '
v == 1 { printf "%10s %10s %10s %10s %8s %s\n", $2, $3, $4, $5, $6, $7; next }
{ printf "%10s %10s %10s %8s %s\n", $2, $4, $5, $6, $7 }
' "$tmpd/files.tsv.sorted"
else
printf ' (no per-function coverage data)\n'
fi
if test "$have_v" -eq 1; then
printf '\n== Functions with no coverage: reachable or unclassified (actionable) ==\n\n'
_gap_function_block "$tmpd/funcs.tsv.sorted" actionable
printf '\n== Functions with no coverage: statically unreachable (expected dead) ==\n\n'
_gap_function_block "$tmpd/funcs.tsv.sorted" dead
else
printf '\n== Functions with no coverage ==\n\n'
_gap_function_block "$tmpd/funcs.tsv.sorted" all
fi
} > "$out"
rm -rf -- "$tmpd"
test "$rc" -eq 0 || return 1
test -s "$out"
}
_gap_function_block() {
local sorted="$1" which="$2" body=""
printf '%10s %10s\n' Uncovered Uncovered
printf '%10s %10s %-38s %s\n' regions lines Function File
if test -s "$sorted"; then
body="$(awk -F '\t' -v w="$which" '
w == "actionable" && $3 == "unreachable" { next }
w == "dead" && $3 != "unreachable" { next }
{ printf "%10s %10s %-38s %s\n", $1, $2, $4, $5 }
' "$sorted")"
fi
if test -n "$body"; then
printf '%s\n' "$body"
else
printf ' (none)\n'
fi
}
merge_profiles() {
local tool="$1" dir="$2" output="$3" failure_mode="${4-}" manifest="${5-}"
if test -z "$manifest"; then
manifest="$dir/profiles.manifest"
write_profile_manifest "$dir" "$manifest"
fi
test -s "$manifest" || return 1
if test -n "$failure_mode"; then
"$tool" merge -sparse "--failure-mode=$failure_mode" "--input-files=$manifest" -o "$output"
else
"$tool" merge -sparse "--input-files=$manifest" -o "$output"
fi
}
paired_clang() {
local compiler="$1" direction="$2" base dir paired
base="$(basename -- "$compiler" 2>/dev/null)"
dir="${compiler%/*}"
test "$dir" = "$compiler" && dir=""
if test "$direction" = "cxx" && test "$base" = "clang"; then
paired="clang++"
elif test "$direction" = "cxx" && [[ "$base" =~ ^clang-([0-9]+)$ ]]; then
paired="clang++-${BASH_REMATCH[1]}"
elif test "$direction" = "cc" && test "$base" = "clang++"; then
paired="clang"
elif test "$direction" = "cc" && [[ "$base" =~ ^clang\+\+-([0-9]+)$ ]]; then
paired="clang-${BASH_REMATCH[1]}"
else
return 1
fi
if test -n "$dir"; then
printf '%s/%s\n' "$dir" "$paired"
else
printf '%s\n' "$paired"
fi
}
append_env_flags() {
local name="$1" addition="$2" current="${!1-}"
if test -n "$current"; then
printf -v "$name" '%s %s' "$current" "$addition"
else
printf -v "$name" '%s' "$addition"
fi
export "$name"
}
directory_nonempty() {
test -d "$1" && test -n "$(find "$1" -mindepth 1 -print -quit 2>/dev/null)"
}
valid_legacy_report() {
local dir="$1"
test -s "$dir/coverage.json" \
&& test -s "$dir/coverage.profdata" \
&& test -s "$dir/summary.txt" \
&& test -s "$dir/html/index.html" \
&& test -d "$dir/text"
}
owned_report() {
local marker="$1/$REPORT_MARKER_NAME" first=""
test -f "$marker" && ! test -L "$marker" || return 1
IFS= read -r first < "$marker" || true
test "$first" = "cov-analysis-report-v1"
}
validate_staged_profdata() {
local dir="$1"
owned_report "$dir" || return 1
test -s "$dir/coverage.profdata" || return 1
}
validate_staged_report() {
local dir="$1" reachability="$2"
owned_report "$dir" || return 1
test -s "$dir/coverage.profdata" || return 1
test -s "$dir/coverage.json" || return 1
test -s "$dir/summary.txt" || return 1
test -s "$dir/gaps.txt" || return 1
test -s "$dir/html/index.html" || return 1
test -d "$dir/text" || return 1
grep -q '"data"' "$dir/coverage.json" || return 1
if test -n "$reachability"; then
grep -q 'reach-banner' "$dir/html/index.html" || return 1
grep -q '^Reachable-only coverage' "$dir/summary.txt" || return 1
grep -q '^== Reachability ' "$dir/summary.txt" || return 1
fi
}
_publish_report_unlocked() {
local stage="$1" destination="$2" parent base rollback=""
parent="$(dirname -- "$destination")"
base="$(basename -- "$destination")"
if test -d "$destination" && mv --exchange -T -- "$stage" "$destination" 2>/dev/null; then
if ! rm -rf -- "$stage"; then
err "Report published, but the previous report could not be removed: $stage"
fi
return 0
fi
if test -e "$destination"; then
rollback="$parent/.${base}.cov-analysis.rollback.$$.${RANDOM}"
while test -e "$rollback"; do
rollback="$parent/.${base}.cov-analysis.rollback.$$.${RANDOM}"
done
if ! mv -T -- "$destination" "$rollback"; then
err "Could not move the previous report aside for publication."
return 1
fi
fi
if ! mv -T -- "$stage" "$destination"; then
err "Could not publish the staged report."
if test -n "$rollback"; then
if ! mv -T -- "$rollback" "$destination"; then
err "Could not restore the previous report from: $rollback"
fi
fi
return 1
fi
if test -n "$rollback"; then
if ! rm -rf -- "$rollback"; then
err "Report published, but the previous report could not be removed: $rollback"
fi
fi
return 0
}
publish_report() {
local guard rc=0
if test -z "$COV_REPORT_TOKEN"; then
_publish_report_unlocked "$@"
return $?
fi
guard="$COV_REPORT_CLEAN_LOCK.takeover"
acquire_report_guard "$guard" || return 1
if ! report_lock_owned; then
rmdir -- "$guard" || true
err "Report lock was taken over; refusing to publish from a superseded run."
return 1
fi
_publish_report_unlocked "$@" || rc=$?
rmdir -- "$guard" || rc=1
return "$rc"
}
COV_REPORT_CLEAN_PROFRAW=""
COV_REPORT_CLEAN_STAGE=""
COV_REPORT_CLEAN_LOCK=""
COV_REPORT_TOKEN=""
COV_LOCK_PID=""
COV_LOCK_HOST=""
COV_LOCK_STARTED=""
COV_LOCK_OWNER_TOKEN=""
COV_REMOTE_HOST=""
COV_REMOTE_WORKDIR=""
COV_REMOTE_SSHOPTS=()
cleanup_report_work() {
test -z "$COV_REPORT_CLEAN_PROFRAW" || rm -rf -- "$COV_REPORT_CLEAN_PROFRAW"
test -z "$COV_REPORT_CLEAN_STAGE" || rm -rf -- "$COV_REPORT_CLEAN_STAGE"
if test -n "$COV_REPORT_CLEAN_LOCK" && test -n "$COV_REPORT_TOKEN"; then
local guard="$COV_REPORT_CLEAN_LOCK.takeover"
if acquire_report_guard "$guard"; then
if report_lock_owned; then
rm -rf -- "$COV_REPORT_CLEAN_LOCK"
fi
rmdir -- "$guard" || true
fi
COV_REPORT_CLEAN_LOCK=""
COV_REPORT_TOKEN=""
fi
remote_cleanup
}
remote_cleanup() {
local host="$COV_REMOTE_HOST" rd="$COV_REMOTE_WORKDIR" script
test -n "$host" && test -n "$rd" || return 0
COV_REMOTE_HOST=""
COV_REMOTE_WORKDIR=""
script="rd=$(printf '%q' "$rd")"'
pid=$(cat "$rd/pid" 2>/dev/null)
if test -n "$pid" && tr "\0" " " < "/proc/$pid/cmdline" 2>/dev/null | grep -qF -- "$rd/cov-analysis"; then
kill -TERM -- "-$pid" 2>/dev/null || kill -TERM "$pid" 2>/dev/null
i=0
while kill -0 "$pid" 2>/dev/null && test "$i" -lt 10; do sleep 1; i=$((i + 1)); done
kill -KILL -- "-$pid" 2>/dev/null
fi
rm -rf -- "$rd"'
ssh ${COV_REMOTE_SSHOPTS[@]+"${COV_REMOTE_SSHOPTS[@]}"} -o ConnectTimeout=15 "$host" 'bash -s' \
<<< "$script" >/dev/null 2>&1 || true
}
parse_ssh_opts() {
local opts="$1" o
COV_REMOTE_SSHOPTS=()
test -n "$opts" || return 0
if ! printf '%s\n' "$opts" | xargs -r printf '' >/dev/null 2>&1; then
err "--ssh-opts has an unbalanced quote: $opts"
return 1
fi
while IFS= read -r -d '' o; do
COV_REMOTE_SSHOPTS+=("$o")
done < <(printf '%s\n' "$opts" | xargs -r printf '%s\0')
}
# A signal must still end the run: a trap that only cleans up would let the
# script carry on with its workspace deleted. HUP is in the list because a
# hung-up ssh session otherwise leaves its staging directory behind — bash runs
# no EXIT trap for a signal it does not trap.
install_cleanup_traps() {
trap cleanup_report_work EXIT
trap 'cleanup_report_work; exit 129' HUP
trap 'cleanup_report_work; exit 130' INT
trap 'cleanup_report_work; exit 143' TERM
}
# ── report directory lock ─────────────────────────────────────────────────────
# Two runs against one -o both used to proceed, and the collision only surfaced
# several steps later as a missing staging file. The lock is a directory, built
# complete with its owner file under a temporary name and renamed into place:
# the rename is atomic, fails while another lock exists, and needs no flock
# (--remote ships this script to hosts whose command set is unknown).
report_lock_path() {
printf '%s/.%s.cov-analysis.lock' "$1" "$2"
}
# Print a pid's start time. It only ever increases, so a recycled pid does not
# pass for the original lock holder. Empty when it cannot be read.
pid_start_time() {
local pid="$1" line rest
test -r "/proc/$pid/stat" || return 0
IFS= read -r line < "/proc/$pid/stat" || return 0
rest="${line##*) }"
printf '%s' "$rest" | awk '{ print $20 }'
}
this_host() {
local h="${HOSTNAME:-}"
test -n "$h" || h="$(uname -n 2>/dev/null || true)"
printf '%s' "$h"
}
# Set COV_LOCK_PID / COV_LOCK_HOST from a lock directory. Returns 1 when the
# lock names no owner.
read_lock_owner() {
local dir="$1" key value
COV_LOCK_PID=""; COV_LOCK_HOST=""; COV_LOCK_STARTED=""; COV_LOCK_OWNER_TOKEN=""
test -r "$dir/owner" || return 1
while read -r key value; do
case "$key" in
pid) COV_LOCK_PID="$value" ;;
host) COV_LOCK_HOST="$value" ;;
started) COV_LOCK_STARTED="$value" ;;
token) COV_LOCK_OWNER_TOKEN="$value" ;;
esac
done < "$dir/owner"
test -n "$COV_LOCK_PID"
}
report_lock_owned() {
test -n "$COV_REPORT_CLEAN_LOCK" && test -n "$COV_REPORT_TOKEN" \
&& read_lock_owner "$COV_REPORT_CLEAN_LOCK" \
&& test "$COV_LOCK_OWNER_TOKEN" = "$COV_REPORT_TOKEN"
}
acquire_report_guard() {
local guard="$1" tries=0
until mkdir -- "$guard" 2>/dev/null; do
tries=$((tries + 1))
if test "$tries" -ge 5; then
err "Could not lock the report directory: another run holds $guard."
err "If no other cov-analysis run is starting, remove it with --clean."
return 1
fi
sleep 1
done
}
# Return 0 when the lock is held by a process that is still running. A lock
# whose owner cannot be identified counts as stale: otherwise a half-written
# lock would wedge the directory with no way out but --force.
report_lock_live() {
local dir="$1" current
test -d "$dir" || return 1
read_lock_owner "$dir" || return 1
# A lock taken on another host cannot be checked from here; assume it holds.
if test -n "$COV_LOCK_HOST" && test "$COV_LOCK_HOST" != "$(this_host)"; then
return 0
fi
# /proc sees processes of every user; kill -0 reports another user's process
# as gone (EPERM), which would break a lock that is very much alive.
if test -d /proc/self; then
test -d "/proc/$COV_LOCK_PID" || return 1
else
kill -0 "$COV_LOCK_PID" 2>/dev/null || return 1
fi
if test -n "$COV_LOCK_STARTED"; then
current="$(pid_start_time "$COV_LOCK_PID")"
test -z "$current" || test "$current" = "$COV_LOCK_STARTED" || return 1
fi
return 0
}
report_lock_refused() {
err "Report directory is in use by cov-analysis (pid $COV_LOCK_PID on ${COV_LOCK_HOST:-this host}):"
err " $1"
err "Wait for that run to finish, or pass --force to take the directory over."
}
acquire_report_lock() {
local parent="$1" base="$2" force="${3:-0}" dir new guard token
dir="$(report_lock_path "$parent" "$base")"
if ! new="$(mktemp -d "$dir.new.XXXXXX")"; then
err "Could not lock the report directory: $dir"
return 1
fi
chmod -- "$(umask -S)" "$new" 2>/dev/null || true
token="${new##*.}"
{
printf 'pid %s\n' "$$"
printf 'started %s\n' "$(pid_start_time "$$")"
printf 'host %s\n' "$(this_host)"
printf 'token %s\n' "$token"
} > "$new/owner"
if mv -T -- "$new" "$dir" 2>/dev/null; then
COV_REPORT_CLEAN_LOCK="$dir"
COV_REPORT_TOKEN="$token"
return 0
fi
if test "$force" -ne 1 && report_lock_live "$dir"; then
rm -rf -- "$new"
report_lock_refused "$parent/$base"
return 1
fi
guard="$dir.takeover"
if ! acquire_report_guard "$guard"; then
rm -rf -- "$new"
return 1
fi
if ! mv -T -- "$new" "$dir" 2>/dev/null; then
if report_lock_live "$dir"; then
if test "$force" -ne 1; then
rm -rf -- "$new"
rmdir -- "$guard"
report_lock_refused "$parent/$base"
return 1
fi
logw "Taking over the report directory from pid $COV_LOCK_PID (--force)."
else
logv "Removing a lock left by a run that is no longer alive: $dir"
fi
rm -rf -- "$dir"
if ! mv -T -- "$new" "$dir" 2>/dev/null; then
rm -rf -- "$new"
rmdir -- "$guard"
err "Could not lock the report directory: $dir"
return 1
fi
fi
rmdir -- "$guard"
COV_REPORT_CLEAN_LOCK="$dir"
COV_REPORT_TOKEN="$token"
return 0
}
# Remove the lock and every staging directory of a report that no live run
# holds. Rollback directories are only reported: one holds the previous report
# of a run that died while publishing, and deleting it would destroy it.
clean_report_workspace() {
local parent="$1" base="$2" lock removed=0 p
lock="$(report_lock_path "$parent" "$base")"
if report_lock_live "$lock"; then
err "Report directory is in use by cov-analysis (pid $COV_LOCK_PID); nothing was removed."
return 1
fi
if test -d "$lock"; then
rm -rf -- "$lock" && { log "Removed stale lock: $lock"; removed=$((removed + 1)); }
fi
if test -d "$lock.takeover"; then
rm -rf -- "$lock.takeover" \
&& { log "Removed a stale lock takeover guard: $lock.takeover"; removed=$((removed + 1)); }
fi
shopt -s nullglob
for p in "$lock.new."*; do
rm -rf -- "$p" && { log "Removed a half-built lock: $p"; removed=$((removed + 1)); }
done
for p in "$parent/.$base.cov-analysis.stage."*; do
rm -rf -- "$p" && { log "Removed stale staging directory: $p"; removed=$((removed + 1)); }
done
for p in "$parent/.$base.cov-analysis.rollback."*; do
logw "Left in place: $p"
logw "It holds the previous report of a run that died while publishing."
done
shopt -u nullglob
test "$removed" -gt 0 || log "Nothing to clean for $parent/$base"
return 0
}
# Validate an option's value. Prints error and exits 1 if $2 is missing or
# begins with '-'. Usage: need_arg <opt-name> <value-or-empty>
need_arg() {
local opt="$1" val="${2-}"
if test -z "$val"; then
err "Option $opt requires an argument"
exit 1
fi
case "$val" in
-*) err "Option $opt requires an argument (got '$val' which looks like a flag)"
exit 1 ;;
esac
}
# Echo the LLVM major version that tool selection should match, or nothing.
# LLVM tools must match the clang that produced the .profraw, otherwise
# `llvm-profdata merge` fails with a version mismatch. We derive the version
# from the selected compiler so e.g. CC=clang-22 maps to llvm-profdata-22:
# 1. an explicit version suffix in CC/CXX (clang-22 -> 22)
# 2. the version reported by the selected clang (CC, else plain `clang`)
llvm_version_hint() {
local c base v out
for c in "${CC-}" "${CXX-}"; do
test -n "$c" || continue
base="$(basename -- "$c" 2>/dev/null)"
if [[ "$base" =~ -([0-9]+)$ ]]; then
echo "${BASH_REMATCH[1]}"
return 0
fi
done
c="${CC:-clang}"
command -v "$c" >/dev/null 2>&1 || return 0
out="$("$c" --version 2>/dev/null || true)"
case "$out" in
*'Apple clang'*) return 0 ;;
esac
v="$(printf '%s\n' "$out" | grep -oiE 'clang version [0-9]+' | grep -oE '[0-9]+' | head -n1)"
test -n "$v" && echo "$v"
return 0
}
# Find an LLVM tool, preferring the version matching the selected clang
# (see llvm_version_hint), then the bare name, then versioned (26 down to 11).
find_tool() {
local tool="$1"
local ver hint
hint="$(llvm_version_hint)"
if test -n "$hint" && command -v "${tool}-${hint}" >/dev/null 2>&1; then
echo "${tool}-${hint}"
return 0
fi
if command -v "$tool" >/dev/null 2>&1; then
echo "$tool"
return 0
fi
for ver in 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11; do
if command -v "${tool}-${ver}" >/dev/null 2>&1; then
echo "${tool}-${ver}"
return 0
fi
done
return 1
}
# Extract the instrumented binary path from a coverage command string.
# Skips leading KEY=VALUE env assignments and @@ placeholders.
extract_binary() {
local cmd="$1"
local token candidate="" rc=1
local -a tokens=()
[[ "$cmd" == *$'\n'* || "$cmd" == *$'\r'* ]] && return 1
case "$cmd" in
*[\'\"\\\;\|\&\<\>\(\)\{\}\[\]\$\`\!]* ) return 1 ;;
esac
read -r -a tokens <<< "$cmd"
for token in "${tokens[@]}"; do
if test "$token" = "@@"; then
continue
fi
if [[ "$token" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
test -n "$candidate" && return 1
continue
fi
case "$token" in
*)
if test -z "$candidate"; then
case "$(basename -- "$token" 2>/dev/null)" in
env|command|exec|nice|nohup|setsid|stdbuf|sudo|doas|chroot|timeout|bash|sh|dash|zsh|fish|python|python3|perl|ruby|libtool|valgrind|rr|gdb|qemu-*) return 1 ;;
esac
candidate="$token"
rc=0
elif test -x "$token"; then
return 1
fi
;;
esac
done
test -n "$candidate" && printf '%s\n' "$candidate"
return $rc
}
resolve_coverage_binary() {
local cmd="$1" explicit="$2" resolved=""
if test -n "$explicit"; then
resolved="$explicit"
elif ! resolved="$(extract_binary "$cmd")"; then
err "Could not safely infer the coverage binary from: $cmd"
err "Use --binary <path> for quoted paths, wrappers, or shell syntax."
return 1
fi
if ! test -f "$resolved" || ! test -x "$resolved"; then
err "Coverage binary not found or not executable: $resolved"