forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB3f482f8Tahinli1 month ago3898 commits

kill-matrix.sh152 lines, 7.6 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# kill-matrix.sh: the heavy-compute steps of a batched mutation kill matrix for one crate.
3# Reusable for any crate; defaults target ec-av1. Run it on a server through scripts/remote-run.sh,
4# from the workspace root (remote-run's cwd), with the EC_* environment set by the caller:
5#
6# EC_MEMGUARD_MAX=12G EC_AV1_AOM_DIR=$HOME/fixtures/av1-aom EC_REQUIRE_FIXTURES=1 \
7# EC_TOOL_CACHE_DIR=$HOME/.cache/ec-tool-cache \
8# scripts/remote-run.sh <lane> --host auto -- scripts/kill-matrix.sh <subcommand> [args]
9#
10# Subcommands (CRATE defaults to ec-av1; OUT is a directory under the workspace):
11# list <OUT> mutants of $CRATE as JSON (cargo mutants --list --json)
12# cover <TESTS> <OUT> per-test region coverage: one instrumented run per test; TESTS
13# lines are "<ordinal>\t<name>"; writes OUT/<ordinal>.json (compact)
14# fastcov <OUT> coverage of the fast profile (the "fast suite")
15# mutrun <SAMPLE.json> <IDXFILE> <OUT> fast|heavy
16# apply each mutant index in IDX (one per line), run the fast
17# profile or the given heavy test list, record the failing tests
18# in OUT/<index>.fail, and revert the tree after each mutant.
19#
20# A mutant with a compile error is recorded as "unviable" and is not a kill or a survivor.
21set -euo pipefail
22
23CRATE=${CRATE:-ec-av1}
24ROOT=$(pwd)
25TMP=${TMPDIR:-$HOME/tmp}
26mkdir -p "$TMP"
27export EC_MEMGUARD_MAX=${EC_MEMGUARD_MAX:-12G}
28export EC_REQUIRE_FIXTURES=${EC_REQUIRE_FIXTURES:-1}
29export EC_AV1_AOM_DIR=${EC_AV1_AOM_DIR:-$HOME/fixtures/av1-aom}
30export EC_TOOL_CACHE_DIR=${EC_TOOL_CACHE_DIR:-$HOME/.cache/ec-tool-cache}
31NEXTEST_J=${NEXTEST_J:-4}
32MUTATE="python3 $ROOT/scripts/mutate.py"
33
34sub=${1:?subcommand}
35shift
36
37# Per-test profraw are kept per run; report merges what is on disk.
38llvm_clean() { cargo llvm-cov clean --profraw-only -p "$CRATE" >/dev/null 2>&1 || true; }
39
40# A child process that dies mid-write leaves a truncated .profraw, and llvm-cov report then fails
41# for the whole merge. Delete every profile that llvm-profdata cannot read, and log the count.
42prune_profraw() {
43 local pd f n=0
44 pd=$(rustc --print sysroot)/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-profdata
45 local dir="${CARGO_TARGET_DIR:-target}/llvm-cov-target"
46 while IFS= read -r f; do
47 if ! "$pd" show "$f" >/dev/null 2>&1; then
48 rm -f "$f"
49 n=$((n + 1))
50 fi
51 done < <(find "$dir" -name '*.profraw' 2>/dev/null)
52 echo "prune_profraw: deleted $n unreadable profraw under $dir" >&2
53}
54
55case $sub in
56list)
57 out=$1; mkdir -p "$out"
58 (cd "crates/$CRATE" && cargo mutants -p "$CRATE" --list --json) > "$out/mutants.json"
59 echo "mutants: $(python3 -c "import json,sys;print(len(json.load(open('$out/mutants.json'))))")"
60 ;;
61cover)
62 tests=$1; out=$2; mkdir -p "$out"
63 # TESTS lines: "<ordinal><TAB><test name>". The ordinal names the output files, so shards
64 # run on different hosts can be merged by ordinal.
65 while IFS=$'\t' read -r i name; do
66 [ -n "$name" ] || continue
67 llvm_clean
68 # A failing test still leaves its profile behind; the coverage is what matters here.
69 cargo llvm-cov nextest --no-report --no-fail-fast -p "$CRATE" -j "$NEXTEST_J" \
70 -E "test(=$name)" > "$out/$i.log" 2>&1 || {
71 # Coverage of a failing run is still reported, but it is not a clean baseline.
72 echo "cover: test $i exited non-zero: $name" >&2
73 printf '%s\n' "$name" >> "$out/failed.txt"
74 }
75 prune_profraw
76 # A missing report is not fatal: the test is recorded as "nocov" and the analysis keeps it.
77 if cargo llvm-cov report --json --output-path "$TMP/cov-$i.json" >/dev/null 2>&1 \
78 && python3 scripts/kill-matrix-analyze.py compact "$TMP/cov-$i.json" "$out/$i.json"; then
79 printf '%s\t%s\n' "$i" "$name" >> "$out/index.tsv"
80 else
81 echo "cover: no coverage report for test $i: $name" >&2
82 printf '%s\n' "$name" >> "$out/nocov.txt"
83 printf '%s\t%s\n' "$i" "$name" >> "$out/index.tsv"
84 fi
85 rm -f "$TMP/cov-$i.json"
86 done < "$tests"
87 ;;
88fastcov)
89 out=$1; mkdir -p "$out"
90 llvm_clean
91 cargo llvm-cov nextest --no-report --no-fail-fast -p "$CRATE" --profile fast -j "$NEXTEST_J" > "$out/fast.log" 2>&1 || true
92 prune_profraw
93 if ! { cargo llvm-cov report --json --output-path "$TMP/fast-cov.json" >/dev/null 2>&1 \
94 && python3 scripts/kill-matrix-analyze.py compact "$TMP/fast-cov.json" "$out/fast.json"; }; then
95 echo "fastcov: no coverage report; see $out/fast.log" >&2
96 fi
97 rm -f "$TMP/fast-cov.json"
98 ;;
99mutrun)
100 # cargo-mutants applies, builds and runs each sampled mutant itself. Each one is selected by its
101 # exact name (-F/--re over the names `--list` shows), so no span is applied by this script.
102 json=$1; idxfile=$2; out=$3; mode=$4
103 mkdir -p "$out"
104 # idxfile lines: "<index>" (fast) or "<index><TAB><tests file>" (heavy, one list per mutant).
105 while IFS=$'\t' read -r idx tests; do
106 [ -n "$idx" ] || continue
107 re=$(python3 scripts/kill-matrix-analyze.py regex "$json" "$idx")
108 if [ "$mode" = fast ]; then
109 sel=(--cargo-test-arg=--profile --cargo-test-arg=fast)
110 else
111 expr=""
112 while IFS= read -r name; do
113 [ -n "$name" ] || continue
114 expr+="${expr:+ | }test(=$name)"
115 done < "$tests"
116 # --no-fail-fast: every covering heavy test runs, so the kill set is complete.
117 sel=(--cargo-test-arg=--no-fail-fast --cargo-test-arg=-E "--cargo-test-arg=$expr")
118 fi
119 # Heavy runs get a hard per-mutant timeout (a hang counts as caught); fast runs keep the default.
120 tmo=(--timeout-multiplier 3)
121 [ "$mode" = fast ] || tmo=(--timeout "${HEAVY_TIMEOUT:-1200}")
122 rm -rf "$out/$idx.d"
123 set +e
124 cargo mutants -p "$CRATE" --test-tool nextest --baseline skip "${tmo[@]}" \
125 --re "$re" "${sel[@]}" --output "$out/$idx.d" > "$out/$idx.log" 2>&1
126 rc=$?
127 set -e
128 mo="$out/$idx.d/mutants.out"
129 if [ -f "$mo/caught.txt" ] && grep -qF "$(python3 scripts/kill-matrix-analyze.py name "$json" "$idx")" "$mo/caught.txt"; then
130 echo fail > "$out/$idx.status" # killed by the selected tests
131 elif [ -f "$mo/timeout.txt" ] && grep -qF "$(python3 scripts/kill-matrix-analyze.py name "$json" "$idx")" "$mo/timeout.txt"; then
132 echo fail > "$out/$idx.status"
133 elif [ -f "$mo/unviable.txt" ] && grep -qF "$(python3 scripts/kill-matrix-analyze.py name "$json" "$idx")" "$mo/unviable.txt"; then
134 echo unviable > "$out/$idx.status"
135 elif [ -f "$mo/missed.txt" ] && grep -qF "$(python3 scripts/kill-matrix-analyze.py name "$json" "$idx")" "$mo/missed.txt"; then
136 echo pass > "$out/$idx.status" # survived the selected tests
137 else
138 echo "error rc=$rc" > "$out/$idx.status"
139 fi
140 # Validity evidence per mutant: which test binaries ran, and rustc vs runner errors.
141 python3 scripts/kill-matrix-analyze.py evidence "$out/$idx.d" > "$out/$idx.evidence" 2>/dev/null || true
142 if [ "$mode" = heavy ] && [ "$(cat "$out/$idx.status")" = fail ]; then
143 python3 scripts/kill-matrix-analyze.py killers "$out/$idx.d" > "$out/$idx.fail"
144 fi
145 echo "mutrun: $mode mutant $idx status=$(cat "$out/$idx.status")" >&2
146 done < "$idxfile"
147 ;;
148*)
149 sed -n '2,20p' "$0" >&2
150 exit 2
151 ;;
152esac