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 / ng / front / gate-front.om

1751 lines · 31262 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/front/gate-front.om: the differential front gate of ng-front (Hive NG; DECISIONS D5, D6, D9).
3note Handwritten seeded Omega, program mode, Linux AArch64, built by the pinned seed/out/omc. The child,
4note segment and SHA-256 helpers follow seed/qualification/om2/gate.om; its SHA-256 constants are read
5note from that directory's bc-derived fixtures, never typed here.
6note
7note Run from the repository root:
8note gate-front OMC OMC_SHA256 SRC DECLARED MUTANTS SCRATCH CASES [CASES ...]
9note OMC the oracle compiler; its sha256 must be OMC_SHA256 (64 lowercase hex) or no case runs (INVALID)
10note SRC the checker source: segments joined by + (a path, pack:PATH:OFF:LEN, cyc:PATH:N, units:LIST for
11note the units of an ng.units-form list in its order) concatenated in
12note order; OMC compiles it to SCRATCH/fecheck. The checker reads one unit on stdin, writes nothing
13note on stdout, or exactly what omc writes there (om2 diagnostics, check mode), and exits 0
14note (accepted) or as omc refuses (stderr `omc: line N: error K`, exit K). A segment in:PATH is not
15note source: the bytes of PATH (4096 at most) go before each case on the checker's stdin only (the
16note mode line of the ng unit, `@ng front-check`); omc reads the case alone.
17note DECLARED lines `NAME beyond K REASON...`: omc refuses NAME with exit K for a code-generator limit (12, 14,
18note 24 or 25) and the checker must accept it (BEYOND_OMC, D6); `NAME scope K REASON...`: a unit form
19note this front end does not take yet; omc must exit K and the checker 64; `NAME ngonly K REASON...`:
20note a bound of the checker's own that omc reaches through another limit first; both must exit K
21note (stderr not compared: the lines differ); `NAME ngcap K REASON...`: an input the checker refuses
22note with exit K at one of ng's own fixed tables (REASON names it) that omc, which has no table
23note capacities since 2026-11-11 (OMEGESE 9.6), takes past it: the checker must exit K and omc a
24note status other than K below 100. Every ngcap row is counted on a line of its own before the
25note verdict (NGCAP), so the gap stays visible until ng's tables are lifted, and a row whose input
26note the checker accepts fails. K is decimal, 0-255. Every listed NAME must be a case.
27note MUTANTS lines `NAME FROM TO`: SRC with the bytes of file FROM (present exactly once) replaced by the bytes
28note of file TO, compiled to SCRATCH/NAME. KILLED when some case FAILs under it, else SURVIVED.
29note SCRATCH a new directory (created 0700; an existing one is INVALID)
30note CASES lines `NAME SRC`, or the C2 manifest form `NAME emit SRC ...`; lines starting with # and blank
31note lines are skipped, and so is a line whose first word is `deadline`.
32note A case is exact unless DECLARED: PASS needs equal exit status and equal stderr bytes from omc and the
33note checker (all of them: each stderr has its own descriptor and the two are compared in 65536-byte
34note chunks), and an empty checker stdout or one equal to omc's. The last line is the verdict:
35note NGCAP ng still refuses C inputs omc accepts: ng front's fixed tables (declared ngcap rows) [when C > 0]
36note FRONTGATE PASS|FAIL|INVALID cases=N exact=E beyond=B scope=S ngonly=O ngcap=C fail=F mutants=M killed=K
37note Exit 0 PASS, 1 FAIL (any failing case, unused declaration or surviving mutant), 2 INVALID.
38
39text msg "FRONTGATE PASS\nFRONTGATE FAIL\nFRONTGATE INVALID\n cases=\n exact=\n beyond=\n scope=\n fail=\n mutants=\n killed=\nFAIL \n omc=\n ng=\n omc stderr: \n ng stderr: \n ng wrote stdout\nKILLED \n at \nSURVIVED \nUNUSED declared \nINVALID: \nusage: gate-front OMC OMC_SHA256 SRC DECLARED MUTANTS SCRATCH CASES [CASES ...]\noracle sha256 differs from OMC_SHA256\nSHA-256 fixture unreadable\nscratch exists or cannot be made\nchecker build failed, omc exit=\nmutant build failed: \nsource unreadable, malformed or too large: \nlist file unreadable or too large: \nfork, memfd or descriptor failure\nchild deadline exceeded\nmutant FROM not present exactly once: \nORACLE \nCHECKER \n sha256=\nmalformed declared line: \n ngonly=\nmalformed mutant line\n ngcap=\nNGCAP ng still refuses \n inputs omc accepts: ng front's fixed tables (declared ngcap rows)\n"
40text words "pack:\ncyc:\nemit\ndeadline\nbeyond\nscope\nfecheck\n/\n.om\n/dev/null\nseed/qualification/om2/fixtures/sha256-k.dec\nseed/qualification/om2/fixtures/sha256-iv.dec\nin:\nunits:\nngonly\nngcap\n"
41global ub uchar 65536
42
43global ar uchar 65536
44global ao ulong 64
45global al ulong 64
46global na ulong 1
47global tb uchar 4096
48global tn ulong 1
49global pb uchar 4096
50global pn ulong 1
51global av uchar 8192
52global ap ulong 2
53global big uchar 16777216
54global bgn ulong 1
55global cs uchar 16777216
56global csn ulong 1
57global lst uchar 4194304
58global lsn ulong 1
59global dec uchar 1048576
60global dnm uchar 262144
61global dnn ulong 1
62global dcn ulong 1
63global dno ulong 4096
64global dnl ulong 4096
65global dcl uchar 4096
66global dcd uchar 4096
67global dus uchar 4096
68global dhit ulong 1
69global ndc ulong 1
70global tko ulong 16
71global tkl ulong 16
72global ntk ulong 1
73global eo uchar 65536
74global eon ulong 1
75global eg uchar 65536
76global ob uchar 65536
77global egn ulong 1
78global buf uchar 65536
79global stb uchar 4
80global fds int 6
81global pfx uchar 4096
82global pfn ulong 1
83global bad ulong 1
84global cnt ulong 9
85global dig uchar 32
86global wpos ulong 2
87global nums ulong 64
88global kk uint 64
89global ivs uint 8
90global hv uint 8
91global ws uint 64
92global st uint 8
93global blk uchar 64
94global bk ulong 2
95global hexd uchar 64
96global mode ulong 1
97global hit ulong 1
98global nfl ulong 1
99global chk uchar 4096
100global chn ulong 1
101
102note ---- output ----------------------------------------------------------------------------------
103fn seg id ulong to void
104k = ulong 1
105s = ulong 0
106for i ulong len msg
107if eq get msg i 10
108if eq k id
109put wpos 0 s
110put wpos 1 sub i s
111ret
112end
113k = add k 1
114s = add i 1
115end
116end
117exit 99
118end
119fn say id ulong to void
120seg id
121r = write 1 msg get wpos 0 get wpos 1
122end
123fn newline to void
124put dig 0 10
125r = write 1 dig 0 1
126end
127fn num v ulong to void
128x = v
129n = ulong 0
130while ge x 10 bound 32
131put dig n uchar add 48 rem x 10
132x = div x 10
133n = add n 1
134end
135put dig n uchar add 48 x
136n = add n 1
137for i ulong n
138r = write 1 dig sub sub n i 1 1
139end
140end
141fn showarg k ulong to void
142r = write 1 ar get ao k get al k
143end
144fn showtok t ulong to void
145r = write 1 lst get tko t get tkl t
146end
147fn showtb to void
148r = write 1 tb 0 get tn 0
149end
150fn showpb to void
151r = write 1 pb 0 get pn 0
152end
153note the first line of a captured stderr (at most 200 bytes)
154fn showeo to void
155n = get eon 0
156i = ulong 0
157while and lt i n lt i 200 bound 256
158if eq get eo i 10
159n = i
160else
161i = add i 1
162end
163end
164r = write 1 eo 0 i
165end
166fn showeg to void
167n = get egn 0
168i = ulong 0
169while and lt i n lt i 200 bound 256
170if eq get eg i 10
171n = i
172else
173i = add i 1
174end
175end
176r = write 1 eg 0 i
177end
178fn setbad id ulong to void
179if eq get bad 0 0
180put bad 0 id
181end
182end
183
184note ---- fixed words ------------------------------------------------------------------------------
185fn wordat id ulong to void
186k = ulong 1
187s = ulong 0
188for i ulong len words
189if eq get words i 10
190if eq k id
191put wpos 0 s
192put wpos 1 sub i s
193ret
194end
195k = add k 1
196s = add i 1
197end
198end
199exit 99
200end
201note tb[o .. o+n) starts with word id
202fn tbpre o ulong n ulong id ulong to bool
203wordat id
204k = get wpos 1
205if lt n k
206ret false
207end
208s = get wpos 0
209for i ulong k
210if ne get tb add o i get words add s i
211ret false
212end
213end
214ret true
215end
216note tb[o .. o+n) is word id
217fn tbis o ulong n ulong id ulong to bool
218wordat id
219if ne n get wpos 1
220ret false
221end
222ret tbpre o n id
223end
224
225note ---- the scratch buffer tb (one token, argument or path at a time) ----------------------------
226fn tbclr to void
227put tn 0 0
228end
229fn tbbyte b uchar to void
230n = get tn 0
231if ge n 4095
232setbad 28
233ret
234end
235put tb n b
236put tn 0 add n 1
237end
238fn tbarg k ulong to void
239tbclr
240o = get ao k
241for i ulong get al k
242tbbyte get ar add o i
243end
244end
245fn tbtok t ulong to void
246tbclr
247o = get tko t
248for i ulong get tkl t
249tbbyte get lst add o i
250end
251end
252fn tbdec o ulong n ulong to void
253tbclr
254for i ulong n
255tbbyte get dec add o i
256end
257end
258note declared names live in dnm, apart from dec, which the mutant list later reuses
259fn tbdnm o ulong n ulong to void
260tbclr
261for i ulong n
262tbbyte get dnm add o i
263end
264end
265
266note ---- paths --------------------------------------------------------------------------------------
267fn pclear to void
268put pn 0 0
269end
270fn pbyte b uchar to void
271n = get pn 0
272if ge n 4095
273setbad 30
274ret
275end
276put pb n b
277put pn 0 add n 1
278end
279fn ptb o ulong n ulong to void
280for i ulong n
281pbyte get tb add o i
282end
283end
284fn pword id ulong to void
285wordat id
286s = get wpos 0
287for i ulong get wpos 1
288pbyte get words add s i
289end
290end
291fn parg k ulong to void
292o = get ao k
293for i ulong get al k
294pbyte get ar add o i
295end
296end
297note SCRATCH/ followed by tb (a mutant name) or by a fixed word
298fn pscr to void
299pclear
300parg 6
301pword 8
302end
303fn popen fl int to int
304r = fopen pb 0 get pn 0 fl 448
305if lt r 0
306ret -1
307end
308ret int r
309end
310fn fclo fd int to void
311if ge fd 0
312r = fclose fd
313end
314end
315
316note ---- decimal fields -------------------------------------------------------------------------------
317fn tbnum o ulong n ulong to ulong
318if or eq n 0 gt n 19
319setbad 28
320ret 0
321end
322v = ulong 0
323for i ulong n
324c = get tb add o i
325if or lt c 48 gt c 57
326setbad 28
327ret 0
328end
329v = add mul v 10 ulong sub c 48
330end
331ret v
332end
333fn tblastc o ulong n ulong to ulong
334i = n
335while gt i 0 bound 4096
336i = sub i 1
337if eq get tb add o i 58
338ret i
339end
340end
341ret n
342end
343
344note ---- sources into big ------------------------------------------------------------------------------
345fn bigpre fd int off ulong k ulong to bool
346b = get bgn 0
347done = ulong 0
348while lt done k bound 100000
349c = sub k done
350if gt c 1048576
351c = ulong 1048576
352end
353r = pread fd big add b done c long add off done
354if le r 0
355ret false
356end
357done = add done ulong r
358end
359ret true
360end
361fn addpack o ulong n ulong to void
362q = tblastc o n
363if ge q n
364setbad 28
365ret
366end
367p = tblastc o q
368if ge p q
369setbad 28
370ret
371end
372k = tbnum add add o q 1 sub sub n q 1
373off = tbnum add add o p 1 sub sub q p 1
374if ne get bad 0 0
375ret
376end
377if gt k sub len big get bgn 0
378setbad 28
379ret
380end
381pclear
382ptb o p
383fd = popen 524288
384if lt fd 0
385setbad 28
386ret
387end
388ok = bigpre fd off k
389fclo fd
390if not ok
391setbad 28
392ret
393end
394put bgn 0 add get bgn 0 k
395end
396fn cycrow b ulong s ulong c ulong m ulong to void
397for i ulong c
398put big add add b s i get buf rem add s i m
399end
400end
401fn addcyc o ulong n ulong to void
402q = tblastc o n
403if ge q n
404setbad 28
405ret
406end
407k = tbnum add add o q 1 sub sub n q 1
408if ne get bad 0 0
409ret
410end
411if gt k sub len big get bgn 0
412setbad 28
413ret
414end
415pclear
416ptb o q
417fd = popen 524288
418if lt fd 0
419setbad 28
420ret
421end
422r = read fd buf 0 len buf
423fclo fd
424if or le r 0 ge r long len buf
425setbad 28
426ret
427end
428m = ulong r
429b = get bgn 0
430for row ulong add div k 65536 1
431s = mul row 65536
432c = sub k s
433if gt c 65536
434c = ulong 65536
435end
436cycrow b s c m
437end
438put bgn 0 add b k
439end
440fn addfile o ulong n ulong to void
441pclear
442ptb o n
443addpb
444end
445note the file at the path in pb appended to big
446fn addpb to void
447fd = popen 524288
448if lt fd 0
449setbad 28
450ret
451end
452go = true
453while go bound 100000
454b = get bgn 0
455if ge b len big
456setbad 28
457go = false
458else
459r = read fd big b sub len big b
460if lt r 0
461setbad 28
462go = false
463else
464if eq r 0
465go = false
466else
467put bgn 0 add b ulong r
468end
469end
470end
471end
472fclo fd
473end
474note in:PATH: the bytes of PATH into pfx (the checker's stdin prefix), not into big
475fn addpfx o ulong n ulong to void
476b0 = get bgn 0
477addfile o n
478k = sub get bgn 0 b0
479if gt k len pfx
480setbad 28
481k = ulong 0
482end
483for i ulong k
484put pfx i get big add b0 i
485end
486put pfn 0 k
487put bgn 0 b0
488end
489note units:PATH: each line `SHA256 PATH` of the units list at PATH (the seed/ng/ng.units form) as a path
490note segment, in order; the pins are ngbuild's to check, not this gate's
491fn addunit o ulong n ulong to void
492pclear
493ptb o n
494fd = popen 524288
495if lt fd 0
496setbad 28
497ret
498end
499r = read fd ub 0 len ub
500fclo fd
501if or le r 0 ge r long len ub
502setbad 28
503ret
504end
505m = ulong r
506s = ulong 0
507for i ulong add m 1
508if or eq i m eq get ub i 10
509unitln s i
510s = add i 1
511end
512end
513end
514note the unit on the list line ub[s, e) (64 hex digits, a space, the path); an empty line adds nothing
515fn unitln s ulong e ulong to void
516if eq s e
517ret
518end
519if le e add s 65
520setbad 28
521ret
522end
523pclear
524for i ulong sub e add s 65
525pbyte get ub add add s 65 i
526end
527addpb
528end
529fn addseg o ulong n ulong to void
530if tbpre o n 14
531addunit add o 6 sub n 6
532ret
533end
534if tbpre o n 13
535addpfx add o 3 sub n 3
536ret
537end
538if tbpre o n 1
539addpack add o 5 sub n 5
540ret
541end
542if tbpre o n 2
543addcyc add o 4 sub n 4
544ret
545end
546addfile o n
547end
548note the +-joined source spec in tb, appended to big
549fn addsrc to void
550n = get tn 0
551s = ulong 0
552for i ulong add n 1
553if or eq i n eq get tb i 43
554if eq i s
555setbad 28
556else
557addseg s sub i s
558end
559s = add i 1
560end
561end
562end
563
564note ---- whole files into lst / dec --------------------------------------------------------------------
565fn readlst to bool
566fd = popen 524288
567if lt fd 0
568ret false
569end
570n = ulong 0
571go = true
572while go bound 100000
573r = read fd lst n sub len lst n
574if le r 0
575go = false
576if lt r 0
577n = len lst
578end
579else
580n = add n ulong r
581if ge n len lst
582go = false
583end
584end
585end
586fclo fd
587put lsn 0 n
588ret lt n len lst
589end
590fn readdec to bool
591fd = popen 524288
592if lt fd 0
593ret false
594end
595n = ulong 0
596go = true
597while go bound 100000
598r = read fd dec n sub len dec n
599if le r 0
600go = false
601if lt r 0
602n = len dec
603end
604else
605n = add n ulong r
606if ge n len dec
607go = false
608end
609end
610end
611fclo fd
612put dcn 0 n
613ret lt n len dec
614end
615
616note lst[j] is inside the line (j < e) and not a blank
617fn intok j ulong e ulong to bool
618c = get lst j
619ret and lt j e and ne c 32 ne c 9
620end
621note ---- tokens of lst[s .. e) -------------------------------------------------------------------------
622fn tokenize s ulong e ulong to void
623put ntk 0 0
624i = s
625while lt i e bound 4194304
626c = get lst i
627if or eq c 32 eq c 9
628i = add i 1
629else
630t = get ntk 0
631j = i
632while intok j e bound 4194304
633j = add j 1
634end
635if lt t 16
636put tko t i
637put tkl t sub j i
638put ntk 0 add t 1
639end
640i = j
641end
642end
643end
644fn lineend p ulong to ulong
645n = get lsn 0
646i = p
647while and lt i n ne get lst i 10 bound 4194304
648i = add i 1
649end
650ret i
651end
652
653note ---- declared table ----------------------------------------------------------------------------------
654note dtok: the next word of dec at or after p, as (offset, length) in wpos; stops at a newline
655fn dword p ulong e ulong to void
656i = p
657while and lt i e dblank i bound 1048576
658i = add i 1
659end
660j = i
661while and lt j e not dblank j bound 1048576
662j = add j 1
663end
664put wpos 0 i
665put wpos 1 sub j i
666end
667fn dblank i ulong to bool
668c = get dec i
669ret or eq c 32 eq c 9
670end
671fn loaddec to bool
672n = get dcn 0
673p = ulong 0
674while lt p n bound 1048576
675e = p
676while and lt e n ne get dec e 10 bound 1048576
677e = add e 1
678end
679dword p e
680o = get wpos 0
681l = get wpos 1
682if and gt l 0 ne get dec o 35
683k = get ndc 0
684if ge k 4096
685ret false
686end
687dword add o l e
688co = get wpos 0
689cl = get wpos 1
690tbdec co cl
691c = ulong 0
692if tbis 0 get tn 0 5
693c = ulong 1
694end
695if tbis 0 get tn 0 6
696c = ulong 2
697end
698if tbis 0 get tn 0 15
699c = ulong 3
700end
701if tbis 0 get tn 0 16
702c = ulong 4
703end
704dword add co cl e
705tbdec get wpos 0 get wpos 1
706v = tbcode
707if or eq c 0 gt v 255
708say 36
709tbdec o l
710showtb
711newline
712put bad 0 36
713ret false
714end
715if not dnmadd k o l
716ret false
717end
718put dnl k l
719put dcl k uchar c
720put dcd k uchar v
721put dus k 0
722put ndc 0 add k 1
723end
724p = add e 1
725end
726ret true
727end
728note tb is a decimal exit code 0-255: its value, else 1000
729fn tbcode to ulong
730n = get tn 0
731if or eq n 0 gt n 3
732ret 1000
733end
734v = ulong 0
735for i ulong n
736c = get tb i
737if or lt c 48 gt c 57
738ret 1000
739end
740v = add mul v 10 ulong sub c 48
741end
742ret v
743end
744note the declared class of case token 0 (0 exact, 1 beyond, 2 scope, 3 ngonly, 4 ngcap), marking the entry used and
745note keeping its index in dhit
746fn dclass to ulong
747o = get tko 0
748l = get tkl 0
749for k ulong get ndc 0
750if eq get dnl k l
751if dsame get dno k o l
752put dus k 1
753put dhit 0 k
754ret ulong get dcl k
755end
756end
757end
758ret 0
759end
760note copy declared name dec[o .. o+l) into dnm as entry k's name; false when dnm is full
761fn dnmadd k ulong o ulong l ulong to bool
762s = get dnn 0
763if gt add s l len dnm
764ret false
765end
766for i ulong l
767put dnm add s i get dec add o i
768end
769put dnn 0 add s l
770put dno k s
771ret true
772end
773note dnm[d .. d+l) equals lst[o .. o+l)
774fn dsame d ulong o ulong l ulong to bool
775for i ulong l
776if ne get dnm add d i get lst add o i
777ret false
778end
779end
780ret true
781end
782
783note ---- children -----------------------------------------------------------------------------------------
784fn aclear to void
785put ap 0 2
786put ap 1 0
787end
788fn ahead n ulong to void
789p = get ap 0
790if gt add add p n 2 len av
791setbad 30
792ret
793end
794put av p uchar and n 255
795put av add p 1 uchar shr n 8
796put ap 0 add p 2
797put ap 1 add get ap 1 1
798end
799fn apb to void
800n = get pn 0
801ahead n
802p = get ap 0
803if le add p n len av
804for i ulong n
805put av add p i get pb i
806end
807put ap 0 add p n
808end
809end
810fn redir s int d int to void
811if ne s d
812if ne dupfd s d 0 long d
813exit 127
814end
815end
816end
817note wait for pid: its exit status, 256 + the signal, or 1000 when the deadline (900 s) passed and it was killed
818fn reap pid int to ulong
819t0 = monons
820while true bound 1000000000
821r = waitpid pid stb 0 len stb 1
822if eq r long pid
823if ne get stb 0 0
824ret add 256 ulong get stb 0
825end
826ret ulong get stb 1
827end
828if lt r 0
829setbad 30
830ret 999
831end
832now = monons
833if gt sub now t0 900000000000
834r = killpid pid 9
835r = waitpid pid stb 0 len stb 0
836setbad 31
837ret 1000
838end
839q = sleepns 100000
840end
841ret 999
842end
843fn spawn fi int fo int fe int to ulong
844put av 0 uchar and get ap 1 255
845put av 1 uchar shr get ap 1 8
846if ne fseek fi 0 0 0
847setbad 30
848ret 999
849end
850pid = fork
851if lt pid 0
852setbad 30
853ret 999
854end
855if eq pid 0
856redir fi 0
857redir fo 1
858redir fe 2
859r = execprog av 0 get ap 0
860exit 127
861end
862ret reap int pid
863end
864note reset descriptor fd to an empty file
865fn empty fd int to void
866if ne ftrunc fd 0 0
867setbad 30
868end
869if ne fseek fd 0 0 0
870setbad 30
871end
872end
873note the first 65536 bytes of a captured stderr, omc's (fds[5]) into eo, the checker's (fds[1]) into eg, for the
874note FAIL lines only; the comparison is samefd over the whole of both descriptors
875fn grab fd int to ulong
876if ne fseek fd 0 0 0
877setbad 30
878ret 0
879end
880n = ulong 0
881go = true
882while go bound 100000
883r = read fd buf n sub len buf n
884if le r 0
885go = false
886else
887n = add n ulong r
888if ge n len buf
889go = false
890end
891end
892end
893ret n
894end
895fn grabo to void
896n = grab get fds 5
897for i ulong n
898put eo i get buf i
899end
900put eon 0 n
901end
902fn grabg to void
903n = grab get fds 1
904for i ulong n
905put eg i get buf i
906end
907put egn 0 n
908end
909note big into descriptor fds[0]; with an in: prefix also pfx then big into fds[4] (the checker's stdin)
910fn putsrc to void
911fd = get fds 0
912empty fd
913wrbig fd
914p = get pfn 0
915if gt p 0
916f4 = get fds 4
917empty f4
918k = ulong 0
919while lt k p bound 100000
920r = write f4 pfx k sub p k
921if le r 0
922setbad 30
923ret
924end
925k = add k ulong r
926end
927wrbig f4
928end
929end
930fn wrbig fd int to void
931n = get bgn 0
932off = ulong 0
933while lt off n bound 100000
934r = write fd big off sub n off
935if le r 0
936setbad 30
937ret
938end
939off = add off ulong r
940end
941end
942
943note ---- SHA-256 (FIPS 180-4), as seed/qualification/om2/gate.om -----------------------------------------
944fn rotr x uint n uint to uint
945ret or shr x n shl x sub 32 n
946end
947fn shasched to void
948for i ulong 16
949b = mul i 4
950v = shl uint get blk b 24
951v = or v shl uint get blk add b 1 16
952v = or v shl uint get blk add b 2 8
953v = or v uint get blk add b 3
954put ws i v
955end
956for j ulong 48
957q = add j 16
958x = get ws sub q 15
959y = get ws sub q 2
960s0 = xor xor rotr x 7 rotr x 18 shr x 3
961s1 = xor xor rotr y 17 rotr y 19 shr y 10
962put ws q add add add get ws sub q 16 s0 get ws sub q 7 s1
963end
964end
965fn shat1 i ulong to uint
966e = get st 4
967s1 = xor xor rotr e 6 rotr e 11 rotr e 25
968ch = xor and e get st 5 and not e get st 6
969ret add add add add get st 7 s1 ch get kk i get ws i
970end
971fn shat2 to uint
972a = get st 0
973b = get st 1
974c = get st 2
975s0 = xor xor rotr a 2 rotr a 13 rotr a 22
976mj = xor xor and a b and a c and b c
977ret add s0 mj
978end
979fn shacomp to void
980shasched
981for i ulong 8
982put st i get hv i
983end
984for i ulong 64
985t1 = shat1 i
986t2 = shat2
987put st 7 get st 6
988put st 6 get st 5
989put st 5 get st 4
990put st 4 add get st 3 t1
991put st 3 get st 2
992put st 2 get st 1
993put st 1 get st 0
994put st 0 add t1 t2
995end
996for i ulong 8
997put hv i add get hv i get st i
998end
999end
1000fn shabyte b uchar to void
1001n = get bk 0
1002put blk n b
1003n = add n 1
1004if eq n 64
1005shacomp
1006n = ulong 0
1007end
1008put bk 0 n
1009put bk 1 add get bk 1 1
1010end
1011fn hexch v uint to uchar
1012if lt v 10
1013ret uchar add v 48
1014end
1015ret uchar add v 87
1016end
1017fn shafinal to void
1018bits = mul get bk 1 8
1019shabyte 128
1020while ne get bk 0 56 bound 64
1021shabyte 0
1022end
1023for i ulong 8
1024shabyte uchar and shr bits mul sub 7 i 8 255
1025end
1026for i ulong 8
1027v = get hv i
1028for j ulong 8
1029put hexd add mul i 8 j hexch and shr v mul sub 7 j 4 15
1030end
1031end
1032end
1033note hash the file named by pb into hexd; false when it cannot be read
1034fn hashfile to bool
1035fd = popen 524288
1036if lt fd 0
1037ret false
1038end
1039for i ulong 8
1040put hv i get ivs i
1041end
1042put bk 0 0
1043put bk 1 0
1044while true bound 100000000
1045r = read fd buf 0 len buf
1046if lt r 0
1047fclo fd
1048ret false
1049end
1050if eq r 0
1051fclo fd
1052shafinal
1053ret true
1054end
1055for i ulong ulong r
1056shabyte get buf i
1057end
1058end
1059ret false
1060end
1061note decimal numbers below 2^32, one per line, exactly want of them, from fixed word id into nums
1062fn loadnums id ulong want ulong to bool
1063pclear
1064pword id
1065fd = popen 524288
1066if lt fd 0
1067ret false
1068end
1069r = read fd buf 0 len buf
1070fclo fd
1071if or le r 0 ge r long len buf
1072ret false
1073end
1074k = ulong 0
1075v = ulong 0
1076nd = ulong 0
1077for i ulong ulong r
1078c = get buf i
1079if and ge c 48 le c 57
1080if ge nd 10
1081ret false
1082end
1083v = add mul v 10 ulong sub c 48
1084nd = add nd 1
1085else
1086if or ne c 10 eq nd 0
1087ret false
1088end
1089if or ge k want gt v 4294967295
1090ret false
1091end
1092put nums k v
1093k = add k 1
1094v = ulong 0
1095nd = ulong 0
1096end
1097end
1098ret and eq nd 0 eq k want
1099end
1100fn showhex to void
1101r = write 1 hexd 0 64
1102end
1103
1104note ---- building ---------------------------------------------------------------------------------------------
1105note write big to pb (created 0700)
1106fn bigfile to bool
1107fd = popen 524865
1108if lt fd 0
1109ret false
1110end
1111n = get bgn 0
1112off = ulong 0
1113while lt off n bound 100000
1114r = write fd big off sub n off
1115if le r 0
1116fclo fd
1117ret false
1118end
1119off = add off ulong r
1120end
1121fclo fd
1122ret true
1123end
1124note compile the file at pb (big was written there) with OMC to pb without its .om suffix, which becomes
1125note chk and pb again; omc's exit
1126fn build to ulong
1127fi = popen 524288
1128if lt fi 0
1129ret 999
1130end
1131put pn 0 sub get pn 0 3
1132setchk
1133fo = popen 524865
1134if lt fo 0
1135fclo fi
1136ret 999
1137end
1138aclear
1139pclear
1140parg 1
1141apb
1142empty get fds 1
1143x = spawn fi fo get fds 1
1144fclo fi
1145fclo fo
1146pclear
1147for i ulong get chn 0
1148pbyte get chk i
1149end
1150ret x
1151end
1152
1153note descriptor b has the bytes of descriptor a, all of them (compared in 65536-byte chunks)
1154fn samefd a int b int to bool
1155if ne fseek a 0 2 fseek b 0 2
1156ret false
1157end
1158if or ne fseek a 0 0 0 ne fseek b 0 0 0
1159ret false
1160end
1161while true bound 1000
1162r = read a buf 0 len buf
1163if le r 0
1164ret eq r 0
1165end
1166if not samechk b ulong r
1167ret false
1168end
1169end
1170ret false
1171end
1172note the next n bytes of fd b equal buf[0 .. n)
1173fn samechk b int n ulong to bool
1174k = ulong 0
1175while lt k n bound 100000
1176r = read b ob k sub n k
1177if le r 0
1178ret false
1179end
1180k = add k ulong r
1181end
1182for i ulong n
1183if ne get ob i get buf i
1184ret false
1185end
1186end
1187ret true
1188end
1189note ---- one case ------------------------------------------------------------------------------------------------
1190note the checker's stdin: fds[4] (prefix and case) with an in: prefix, else fds[0] (the case, as omc reads it)
1191fn chkin to int
1192if gt get pfn 0 0
1193ret get fds 4
1194end
1195ret get fds 0
1196end
1197note run the source in big through omc and through the checker at chk; judge the pair
1198fn runcase cls ulong to bool
1199putsrc
1200aclear
1201pclear
1202parg 1
1203apb
1204empty get fds 5
1205empty get fds 2
1206xo = spawn get fds 0 get fds 2 get fds 5
1207grabo
1208aclear
1209pclear
1210for i ulong get chn 0
1211pbyte get chk i
1212end
1213apb
1214empty get fds 1
1215empty get fds 3
1216xn = spawn chkin get fds 3 get fds 1
1217grabg
1218sz = fseek get fds 3 0 2
1219ok = false
1220cd = ulong get dcd get dhit 0
1221if eq cls 0
1222ok = and eq xo xn samefd get fds 5 get fds 1
1223end
1224if eq cls 1
1225ok = and eq xo cd eq xn 0
1226end
1227if eq cls 2
1228ok = and eq xo cd eq xn 64
1229end
1230if eq cls 3
1231ok = and eq xo cd eq xn cd
1232end
1233if eq cls 4
1234ok = and and eq xn cd ne xo cd lt xo 100
1235end
1236if ne sz 0
1237if not samefd get fds 2 get fds 3
1238ok = false
1239else
1240sz = long 0
1241end
1242end
1243if and not ok eq get mode 0 0
1244put nfl 0 add get nfl 0 1
1245if le get nfl 0 60
1246say 11
1247showtok 0
1248say 12
1249num xo
1250say 13
1251num xn
1252if ne sz 0
1253say 16
1254end
1255newline
1256say 14
1257showeo
1258newline
1259say 15
1260showeg
1261newline
1262end
1263end
1264ret ok
1265end
1266note the cases of list file arg k; in mode 1 stop at the first failing case (kept in hit) and return false
1267fn runlist k ulong to bool
1268tbarg k
1269pclear
1270ptb 0 get tn 0
1271if not readlst
1272say 21
1273say 29
1274showpb
1275newline
1276put bad 0 29
1277ret false
1278end
1279n = get lsn 0
1280p = ulong 0
1281while lt p n bound 4194304
1282e = lineend p
1283tokenize p e
1284t = get ntk 0
1285skip = eq t 0
1286if not skip
1287c = get lst get tko 0
1288skip = eq c 35
1289end
1290if not skip
1291tbtok 0
1292skip = tbis 0 get tn 0 4
1293end
1294if and not skip lt t 2
1295skip = true
1296end
1297if not skip
1298s = ulong 1
1299if gt t 2
1300tbtok 1
1301if tbis 0 get tn 0 3
1302s = ulong 2
1303end
1304end
1305tbtok s
1306put bgn 0 0
1307addsrc
1308if ne get bad 0 0
1309say 21
1310say 28
1311showtok 0
1312newline
1313ret false
1314end
1315cls = dclass
1316ok = runcase cls
1317if ne get bad 0 0
1318say 21
1319say get bad 0
1320showtok 0
1321newline
1322ret false
1323end
1324if eq get mode 0 0
1325put cnt 0 add get cnt 0 1
1326if ok
1327ci = add cls 1
1328if eq cls 3
1329ci = ulong 7
1330end
1331if eq cls 4
1332ci = ulong 8
1333end
1334put cnt ci add get cnt ci 1
1335else
1336put cnt 4 add get cnt 4 1
1337end
1338else
1339if not ok
1340put hit 0 p
1341ret false
1342end
1343end
1344end
1345p = add e 1
1346end
1347ret true
1348end
1349
1350note ---- main ------------------------------------------------------------------------------------------------------
1351fn verdict v ulong to int
1352if gt get cnt 8 0
1353say 40
1354num get cnt 8
1355say 41
1356newline
1357end
1358say v
1359say 4
1360num get cnt 0
1361say 5
1362num get cnt 1
1363say 6
1364num get cnt 2
1365say 7
1366num get cnt 3
1367say 37
1368num get cnt 7
1369say 39
1370num get cnt 8
1371say 8
1372num get cnt 4
1373say 9
1374num get cnt 5
1375say 10
1376num get cnt 6
1377newline
1378if eq v 1
1379ret 0
1380end
1381if eq v 2
1382ret 1
1383end
1384ret 2
1385end
1386fn invalid id ulong to int
1387say 21
1388say id
1389newline
1390ret verdict 3
1391end
1392fn loadargs to bool
1393n = argcnt
1394if or lt n 8 gt n 64
1395ret false
1396end
1397o = ulong 0
1398for i ulong ulong n
1399r = argread i ar o sub len ar o
1400if le r 0
1401ret false
1402end
1403put ao i o
1404put al i ulong r
1405o = add o ulong r
1406end
1407put na 0 ulong n
1408ret true
1409end
1410note run the mutants of file arg 5 against every case list; mode 1
1411fn mutants to void
1412tbarg 5
1413pclear
1414ptb 0 get tn 0
1415if not readdec
1416setbad 29
1417ret
1418end
1419n = get dcn 0
1420p = ulong 0
1421while lt p n bound 1048576
1422e = p
1423while and lt e n ne get dec e 10 bound 1048576
1424e = add e 1
1425end
1426dword p e
1427mo = get wpos 0
1428ml = get wpos 1
1429if and gt ml 0 ne get dec mo 35
1430onemut mo ml e
1431if ne get bad 0 0
1432ret
1433end
1434end
1435p = add e 1
1436end
1437end
1438note substitute and build one mutant (dec name at mo, ml; its FROM and TO words follow before e), then run it
1439fn onemut mo ulong ml ulong e ulong to void
1440dword add mo ml e
1441fo = get wpos 0
1442fl = get wpos 1
1443dword add fo fl e
1444tw = get wpos 0
1445tl = get wpos 1
1446if or eq fl 0 eq tl 0
1447setbad 38
1448ret
1449end
1450put cnt 5 add get cnt 5 1
1451if not mutsub fo fl tw tl
1452if eq get bad 0 0
1453say 32
1454tbdec mo ml
1455showtb
1456newline
1457setbad 32
1458end
1459ret
1460end
1461tbdec mo ml
1462pscr
1463ptb 0 get tn 0
1464pword 9
1465if not bigfile
1466setbad 30
1467ret
1468end
1469x = build
1470if ne x 0
1471say 27
1472showtb
1473newline
1474setbad 27
1475ret
1476end
1477pscr
1478ptb 0 get tn 0
1479setchk
1480mutrun mo ml
1481pscr
1482pword 7
1483setchk
1484end
1485note the checker path chk = pb
1486fn setchk to void
1487for i ulong get pn 0
1488put chk i get pb i
1489end
1490put chn 0 get pn 0
1491end
1492note big = cs with the bytes of file dec[fo, fl) replaced by those of file dec[tw, tl); false when the
1493note first file is empty, longer than 65536 bytes, or not present in cs exactly once
1494fn mutsub fo ulong fl ulong tw ulong tl ulong to bool
1495tbdec fo fl
1496put bgn 0 0
1497addfile 0 get tn 0
1498fz = get bgn 0
1499if or ne get bad 0 0 or eq fz 0 gt fz 65536
1500ret false
1501end
1502for i ulong fz
1503put buf i get big i
1504end
1505at = findonce fz
1506if ge at get csn 0
1507ret false
1508end
1509for i ulong at bound 16777216
1510put big i get cs i
1511end
1512put bgn 0 at
1513tbdec tw tl
1514addfile 0 get tn 0
1515if ne get bad 0 0
1516ret false
1517end
1518ret mutrest add at fz
1519end
1520note append cs[s .. csn) to big
1521fn mutrest s ulong to bool
1522b = get bgn 0
1523rest = sub get csn 0 s
1524if gt rest sub len big b
1525setbad 28
1526ret false
1527end
1528for i ulong rest bound 16777216
1529put big add b i get cs add s i
1530end
1531put bgn 0 add b rest
1532ret true
1533end
1534note run the mutant checker (chk) over the case lists until a case fails
1535fn mutrun mo ulong ml ulong to void
1536put mode 0 1
1537killed = false
1538k = ulong 7
1539while and not killed lt k get na 0 bound 64
1540if not runlist k
1541killed = true
1542end
1543if ne get bad 0 0
1544ret
1545end
1546k = add k 1
1547end
1548put mode 0 0
1549tbdec mo ml
1550if killed
1551put cnt 6 add get cnt 6 1
1552say 17
1553showtb
1554say 18
1555p = get hit 0
1556tokenize p lineend p
1557showtok 0
1558newline
1559else
1560say 19
1561showtb
1562newline
1563put cnt 4 add get cnt 4 1
1564end
1565end
1566note the only place in cs where buf[0 .. fz) starts, or csn when it is absent or appears twice
1567fn findonce fz ulong to ulong
1568n = get csn 0
1569if lt n fz
1570ret n
1571end
1572found = n
1573for i ulong add sub n fz 1 bound 16777216
1574if matchat i fz
1575if lt found n
1576ret n
1577end
1578found = i
1579end
1580end
1581ret found
1582end
1583note cs[i .. i+fz) equals buf[0 .. fz)
1584fn matchat i ulong fz ulong to bool
1585for j ulong fz
1586if ne get cs add i j get buf j
1587ret false
1588end
1589end
1590ret true
1591end
1592
1593note the oracle identity, the scratch directory and the child descriptors; 0 or an INVALID message id
1594fn setup to ulong
1595if not loadnums 11 64
1596ret 24
1597end
1598for i ulong 64
1599put kk i uint get nums i
1600end
1601if not loadnums 12 8
1602ret 24
1603end
1604for i ulong 8
1605put ivs i uint get nums i
1606end
1607pclear
1608parg 1
1609if not hashfile
1610ret 23
1611end
1612say 33
1613showpb
1614say 35
1615showhex
1616newline
1617tbarg 2
1618if ne get tn 0 64
1619ret 23
1620end
1621for i ulong 64
1622if ne get tb i get hexd i
1623ret 23
1624end
1625end
1626pclear
1627parg 6
1628if lt makedir pb 0 get pn 0 448 0
1629ret 25
1630end
1631ret fdsetup
1632end
1633fn fdsetup to ulong
1634m0 = memfd words 0 4 1
1635m1 = memfd words 0 4 1
1636m2 = memfd words 0 4 1
1637m3 = memfd words 0 4 1
1638m4 = memfd words 0 4 1
1639if or or lt m0 0 lt m1 0 or or lt m2 0 lt m3 0 lt m4 0
1640ret 30
1641end
1642put fds 0 int m0
1643put fds 1 int m1
1644put fds 2 int m2
1645put fds 3 int m3
1646put fds 4 int m4
1647m5 = memfd words 0 4 1
1648if lt m5 0
1649ret 30
1650end
1651put fds 5 int m5
1652tbarg 4
1653pclear
1654ptb 0 get tn 0
1655if not readdec
1656ret 29
1657end
1658if not loaddec
1659if eq get bad 0 36
1660ret 36
1661end
1662ret 29
1663end
1664ret 0
1665end
1666note SRC into cs and compiled to SCRATCH/fecheck (chk); 0, an INVALID message id, or 1000 + omc's exit
1667fn mkcheck to ulong
1668tbarg 3
1669put bgn 0 0
1670addsrc
1671if ne get bad 0 0
1672ret 28
1673end
1674n = get bgn 0
1675for i ulong n bound 16777216
1676put cs i get big i
1677end
1678put csn 0 n
1679pscr
1680pword 7
1681pword 9
1682if not bigfile
1683ret 30
1684end
1685x = build
1686if ne x 0
1687ret add 1000 x
1688end
1689if not hashfile
1690ret 30
1691end
1692say 34
1693showpb
1694say 35
1695showhex
1696newline
1697ret 0
1698end
1699note every case list, then the unused declarations; false on a setup failure (bad)
1700fn runall to bool
1701k = ulong 7
1702while lt k get na 0 bound 64
1703if not runlist k
1704ret false
1705end
1706k = add k 1
1707end
1708for d ulong get ndc 0
1709if eq get dus d 0
1710say 20
1711tbdnm get dno d get dnl d
1712showtb
1713newline
1714put cnt 4 add get cnt 4 1
1715end
1716end
1717ret true
1718end
1719fn main to int
1720if not loadargs
1721say 22
1722newline
1723ret 2
1724end
1725e = setup
1726if ne e 0
1727ret invalid e
1728end
1729e = mkcheck
1730if ge e 1000
1731say 26
1732num sub e 1000
1733newline
1734put cnt 4 1
1735ret verdict 2
1736end
1737if ne e 0
1738ret invalid e
1739end
1740if not runall
1741ret invalid get bad 0
1742end
1743mutants
1744if ne get bad 0 0
1745ret invalid get bad 0
1746end
1747if eq get cnt 4 0
1748ret verdict 1
1749end
1750ret verdict 2
1751end