interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tools / mutation_runner.sh

236 lines · 10918 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# One mutation runner (POSIX sh, no Python). Deep module, small interface.
18#
19# A MUTATION is one record:
20# { name, file(s), edit, test command, what the check guards, equivalence }
21# The runner copies the source tree to a scratch directory (the source tree is
22# never edited), applies the edit there, checks that the files really changed
23# (otherwise ERROR "edit did not apply"), runs the optional build command
24# (failure => ERROR), then runs the test command inside the scratch tree:
25# exit nonzero => KILLED exit 0 => SURVIVED
26# EQUIVALENCE is a data column: class none|redundant|gap plus rationale text.
27# A SURVIVED mutant with class redundant or gap is reported SURVIVED_REDUNDANT
28# or SURVIVED_GAP and the JSON note is "<class>: <rationale>" (the same note
29# tools/c4_requal_mutants.sh writes today). Class none (or no class) leaves it
30# SURVIVED, which fails the sweep.
31#
32# EDIT KINDS
33# sed files comma-separated, expressions separated by @@ (one per
34# file, in order), as tools/c4_requal_mutants.sh does today.
35# marker-sed one file, one sed expression applied only to lines containing
36# MUT:<name>, as tensor/autodiff/m20 mutation sweeps do today.
37# replace edit text "R:<from>@>@<to>[@@<from>@>@<to>...]": <from> must be
38# found exactly once in its file (else ERROR), @NL@ in <to> is a
39# newline, as numeric_oracle_mutations.sh does today.
40#
41# READERS (two; the older sweeps become adapters over them)
42# table rows id~files~edit~guards[~class~rationale] (kinds sed, replace)
43# Legacy rows are accepted: 4 columns (no equivalence), or 5 columns
44# whose last column is prose starting "redundant:" or "gap:".
45# Lines that are empty or start with # are skipped.
46# marker rows name|file|sed-expr (kind marker-sed; the expression may
47# contain |). Marker rows have no equivalence column: a survivor is
48# always a failure.
49#
50# USAGE
51# tools/mutation_runner.sh -k table|marker -m ROWS|- -t TESTCMD
52# [-d SRCDIR] [-b BUILDCMD] [-e TEXT] [-c "PATH ..."] [-o out.json] [-f PREFIX] [-B]
53# -k reader kind. -m rows file, or - for stdin. -t test command (sh -c, run
54# in the scratch tree). -d source tree (default .). -b build command run in
55# the scratch tree before the test. -e TEXT optional: when the build step
56# fails and its log is empty (make killed or never started) the row note
57# is "build failed: TEXT" instead of "build failed: <first error line>";
58# without -e nothing changes. -c paths (relative to SRCDIR) to copy
59# (default: whole tree). -o JSON receipt path. -f line prefix that marks a
60# failing case in test output, used only for the KILLED note. -B run build
61# and test on the unmutated tree first; if that fails the sweep FAILS.
62# Env ONLY="id id" restricts the sweep to those ids.
63#
64# OUTPUT
65# stderr: one line per mutant "<id> <STATUS> <guards> (<note>)", STATUS is
66# KILLED, SURVIVED, SURVIVED_REDUNDANT, SURVIVED_GAP or ERROR.
67# stdout: one summary line.
68# JSON (-o), the c4 receipt layout, same whitespace and key order:
69# {"mutants":[{"id","file","checks","status","note"}, ...]}
70# Every string value is JSON-escaped losslessly (\" \\ \n \r \t, other
71# control bytes as \u00XX); text is never dropped to make it parse.
72# Exit 0 only if at least one mutant ran and every mutant is KILLED or
73# justified-survived; 2 on bad usage.
74set -u
75
76kind=; rows=; test_cmd=; src=.; build_cmd=; copy=.; out=; fprefix=; baseline=0; empty_note=
77while getopts k:m:t:d:b:e:c:o:f:B opt; do
78 case $opt in
79 k) kind=$OPTARG;; m) rows=$OPTARG;; t) test_cmd=$OPTARG;; d) src=$OPTARG;;
80 b) build_cmd=$OPTARG;; e) empty_note=$OPTARG;; c) copy=$OPTARG;; o) out=$OPTARG;; f) fprefix=$OPTARG;;
81 B) baseline=1;; *) kind=;;
82 esac
83done
84case $kind in table|marker) ;; *) kind=;; esac
85if [ -z "$kind" ] || [ -z "$rows" ] || [ -z "$test_cmd" ]; then
86 echo "usage: mutation_runner.sh -k table|marker -m ROWS|- -t TESTCMD [-d SRCDIR] [-b BUILDCMD] [-e TEXT] [-c PATHS] [-o out.json] [-f PREFIX] [-B]" >&2
87 exit 2
88fi
89src=$(cd "$src" 2>/dev/null && pwd) || { echo "mutation_runner: no such source dir" >&2; exit 2; }
90work=$(mktemp -d "${TMPDIR:-/tmp}/mutrun.XXXXXX") || exit 2
91trap 'rm -rf "$work"' EXIT INT TERM
92if [ "$rows" = - ]; then cat > "$work/rows"; rows=$work/rows; fi
93[ -r "$rows" ] || { echo "mutation_runner: cannot read rows" >&2; exit 2; }
94[ -n "$out" ] || out=$work/out.json
95
96# Fresh scratch copy of the tree in $1.
97copy_tree() {
98 mkdir -p "$1"
99 for p in $copy; do
100 if [ "$p" = . ]; then cp -R "$src/." "$1/"
101 else
102 mkdir -p "$1/$(dirname "$p")"
103 cp -R "$src/$p" "$1/$p" 2>/dev/null
104 fi
105 done
106}
107
108if [ $baseline -eq 1 ]; then
109 copy_tree "$work/base"
110 if { [ -z "$build_cmd" ] || (cd "$work/base" && sh -c "$build_cmd") </dev/null >"$work/base.log" 2>&1; } \
111 && (cd "$work/base" && sh -c "$test_cmd") </dev/null >>"$work/base.log" 2>&1; then :
112 else
113 head -5 "$work/base.log" >&2
114 echo "mutation runner: FAIL (unmutated baseline does not pass)"
115 exit 1
116 fi
117 rm -rf "$work/base"
118fi
119
120# Edit step. Sets $note on failure and returns nonzero.
121apply_edit() { # dir files ekind edit id
122 a_dir=$1; a_files=$(echo "$2" | tr ',' ' '); a_kind=$3; a_edit=$4; a_id=$5
123 for f in $a_files; do
124 [ -f "$a_dir/$f" ] || { note="edit did not apply (no such file $f)"; return 1; }
125 done
126 a_before=$(for f in $a_files; do sha256sum "$a_dir/$f"; done | sha256sum)
127 i=1
128 for f in $a_files; do
129 e=$(printf '%s\n' "$a_edit" | awk -F'@@' -v n=$i '{print $n}')
130 case $a_kind in
131 sed) sed -i "$e" "$a_dir/$f" ;;
132 marker-sed) sed -i "/MUT:$a_id/ $e" "$a_dir/$f" ;;
133 replace)
134 e=${e#R:}
135 FROM=${e%%@>@*} TO=${e#*@>@} perl -0pi -e '$n += s/\Q$ENV{FROM}\E/$ENV{TO} =~ s{\@NL\@}{\n}gr/ge; END { $? = $n == 1 ? 0 : 3 }' "$a_dir/$f" \
136 || { note="replace text not found exactly once in $f"; return 1; }
137 ;;
138 esac
139 i=$((i + 1))
140 done
141 a_after=$(for f in $a_files; do sha256sum "$a_dir/$f"; done | sha256sum)
142 [ "$a_before" != "$a_after" ] || { note="edit did not apply"; return 1; }
143}
144
145# JSON string body of $1: quote, backslash and every control byte escaped.
146json_str() {
147 printf '%s' "$1" | LC_ALL=C awk '
148 BEGIN { for (i = 1; i < 32; i++) c[sprintf("%c", i)] = sprintf("\\u%04x", i)
149 c["\t"] = "\\t"; c["\r"] = "\\r"; c["\b"] = "\\b"; c["\f"] = "\\f" }
150 { o = ""
151 for (i = 1; i <= length($0); i++) {
152 ch = substr($0, i, 1)
153 if (ch == "\\") o = o "\\\\"; else if (ch == "\"") o = o "\\\""
154 else if (ch in c) o = o c[ch]; else o = o ch
155 }
156 printf "%s%s", (NR > 1 ? "\\n" : ""), o }'
157}
158
159n=0 k=0 r=0 g=0
160first=1
161printf '{\n "mutants": [\n' > "$out" || {
162 echo "mutation runner: cannot write receipt $out" >&2
163 exit 2
164}
165
166# run_one id files ekind edit guards justification (justification is the
167# prose "redundant: ..." / "gap: ..." or empty)
168run_one() {
169 id=$1; files=$2; ekind=$3; edit=$4; what=$5; just=$6
170 if [ -n "${ONLY:-}" ]; then case " $ONLY " in *" $id "*) ;; *) return 0;; esac; fi
171 dir=$work/m.$id
172 copy_tree "$dir"
173 note=
174 if ! apply_edit "$dir" "$files" "$ekind" "$edit" "$id"; then status=ERROR
175 else
176 log=$work/$id.log
177 if [ -n "$build_cmd" ] && ! (cd "$dir" && sh -c "$build_cmd") </dev/null >"$log" 2>&1; then
178 status=ERROR; note="build failed: $(grep -m1 -E "error:|Error " "$log" | cut -c1-120)"
179 [ -z "$empty_note" ] || [ -s "$log" ] || note="build failed: $empty_note"
180 else
181 (cd "$dir" && sh -c "$test_cmd") </dev/null >"$log.run" 2>&1
182 rc=$?
183 nf=0
184 [ -z "$fprefix" ] || nf=$(grep -c "^$fprefix" "$log.run")
185 if [ $rc -ne 0 ] && [ "$nf" -gt 0 ]; then
186 cut_from=$((${#fprefix} + 2))
187 status=KILLED; note="$nf failing case(s), first: $(grep -m1 "^$fprefix" "$log.run" | cut -c"$cut_from"-$((cut_from + 64)))"
188 elif [ $rc -ne 0 ]; then status=KILLED; note="harness exit $rc (crash or abort)"
189 else
190 case "$just" in
191 redundant:*) status=SURVIVED_REDUNDANT; note="$just" ;;
192 gap:*) status=SURVIVED_GAP; note="$just" ;;
193 *) status=SURVIVED; note="harness still PASS, no justification recorded" ;;
194 esac
195 fi
196 fi
197 fi
198 echo "$id $status $what ($note)" >&2
199 [ $first -eq 1 ] || printf ',\n' >> "$out"
200 first=0
201 printf ' {"id": "%s", "file": "%s", "checks": "%s", "status": "%s", "note": "%s"}' \
202 "$(json_str "$id")" "$(json_str "$files")" "$(json_str "$what")" "$(json_str "$status")" "$(json_str "$note")" >> "$out"
203 n=$((n + 1))
204 case $status in KILLED) k=$((k + 1));; SURVIVED_REDUNDANT) r=$((r + 1));; SURVIVED_GAP) g=$((g + 1));; esac
205 rm -rf "$dir"
206}
207
208# Reader 1: id~files~edit~guards[~class~rationale]
209read_table() {
210 while IFS='~' read -r t_id t_files t_edit t_guards t_c5 t_c6; do
211 case $t_id in ''|'#'*) continue;; esac
212 t_kind=sed
213 case $t_edit in R:*) t_kind=replace;; esac
214 case $t_c5 in
215 none|redundant|gap)
216 if [ "$t_c5" = none ]; then t_just=; else t_just="$t_c5: $t_c6"; fi ;;
217 *) t_just=$t_c5; [ -z "$t_c6" ] || t_just="$t_c5~$t_c6" ;;
218 esac
219 run_one "$t_id" "$t_files" "$t_kind" "$t_edit" "$t_guards" "$t_just"
220 done < "$rows"
221}
222
223# Reader 2: name|file|sed-expr (expr may contain |)
224read_marker() {
225 while IFS='|' read -r m_name m_file m_expr; do
226 case $m_name in ''|'#'*) continue;; esac
227 run_one "$m_name" "$m_file" marker-sed "$m_expr" "MUT:$m_name" ""
228 done < "$rows"
229}
230
231read_$kind
232
233printf '\n ]\n}\n' >> "$out"
234echo "mutants: $n total, $k killed, $r survived as redundant, $g survived as test gaps (receipt $out)"
235# Fail on any unjustified survivor or build/edit error, and on an empty sweep.
236[ "$n" -gt 0 ] && [ $((k + r + g)) -eq "$n" ]