omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / runtime / rx_operator_mutants.sh
| 1 | #!/bin/bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 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 | # R16 G6 operator control: every link from the outside request to the world's |
| 18 | # stop is load-bearing. Each mutant changes one place in a temporary copy of the |
| 19 | # PRODUCTION sources; the production host program is rebuilt from that copy with |
| 20 | # the production flags, and tests/runtime/rx_operator_host.sh must FAIL against it |
| 21 | # (exit non-zero with at least one failed check). The unmutated copy must PASS. |
| 22 | # A pattern that does not match exactly once fails this script, never a skip. |
| 23 | # Every run writes its receipt and failed logs under its own directory in the |
| 24 | # scratch area (RX_OP_OUT), never over build/r16-operator. |
| 25 | # |
| 26 | # rx_operator_mutants.sh <rx_operator client> |
| 27 | # Environment from make: CC CFLAGS RX_PROD_ARGUS_FLAGS RX_R13_SRCS RX_PROD_ARGUS_SRCS AIENOS_CAP_LIB |
| 28 | set -u |
| 29 | HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) |
| 30 | CLI=$(cd "$(dirname "$1")" && pwd)/$(basename "$1") |
| 31 | W=$(mktemp -d "${TMPDIR:-/tmp}/rx-op-mut.XXXXXX") |
| 32 | # Evidence rule (omega #313 item 2, #314): a run that does not end in PASS keeps its logs. |
| 33 | # RX_OP_MUT_KEEP (default: a new folder in TMPDIR) receives the logs of every case; the work |
| 34 | # folder itself (copies of the sources, built programs) is then removed. Nothing is ever kept |
| 35 | # under build/qual-runs, and an existing folder is never overwritten. |
| 36 | KEEP=${RX_OP_MUT_KEEP:-${TMPDIR:-/tmp}/rx-op-mut-kept.$(date -u +%Y%m%dT%H%M%SZ).$$} |
| 37 | keep_logs() { |
| 38 | [ "$1" = 0 ] && return 0 |
| 39 | [ -e "$KEEP" ] && { echo "RX_OPERATOR_MUTANTS: not keeping logs, $KEEP already exists" >&2; return 0; } |
| 40 | mkdir -p "$KEEP" || return 0 |
| 41 | local d; for d in "$W"/*/; do |
| 42 | d=${d%/}; mkdir -p "$KEEP/${d##*/}" |
| 43 | cp -a "$d"/build.log "$d"/run.log "$d"/name "$d"/phases "$d"/out "$d"/build/qual-runs "$KEEP/${d##*/}/" 2>/dev/null |
| 44 | done |
| 45 | echo "RX_OPERATOR_MUTANTS: logs kept in $KEEP" >&2 |
| 46 | } |
| 47 | trap 'rc=$?; keep_logs "$rc"; rm -rf "$W"' EXIT |
| 48 | |
| 49 | # Mutant runs write their R13 receipts under their own build/qual-runs inside the work folder |
| 50 | # (the run directory is the mutant copy, and its build/ is a real folder that links to the real build/ products (argus sources, libraries) but not to qual-runs). A mutant run must never add anything to the real build/qual-runs (#314). |
| 51 | REAL_RUNS="$HERE/build/qual-runs" |
| 52 | runs_listing() { [ -d "$REAL_RUNS" ] && find "$REAL_RUNS" -mindepth 1 | LC_ALL=C sort; true; } |
| 53 | RUNS_BEFORE=$(runs_listing) |
| 54 | JOBS=${RX_OP_MUT_JOBS:-4} |
| 55 | |
| 56 | # name|file|phases the test runs against it|perl substitution (applied with -0, must match once) |
| 57 | MUTANTS='O1 wiring missing: no durable halt directory|tests/runtime/rx_r13_living.c|control|s/int halt_mark = rx_world_set_halt_dir\(&r->w, r->generation_dir\);/int halt_mark = 0;/ |
| 58 | O2 wiring only in a comment|tests/runtime/rx_r13_living.c|control|s/int halt_mark = rx_world_set_halt_dir\(&r->w, r->generation_dir\);/int halt_mark = 0; \/* rx_world_set_halt_dir(\&r->w, r->generation_dir); *\// |
| 59 | O3 entry point referenced, never called|tests/runtime/rx_r13_living.c|control|s/ if \(operator_open\(r\) != 0\) return -1;/ if (0 \&\& operator_open(r) != 0) return -1;/ |
| 60 | O4 command handler disconnected (stop never routed)|src/runtime/rx_operator.c|control|s/int stop = strcmp\(q->cmd, "stop"\) == 0,/int stop = 0,/ |
| 61 | O5 success reply without a state transition|src/runtime/rx_operator.c|control|s/rc = rx_world_emergency_stop\(w, q->subject, &q->cred, q->cap, q->reason, &h\);/rc = RX_OK; h.seq = 1; h.durable = 1;/ |
| 62 | O6 unauthorized resume accepted (check called, result unused)|src/runtime/rx_world.c|control|s/int rc = halt_authorize\(w, subject, cred, cap\);\n if \(rc == RX_OK && !w->halted\) rc = RX_HALT_NOT_STOPPED;/int rc = (halt_authorize(w, subject, cred, cap), RX_OK);\n if (rc == RX_OK \&\& !w->halted) rc = RX_HALT_NOT_STOPPED;/ |
| 63 | O7 peer uid check removed|src/runtime/rx_operator.c|control|s/pl != sizeof pc \|\|\n pc\.uid != geteuid\(\)\) \{/pl != sizeof pc) {/ |
| 64 | O8 status without the authority check|src/runtime/rx_operator.c|control|s/rc = rx_world_operator_authorize\(w, q->subject, &q->cred, q->cap\);/rc = RX_OK;/ |
| 65 | O9 revoke-cap does not revoke|src/runtime/rx_operator.c|control|s/aienos_cap_revoke\(op->cfg\.admin, office,\n\s+\(AienosCapRef\)\{q->cap\.cap_id, q->cap\.generation\}\) != 0/0/ |
| 66 | O10 restart does not wait for the resume|tests/runtime/rx_r13_living.c|restart|s/ if \(world_halted\(r\)\) \{\n r->op_restored = 1;/ if (0) {\n r->op_restored = 1;/ |
| 67 | O11 startup accepts a group-readable directory|src/runtime/rx_operator.c|startup|s/if \(st\.st_mode & 077\) \{/if (0) {/ |
| 68 | O12 audit does not count stops|tests/runtime/rx_r13_living.c|control|s/ stops\+\+;\n// |
| 69 | O13 shutdown accepted while running|src/runtime/rx_operator.c|control|s/if \(!h\.halted\) \{ snprintf\(out, n, "NOT_STOPPED state=running"\); return; \}// |
| 70 | O14 stop reply before the world call (handler answers from the request)|src/runtime/rx_operator.c|control|s/ rc = rx_world_emergency_stop\(w, q->subject, &q->cred, q->cap, q->reason, &h\);\n if \(rc == RX_OK\) \{/ rc = RX_OK; rx_world_halt_status(w, \&h);\n if (rc == RX_OK) {/ |
| 71 | O15 end-of-episode wait turned off (G5 M-a)|tests/runtime/rx_r13_living.c|control|s/if \(rc == 0 && r->ready && world_halted\(r\)\) \{/if (0 \&\& rc == 0 \&\& r->ready \&\& world_halted(r)) {/ |
| 72 | O16 episode shape ignores halted seat claims (G5 M-b)|tests/runtime/rx_r13_living.c|control|s/r->w\.stats\.resident_claims - r->w\.stats\.resident_halted != RX_LIVING_TRIALS/r->w.stats.resident_claims != RX_LIVING_TRIALS/ |
| 73 | O17 control directory lock not taken (S1)|src/runtime/rx_operator.c|control|s/if \(flock\(fd, LOCK_EX \| LOCK_NB\) != 0\) \{/if (0) {/ |
| 74 | O18 state directory not locked (S1)|tests/runtime/rx_r13_living.c|control|s/rx_operator_lock_dir\(g_state_dir, 1, &g_state_lock, why, sizeof why\) != 0/(g_state_lock < -1 || rx_operator_dir_check(g_state_dir, 1, why, sizeof why) != 0)/ |
| 75 | O19 generation directory not locked (S1)|tests/runtime/rx_r13_living.c|control|s/\(g_gen_lock\[mode\] < 0 &&\n\s+rx_operator_lock_dir\(r->generation_dir, 1, &g_gen_lock\[mode\], why, sizeof why\) != 0\)/(g_gen_lock[mode] < -1 || rx_operator_dir_check(r->generation_dir, 1, why, sizeof why) != 0)/ |
| 76 | O20 close removes files it did not create (S1)|src/runtime/rx_operator.c|control|s/ if \(fstatat\(op->dir_fd, name, &st, AT_SYMLINK_NOFOLLOW\) == 0 &&\n st\.st_dev == made->st_dev && st\.st_ino == made->st_ino\)\n unlinkat/ (void)st; (void)made;\n unlinkat/ |
| 77 | O21 a stop that is not durable replies OK (S2)|src/runtime/rx_operator.c|control|s/if \(h\.durable == 1\)\n/if (1)\n/ |
| 78 | O22 unknown commands fall through to the authorized commands (S3)|src/runtime/rx_operator.c|control|s/ if \(!status && !shutdown && !revoke && !revoke_cap\) \{ snprintf\(out, n, "BAD_REQUEST"\); return; \}\n/ (void)revoke_cap;\n/ |
| 79 | O23 an expired resume still runs (S4)|src/runtime/rx_operator.c|control|s/if \(strcmp\(q\.cmd, "stop"\) != 0 && monotonic_ns\(\) > q\.deadline_ns\)/if (0)/ |
| 80 | O24 a request without a deadline accepted (S4)|src/runtime/rx_operator.c|control|s/ \|\| !q->have_deadline\)\n return -1;/)\n return -1;/ |
| 81 | O25 any deadline accepted (S4)|src/runtime/rx_operator.c|control|s/ if \(q->deadline_ns > monotonic_ns\(\) \+ RX_OPERATOR_DEADLINE_MAX_NS\) return -1;\n// |
| 82 | ' |
| 83 | |
| 84 | copy() { mkdir -p "$1"; (cd "$HERE" && tar -cf - src tests/runtime tests/fabric tools) | tar -xf - -C "$1"; mkdir -p "$1/build"; local e; for e in "$HERE"/build/*; do [ -e "$e" ] && [ "${e##*/}" != qual-runs ] && ln -s "$e" "$1/build/${e##*/}"; done; } |
| 85 | build() { # dir: the production host program, production flags and sources |
| 86 | (cd "$1" && $CC $CFLAGS $RX_PROD_ARGUS_FLAGS -pthread -o prog $RX_R13_SRCS $RX_PROD_ARGUS_SRCS \ |
| 87 | $AIENOS_CAP_LIB -lm) > "$1/build.log" 2>&1 |
| 88 | } |
| 89 | |
| 90 | fails=0; n_mut=0 |
| 91 | copy "$W/base" |
| 92 | names="" |
| 93 | while IFS='|' read -r name file phases sub; do |
| 94 | [ -n "$name" ] || continue |
| 95 | id=${name%% *}; d="$W/$id"; copy "$d" |
| 96 | n=$(perl -0 -e 'my $s = do { local $/; <STDIN> }; my $c = eval "\$s =~ $ARGV[0]g"; die $@ if $@; print $c ? $c + 0 : 0' "$sub" < "$d/$file") |
| 97 | if [ "$n" != 1 ]; then echo "FAIL $name: pattern matched ${n:-?} times (must be 1)"; fails=$((fails + 1)); continue; fi |
| 98 | perl -0pi -e "$sub" "$d/$file" |
| 99 | if cmp -s "$d/$file" "$HERE/$file"; then echo "FAIL $name: substitution changed nothing"; fails=$((fails + 1)); continue; fi |
| 100 | echo "$phases" > "$d/phases"; echo "$name" > "$d/name" |
| 101 | names="$names $id" |
| 102 | done <<< "$MUTANTS" |
| 103 | |
| 104 | # Builds in parallel, tests one at a time (each test starts the production program). |
| 105 | export CC CFLAGS RX_PROD_ARGUS_FLAGS RX_R13_SRCS RX_PROD_ARGUS_SRCS AIENOS_CAP_LIB |
| 106 | export -f build |
| 107 | printf '%s\n' base $names | xargs -P "$JOBS" -I{} bash -c 'build "$0/{}" || echo "build failed: {}"' "$W" |
| 108 | |
| 109 | if [ -x "$W/base/prog" ] && (cd "$W/base" && RX_OP_OUT="$W/base/out" bash "$HERE/tests/runtime/rx_operator_host.sh" host "$W/base/prog" "$CLI" > "$W/base/run.log" 2>&1); then |
| 110 | echo "ok unmutated: PASS ($(grep -c '^\[+\]' "$W/base/run.log") checks)" |
| 111 | else |
| 112 | echo "FAIL unmutated: did not pass"; grep -E '^\[-\]|SKIPPED|build failed' "$W/base/run.log" "$W/base/build.log" | head -5 |
| 113 | fails=$((fails + 1)) |
| 114 | fi |
| 115 | skipped=$(grep -c '^\[SKIPPED\]' "$W/base/run.log" 2>/dev/null); skipped=${skipped:-0} |
| 116 | for id in $names; do |
| 117 | d="$W/$id"; name=$(cat "$d/name"); n_mut=$((n_mut + 1)) |
| 118 | if [ ! -x "$d/prog" ]; then echo "FAIL $name: mutant did not build"; tail -3 "$d/build.log"; fails=$((fails + 1)); continue; fi |
| 119 | (cd "$d" && RX_OP_OUT="$d/out" RX_OP_PHASES=$(cat "$d/phases") RX_OP_FAILFAST=1 bash "$HERE/tests/runtime/rx_operator_host.sh" host "$d/prog" "$CLI" > "$d/run.log" 2>&1); rc=$? |
| 120 | nf=$(grep -c '^\[-\]' "$d/run.log" 2>/dev/null); nf=${nf:-0} |
| 121 | first=$(grep -m1 '^\[-\]' "$d/run.log" | cut -c1-110) |
| 122 | if [ $rc != 0 ] && [ "$nf" -gt 0 ]; then echo "ok $name: killed by: ${first#\[-\] }" |
| 123 | elif grep -q '^\[SKIPPED\]' "$d/run.log"; then echo "SKIPPED $name: the test could not run the case that kills it"; skipped=$((skipped + 1)) |
| 124 | else echo "FAIL $name: SURVIVED (exit $rc)"; fails=$((fails + 1)); fi |
| 125 | done |
| 126 | RUNS_AFTER=$(runs_listing) |
| 127 | if [ "$RUNS_BEFORE" != "$RUNS_AFTER" ]; then |
| 128 | echo "FAIL mutant runs wrote under the real $REAL_RUNS:"; diff <(echo "$RUNS_BEFORE") <(echo "$RUNS_AFTER") | head -5 |
| 129 | fails=$((fails + 1)) |
| 130 | fi |
| 131 | echo "RX_OPERATOR_MUTANTS: $n_mut mutants, $fails failures, $skipped skipped" |
| 132 | if [ "$fails" = 0 ] && [ "$skipped" = 0 ]; then echo "RX_OPERATOR_MUTANTS: PASS"; exit 0; fi |
| 133 | if [ "$fails" = 0 ]; then echo "RX_OPERATOR_MUTANTS: NOT_RUN (SKIPPED cases)"; exit 3; fi |
| 134 | echo "RX_OPERATOR_MUTANTS: FAIL"; exit 1 |