forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB5244564Tahinli6 days ago3954 commits

kill-matrix.sh180 lines, 8.5 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
55# Heavy mode: apply one mutant to this tree (scripts/mutate.py), run the listed heavy tests with
56# --no-fail-fast, and record every failing test in OUT/<idx>.fail. cargo-mutants stops at the first
57# failing test, so it cannot produce the full kill set that the matrix needs.
58# A timeout records every listed test as killing (conservative: it keeps them).
59heavy_one() {
60 local json=$1 idx=$2 tests=$3 out=$4 expr="" name rc
61 while IFS= read -r name; do
62 [ -n "$name" ] || continue
63 expr+="${expr:+ | }test(=$name)"
64 done < "$tests"
65 rm -f "$out/$idx.fail" "$out/$idx.status"
66 if [ -z "$expr" ]; then
67 : > "$out/$idx.fail"; echo pass > "$out/$idx.status"; return 0
68 fi
69 python3 scripts/mutate.py apply "$json" "$idx" "$ROOT" > "$out/$idx.apply.txt" 2>&1
70 set +e
71 timeout 7200 cargo nextest run -p "$CRATE" --profile mut --no-fail-fast -j "$NEXTEST_J" -E "$expr" > "$out/$idx.log" 2>&1
72 rc=$?
73 set -e
74 python3 scripts/mutate.py revert "$ROOT"
75 if [ "$rc" -eq 124 ]; then
76 cp "$tests" "$out/$idx.fail"; echo fail > "$out/$idx.status"
77 echo "heavy_one: mutant $idx timed out; all covering tests recorded" >&2
78 return 0
79 fi
80 if ! grep -q 'Summary' "$out/$idx.log"; then
81 echo unviable > "$out/$idx.status"; return 0
82 fi
83 python3 scripts/kill-matrix-analyze.py failing "$out/$idx.log" "$tests" "$out/$idx.fail"
84 if [ -s "$out/$idx.fail" ]; then echo fail > "$out/$idx.status"; else echo pass > "$out/$idx.status"; fi
85}
86
87# Fast mode: apply one mutant, run the fast profile (fail-fast is fine: any failure is a kill).
88# Status: fail (a test failed, timed out or the test run was killed), pass, unviable (no test run).
89# Baseline-failing test (unmutated fast run fails it), excluded so fail-fast cannot make every mutant "killed".
90FAST_EXCL='not test(/aom_vectors_match_per_frame_md5$/)'
91fast_one() {
92 local json=$1 idx=$2 out=$3 rc
93 rm -f "$out/$idx.status" "$out/$idx.fail"
94 python3 scripts/mutate.py apply "$json" "$idx" "$ROOT" > "$out/$idx.apply.txt" 2>&1
95 set +e
96 timeout 7200 cargo nextest run -p "$CRATE" --profile fast -j "$NEXTEST_J" -E "$FAST_EXCL" > "$out/$idx.log" 2>&1
97 rc=$?
98 set -e
99 python3 scripts/mutate.py revert "$ROOT"
100 if [ "$rc" -eq 124 ]; then echo fail > "$out/$idx.status"; return 0; fi
101 if ! grep -q 'Summary' "$out/$idx.log"; then echo unviable > "$out/$idx.status"; return 0; fi
102 if grep -qE '^\s*(FAIL|TIMEOUT|SIG[A-Z]+|LEAK|ABORT)\b' "$out/$idx.log"; then echo fail > "$out/$idx.status"; else echo pass > "$out/$idx.status"; fi
103}
104
105case $sub in
106fastbase)
107 # Unmutated fast profile, no fail-fast: the baseline. Failing tests are listed in OUT/failing.txt.
108 out=$1; mkdir -p "$out"
109 set +e
110 timeout 7200 cargo nextest run -p "$CRATE" --profile fast --no-fail-fast -j "$NEXTEST_J" > "$out/fast.log" 2>&1
111 echo "rc=$?" > "$out/rc.txt"
112 set -e
113 grep -E '^\s*(FAIL|TIMEOUT|SIG[A-Z]+)\b' "$out/fast.log" | sed 's/\x1b\[[0-9;]*m//g' | sort -u > "$out/failing.txt" || true
114 grep Summary "$out/fast.log" | tail -n 1 > "$out/summary.txt" || true
115 ;;
116list)
117 out=$1; mkdir -p "$out"
118 (cd "crates/$CRATE" && cargo mutants -p "$CRATE" --list --json) > "$out/mutants.json"
119 echo "mutants: $(python3 -c "import json,sys;print(len(json.load(open('$out/mutants.json'))))")"
120 ;;
121cover)
122 tests=$1; out=$2; mkdir -p "$out"
123 # TESTS lines: "<ordinal><TAB><test name>". The ordinal names the output files, so shards
124 # run on different hosts can be merged by ordinal.
125 while IFS=$'\t' read -r i name; do
126 [ -n "$name" ] || continue
127 llvm_clean
128 # A failing test still leaves its profile behind; the coverage is what matters here.
129 cargo llvm-cov nextest --no-report --no-fail-fast -p "$CRATE" -j "$NEXTEST_J" \
130 -E "test(=$name)" > "$out/$i.log" 2>&1 || {
131 # Coverage of a failing run is still reported, but it is not a clean baseline.
132 echo "cover: test $i exited non-zero: $name" >&2
133 printf '%s\n' "$name" >> "$out/failed.txt"
134 }
135 prune_profraw
136 # A missing report is not fatal: the test is recorded as "nocov" and the analysis keeps it.
137 if cargo llvm-cov report --json --output-path "$TMP/cov-$i.json" >/dev/null 2>&1 \
138 && python3 scripts/kill-matrix-analyze.py compact "$TMP/cov-$i.json" "$out/$i.json"; then
139 printf '%s\t%s\n' "$i" "$name" >> "$out/index.tsv"
140 else
141 echo "cover: no coverage report for test $i: $name" >&2
142 printf '%s\n' "$name" >> "$out/nocov.txt"
143 printf '%s\t%s\n' "$i" "$name" >> "$out/index.tsv"
144 fi
145 rm -f "$TMP/cov-$i.json"
146 done < "$tests"
147 ;;
148fastcov)
149 out=$1; mkdir -p "$out"
150 llvm_clean
151 cargo llvm-cov nextest --no-report --no-fail-fast -p "$CRATE" --profile fast -j "$NEXTEST_J" > "$out/fast.log" 2>&1 || true
152 prune_profraw
153 if ! { cargo llvm-cov report --json --output-path "$TMP/fast-cov.json" >/dev/null 2>&1 \
154 && python3 scripts/kill-matrix-analyze.py compact "$TMP/fast-cov.json" "$out/fast.json"; }; then
155 echo "fastcov: no coverage report; see $out/fast.log" >&2
156 fi
157 rm -f "$TMP/fast-cov.json"
158 ;;
159mutrun)
160 # json: the sample; idxfile lines: "<index>" (fast) or "<index><TAB><tests file>" (heavy).
161 # Each run applies exactly one mutant (mutate.py, exact span) and runs the tests. cargo-mutants'
162 # --re/-F does not filter by name in this version (it tests the first N mutants of the list), so
163 # it is not used for selection here.
164 json=$1; idxfile=$2; out=$3; mode=$4
165 mkdir -p "$out"
166 while IFS=$'\t' read -r idx tests; do
167 [ -n "$idx" ] || continue
168 if [ "$mode" = heavy ]; then
169 heavy_one "$json" "$idx" "$tests" "$out"
170 else
171 fast_one "$json" "$idx" "$out"
172 fi
173 echo "mutrun: $mode mutant $idx status=$(cat "$out/$idx.status")" >&2
174 done < "$idxfile"
175 ;;
176*)
177 sed -n '2,20p' "$0" >&2
178 exit 2
179 ;;
180esac