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 / seed / omc / gate-pools.sh

735 lines · 35020 bytes Raw Blame History
1#!/bin/sh
2# gate-pools.sh [OMC] -- run-time pools (docs/RUNTIME-POOLS.md sections 3, 4, 5, 7 and 11). The compiler
3# under test is the argument, else $OMC, else seed/out/omc. The control CTRL (environment) defaults to the
4# base omc 1e628c80..., read from history (f615b139), with the pinned bool-field correction and the rules
5# copied from the current compiler below. Cases: seed/omc/tests/pools
6# (NAME.om, NAME.want: stdout, then `status N`). One line per check; last line RC=0 when all passed.
7# 0. Controls: CTRL refuses `global P ulong *` (error 16) and `enlarge` (error 3), and every pools case.
8# The cases are generated: `python3 seed/omc/tests/pools/mk.py DIR` gives every committed NAME.om and
9# NAME.want byte for byte (fuzz.py writes the fuzz units at run time).
10# 1. Growth and contents (g1): 0 -> 1 .. 4096 -> 2^20 -> 2^27 elements, a uchar pool past 16777216; old
11# values kept, new elements 0, an enlarge at or below the length changes nothing. gf: an f64 and an
12# f32 pool grown and moved, values read back across every grow, len, index = length traps 3.
13# 2. Range (g2a-d): index = length traps 3 (exit 103) with no write past it; length + 1 is in range after
14# a grow; a number index past the length compiles (no error 21) and traps 3; a write slice past it.
15# 3. Stale base (g3, g3i as OM-2 inline bodies): enlarge inside the value of put, the index of get and
16# the counts of read and write; strace shows mremap moved the regions and their addresses reused.
17# gr: a uchar pool filled by `read 0 P off n` from a pipe (seq 1 200000), n enlarging P every read.
18# 4. Pinning (g4): an OM-2 unit whose first global is a pool; objdump shows the same set of X18..X26
19# written as the unit without the pool (g4base), so no pinned register holds the pool.
20# 5. Contracts (g5a, g5b): `requires eq len P 0` and `invariant le i len P` are reported
21# `omc-check 1 kept ... rule=runtime` by om2 diag and evaluated at run time (exit 109, 110); a constant
22# index under unroll (g5c) and inline (g5d) keeps its run-time check. Control: len of a sized global
23# folds (g5ctl: erased rule=constant-true).
24# 7. Fuzz differential: fuzz.py seeds $POOLS_FUZZ_SEEDS (default 1..8), 400 operations each, against a
25# Python list model, compared at every step (first differing step named).
26# 8. Host refusal: under prlimit --as=268435456 enlarge returns -12 exactly, the pool is unchanged and the
27# program ends with its own status 7 (g8a; control: without the limit the same enlarge gives 0);
28# n * element size at or past 2^63 gives -22 (g8b). Recorded only: g8c touching 128 MiB under
29# systemd-run --user --scope -p MemoryMax=64M (expected 137), NOT_RUN without a user manager.
30# 9. Untouched pages (g9): a 2 GiB pool with one page touched adds at most 16 kB of VmRSS; control:
31# touching 1024 more pages adds at least 3584 kB.
32# 10. Refusals (err-*.om, exact `omc: line N: error K`, N `*` = any line); for the host-word and target
33# cases, the same unit with a sized global compiles (the pool is what is refused).
34# 11. Byte identity: `git grep -nw enlarge` names no tracked unit outside tests/pools except in `note`
35# lines (comments, listed: omc.om documents its own keyword); every other tracked
36# .om gives the same status, stdout and stderr under OMC and CTRL. POOLS_SKIP_EQUIV=1 skips the
37# comparison (NOT_RUN); POOLS_EQUIV_MAX=N compares only the first N files.
38# S. Server (design 11, srv): 2000 requests of growing and shrinking size from one pool; enlarge calls
39# and growth syscalls (strace) equal the high-water marks of the model; VmRSS at the end is at most
40# the high-water VmRSS + 1024 kB. Control: the leak mode (stdin L) rises past it.
41# M. Mutants (mutants.tsv: NAME, CASE, MARKERS): omc.om with, for each marker m, the line after
42# `note POOLMUT m` replaced by the text after `note POOLMUT-ALT m ` (`note removed` deletes it),
43# compiled by OMC; it must compile g4base right and FAIL its CASE.
44# X. x86linux (docs/X86-LINUX.md): OMC builds the x86-64 compiler from `target x86linux` + omc.om; under
45# qemu-x86_64 (EMULATED, not native x86) it rebuilds itself byte for byte and builds OMC's AArch64
46# compiler byte for byte from omc.om; every g* case and gr, prefixed `target x86linux`, gives
47# NAME.want. Mutants must FAIL their case: x86cmp (no bounds check, g2a), x86base (base not stored
48# after mremap, g1), x86belowlen (n below the length shrinks, g1), x86ovf (no 2^63 overflow test, g8b),
49# x86slotfail (a refused mmap/mremap stored as the base, g8a). Without qemu-x86_64 the section is
50# NOT_RUN and the gate's last line says so.
51# POOLS_ONLY="1 3 M" runs only the named sections (0 always runs).
52set -eu
53O0=$(pwd)
54R=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd)
55cd "$R"
56S=${1:-${OMC:-seed/out/omc}}
57case $S in /*) ;; *) if [ -n "${1:-${OMC:-}}" ] && [ -x "$O0/$S" ]; then S=$O0/$S; else S=$R/$S; fi ;; esac
58[ -x "$S" ] || { printf 'FAIL: %s missing\n' "$S" >&2; exit 1; }
59T=seed/omc/tests/pools
60mkdir -p build/seed
61B=$(mktemp -d build/seed/pools.XXXXXX)
62B=$R/$B
63trap 'rm -rf "$B"' EXIT HUP INT TERM
64bad() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
65say() { printf '%s\n' "$*"; }
66on() { [ -z "${POOLS_ONLY:-}" ] && return 0; case " $POOLS_ONLY " in *" $1 "*) return 0 ;; esac; return 1; }
67for t in python3 objdump strace prlimit timeout; do command -v $t > /dev/null || bad "$t not found"; done
68printf abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN > $B/letters
69seq 1 200000 > $B/seqin
70{ cat $B/seqin; wc -c < $B/seqin; echo "status 0"; } > $B/gr.want
71FUZZOPS=400
72say "compiler under test $S $(sha256sum "$S" | cut -c1-16)"
73
74BASE=1e628c80d5bd2ad0b08eb46d8abff66eff284a74e5a302e589c2d61cfe9f7347
75if [ -n "${CTRL:-}" ]; then
76 case $CTRL in /*) ;; *) CTRL=$O0/$CTRL ;; esac
77 [ -x "$CTRL" ] || bad "control: $CTRL missing"
78else
79 git cat-file blob f615b139:seed/out/omc > $B/ctrl 2> /dev/null || bad "control: commit f615b139 is not in this clone"
80 [ "$(sha256sum $B/ctrl | cut -d' ' -f1)" = $BASE ] || bad "control: f615b139:seed/out/omc is not omc $BASE"
81 chmod +x $B/ctrl
82 # Historical no-pools compiler (f615b139). The bool-field opcode correction is
83 # checked on its own pinned image. The reference then learns later rules by
84 # copying them out of the current seed/omc/omc.om (the fourth argument), not
85 # by freezing that source:
86 # kwa, kwx, and the kfdctl/kfdmap/kfdres keyword ids 241..243
87 # fn kfdreg through fn hostval, which is the current KFD lowering, including
88 # the drm/xe ioctl allowlist and kxezero
89 # mnew, xhmnew, aret, aefival
90 # a64efi: xefa, bare-unit error 25, host words error 25, read/write error 25,
91 # reserved firmware names error 16, missing efirt error 16
92 # s_global: a pool declaration with no fn main is error 9; with fn main it
93 # stays error 16, because this control still has no pool lowering
94 mv "$B/ctrl" "$B/ctrl-raw"
95 git cat-file blob f615b139:seed/omc/omc.om > "$B/ctrl-src.om"
96 python3 - "$B/ctrl-raw" "$B/ctrl-src.om" "$B/ctrl" "$R/seed/omc/omc.om" <<'PYBOOL'
97from pathlib import Path
98import hashlib, subprocess, sys
99raw, source, out, current = map(Path, sys.argv[1:])
100sha = lambda b: hashlib.sha256(b).hexdigest()
101assert sha(raw.read_bytes()) == '1e628c80d5bd2ad0b08eb46d8abff66eff284a74e5a302e589c2d61cfe9f7347'
102data = source.read_bytes()
103assert sha(data) == 'de4099cad761a315d6bf9898bc29eb18b9eedddb568594d41e80d0de462f4199'
104s = data.decode()
105for fn, old, new in [
106 ('ldop', 'if eq t 2', 'if or eq t 1 eq t 2'),
107 ('stop', 'if or eq t 2 eq t 6', 'if or or eq t 1 eq t 2 eq t 6'),
108]:
109 start = s.index('fn '+fn+' t ulong to ulong\n')
110 end = s.index('\nfn ', start + 1)
111 part = s[start:end]
112 assert part.count(old+'\n') == 1
113 s = s[:start] + part.replace(old+'\n', new+'\n') + s[end:]
114assert sha(s.encode()) == '33e993b566c5dbd36fa9d3a063584019485777841d4e4252655503bd9374b418'
115bool_image = subprocess.run([raw], input=s.encode(), capture_output=True, check=True, timeout=60).stdout
116assert sha(bool_image) == 'a0db6165a56811f1315cb445b037568a13bd40d82a155636a53550d123db8748'
117bool_path = out.with_name('ctrl-bool')
118bool_path.write_bytes(bool_image); bool_path.chmod(0o700)
119probe = b'struct W a uint f bool end\nfn main to int\nw = new W 1 false\nput w f true\nif get w f\nret 7\nend\nret 1\nend\n'
120for compiler, want, suffix in [(raw, -4, 'raw'), (bool_path, 7, 'corrected')]:
121 p = subprocess.run([compiler], input=probe, capture_output=True, check=True, timeout=60)
122 exe = out.with_name('bool-probe-'+suffix)
123 exe.write_bytes(p.stdout); exe.chmod(0o700)
124 result = subprocess.run([exe], capture_output=True, timeout=10)
125 assert result.returncode == want, (suffix, result.returncode, want)
126print('PASS historical control bool correction: original SIGILL, corrected exit 7; source and image pins match')
127
128def fn_span(text, name, end_name=None):
129 lines = text.splitlines(keepends=True)
130 start = next(i for i, line in enumerate(lines) if line.startswith('fn '+name+' '))
131 if end_name is None:
132 end = next(i for i, line in enumerate(lines) if i > start and line.startswith('fn '))
133 else:
134 end = next(i for i, line in enumerate(lines) if i > start and line.startswith('fn '+end_name+' '))
135 return ''.join(lines[start:end])
136
137def replace_fn(text, name, body):
138 lines = text.splitlines(keepends=True)
139 start = next(i for i, line in enumerate(lines) if line.startswith('fn '+name+' '))
140 end = next(i for i, line in enumerate(lines) if i > start and line.startswith('fn '))
141 if not body.endswith('\n'):
142 body += '\n'
143 return ''.join(lines[:start]) + body + ''.join(lines[end:])
144
145cur = current.read_text()
146kfd = fn_span(cur, 'kfdreg', 'hostval')
147mnew = fn_span(cur, 'mnew')
148xhmnew = fn_span(cur, 'xhmnew')
149aret = fn_span(cur, 'aret', 'aefival')
150aefival = fn_span(cur, 'aefival', 'afw')
151kwa = fn_span(cur, 'kwa')
152kwx = fn_span(cur, 'kwx')
153assert 'fn kxezero ' in kfd
154assert '3224396870' in kfd
155assert 'global d2site ' not in s
156s = s.replace(
157 'global xtarget ulong 1\n',
158 'global xtarget ulong 1\n'
159 'note xefa: 1 when the unit\'s first line is target a64efi. 0 on every other unit.\n'
160 'global xefa ulong 1\n'
161 'note d2site: one cell for the fail-path writes copied with the KFD and a64efi code.\n'
162 'global d2site ulong 1\n',
163 1)
164s = s.replace('fn kw w ulong to ulong\n', kwa + kwx + 'fn kw w ulong to ulong\n', 1)
165old_kw = (
166 'if eq w 31359662648747367\n'
167 'ret 240\n'
168 'end\n'
169 'if eq w 461229747302\n'
170)
171new_kw = (
172 'if eq w 31359662648747367\n'
173 'ret 240\n'
174 'end\n'
175 'if eq w 119247139530347\n'
176 'ret 241\n'
177 'end\n'
178 'if eq w 123563749434987\n'
179 'ret 242\n'
180 'end\n'
181 'if eq w 126879548073579\n'
182 'ret 243\n'
183 'end\n'
184 'if eq w 461229747302\n'
185)
186assert s.count(old_kw) == 1
187s = s.replace(old_kw, new_kw, 1)
188s = replace_fn(s, 'mnew', mnew)
189s = replace_fn(s, 'xhmnew', xhmnew)
190hook = (
191 'if and ge c 241 le c 243\n'
192 'mtinit\n'
193 'if eq c 241\n'
194 'kctl dst x\n'
195 'else\n'
196 'mnew c dst x\n'
197 'end\n'
198 'ret\n'
199 'end\n'
200)
201anchor = (
202 'x = mul get depth 0 32\n'
203 'put a_ar add get a_rb 77 add x 7 get nslot 0\n'
204 'note VFIO words are program-only. These IDs are disjoint from efi178, qty223 and peername224.\n'
205)
206assert s.count(anchor) == 1
207s = s.replace(anchor, anchor.split('note VFIO')[0] + hook + 'note VFIO words are program-only. These IDs are disjoint from efi178, qty223 and peername224.\n', 1)
208xhook = (
209 'if and ge c 241 le c 243\n'
210 'mtinit\n'
211 'if eq c 241\n'
212 'kctl dst x\n'
213 'else\n'
214 'xhmnew c dst x\n'
215 'end\n'
216 'ret\n'
217 'end\n'
218)
219xanchor = (
220 'x = mul get depth 0 32\n'
221 'put a_ar add get a_rb 77 add x 7 get nslot 0\n'
222 'if and ge c 236 le c 238\n'
223)
224assert s.count(xanchor) == 1
225s = s.replace(xanchor, xanchor.split('if and ge c 236')[0] + xhook + 'if and ge c 236 le c 238\n', 1)
226disp = 'if or or and ge c 228 le c 232 or eq c 233 eq c 235 and ge c 236 le c 240\n'
227assert s.count(disp) == 1
228s = s.replace(disp, disp.replace('le c 240', 'le c 243'), 1)
229host = 'fn hostval c ulong dst ulong to void\n'
230assert s.count(host) == 1
231s = s.replace(host, kfd + host, 1)
232s = s.replace(
233 'fn newname w ulong l ulong s ulong to void\n',
234 'fn newname w ulong l ulong s ulong to void\n'
235 'if le l 8\n'
236 'if ne kwx w 0\n'
237 'fail 16\n'
238 'end\n'
239 'end\n',
240 1)
241s = s.replace(
242 'fn addname w ulong r ulong t ulong ln ulong k ulong a ulong to void\n',
243 'fn addname w ulong r ulong t ulong ln ulong k ulong a ulong to void\n'
244 'if ne kwx w 0\n'
245 'fail 16\n'
246 'end\n',
247 1)
248s = s.replace(
249 'xhost c dst\n'
250 'ret\n'
251 'end\n'
252 'if ne get xtarget 0 0\n'
253 'fail 25\n'
254 'end\n'
255 'note Function mode (no program image: no argv in x27, no x28 stack floor, no globals) refuses host\n',
256 'xhost c dst\n'
257 'ret\n'
258 'end\n'
259 'if ne get xtarget 0 0\n'
260 'fail 25\n'
261 'end\n'
262 'note target a64efi has no Linux beneath it: every host word is error 25.\n'
263 'if ne get xefa 0 0\n'
264 'fail 25\n'
265 'end\n'
266 'note Function mode (no program image: no argv in x27, no x28 stack floor, no globals) refuses host\n',
267 1)
268s = s.replace(
269 'if and eq get tw 0 115888505632888 eq get tl 0 6\n'
270 'put xtarget 0 2\n'
271 'else\n'
272 'fail 25\n'
273 'end\n',
274 'if and eq get tw 0 115888505632888 eq get tl 0 6\n'
275 'put xtarget 0 2\n'
276 'else\n'
277 'if and eq get tw 0 115888505501281 eq get tl 0 6\n'
278 'put xefa 0 1\n'
279 'else\n'
280 'fail 25\n'
281 'end\n'
282 'end\n',
283 1)
284s = s.replace(
285 'c = kw get tw 0\n'
286 'if eq c 178\n'
287 'xefival dst\n'
288 'ret\n'
289 'end\n',
290 'c = kw get tw 0\n'
291 'if ne get xefa 0 0\n'
292 'ca = kwa get tw 0\n'
293 'if eq ca 178\n'
294 'aefival dst\n'
295 'ret\n'
296 'end\n'
297 'end\n'
298 'if eq c 178\n'
299 'xefival dst\n'
300 'ret\n'
301 'end\n',
302 1)
303s = s.replace(
304 'if ne get xtarget 0 0\n'
305 'put oc 0 512\n'
306 'if eq get xtarget 0 2\n'
307 'if eq get progmode 0 0\n'
308 'fail 25\n'
309 'end\n',
310 'if ne get xefa 0 0\n'
311 'if eq get progmode 0 0\n'
312 'fail 25\n'
313 'end\n'
314 'end\n'
315 'if ne get xtarget 0 0\n'
316 'put oc 0 512\n'
317 'if eq get xtarget 0 2\n'
318 'if eq get progmode 0 0\n'
319 'fail 25\n'
320 'end\n',
321 1)
322old_vs = (
323 'fn vs_call c ulong fk ulong fv ulong e ulong ro ulong rn ulong to void\n'
324 'if ne get xtarget 0 0\n'
325 'xio c fk fv e ro rn\n'
326 'ret\n'
327 'end\n'
328 'save\n'
329)
330new_vs = (
331 'fn vs_call c ulong fk ulong fv ulong e ulong ro ulong rn ulong to void\n'
332 'if ne get xtarget 0 0\n'
333 'xio c fk fv e ro rn\n'
334 'ret\n'
335 'end\n'
336 'note target a64efi: read and write are Linux system calls; there is no Linux beneath the image\n'
337 'if ne get xefa 0 0\n'
338 'fail 25\n'
339 'end\n'
340 'save\n'
341)
342assert s.count(old_vs) == 1
343s = s.replace(old_vs, new_vs, 1)
344needle = 'fn xefival '
345assert needle in s
346s = s.replace(needle, aret + aefival + needle, 1)
347old_sg = (
348 'fn s_global to void\n'
349 'w = topname\n'
350 't = typeword\n'
351 'elemtype t\n'
352 'n = lenval 16\n'
353)
354new_sg = (
355 'fn s_global to void\n'
356 'w = topname\n'
357 't = typeword\n'
358 'elemtype t\n'
359 'note a lone * after the element type is a run-time pool. With no fn main, program mode is\n'
360 'note off and the current compiler refuses that declaration with error 9 (sg_pool). A pool\n'
361 'note in program mode stays unknown here, so lenval still reports error 16.\n'
362 'pc = get cur 0\n'
363 'pl = get line 0\n'
364 'tok\n'
365 'if eq get tk 0 1\n'
366 'if eq get tl 0 1\n'
367 'if eq get tw 0 42\n'
368 'if eq get progmode 0 0\n'
369 'fail 9\n'
370 'end\n'
371 'end\n'
372 'end\n'
373 'end\n'
374 'put cur 0 pc\n'
375 'put line 0 pl\n'
376 'n = lenval 16\n'
377)
378assert s.count(old_sg) == 1
379s = s.replace(old_sg, new_sg, 1)
380assert sha(s.encode()) == 'e1b19576c759b78cb3febb761e1e6409ea29694dfe7abda9d20248db433f5852'
381image = subprocess.run([raw], input=s.encode(), capture_output=True, check=True, timeout=180).stdout
382assert sha(image) == '58f91dbdebf6ba0d4d17a12372897916dbf3b7d6cb62a3d17a607d91b21ba766'
383out.write_bytes(image); out.chmod(0o700)
384p = subprocess.run([out], input=probe, capture_output=True, check=True, timeout=60)
385exe = out.with_name('bool-probe-final')
386exe.write_bytes(p.stdout); exe.chmod(0o700)
387result = subprocess.run([exe], capture_output=True, timeout=10)
388assert result.returncode == 7, result.returncode
389print('PASS historical control a64efi refusals, KFD words, xe allowlist and pool-without-main: source and image pins match')
390PYBOOL
391 CTRL=$B/ctrl
392fi
393say "control $CTRL $(sha256sum "$CTRL" | cut -c1-16)"
394
395firstdiff() { # firstdiff GOT WANT -> the first differing line (the step), on stdout
396 awk 'NR == FNR { g[FNR] = $0; n = FNR; next }
397 { m = FNR; if (!(FNR in g) || g[FNR] != $0) { printf "step %d: got \"%s\", wanted \"%s\"", FNR, (FNR in g) ? g[FNR] : "(nothing)", $0; f = 1; exit } }
398 END { if (!f && n > m) printf "step %d: got \"%s\", wanted nothing", m + 1, g[m + 1] }' "$1" "$2"
399}
400regs() { # regs BIN -> the sorted set of X18..X26 that the code writes (ELF headers skipped)
401 ph=$(od -An -j56 -N2 -tu2 "$1" | tr -d ' ')
402 objdump -D -b binary -m aarch64 --start-address=$((64 + 56 * ph)) "$1" | awk -F'\t' 'NF >= 4 {
403 m = $3; gsub(/ /, "", m)
404 if (m ~ /^(st.*|cmp|cmn|tst|cbn?z|tbn?z|b|b\..*|bl|ret|br|blr|prfm)$/) next
405 n = split($4, o, ","); k = (m ~ /^ldp/) ? 2 : 1
406 for (j = 1; j <= k && j <= n; j++) { r = o[j]; gsub(/[ \t]/, "", r); sub(/^w/, "x", r)
407 if (r ~ /^x(1[89]|2[0-6])$/) s[r] = 1 } }
408 END { for (r in s) print r }' | sort | tr '\n' ' '
409}
410dline() { grep -nxF "$2" "$T/$1.om" | cut -d: -f1; }
411ck() { # ck CC NAME: compile and run case NAME with compiler CC; 0 when it gives NAME.want, else 1 and $B/why
412 cc=$1; n=$2; src=$T/$n.om; want=$T/$n.want; pre=; inp=/dev/null
413 case $n in fuzz:*)
414 s=${n#fuzz:}; src=$B/fz$s.om; want=$B/fz$s.want
415 [ -f $src ] || python3 $T/fuzz.py $s $FUZZOPS $src $want ;;
416 esac
417 case $n in g3|g3i) inp=$B/letters ;; gr) inp=$B/seqin; want=$B/gr.want ;; g8a) pre="prlimit --as=268435456" ;; esac
418 "$cc" < $src > $B/c.bin 2> $B/c.err; rc=$?
419 [ $rc = 0 ] || { echo "$n: compile $rc ($(head -c 200 $B/c.err))" > $B/why; return 1; }
420 chmod +x $B/c.bin
421 if [ $n = gr ]; then cat $inp | $pre timeout ${TO:-300} $B/c.bin > $B/c.out 2> /dev/null; x=$?
422 else $pre timeout ${TO:-300} $B/c.bin < $inp > $B/c.out 2> /dev/null; x=$?; fi
423 echo "status $x" >> $B/c.out
424 cmp -s $B/c.out $want || { echo "$n: $(firstdiff $B/c.out $want)" > $B/why; return 1; }
425 case $n in
426 g4) r1=$(regs $B/c.bin)
427 "$cc" < $T/g4base.om > $B/c.base 2> /dev/null || { echo "g4base: compile failed" > $B/why; return 1; }
428 r0=$(regs $B/c.base)
429 [ "$r1" = "$r0" ] || { echo "g4: X18..X26 written {$r1}, without the pool {$r0}" > $B/why; return 1; }
430 echo "$r1" > $B/regs ;;
431 g5a|g5b)
432 if [ $n = g5a ]; then L=$(dline g5a 'requires eq len P 0'); k=requires; else L=$(dline g5b 'invariant le i len P'); k=invariant; fi
433 w="omc-check 1 kept line=$L rule=runtime kind=$k"
434 grep -qxF "$w" $B/c.err || { echo "$n: no '$w' in om2 diag ($(grep omc-check $B/c.err | head -2 | tr '\n' ';'))" > $B/why; return 1; } ;;
435 esac
436 return 0
437}
438run() { # run NAME: ck with the compiler under test, or fail the gate
439 ck "$S" $1 || bad "$(cat $B/why)"
440}
441
442# 0. controls: the base compiler does not know the new words
443printf 'global P ulong *\nfn main to int\nret 0\nend\n' > $B/w.om
444"$CTRL" < $B/w.om > /dev/null 2> $B/w.err && bad "control: CTRL accepted 'global P ulong *'"
445[ "$(head -1 $B/w.err)" = "omc: line 1: error 16" ] || bad "control: CTRL on 'global P ulong *': $(head -1 $B/w.err)"
446printf 'global P ulong 4\nfn main to int\nr = enlarge P ulong 8\nret 0\nend\n' > $B/w.om
447"$CTRL" < $B/w.om > /dev/null 2> $B/w.err && bad "control: CTRL accepted enlarge"
448[ "$(head -1 $B/w.err)" = "omc: line 3: error 3" ] || bad "control: CTRL on enlarge: $(head -1 $B/w.err)"
449say "PASS control: CTRL refuses 'global P ulong *' (error 16) and enlarge (error 3)"
450set +e; python3 $T/mk.py $B/regen > $B/regen.log 2>&1; rc=$?; set -e
451[ $rc = 0 ] || bad "control: mk.py exit $rc ($(head -c 200 $B/regen.log))"
452n=0
453for f in $B/regen/*; do
454 cmp -s $f $T/$(basename $f) || bad "control: mk.py output $(basename $f) differs from $T/$(basename $f)"
455 n=$((n + 1))
456done
457for f in $T/*.om $T/*.want; do [ -f $B/regen/$(basename $f) ] || bad "control: $f is not written by mk.py"; done
458say "PASS control: mk.py (exit 0) regenerates all $n committed case files byte for byte"
459n=0
460for f in $T/g*.om $T/srv.om; do
461 c=$(basename $f .om); case $c in g4base|g5ctl) continue ;; esac
462 "$CTRL" < $f > /dev/null 2> $B/w.err && bad "control: CTRL accepted $c"
463 grep -q '^omc: line [0-9]*: error \(16\|3\)$' $B/w.err || bad "control: CTRL on $c: $(head -1 $B/w.err)"
464 n=$((n + 1))
465done
466say "PASS control: CTRL refuses all $n pools cases (error 16 or 3)"
467
468# 1. growth and contents
469if on 1; then
470 run g1; say "PASS 1 growth: g1 0..2^27 elements, uchar pool past 16777216, old kept, new 0"
471 run gf; say "PASS 1 growth: gf f64 and f32 pools grown 40 times beside a blocker pool, values kept, len, index = len traps 3"
472fi
473
474# 2. range
475if on 2; then
476 for c in g2a g2b g2c g2d; do run $c; say "PASS 2 range: $c, $(sed -n '1s/^note gate-pools 2: //p' $T/$c.om | cut -c1-110)"; done
477fi
478
479# 3. stale base
480if on 3; then
481 for c in g3 g3i; do
482 run $c
483 strace -f -qq -e trace=mmap,mremap,munmap -o $B/st $B/c.bin < $B/letters > /dev/null 2>&1 || true
484 mr=$(python3 - $B/st << 'EOF'
485import re, sys
486moves = reuse = 0; freed = []
487def hit(a, l):
488 return any(f < a + l and a < f + fl for f, fl in freed)
489for line in open(sys.argv[1]):
490 m = re.search(r"mremap\((0x[0-9a-f]+), (\d+), (\d+), [^)]*\) = (0x[0-9a-f]+)", line)
491 if m:
492 old, ol, nl, new = int(m[1], 16), int(m[2]), int(m[3]), int(m[4], 16)
493 if new != old:
494 reuse += hit(new, nl); moves += 1; freed.append((old, ol))
495 continue
496 m = re.search(r"mmap\(NULL, (\d+),.*\) = (0x[0-9a-f]+)", line)
497 if m:
498 reuse += hit(int(m[2], 16), int(m[1]))
499print(moves, reuse)
500EOF
501)
502 set -- $mr
503 [ "$1" -gt 0 ] && [ "$2" -gt 0 ] || bad "3 $c: mremap moves $1, address reuses $2 (both must be above 0)"
504 say "PASS 3 stale base: $c right after $1 moving mremaps and $2 reuses of a vacated address"
505 done
506 run gr
507 say "PASS 3 stale base: gr read 0 P off n from a pipe, n enlarging P before each read: $(wc -c < $B/seqin) bytes back"
508fi
509
510# 4. pinning
511if on 4; then run g4; say "PASS 4 pinning: g4 X18..X26 written {$(cat $B/regs | sed 's/ $//')} = g4base's; output right"; fi
512
513# 5. contracts
514if on 5; then
515 run g5a; say "PASS 5 contracts: g5a requires eq len P 0 kept rule=runtime, false after the grow (109)"
516 run g5b; say "PASS 5 contracts: g5b invariant le i len P kept rule=runtime, false at 9 (110)"
517 run g5c; say "PASS 5 contracts: g5c constant index under unroll keeps its run-time check (103)"
518 run g5d; say "PASS 5 contracts: g5d constant index in an inline body keeps its run-time check (103)"
519 "$S" < $T/g5ctl.om > /dev/null 2> $B/w.err || bad "5 control: g5ctl does not compile"
520 w="omc-check 1 erased line=$(dline g5ctl 'requires eq len G 8') rule=constant-true kind=requires"
521 grep -qxF "$w" $B/w.err || bad "5 control: g5ctl has no '$w'"
522 say "PASS 5 control: len of a sized global folds (g5ctl erased rule=constant-true)"
523fi
524
525# 7. fuzz differential
526if on 7; then
527 for s in ${POOLS_FUZZ_SEEDS:-1 2 3 4 5 6 7 8}; do
528 run fuzz:$s
529 say "PASS 7 fuzz seed $s: $(($(wc -l < $B/fz$s.want) - 1)) steps equal to the list model, $(tail -1 $B/fz$s.want)"
530 done
531fi
532
533# 8. host refusal
534if on 8; then
535 run g8a; say "PASS 8 refusal: g8a under prlimit --as=268435456 enlarge gives -12, pool unchanged, own status 7"
536 timeout 300 $B/c.bin > $B/c.out 2> /dev/null || true
537 [ "$(sed -n 2p $B/c.out)" = 0 ] || bad "8 control: without the limit the 8 GiB enlarge gave '$(sed -n 2p $B/c.out)', wanted 0"
538 say "PASS 8 control: without the limit the same 8 GiB enlarge gives 0"
539 run g8b; say "PASS 8 overflow: g8b n * size at or past 2^63 gives -22, just below -12, pool unchanged"
540 "$S" < $T/g8c.om > $B/g8c 2> $B/w.err || bad "8 g8c: compile failed ($(head -1 $B/w.err))"
541 chmod +x $B/g8c
542 if systemd-run --user --scope -q true > /dev/null 2>&1; then
543 set +e; systemd-run --user --scope -q -p MemoryMax=64M $B/g8c > /dev/null 2>&1; x=$?; set -e
544 say "RECORD 8 cgroup: g8c touching 128 MiB under systemd-run --user --scope -p MemoryMax=64M: exit $x (expected 137)"
545 else
546 say "NOT_RUN 8 cgroup: systemd-run --user is unavailable (g8c, MemoryMax=64M)"
547 fi
548fi
549
550# 9. untouched pages
551if on 9; then
552 "$S" < $T/g9.om > $B/g9 2> $B/w.err || bad "9: compile failed ($(head -1 $B/w.err))"
553 chmod +x $B/g9
554 set +e; timeout 300 $B/g9 > $B/g9.out; x=$?; set -e
555 [ $x = 0 ] || bad "9: g9 exit $x"
556 set -- $(cat $B/g9.out)
557 [ "$1" = 2147483648 ] || bad "9: len $1"
558 [ $(($3 - $2)) -le 16 ] || bad "9: a 2 GiB pool with one page touched added $(($3 - $2)) kB of VmRSS (at most 16)"
559 [ $(($4 - $3)) -ge 3584 ] || bad "9 control: 1024 pages touched added only $(($4 - $3)) kB"
560 say "PASS 9 untouched pages: 2 GiB pool, one page touched: VmRSS +$(($3 - $2)) kB; control 1024 pages: +$(($4 - $3)) kB"
561fi
562
563# 10. refusals
564if on 10; then
565 n=0
566 for f in $T/err-*.om; do
567 c=$(basename $f .om); w=$(cat $T/$c.want)
568 set +e; "$S" < $f > /dev/null 2> $B/w.err; rc=$?; set -e
569 k=${w##* }; got=$(head -1 $B/w.err)
570 [ $rc = $k ] || bad "10 $c: status $rc ($got), wanted error $k"
571 case $w in
572 *"line *:"*) case $got in "omc: line "*": error $k") ;; *) bad "10 $c: '$got'" ;; esac ;;
573 *) [ "$got" = "$w" ] || bad "10 $c: '$got', wanted '$w'" ;;
574 esac
575 n=$((n + 1))
576 done
577 say "PASS 10 refusals: $n units refused with their exact code and line"
578 for c in err-x86efi err-devctl err-mapwrite; do
579 sed 's/^global P \([a-z0-9]*\) \*$/global P \1 64/; s/^r = enlarge P .*/r = long 0/' $T/$c.om > $B/tw.om
580 "$S" < $B/tw.om > /dev/null 2> $B/w.err || bad "10 control: $c with a sized global: $(head -1 $B/w.err)"
581 done
582 say "PASS 10 control: err-x86efi, err-devctl, err-mapwrite compile with a sized global in place of the pool"
583fi
584
585# 11. byte identity
586if on 11; then
587 git grep -nw enlarge -- '*.om' '*.omegese' | grep -v "^$T/" > $B/scan || true
588 grep -v '^[^:]*:[0-9]*:note ' $B/scan > $B/scanuse || true
589 sed 's/^/SCAN /' $B/scan | cut -c1-160
590 # Units that use the word on purpose are listed by name in $T/enlarge-users.txt (design 7.11: "except the files
591 # that name enlarge, listed by name"); any other user fails, and the listed ones leave the byte identity list.
592 cut -d: -f1 $B/scanuse | sort -u > $B/users
593 grep -v '^#' $T/enlarge-users.txt | sort -u > $B/allowed
594 # An exemption must name an actual tracked unit with the excluded feature.
595 # This also prevents a future file being exempted outside its own change.
596 while IFS= read -r unit; do
597 [ -n "$unit" ] || bad "11 inventory: blank entry"
598 case "$unit" in *.om) ;; *) bad "11 inventory: not an Omega unit: $unit" ;; esac
599 [ -f "$unit" ] && git --literal-pathspecs ls-files --error-unmatch -- "$unit" > /dev/null 2>&1 ||
600 bad "11 inventory: not a tracked file: $unit"
601 grep -qxF "$unit" "$B/users" || grep -qE '^global [^ ]+ [^ ]+ \*$' "$unit" ||
602 bad "11 inventory: no enlarge or run-time pool declaration: $unit"
603 done < $B/allowed
604 if [ -n "$(comm -23 $B/users $B/allowed)" ]; then bad "11 scan: units use enlarge outside $T and are not listed in $T/enlarge-users.txt: $(comm -23 $B/users $B/allowed | tr '\n' ' ')"; fi
605 say "PASS 11 scan: git grep -nw enlarge outside $T: $(wc -l < $B/scan) lines; units using the word: $(tr '\n' ' ' < $B/users)(each listed in enlarge-users.txt); the rest are notes"
606 if [ "${POOLS_SKIP_EQUIV:-0}" = 1 ]; then
607 say "NOT_RUN 11 byte identity (POOLS_SKIP_EQUIV=1)"
608 else
609 git ls-files '*.om' | grep -v "^$T/" | grep -vxF -f $B/allowed > $B/files
610 [ -z "${POOLS_EQUIV_MAX:-}" ] || { head -n "$POOLS_EQUIV_MAX" $B/files > $B/f2; mv $B/f2 $B/files; }
611 n=0; d=0
612 while read -r f; do
613 n=$((n + 1))
614 set +e
615 timeout 60 "$S" < "$f" > $B/ea 2> $B/eae; a=$?
616 timeout 60 "$CTRL" < "$f" > $B/eb 2> $B/ebe; b=$?
617 set -e
618 if [ $a != $b ] || ! cmp -s $B/ea $B/eb || ! cmp -s $B/eae $B/ebe; then d=$((d + 1)); say "DIFF $f $a $b"; fi
619 done < $B/files
620 [ $d = 0 ] || bad "11 byte identity: $d of $n units differ"
621 "$S" < $T/g1.om > $B/ea 2> $B/eae || true; "$CTRL" < $T/g1.om > $B/eb 2> $B/ebe || true
622 ! cmp -s $B/eae $B/ebe || bad "11 control: the comparison does not tell g1 apart"
623 say "PASS 11 byte identity: $n tracked units, status stdout stderr equal under OMC and CTRL${POOLS_EQUIV_MAX:+ (first $POOLS_EQUIV_MAX only)}; control g1 differs"
624 fi
625fi
626
627# S. server
628if on S; then
629 "$S" < $T/srv.om > $B/srv 2> $B/w.err || bad "S: compile failed ($(head -1 $B/w.err))"
630 chmod +x $B/srv
631 set +e; timeout 300 strace -f -qq -e trace=mmap,mremap -o $B/st $B/srv < /dev/null > $B/srv.out; x=$?; set -e
632 [ $x = 0 ] || bad "S: srv exit $x"
633 set -- $(cat $B/srv.out)
634 w() { sed -n "s/^$1 //p" $T/srv.want; }
635 [ "$1" = "$(w hw)" ] && [ "$2" = "$(w calls)" ] && [ "$3" = "$(w marks)" ] && [ "$6" = "$(w cs)" ] ||
636 bad "S: hw $1 calls $2 marks $3 cs $6, wanted $(tr '\n' ' ' < $T/srv.want)"
637 sc=$(grep -c 'm\(re\)\?map(' $B/st)
638 [ "$sc" = "$3" ] || bad "S: $sc mmap/mremap calls, wanted one per high-water mark ($3)"
639 [ "$4" -ge 65536 ] || bad "S: VmRSS at the high-water point $4 kB, below the 64 MiB served"
640 [ "$5" -le $(($4 + 1024)) ] || bad "S: VmRSS $5 kB at the end, high-water $4 kB (+1024 allowed)"
641 say "PASS S server: 2000 requests, $2 enlarge calls = $sc growth syscalls = $3 marks; VmRSS $4 kB at high-water, $5 kB at end"
642 set +e; printf L | timeout 300 $B/srv > $B/leak.out; x=$?; set -e
643 set -- $(cat $B/leak.out)
644 [ $x = 0 ] && [ "$5" -gt $(($4 + 1024)) ] || bad "S control: the leak mode gave exit $x, VmRSS $4 -> $5 kB"
645 say "PASS S control: enlarging per request raises VmRSS $4 -> $5 kB (the check trips)"
646fi
647
648# M. mutants
649if on M; then
650 fails=0
651 while IFS=' ' read -r name case marks; do
652 [ -n "$name" ] || continue
653 cp seed/omc/omc.om $B/m.om; why=
654 for m in $(echo "$marks" | tr , ' '); do
655 cnt=$(awk -v t="note POOLMUT $m" -v p="note POOLMUT-ALT $m " '$0 == t { c++ } index($0, p) == 1 { a++ } END { print c + 0, a + 0 }' $B/m.om)
656 [ "$cnt" = "1 1" ] || { why="marker missing"; [ "$cnt" = "0 0" ] || why="marker $m found $cnt times (target, alt)"; break; }
657 awk -v t="note POOLMUT $m" -v p="note POOLMUT-ALT $m " '
658 NR == FNR { if (index($0, p) == 1) alt = substr($0, length(p) + 1); next }
659 skip { skip = 0; if (alt != "note removed") print alt; next }
660 { print } $0 == t { skip = 1 }' $B/m.om $B/m.om > $B/m2.om
661 ! cmp -s $B/m.om $B/m2.om || { why="marker $m changes nothing"; break; }
662 mv $B/m2.om $B/m.om
663 done
664 if [ -n "$why" ]; then say "MUTANT $name NOT_BUILT: $why"; fails=$((fails + 1)); continue; fi
665 if ! "$S" < $B/m.om > $B/mutomc 2> $B/w.err; then
666 say "MUTANT $name NOT_BUILT: omc refuses the mutated omc.om ($(head -1 $B/w.err))"; fails=$((fails + 1)); continue
667 fi
668 chmod +x $B/mutomc
669 if ! TO=120 ck $B/mutomc g4base; then
670 say "MUTANT $name NOT_BUILT: the mutant compiles g4base wrong ($(cat $B/why))"; fails=$((fails + 1)); continue
671 fi
672 if TO=120 ck $B/mutomc $case; then
673 say "FAIL mutant $name ($marks): $case passed"; fails=$((fails + 1))
674 else
675 say "PASS mutant $name ($marks): caught by $case: $(cat $B/why | cut -c1-160)"
676 fi
677 done < $T/mutants.tsv
678 [ $fails = 0 ] || bad "$fails mutants not built or not caught"
679fi
680
681# X. x86linux under qemu-x86_64 (EMULATED)
682xnr=
683if on X; then
684 if command -v qemu-x86_64 > /dev/null; then
685 X=$B/x86
686 mkdir -p $X
687 cat si/directives/target-x86linux.om seed/omc/omc.om > $X/c.om
688 "$S" < $X/c.om > $X/omc 2> $X/w.err || bad "X: OMC refuses target x86linux + omc.om ($(head -1 $X/w.err))"
689 chmod +x $X/omc
690 qemu-x86_64 $X/omc < $X/c.om > $X/omc2 2> $X/w.err || bad "X: the x86 compiler refuses its own source ($(head -1 $X/w.err))"
691 cmp -s $X/omc $X/omc2 || bad "X: the x86 compiler does not rebuild itself byte for byte"
692 "$S" < seed/omc/omc.om > $X/a64 2> /dev/null || bad "X: OMC refuses omc.om"
693 qemu-x86_64 $X/omc < seed/omc/omc.om > $X/a64x 2> $X/w.err || bad "X: the x86 compiler refuses omc.om ($(head -1 $X/w.err))"
694 cmp -s $X/a64 $X/a64x || bad "X: the x86 compiler's AArch64 compiler differs from OMC's"
695 say "PASS X fixpoint: x86 omc $(sha256sum $X/omc | cut -c1-16) rebuilds itself and OMC's AArch64 compiler byte for byte (qemu-x86_64, EMULATED)"
696 ckx() { # ckx COMPILER NAME: $T/NAME.om after `target x86linux`, compiled and run under qemu-x86_64
697 inp=/dev/null; want=$T/$2.want; pre=
698 case $2 in g3|g3i) inp=$B/letters ;; gr) inp=$B/seqin; want=$B/gr.want ;; g8a) pre="prlimit --as=1073741824" ;; esac
699 { echo 'target x86linux'; cat $T/$2.om; } > $X/p.om
700 qemu-x86_64 "$1" < $X/p.om > $X/p.bin 2> $X/p.err || { echo "$2: compile failed ($(head -1 $X/p.err))" > $B/why; return 1; }
701 chmod +x $X/p.bin
702 set +e; $pre timeout ${TO:-300} qemu-x86_64 $X/p.bin < $inp > $X/p.out 2> /dev/null; x=$?; set -e
703 echo "status $x" >> $X/p.out
704 cmp -s $X/p.out $want || { echo "$2: $(firstdiff $X/p.out $want)" > $B/why; return 1; }
705 }
706 n=0
707 for f in $T/g*.om; do
708 c=$(basename $f .om)
709 [ -f $T/$c.want ] || [ "$c" = gr ] || continue
710 [ "$c" = g9 ] && continue
711 ckx $X/omc $c || bad "X $(cat $B/why)"
712 n=$((n + 1))
713 done
714 say "PASS X cases: $n pool cases give their .want on x86linux (g8a under prlimit --as=1073741824: qemu needs room for its own guest map)"
715 for mut in x86cmp:g2a x86base:g1 x86belowlen:g1 x86ovf:g8b x86slotfail:g8a; do
716 m=${mut%%:*}; c=${mut#*:}
717 awk -v t="note POOLMUT $m" -v p="note POOLMUT-ALT $m " '
718 NR == FNR { if (index($0, p) == 1) alt = substr($0, length(p) + 1); next }
719 skip { skip = 0; if (alt != "note removed") print alt; next }
720 { print } $0 == t { skip = 1 }' seed/omc/omc.om seed/omc/omc.om > $X/m.om
721 ! cmp -s seed/omc/omc.om $X/m.om || bad "X mutant $m: marker changes nothing"
722 cat si/directives/target-x86linux.om $X/m.om | "$S" > $X/momc 2> $X/w.err || bad "X mutant $m: not built ($(head -1 $X/w.err))"
723 chmod +x $X/momc
724 TO=120 ckx $X/momc g4base || bad "X mutant $m: compiles g4base wrong ($(cat $B/why))"
725 if TO=120 ckx $X/momc $c; then bad "X mutant $m: $c passed"; fi
726 say "PASS X mutant $m: caught by $c: $(cut -c1-160 $B/why)"
727 done
728 else
729 xnr=" (X NOT_RUN: no qemu-x86_64)"
730 say "NOT_RUN X: no qemu-x86_64"
731 fi
732fi
733
734say "PASS gate-pools${POOLS_ONLY:+ (sections 0 $POOLS_ONLY only)}$xnr"
735say "RC=0"