interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / r16_negative / mutate.sh

221 lines · 11384 bytes Raw Blame History
1#!/bin/sh
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-G4 guard mutants (spec/r16-orchestrator-retirement.md ยง5 R16-G4:
18# "removing any one guard turns the test red").
19#
20# Usage: tests/r16_negative/mutate.sh <cc> <cflags> <aienos-r7-dir> <sources...>
21#
22# For each mutant, one guard is removed from a scratch copy of one file (the
23# tree is never edited), the G4 test is rebuilt with that copy in place of the
24# original, and run. A mutant is KILLED when the test does not print its gate
25# PASS line ("R16 gate: R16_G4_LEGACY_REFUSED=PASS"). Since spec C5 is closed
26# (runtime-issued caller credentials, src/runtime/rx_caller.h) the gate line
27# covers the six acts, the promoter-subject exploit probes, the C5 identity
28# probes, the promotion control and the C7 review probes (spec C7), so it is
29# judged rather than the core line.
30# Every mutant must be killed.
31# The control build (no mutation) must pass, and every edit must apply exactly
32# where named, or the run fails.
33#
34# Host only; no chip. Each run is capped at 120 s (allowed only because this is a
35# host test: never copy the cap into a silicon target) because a removed guard can
36# let a legacy loop spin; the G4 test itself finishes in under a second.
37set -u
38[ $# -ge 4 ] || { echo "usage: $0 <cc> <cflags> <aienos-r7-dir> <sources...>" >&2; exit 2; }
39cc=$1; cflags=$2; r7=$3; shift 3
40srcs="$*"
41lib=${R16_CAP_LIB:-$r7/native/capability/out/libaienos_capability.a} # make passes $(AIENOS_CAP_LIB)
42[ -f "$lib" ] || { echo "R16-G4 mutants: missing $lib" >&2; exit 2; }
43tmp=$(mktemp -d "${TMPDIR:-/tmp}/r16-mutants.XXXXXX") || exit 2
44trap 'rm -rf "$tmp"' EXIT INT TERM
45. "$(dirname "$0")/../../tools/mutpool.sh"; mp_init || exit 2
46
47# replace FILE OUT OCCURRENCE: literal OLD (env R16_OLD) -> NEW (env R16_NEW).
48# OCCURRENCE is a number (only that match) or "all". Fails unless it applied.
49replace() {
50 awk -v nth="$3" '
51 BEGIN { old = ENVIRON["R16_OLD"]; new = ENVIRON["R16_NEW"]; seen = 0; done = 0 }
52 {
53 line = $0
54 p = index(line, old)
55 if (p > 0) {
56 seen++
57 if (nth == "all" || seen == nth + 0) {
58 line = substr(line, 1, p - 1) new substr(line, p + length(old))
59 done++
60 }
61 }
62 print line
63 }
64 END { if (done == 0) exit 3 }' "$1" > "$2"
65}
66
67build_run() { # name out-dir sources lib
68 name=$1; d=$2
69 # shellcheck disable=SC2086
70 if ! $cc $cflags -iquote src/runtime -iquote src -pthread -o "$d/g4" $3 "$4" -lm \
71 > "$d/build.log" 2>&1; then
72 return 2
73 fi
74 timeout 120 "$d/g4" > "$d/run.log" 2>&1
75 if grep -q "^R16 gate: R16_G4_LEGACY_REFUSED=PASS" "$d/run.log"; then
76 return 0
77 fi
78 return 1
79}
80
81# mutant NAME FILE OCCURRENCE OLD NEW (FILE is an omega source, or "aienos")
82# (mutants run on a bounded parallel pool, tools/mutpool.sh, MUTANT_JOBS; output order is the order below)
83mutant() { mp_run mutant_job "$@"; }
84mutant_job() {
85 name=$1; file=$2; nth=$3
86 d="$tmp/$name"; mkdir -p "$d"
87 use_srcs=$srcs; use_lib=$lib
88 if [ "$file" = aienos ]; then
89 if ! R16_OLD=$4 R16_NEW=$5 replace "src/runtime/aienos_cap_om2.c" \
90 "$d/aienos_cap_om2.c" "$nth"; then
91 echo "R16-G4 mutant $name: edit did not apply BROKEN"; return
92 fi
93 # shellcheck disable=SC2086
94 if ! $cc $cflags -iquote src/runtime -iquote src -c -o "$d/aienos_cap_om2.o" "$d/aienos_cap_om2.c" \
95 > "$d/lib.log" 2>&1 || ! ar rcs "$d/libaienos_capability_om2.a" "$d/aienos_cap_om2.o" >> "$d/lib.log" 2>&1; then
96 echo "R16-G4 mutant $name: library build failed BROKEN"; return
97 fi
98 use_lib="$d/libaienos_capability_om2.a"
99 else
100 base=$(basename "$file")
101 if ! R16_OLD=$4 R16_NEW=$5 replace "$file" "$d/$base" "$nth"; then
102 echo "R16-G4 mutant $name: edit did not apply BROKEN"; return
103 fi
104 use_srcs=""
105 for s in $srcs; do
106 if [ "$s" = "$file" ]; then use_srcs="$use_srcs $d/$base"; else use_srcs="$use_srcs $s"; fi
107 done
108 fi
109 build_run "$name" "$d" "$use_srcs" "$use_lib"
110 case $? in
111 0) echo "R16-G4 mutant $name: test still PASSES SURVIVED" ;;
112 1) why=$(grep -E 'FAIL|not refused|ACCEPTED|changed' "$d/run.log" | head -1)
113 [ -n "$why" ] || why="exit or missing gate line: $(tail -1 "$d/run.log")"
114 echo "R16-G4 mutant $name: test red KILLED ($why)" ;;
115 *) echo "R16-G4 mutant $name: build failed BROKEN ($(grep -m1 error "$d/build.log"))"
116 esac
117}
118
119W=src/runtime/rx_world.c
120G=src/runtime/rx_generation.c
121C=src/runtime/rx_coherent.c
122S=src/runtime/rx_seq_reference.c
123
124# control: the unmutated build must pass, or nothing below means anything
125mkdir -p "$tmp/control"
126if ! build_run control "$tmp/control" "$srcs" "$lib"; then
127 echo "R16-G4 mutants: control build does not pass (see run below)"
128 tail -5 "$tmp/control/run.log" "$tmp/control/build.log" 2>/dev/null
129 echo "R16 G4 mutants: CONTROL FAIL"; exit 1
130fi
131echo "R16-G4 control (no guard removed): PASS"
132
133# acts 1, 2, 6: external writes and reaction admission / activation
134mutant publish_external_cap $W 1 \
135 'if (rc != RX_CAP_OK) { pthread_mutex_unlock(&w->mu); return RX_ERR_AUTHORITY; }' '(void)rc;'
136mutant add_reaction_write_cover $W 1 \
137 'if (!covered(w, d, d->writes[i].obj, RX_RIGHT_WRITE)) { rc = RX_ERR_AUTHORITY; goto out; }' '(void)0;'
138mutant activation_cap_check $W 1 \
139 'if (validate_caps(w, d, &cap_err) != 0) {' 'if (0 && validate_caps(w, d, &cap_err) != 0) {'
140mutant commit_cap_recheck $W 2 \
141 'if (validate_caps(w, d, &cap_err) != 0) {' 'if (0 && validate_caps(w, d, &cap_err) != 0) {'
142mutant both_cap_checks $W all \
143 'if (validate_caps(w, d, &cap_err) != 0) {' 'if (0 && validate_caps(w, d, &cap_err) != 0) {'
144# act 3: cognition mint / admin in the native Aienux authority
145mutant cognition_mint aienos 1 '(void)token;' '(void)token; if (view) return 0;'
146mutant cognition_admin aienos 1 '(void)target;' '(void)target; if (view) return 0;'
147# act 4: promotion
148mutant promote_self $G 1 \
149 'if (request->subject == c->proposer) {' 'if (0 && request->subject == c->proposer) {'
150mutant promote_rights $G 1 \
151 'if (request->resource != RX_GEN_RES_PROMOTION || request->rights != RX_GEN_RIGHT_PROMOTE) {' \
152 'if (0 && (request->resource != RX_GEN_RES_PROMOTION || request->rights != RX_GEN_RIGHT_PROMOTE)) {'
153mutant promote_auth $G 1 'if (auth_rc != 0) {' 'if (0 && auth_rc != 0) {'
154# act 5: coherent boundary
155mutant boundary_cap_identity $C 1 \
156 'if (cap.cap_id != o->cap.cap_id || cap.generation != o->cap.generation)' \
157 'if (0 && (cap.cap_id != o->cap.cap_id || cap.generation != o->cap.generation))'
158mutant boundary_cap_validate $C 1 \
159 'if (vrc != RX_CAP_OK) fault = RX_FAULT_CAP;' 'if (0 && vrc != RX_CAP_OK) fault = RX_FAULT_CAP;'
160mutant boundary_diverged $C 1 \
161 'else if (p->object_id != o->id || p->generation != o->generation)' \
162 'else if (0 && (p->object_id != o->id || p->generation != o->generation))'
163# extra: the SEQ reference must refuse a production world
164mutant seq_pulse_production $S 1 \
165 'if (!w || !w->sequential || !plan) return RX_ERR_ARG;' 'if (!w || !plan) return RX_ERR_ARG;'
166mutant seq_activate_production $W 1 \
167 'if (!w || !w->sequential || rid >= w->n_reactions) return RX_ERR_ARG;' \
168 'if (!w || rid >= w->n_reactions) return RX_ERR_ARG;'
169
170# R16 C5: caller identity (runtime-issued credentials). Each removes one check.
171mutant id_register $W 1 \
172 'if (irc != RX_CALLER_OK) { rc = RX_ERR_IDENTITY; goto out; }' '(void)irc;'
173mutant id_activation_commit_recheck $W 1 \
174 'if (irc != RX_CALLER_OK) { if (first_err) *first_err = RX_ERR_IDENTITY; return -1; }' '(void)irc;'
175mutant id_secret_digest $W 1 \
176 'return diff ? RX_CALLER_ERR_FORGED : RX_CALLER_OK;' 'return (void)diff, RX_CALLER_OK;'
177mutant id_generation $W 1 \
178 'if (cred->generation != w->callers[s].generation) return RX_CALLER_ERR_STALE;' '(void)0;'
179mutant id_revoked_check $W 1 \
180 'if (!w->callers[s].live) return RX_CALLER_ERR_REVOKED;' '(void)0;'
181mutant id_revoked_still_live $W 1 \
182 ': !w->callers[s].live ? RX_CALLER_ERR_REVOKED' ': 0 ? RX_CALLER_ERR_REVOKED'
183mutant id_revoke_takes_effect $W 1 'w->callers[s].live = false;' '(void)0;'
184mutant id_revoke_needs_credential $W 1 'if (rc == RX_CALLER_OK) {' 'if (rc == RX_CALLER_OK || 1) {'
185mutant id_enroll_closed $W 1 \
186 'if (w->callers_bound) { rc = RX_CALLER_ERR_CLOSED; goto out; }' '(void)0;'
187mutant id_promote $G 1 \
188 'int crc = store->caller(store->caller_ctx, request->subject, &request->caller, RX_CALLER_OP_CHECK);' 'int crc = 0;'
189mutant id_propose $G 1 \
190 'int crc = store->caller(store->caller_ctx, proposer, cred, RX_CALLER_OP_CHECK);' 'int crc = 0; (void)proposer; (void)cred;'
191mutant id_bound_authority $G 1 'auth = store->bound_auth;' '(void)0;'
192
193# R16 C7: outside review of C5/C6. Each removes one fix; spec C7 names the probe.
194mutant c7_flip_recheck $G 1 'int recheck = store_bound(store);' 'int recheck = 0;'
195mutant c7_revoke_serialized $W 1 'const int serialize = 1;' 'const int serialize = 0;'
196mutant c7_prebind_check $W 1 \
197 'if (w->callers_bound || d->caller.generation != 0) {' 'if (w->callers_bound) {'
198mutant c7_bind_refuses_unauthenticated $W 1 \
199 'if (!w->reactions[i].removed && w->reactions[i].desc.caller.generation == 0)' \
200 'if (0 && !w->reactions[i].removed && w->reactions[i].desc.caller.generation == 0)'
201mutant c7_mutate_object_bound $G 1 \
202 'if (store_bound(store)) return RX_GEN_ERR_IDENTITY;' '(void)0;'
203mutant c7_set_evidence_bound $G 2 \
204 'if (store_bound(store)) return RX_GEN_ERR_IDENTITY;' '(void)0;'
205mutant c7_add_work_credential $G 1 \
206 'int crc = store->caller(store->caller_ctx, subject, cred, RX_CALLER_OP_CHECK);' 'int crc = 0; (void)subject; (void)cred;'
207mutant c7_finish_work_credential $G 2 \
208 'int crc = store->caller(store->caller_ctx, subject, cred, RX_CALLER_OP_CHECK);' 'int crc = 0; (void)subject; (void)cred;'
209mutant c7_add_work_state $G 1 'if (!starts_open) return RX_GEN_ERR_ARG;' '(void)starts_open;'
210
211mp_wait > "$tmp/mutants.log"; pool_rc=$?
212cat "$tmp/mutants.log"
213total=$(grep -c '^R16-G4 mutant ' "$tmp/mutants.log")
214killed=$(grep -c '^R16-G4 mutant .*: test red KILLED (' "$tmp/mutants.log")
215survived=$(grep -c '^R16-G4 mutant .*: test still PASSES SURVIVED$' "$tmp/mutants.log")
216broken=$(grep -c '^R16-G4 mutant .* BROKEN' "$tmp/mutants.log")
217echo "R16 G4 mutants: $total total, $killed killed, $survived survived, $broken broken"
218if [ $survived -eq 0 ] && [ $broken -eq 0 ] && [ $pool_rc -eq 0 ] && [ $killed -eq $total ]; then
219 echo "R16 gate: R16_G4_GUARDS_LOAD_BEARING=PASS"; exit 0
220fi
221echo "R16 gate: R16_G4_GUARDS_LOAD_BEARING=FAIL"; exit 1