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 / turing / tc_range_om.sh

373 lines · 20736 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# tests/turing/tc_range_om.sh: gate glue for the Omega range coder src/turing/tc_range.om (L111).
18# POSIX sh, sequencing only (disclosed host glue, as tests/turing/origsel-run.sh): it builds the
19# unit with the repository's own seed/out/omc, runs it next to the C reference turing-coder and
20# compares bytes and exit codes. Every length number and the bound verdict come from tc_range.om.
21# usage: sh tests/turing/tc_range_om.sh <turing-coder> <out-dir>
22# Checks, each printed PASS/FAIL, any FAIL exits 1:
23# 1 build; 2 selftest (synthetic round trips, length identity, upper and lower bound, one random
24# flipped payload byte per case detected, exhaustive ub rule check);
25# 3 for 2 committed traces x 7 models: Omega encode byte-identical to C, C verifies the Omega
26# file, Omega verifies the C file, Omega decode byte-identical to the TSY1, ideal_ub equal,
27# bound=PASS;
28# 4 single-fault inputs: Omega and the C reference refuse with the same CODER_SPEC s.10 name
29# (flipped payload byte, truncated, trailing byte, other TPS1, altered magic, corrupted TPS1,
30# TSY1 of another trace, corrupted TSY1); a refusal after the output exists removes it; an
31# output path naming an input is refused and the input is left intact; usage;
32# 4b n = 1152514738907 (cap+1) refuses ARG. The file is a digest-valid 160-byte TPS1, not a
33# terabyte body. A sibling with n = 1 and the same shape refuses FORMAT, so a size-check
34# miss is not counted as the cap. `selftest overflow` raises ALARM 7 on the wrapping sum.
35# 4c ofinish: a forced fsync failure (seccomp EIO on fsync/fdatasync; compile-time
36# AUDIT_ARCH_X86_64 / AUDIT_ARCH_AARCH64 and __NR_fsync/__NR_fdatasync) refuses
37# IO (102) and removes the partial output only if rc is 2 and the IO line is
38# present; a SIGSYS or other rc with no file must not PASS. A mutant that
39# clears the open flag before that check leaves the file.
40# 5 mutants that must fail: range >> 15 on every real unit (exit 0 and bytes differ from C, or
41# exit 3 from an ALARM; a refusal of every unit is a failed mutant), decoder renorm count
42# doubled (ALARM 1 on verify, the decode still finishes), upper-bound constants, ub rounding,
43# lower-bound constant, ofinish flag-clear order;
44# 6 Lean: docs/turing/proofs/TcrBound.lean checks with the pinned toolchain, 10 theorems, no
45# sorry, standard axioms only (lean found as $LEAN, on PATH or in ~/.elan/bin; missing = FAIL).
46set -u
47ROOT=$(cd "$(dirname "$0")/../.." && pwd)
48TC=$1
49OUT=$2
50OMC=${OMC:-$ROOT/seed/out/omc}
51mkdir -p "$OUT"
52fails=0
53pass() { echo "PASS $*"; }
54fail() { echo "FAIL $*"; fails=$((fails + 1)); }
55
56build() { # build <src.om> <exe>
57 { printf 'om2\n'; cat "$ROOT/seed/ng/sha256.om" "$1"; } > "$2.unit.om"
58 "$OMC" < "$2.unit.om" > "$2" 2> "$2.err" || return 1
59 chmod +x "$2"
60}
61expect_rc() { # expect_rc <name> <want> <cmd...>
62 name=$1; want=$2; shift 2
63 "$@" > "$OUT/last.out" 2> "$OUT/last.err"; rc=$?
64 if [ "$rc" -eq "$want" ]; then pass "$name (exit $rc: $(head -c 120 "$OUT/last.err"))"
65 else fail "$name: exit $rc, want $want"; fi
66}
67
68echo "omc sha256 $(sha256sum "$OMC" | cut -d' ' -f1)"
69B=$OUT/tcr-om
70if build "$ROOT/src/turing/tc_range.om" "$B"; then pass "build"; else fail "build: $(cat "$B.err")"; exit 1; fi
71
72if "$B" selftest > "$OUT/selftest.txt" 2>&1 && grep -q '^selftest=PASS$' "$OUT/selftest.txt"; then
73 pass "selftest $(head -n 1 "$OUT/selftest.txt")"
74else fail "selftest: $(cat "$OUT/selftest.txt")"; fi
75
76Z=0000000000000000000000000000000000000000000000000000000000000000
77C=$ROOT/calibration/experiments/EXP-001/candidates
78i=0
79for TR in tests/turing/fixtures/ctr1_seed1_crumbs05-07.ctr tests/turing/fixtures/ctr1_seed1_crumbs10-12.ctr; do
80 for M in evidence/TURING_YIELD/ty2_candidate.tym uniform:9 $C/B0_uniform.tym $C/B1_order0.tym \
81 $C/B2_order1.tym $C/B3_heuristic.tym $C/M_candidate.tym; do
82 i=$((i + 1)); D=$OUT/u$i; mkdir -p "$D"
83 case $M in uniform:*) MP=$M ;; /*) MP=$M ;; *) MP=$ROOT/$M ;; esac
84 if ! "$TC" pstream "$MP" $Z "$ROOT/$TR" "$D/s.tps" "$D/s.tsy" > "$D/pstream.txt" 2>&1; then
85 fail "unit $i pstream ($TR $M)"; continue; fi
86 "$TC" encode range "$D/s.tps" "$D/s.tsy" "$D/c.tcr" > /dev/null 2>&1 || fail "unit $i C encode"
87 if "$B" encode range "$D/s.tps" "$D/s.tsy" "$D/o.tcr" > "$D/enc.txt" 2> "$D/enc.err" \
88 && grep -q '^bound=PASS$' "$D/enc.txt" && grep -q '^lower=PASS$' "$D/enc.txt"; then :; else fail "unit $i Omega encode: $(cat "$D/enc.err")"; continue; fi
89 cmp -s "$D/o.tcr" "$D/c.tcr" || fail "unit $i Omega bytes differ from C"
90 "$TC" verify range "$D/s.tps" "$D/s.tsy" "$D/o.tcr" > /dev/null 2>&1 || fail "unit $i C rejects the Omega file"
91 "$B" verify range "$D/s.tps" "$D/s.tsy" "$D/c.tcr" > "$D/ver.txt" 2>&1 || fail "unit $i Omega rejects the C file"
92 "$B" decode range "$D/s.tps" "$D/o.tcr" "$D/o.tsy" > /dev/null 2>&1 || fail "unit $i Omega decode"
93 cmp -s "$D/o.tsy" "$D/s.tsy" || fail "unit $i decoded TSY1 differs"
94 ci=$(sed -n 's/^ideal_ub=//p' "$D/pstream.txt"); oi=$(sed -n 's/^ideal_ub=//p' "$D/enc.txt")
95 [ "$ci" = "$oi" ] || fail "unit $i ideal_ub C $ci Omega $oi"
96 echo "unit $i n=$(sed -n 's/^count=//p' "$D/enc.txt") ideal_ub=$oi coded_bits=$(sed -n 's/^coded_bits=//p' "$D/enc.txt") slack_ub=$(sed -n 's/^slack_ub=//p' "$D/enc.txt") lower_ub=$(sed -n 's/^lower_ub=//p' "$D/enc.txt") $(basename "$TR") $(basename "$M")"
97 done
98done
99[ "$fails" -eq 0 ] && pass "14 units: byte-identical to C both ways, decode exact, ideal_ub equal, bound=PASS, lower=PASS"
100
101# Refusals (unit 1 files). Each input has one fault; the Omega refusal name must equal the C
102# reference's (turing-coder prints TC_E_<NAME>) and both must exit 2.
103D=$OUT/u1; N=$OUT/neg; mkdir -p "$N"
104same_refusal() { # same_refusal <label> <want NAME> <args after the tool...>
105 label=$1; want=$2; shift 2
106 "$B" "$@" > "$N/o.out" 2> "$N/o.err"; orc=$?
107 "$TC" "$@" > "$N/c.out" 2> "$N/c.err"; crc=$?
108 on=$(sed -n 's/^tcr-om: refused -[0-9]* \([A-Z]*\) at .*/\1/p' "$N/o.err")
109 cn=$(sed -n 's/.*TC_E_\([A-Z]*\).*/\1/p' "$N/c.err" | head -n 1)
110 if [ "$orc" -eq 2 ] && [ "$crc" -eq 2 ] && [ "$on" = "$want" ] && [ "$cn" = "$want" ]; then
111 pass "$label: Omega and C both refuse $want"
112 else fail "$label: Omega exit $orc '$on', C exit $crc '$cn', want $want"; fi
113}
114sz=$(wc -c < "$D/o.tcr")
115cp "$D/o.tcr" "$N/flip.tcr"; printf '\125' | dd of="$N/flip.tcr" bs=1 seek=$((sz - 3)) conv=notrunc 2> /dev/null
116cmp -s "$N/flip.tcr" "$D/o.tcr" && fail "flip control did not change the file"
117same_refusal "flipped payload byte (verify)" CORRUPT verify range "$D/s.tps" "$D/s.tsy" "$N/flip.tcr"
118head -c $((sz - 1)) "$D/o.tcr" > "$N/trunc.tcr"
119same_refusal "truncated file" TRUNC verify range "$D/s.tps" "$D/s.tsy" "$N/trunc.tcr"
120{ cat "$D/o.tcr"; printf 'x'; } > "$N/trail.tcr"
121same_refusal "trailing byte" TRAIL verify range "$D/s.tps" "$D/s.tsy" "$N/trail.tcr"
122same_refusal "other TPS1" BINDING decode range "$OUT/u2/s.tps" "$D/o.tcr" "$N/x.tsy"
123cp "$D/o.tcr" "$N/magic.tcr"; printf 'X' | dd of="$N/magic.tcr" bs=1 seek=0 conv=notrunc 2> /dev/null
124same_refusal "altered magic" HEADER verify range "$D/s.tps" "$D/s.tsy" "$N/magic.tcr"
125cp "$D/s.tps" "$N/bad.tps"; printf '\001' | dd of="$N/bad.tps" bs=1 seek=200 conv=notrunc 2> /dev/null
126same_refusal "corrupted TPS1" DIGEST encode range "$N/bad.tps" "$D/s.tsy" "$N/y.tcr"
127same_refusal "TSY1 of another trace" COUNT encode range "$D/s.tps" "$OUT/u8/s.tsy" "$N/z.tcr"
128cp "$D/s.tsy" "$N/badsym.tsy"; printf '\001' | dd of="$N/badsym.tsy" bs=1 seek=60 conv=notrunc 2> /dev/null
129same_refusal "corrupted TSY1" DIGEST verify range "$D/s.tps" "$N/badsym.tsy" "$D/o.tcr"
130# A refusal after the output file was created must remove it (decode of the flipped file fails
131# its termination check at the end of the stream, after symbols were written).
132rm -f "$N/dflip.tsy"
133expect_rc "decode refusal after the output was created" 2 "$B" decode range "$D/s.tps" "$N/flip.tcr" "$N/dflip.tsy"
134grep -q 'refused -115 CORRUPT' "$OUT/last.err" || fail "decode of the flipped file: not CORRUPT"
135[ -e "$N/dflip.tsy" ] && fail "a refused decode left its output file" || pass "refused decode removed its partial output"
136# An output path naming an input is refused before anything is truncated.
137cp "$D/s.tps" "$N/self.tps"; cp "$D/s.tps" "$N/self.ref"
138expect_rc "output path = input refused" 2 "$B" encode range "$N/self.tps" "$D/s.tsy" "$N/self.tps"
139grep -q 'refused -101 ARG' "$OUT/last.err" || fail "output = input: not ARG"
140cmp -s "$N/self.tps" "$N/self.ref" && pass "input left intact" || fail "the input was changed or removed"
141expect_rc "usage" 1 "$B" encode rans "$D/s.tps" "$D/s.tsy" "$N/w.tcr"
142grep -q '^lower=PASS$' "$D/enc.txt" || fail "unit 1 has no lower=PASS line"
143
144# ofinish fsync failure. A small seccomp wrapper makes fsync and fdatasync return EIO.
145# The filter is compile-time arch-aware and lives in the generated fail_fsync.c:
146# #if defined(__x86_64__) AUDIT_ARCH_X86_64
147# #elif defined(__aarch64__) AUDIT_ARCH_AARCH64
148# #else #error
149# and the syscall numbers are __NR_fsync / __NR_fdatasync from <sys/syscall.h>.
150# On a native AArch64 host (Spark) that is the process arch and the AArch64
151# syscall numbers. On x86_64, qemu-user translates the guest fsync to the host
152# syscall, and the x86_64 wrapper catches that. Writes already succeeded, so
153# this hits ofinish, not oflush. The open flag must still be set so cleanup
154# unlinks the partial output.
155# "removed the partial output" PASSes only after rc 2 AND a refused -102 IO
156# line AND the file is gone. A SIGSYS (or any other rc) with no file must not
157# PASS: that was the vacuous pass in Test Master R-07 when the x86-only filter
158# killed the process before the file was created.
159ofinish_removed_ok() { # ofinish_removed_ok <rc> <errfile> <outfile>
160 [ "$1" -eq 2 ] && grep -q 'refused -102 IO' "$2" && [ ! -e "$3" ]
161}
162cat > "$N/fail_fsync.c" << 'C'
163#define _GNU_SOURCE
164#include <errno.h>
165#include <linux/audit.h>
166#include <linux/filter.h>
167#include <linux/seccomp.h>
168#include <stddef.h>
169#include <stdio.h>
170#include <sys/prctl.h>
171#include <sys/syscall.h>
172#include <unistd.h>
173#if defined(__x86_64__)
174#define FAIL_FSYNC_AUDIT_ARCH AUDIT_ARCH_X86_64
175#elif defined(__aarch64__)
176#define FAIL_FSYNC_AUDIT_ARCH AUDIT_ARCH_AARCH64
177#else
178#error "fail_fsync: only x86_64 and aarch64 are supported"
179#endif
180static int install(void) {
181 struct sock_filter filter[] = {
182 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch)),
183 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, FAIL_FSYNC_AUDIT_ARCH, 1, 0),
184 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),
185 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
186 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_fsync, 0, 1),
187 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | (EIO & 0xffff)),
188 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_fdatasync, 0, 1),
189 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | (EIO & 0xffff)),
190 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
191 };
192 struct sock_fprog prog = { .len = (unsigned short)(sizeof filter / sizeof filter[0]), .filter = filter };
193 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) return -1;
194 return (int)syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0, &prog);
195}
196int main(int argc, char **argv) {
197 if (argc < 2) { fprintf(stderr, "usage: fail_fsync CMD [ARGS]\n"); return 127; }
198 if (install() != 0) { perror("seccomp"); return 127; }
199 execvp(argv[1], argv + 1);
200 perror("exec");
201 return 127;
202}
203C
204if gcc -O2 -Wall -Werror -o "$OUT/fail_fsync" "$N/fail_fsync.c"; then
205 rm -f "$N/fsync.tcr"
206 expect_rc "ofinish fsync failure is IO" 2 "$OUT/fail_fsync" "$B" encode range "$D/s.tps" "$D/s.tsy" "$N/fsync.tcr"
207 grep -q 'refused -102 IO' "$OUT/last.err" || fail "ofinish fsync failure: not IO ($(head -c 160 "$OUT/last.err"))"
208 if ofinish_removed_ok "$rc" "$OUT/last.err" "$N/fsync.tcr"; then
209 pass "ofinish fsync failure removed the partial output"
210 else
211 fail "ofinish fsync failure: rc=$rc file=$([ -e "$N/fsync.tcr" ] && echo present || echo absent) (need rc 2, IO, and no file)"
212 fi
213 # Vacuous-removal guard: SIGSYS (exit 159) with no output file must not PASS.
214 # This is the R-07 shape (exit 159, no file, no IO line).
215 rm -f "$N/sigsys.tcr"
216 sh -c 'kill -s SYS $$' > "$N/sigsys.out" 2> "$N/sigsys.err"
217 src=$?
218 if ofinish_removed_ok "$src" "$N/sigsys.err" "$N/sigsys.tcr"; then
219 fail "ofinish cleanup PASSed on exit $src with no IO refusal (vacuous removal)"
220 else
221 pass "SIGSYS or other non-IO exit does not count as ofinish cleanup (exit $src)"
222 fi
223else
224 fail "fail_fsync wrapper did not compile"
225fi
226
227# Symbol-count cap. Both files are 128-byte headers plus a real trailer (160 bytes). n = 1
228# still dies on the body-size check (FORMAT). n = cap+1 dies on the word cap (ARG), which is
229# checked first so this does not require a terabyte of records.
230python3 - "$N/n1.tps" 1 "$N/ncap.tps" 1152514738907 << 'PY'
231import hashlib, struct, sys
232def write(path, n):
233 hdr = bytearray(128)
234 hdr[0:4] = b"TPS1"
235 struct.pack_into("<H", hdr, 4, 1)
236 hdr[6] = 2
237 hdr[7] = 16
238 struct.pack_into("<Q", hdr, 12, n)
239 struct.pack_into("<I", hdr, 20, 28)
240 dig = hashlib.sha256(b"turing.tc.pstream.v1\x00" + bytes(hdr)).digest()
241 with open(path, "wb") as f:
242 f.write(bytes(hdr) + dig)
243write(sys.argv[1], int(sys.argv[2]))
244write(sys.argv[3], int(sys.argv[4]))
245PY
246expect_rc "n = 1 with no records still fails the size check" 2 "$B" encode range "$N/n1.tps" "$N/absent.tsy" "$N/n1.out"
247grep -q 'refused -103 FORMAT' "$OUT/last.err" || fail "n = 1 header: not FORMAT ($(head -c 160 "$OUT/last.err"))"
248expect_rc "n = cap+1 refused" 2 "$B" encode range "$N/ncap.tps" "$N/absent.tsy" "$N/ncap.out"
249grep -q 'refused -101 ARG' "$OUT/last.err" || fail "n = cap+1: not ARG ($(head -c 160 "$OUT/last.err"))"
250expect_rc "wrapping micro-bit sum raises the overflow alarm" 3 "$B" selftest overflow
251grep -q 'would wrap' "$OUT/last.err" || fail "overflow alarm text missing ($(head -c 160 "$OUT/last.err"))"
252
253# Mutants.
254sed 's/^r = shr rg 16$/r = shr rg 15/' "$ROOT/src/turing/tc_range.om" > "$N/mut1.om"
255cmp -s "$N/mut1.om" "$ROOT/src/turing/tc_range.om" && fail "mutant 1 did not change the source"
256if build "$N/mut1.om" "$N/mut1"; then
257 good=0; refused=0; other=0; i=1
258 while [ "$i" -le 14 ]; do
259 "$N/mut1" encode range "$OUT/u$i/s.tps" "$OUT/u$i/s.tsy" "$N/m1-$i.tcr" > "$N/m1-$i.out" 2> "$N/m1-$i.err"
260 rc=$?
261 if [ "$rc" -eq 0 ] && [ -f "$N/m1-$i.tcr" ] && ! cmp -s "$N/m1-$i.tcr" "$OUT/u$i/c.tcr"; then
262 good=$((good + 1))
263 elif [ "$rc" -eq 3 ] && grep -q 'tcr-om: ALARM \(length identity\|length bound\|lower bound\)' "$N/m1-$i.err"; then
264 good=$((good + 1))
265 elif [ "$rc" -eq 2 ]; then
266 refused=$((refused + 1))
267 else
268 other=$((other + 1))
269 fi
270 i=$((i + 1))
271 done
272 if [ "$good" -ge 2 ]; then
273 pass "coder mutant (range >> 15) caught on $good units (exit 0 and bytes differ, or exit 3 identity/bound alarm); refusals=$refused other=$other"
274 else
275 fail "coder mutant: good=$good refused=$refused other=$other (need a real difference or the bound alarm on more than unit 1; a total refusal is not a catch)"
276 fi
277else fail "mutant 1 build"; fi
278# One renorm byte counted twice: the decode still consumes the payload, and ALARM 1 fires
279# because shifts + 4 is no longer the payload. A binary that only refuses does not pass.
280sed 's/^put DC 5 add get DC 5 1$/put DC 5 add get DC 5 2/' "$ROOT/src/turing/tc_range.om" > "$N/mut5.om"
281cmp -s "$N/mut5.om" "$ROOT/src/turing/tc_range.om" && fail "decoder shift mutant did not change the source"
282if build "$N/mut5.om" "$N/mut5"; then
283 i=1
284 while [ "$i" -le 2 ]; do
285 expect_rc "decoder shift mutant alarms on unit $i" 3 "$N/mut5" verify range "$OUT/u$i/s.tps" "$OUT/u$i/s.tsy" "$OUT/u$i/c.tcr"
286 grep -q 'length identity' "$OUT/last.err" || fail "decoder shift mutant unit $i: not the identity alarm ($(head -c 160 "$OUT/last.err"))"
287 i=$((i + 1))
288 done
289else fail "decoder shift mutant build"; fi
290sed 's/umul n 5647 480000000/umul n 0 470000000/' \
291 "$ROOT/src/turing/tc_range.om" > "$N/mut2.om"
292cmp -s "$N/mut2.om" "$ROOT/src/turing/tc_range.om" && fail "mutant 2 did not change the source"
293if build "$N/mut2.om" "$N/mut2"; then
294 expect_rc "bound mutant raises the ALARM" 3 "$N/mut2" encode range "$D/s.tps" "$D/s.tsy" "$N/m2.tcr"
295else fail "mutant 2 build"; fi
296sed 's/^ret shr add mul x 1000000 2147483648 32$/ret shr add mul x 1000000 0 32/' \
297 "$ROOT/src/turing/tc_range.om" > "$N/mut3.om"
298cmp -s "$N/mut3.om" "$ROOT/src/turing/tc_range.om" && fail "mutant 3 did not change the source"
299if build "$N/mut3.om" "$N/mut3"; then
300 expect_rc "ub rounding mutant fails the exhaustive ub check" 3 "$N/mut3" selftest
301else fail "mutant 3 build"; fi
302sed 's/^lw = uadd get ST 1 472000000$/lw = uadd get ST 1 482000000/' "$ROOT/src/turing/tc_range.om" > "$N/mut4.om"
303cmp -s "$N/mut4.om" "$ROOT/src/turing/tc_range.om" && fail "mutant 4 did not change the source"
304if build "$N/mut4.om" "$N/mut4"; then
305 expect_rc "lower-bound mutant (10 bits too high) raises the ALARM" 3 "$N/mut4" encode range "$OUT/u2/s.tps" "$OUT/u2/s.tsy" "$N/m4.tcr"
306else fail "mutant 4 build"; fi
307# Old ofinish order: clear OS[4] after fclose, then refuse. The fsync-failure case must
308# leave the output (cleanup sees the flag already clear). A binary that still unlinks
309# does not catch the bug.
310python3 - "$ROOT/src/turing/tc_range.om" "$N/mut6.om" << 'PY'
311import sys
312src = open(sys.argv[1]).read()
313old = """fn ofinish to void
314note Flush, fsync and close. Refuse IO (102) on fsync or fclose failure before clearing
315note OS[4], so cleanup still unlinks the partial output. After a successful close,
316note zero OS[0] so the stale descriptor is not reused by a later fclose.
317oflush
318f = int get OS 0
319k = fsync f
320z = fclose f
321if lt k 0
322refuse 102 0
323end
324if lt z 0
325refuse 102 0
326end
327put OS 0 0
328put OS 4 0
329end
330"""
331new = """fn ofinish to void
332oflush
333f = int get OS 0
334k = fsync f
335z = fclose f
336put OS 4 0
337if lt k 0
338refuse 102 0
339end
340end
341"""
342if old not in src:
343 sys.stderr.write("ofinish block not found for the old-order mutant\n")
344 sys.exit(1)
345open(sys.argv[2], "w").write(src.replace(old, new, 1))
346PY
347cmp -s "$N/mut6.om" "$ROOT/src/turing/tc_range.om" && fail "ofinish old-order mutant did not change the source"
348if [ -x "$OUT/fail_fsync" ] && build "$N/mut6.om" "$N/mut6"; then
349 rm -f "$N/mut6.tcr"
350 "$OUT/fail_fsync" "$N/mut6" encode range "$D/s.tps" "$D/s.tsy" "$N/mut6.tcr" > "$OUT/last.out" 2> "$OUT/last.err"
351 rc=$?
352 if [ "$rc" -eq 2 ] && grep -q 'refused -102 IO' "$OUT/last.err" && [ -e "$N/mut6.tcr" ]; then
353 pass "ofinish old-order mutant leaves the output on fsync failure (exit 2 IO)"
354 else
355 fail "ofinish old-order mutant: exit $rc, file exists=$([ -e "$N/mut6.tcr" ] && echo yes || echo no) ($(head -c 160 "$OUT/last.err"))"
356 fi
357else fail "ofinish old-order mutant build or fail_fsync missing"; fi
358
359# Lean: the bound's proof must still check, with no sorry and only the standard axioms.
360# docs/turing/proofs/lean-toolchain pins the toolchain; elan's `lean` picks it from that folder.
361LEAN=${LEAN:-$(command -v lean || echo "$HOME/.elan/bin/lean")}
362if [ -x "$LEAN" ] || command -v "$LEAN" > /dev/null 2>&1; then
363 ( cd "$ROOT/docs/turing/proofs" && "$LEAN" TcrBound.lean ) > "$OUT/lean.txt" 2>&1; lrc=$?
364 nax=$(grep -c "depends on axioms\|does not depend on any axioms" "$OUT/lean.txt")
365 badax=$(grep "depends on axioms" "$OUT/lean.txt" | sed 's/.*\[\(.*\)\]/\1/' | tr ',' '\n' | tr -d ' ' |
366 grep -v '^propext$\|^Classical.choice$\|^Quot.sound$' | sort -u | tr '\n' ' ')
367 if [ "$lrc" -eq 0 ] && [ "$nax" -eq 10 ] && [ -z "$badax" ] && ! grep -q "sorry" "$OUT/lean.txt"; then
368 pass "lean TcrBound.lean ($(cat "$ROOT/docs/turing/proofs/lean-toolchain")): 10 theorems, standard axioms only"
369 else fail "lean: exit $lrc, $nax axiom lines, other axioms '$badax': $(head -c 400 "$OUT/lean.txt")"; fi
370else fail "lean not found (set LEAN): the proof of the bound cannot be checked"; fi
371
372if [ "$fails" -eq 0 ]; then echo "TC-RANGE-OM PASS"; exit 0; fi
373echo "TC-RANGE-OM FAIL ($fails)"; exit 1