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 / core / units-gate.om

797 lines · 15099 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/units-gate.om: OMEGA-CORE-UNITS, the gate of the pure-Omega units of Sovereign Intelligence layer 'core', in
3note seeded Omega (Linux AArch64). Build from the repository root (OM-2 program mode; seed/ng/sha256.om supplies
4note sh_dat, sh_all, sh_dig):
5note { printf 'om2\n'; cat seed/ng/sha256.om core/units-gate.om; } | seed/out/omc > core-units-gate
6note Run from the repository root: core-units-gate OMC SCRATCH
7note OMC the compiler under test; its sha256 must equal the first word of seed/out/omc.sha256, or nothing
8note runs (exit 69, nothing written)
9note SCRATCH a new directory (created 0700), at most 1024 bytes of path; an existing one is refused (exit 2)
10note Units. core/units.tsv lists every unit, one per line: NAME then the paths of its parts, single spaces; a line
11note starting with # is a comment and an empty line is skipped. NAME is 1 to 249 of A-Z a-z 0-9 _ and appears once
12note (249 leaves room for the longest scratch suffix, ".1.err", inside a 255-byte file name).
13note The parts are concatenated in order, each followed by a newline (the composition of core/units.tsv), into
14note SCRATCH/NAME.om and compiled twice by OMC: each compile exits 0 with empty stderr, and both outputs hold the same
15note bytes. A malformed or repeated row, a missing part, a failed compile, two different outputs or a table with no
16note unit is a FAIL line. The runner (core/README.md) also requires the exact unit count.
17note Control, must be caught: a compiler one byte longer fails the digest check.
18note Output: one line per unit, then OMEGA-CORE-UNITS PASS|FAIL units=U controls=C caught=K hardware=NOT_RUN.
19note Exit 0 PASS, 1 FAIL, 2 bad usage or setup, 69 compiler digest refused. Every input is length-checked; a trap
20note exit (101..114) would be a defect of this gate, never a pass.
21note This gate checks that the units compile reproducibly with this repository's compiler. It does not run them:
22note their behaviour checks are still C, Python and shell upstream (core/UNITS.md lists them).
23text t_pin "seed/out/omc.sha256"
24text t_tab "core/units.tsv"
25text t_om ".om"
26text t_b1 ".1"
27text t_b2 ".2"
28text t_e1 ".1.err"
29text t_e2 ".2.err"
30text t_badname "units.tsv: name outside A-Z a-z 0-9 _"
31text t_dup "units.tsv: name repeated, or more than 64 KiB of names"
32text t_empty "units.tsv names no unit"
33text t_plus "omc-plus-one"
34text t_usage "usage: core-units-gate OMC SCRATCH (run from the omega-mv repository root)\n"
35text t_refd "OMEGA-CORE-UNITS REFUSED: compiler digest differs from seed/out/omc.sha256 (exit 69)\n"
36text t_nopin "OMEGA-CORE-UNITS INVALID: cannot read a 64-hex digest from seed/out/omc.sha256\n"
37text t_noscr "OMEGA-CORE-UNITS INVALID: SCRATCH exists or cannot be made\n"
38text t_notab "OMEGA-CORE-UNITS INVALID: cannot read core/units.tsv\n"
39text t_long "OMEGA-CORE-UNITS INVALID: SCRATCH path longer than 1024 bytes\n"
40text t_ok "ok "
41text t_fail "FAIL "
42text t_unit "unit "
43text t_ctl "control "
44text t_bytes " bytes="
45text t_sha " sha256="
46text t_exit " exit="
47text t_src "source unreadable: "
48text t_big "output unreadable or over 16 MiB"
49text t_cmp "compile"
50text t_stderr " stderr not empty"
51text t_twice " two builds differ"
52text t_write "cannot write the composed unit in SCRATCH"
53text t_row "units.tsv: malformed row (name 1..249 bytes, at least one part)"
54text t_plong "units.tsv: a part path is longer than 4000 bytes"
55text t_caught " caught"
56text t_esc " ESCAPED"
57text t_sum "OMEGA-CORE-UNITS "
58text t_pass "PASS"
59text t_fl "FAIL"
60text t_su " units="
61text t_sc " controls="
62text t_sk " caught="
63text t_hw " hardware=NOT_RUN\n"
64global g_pa uchar 4096
65global g_scr uchar 4096
66global g_omc uchar 4096
67global g_nm uchar 256
68global g_av uchar 8192
69global g_b uchar 16777216
70global g_u uchar 1048576
71global g_o uchar 8192
72global g_hx uchar 64
73global g_pin uchar 64
74global g_d1 uchar 32
75global g_stb uchar 4
76global g_n ulong 32
77global g_seen uchar 65536
78note g_n: 0 path length, 1 omc length, 2 scratch length (with '/'), 3 argv bytes, 4 argc, 5 output length,
79note 6 buffer length, 7 table length, 8 units, 11 failures, 12 controls, 13 caught, 14 name length, 15 g_seen length,
80note 16 unit rows
81note ---- output
82fn oc c ulong to void
83n = get g_n 5
84put g_o n uchar c
85put g_n 5 add n 1
86end
87fn ot t view uchar * to void
88for i ulong len t bound 4096
89oc ulong get t i
90end
91end
92fn onm to void
93for i ulong get g_n 14 bound 256
94oc ulong get g_nm i
95end
96end
97fn onum v ulong to void
98if ge v 10
99onum div v 10
100end
101oc add 48 rem v 10
102end
103fn ohex to void
104for j ulong 32
105b = get sh_dig j
106oc ulong sh_hexd uint shr b 4
107oc ulong sh_hexd uint and b 15
108end
109end
110fn ohx to void
111for j ulong 64
112oc ulong get g_hx j
113end
114end
115fn oflush to void
116r = write 1 g_o 0 get g_n 5
117put g_n 5 0
118end
119fn onl to void
120oc 10
121oflush
122end
123note a failed check: FAIL KIND NAME then the reason; counts a failure
124fn ofail kind view uchar * why view uchar * to void
125ot t_fail
126ot kind
127onm
128oc 32
129ot why
130onl
131put g_n 11 add get g_n 11 1
132end
133note ---- paths: g_pa[0, g_n 0)
134fn pclr to void
135put g_n 0 0
136end
137fn pc c ulong to void
138n = get g_n 0
139put g_pa n uchar c
140put g_n 0 add n 1
141end
142fn pt t view uchar * to void
143for i ulong len t bound 4096
144pc ulong get t i
145end
146end
147fn pscr to void
148for j ulong get g_n 2 bound 4096
149pc ulong get g_scr j
150end
151end
152fn pnm to void
153for j ulong get g_n 14 bound 256
154pc ulong get g_nm j
155end
156end
157note scratch file NAME then suffix
158fn psn suf view uchar * to void
159pclr
160pscr
161pnm
162pt suf
163end
164note scratch file named by a text
165fn pst t view uchar * suf view uchar * to void
166pclr
167pscr
168pt t
169pt suf
170end
171note scratch file named by a text alone
172fn psc t view uchar * to void
173pclr
174pscr
175pt t
176end
177fn nmset t view uchar * to void
178for i ulong len t bound 256
179put g_nm i get t i
180end
181put g_n 14 len t
182end
183note ---- files
184fn fopn fl ulong mode ulong to int
185r = fopen g_pa 0 get g_n 0 int fl int mode
186if lt r 0
187ret -1
188end
189ret int r
190end
191fn frd to int
192ret fopn 524288 0
193end
194fn fwr to int
195ret fopn 524865 420
196end
197fn fexe to int
198r = unlink g_pa 0 get g_n 0
199ret fopn 524865 448
200end
201fn fclo fd int to void
202if ge fd 0
203r = fclose fd
204end
205end
206note append the file at g_pa to g_b; false when unreadable or past the buffer
207fn bload to bool
208fd = frd
209if lt fd 0
210ret false
211end
212n = get g_n 6
213ok = true
214while true bound 16777218
215x = read fd g_b n sub len g_b n
216if lt x 0
217ok = false
218break
219end
220if eq x 0
221break
222end
223n = add n ulong x
224if ge n len g_b
225ok = false
226break
227end
228end
229fclo fd
230put g_n 6 n
231ret ok
232end
233note write g_b[0, g_n 6) to the file at g_pa (0644, or 0700 when exe)
234fn bsave exe bool to bool
235fd = int -1
236if exe
237fd = fexe
238else
239fd = fwr
240end
241if lt fd 0
242ret false
243end
244n = get g_n 6
245o = ulong 0
246ok = true
247while lt o n bound 16777217
248x = write fd g_b o sub n o
249if le x 0
250ok = false
251break
252end
253o = add o ulong x
254end
255fclo fd
256ret ok
257end
258note sha256 of the file at g_pa into sh_dig; false when unreadable or past 16 MiB
259fn fhash to bool
260fd = frd
261if lt fd 0
262ret false
263end
264n = ulong 0
265ok = true
266while true bound 16777218
267x = read fd sh_dat n sub len sh_dat n
268if lt x 0
269ok = false
270break
271end
272if eq x 0
273break
274end
275n = add n ulong x
276if ge n len sh_dat
277ok = false
278break
279end
280end
281fclo fd
282if not ok
283ret false
284end
285sh_all n
286ret true
287end
288note the size of the file at g_pa, or 2^63 when unreadable
289fn fsize to ulong
290put g_n 6 0
291if not bload
292ret shl ulong 1 63
293end
294ret get g_n 6
295end
296note sh_dig equals the hex in g_hx
297fn digeq to bool
298for j ulong 32
299b = get sh_dig j
300if ne get g_hx mul j 2 sh_hexd uint shr b 4
301ret false
302end
303if ne get g_hx add mul j 2 1 sh_hexd uint and b 15
304ret false
305end
306end
307ret true
308end
309fn dsave to void
310for j ulong 32
311put g_d1 j get sh_dig j
312end
313end
314fn dsame to bool
315for j ulong 32
316if ne get g_d1 j get sh_dig j
317ret false
318end
319end
320ret true
321end
322note ---- processes
323fn aclr to void
324put g_n 3 2
325put g_n 4 0
326put g_av 0 0
327put g_av 1 0
328end
329note append g_pa as one argument
330fn apa to void
331p = get g_n 3
332n = get g_n 0
333for j ulong n bound 4096
334put g_av add add p 2 j get g_pa j
335end
336put g_av p uchar and n 255
337put g_av add p 1 uchar shr n 8
338put g_n 3 add p add n 2
339c = add get g_n 4 1
340put g_n 4 c
341put g_av 0 uchar and c 255
342put g_av 1 uchar shr c 8
343end
344fn atx t view uchar * to void
345pclr
346pt t
347apa
348end
349fn redir s int d int to void
350if ne dupfd s d 0 long d
351exit 127
352end
353end
354note run the argument list with stdin, stdout, stderr fds (closed here): the exit code, 256 + the signal, 512
355note when a file could not be opened, 1000 when the child passed the 600 s deadline (killed)
356fn spawn fi int fo int fe int to ulong
357if or lt fi 0 or lt fo 0 lt fe 0
358fclo fi
359fclo fo
360fclo fe
361ret 512
362end
363pid = fork
364if lt pid 0
365exit 2
366end
367if eq pid 0
368redir fi 0
369redir fo 1
370redir fe 2
371r = execprog g_av 0 get g_n 3
372exit 127
373end
374fclo fi
375fclo fo
376fclo fe
377t0 = monons
378while true bound 1000000000
379r = waitpid int pid g_stb 0 len g_stb 1
380if eq r pid
381x = ulong get g_stb 0
382if ne x 0
383ret add 256 x
384end
385ret ulong get g_stb 1
386end
387if lt r 0
388exit 2
389end
390if gt sub monons t0 600000000000
391r = killpid int pid 9
392r = waitpid int pid g_stb 0 len g_stb 0
393ret 1000
394end
395q = sleepns 200000
396end
397exit 2
398end
399note OMC < SCRATCH/NAME.om > SCRATCH/NAME.SUF 2> SCRATCH/NAME.ERR
400fn omc suf view uchar * err view uchar * to ulong
401psn t_om
402fi = frd
403aclr
404pclr
405for j ulong get g_n 1 bound 4096
406pc ulong get g_omc j
407end
408apa
409psn suf
410fo = fexe
411psn err
412fe = fwr
413ret spawn fi fo fe
414end
415note ---- the compiler digest
416note the first 64 characters of seed/out/omc.sha256 into g_pin; false unless lowercase hex
417fn pinread to bool
418pclr
419pt t_pin
420put g_n 6 0
421if not bload
422ret false
423end
424if lt get g_n 6 64
425ret false
426end
427for j ulong 64
428c = get g_b j
429ok = or and ge c 48 le c 57 and ge c 97 le c 102
430if not ok
431ret false
432end
433put g_pin j c
434end
435ret true
436end
437note the file at g_pa hashes to g_pin
438fn pinok to bool
439if not fhash
440ret false
441end
442for j ulong 64
443put g_hx j get g_pin j
444end
445ret digeq
446end
447note ---- units
448note g_u[i] is c, for i below e (never reads at or past e)
449fn uis i ulong e ulong c ulong to bool
450if ge i e
451ret false
452end
453ret eq ulong get g_u i c
454end
455fn uisnt i ulong e ulong c ulong to bool
456if ge i e
457ret false
458end
459ret ne ulong get g_u i c
460end
461note append one newline to g_b (units.sh writes one after every part)
462fn nl to bool
463n = get g_n 6
464if ge add n 1 len g_b
465ret false
466end
467put g_b n 10
468put g_n 6 add n 1
469ret true
470end
471note a missing or unreadable part: FAIL unit NAME source unreadable: PATH (the path is in g_pa, at most 4000 bytes)
472fn psrcfail to void
473ot t_fail
474ot t_unit
475onm
476oc 32
477ot t_src
478for q ulong get g_n 0 bound 4096
479oc ulong get g_pa q
480end
481onl
482put g_n 11 add get g_n 11 1
483end
484note compose the parts g_u[s, e) into g_b as core/units.tsv does: each part's bytes, then one newline; false
485note (reported) when a part is unreadable or its path is longer than 4000 bytes
486fn compose s ulong e ulong to bool
487put g_n 6 0
488i = s
489while lt i e bound 1048576
490while uis i e 32 bound 1048576
491i = add i 1
492end
493if ge i e
494break
495end
496j = i
497while uisnt j e 32 bound 1048576
498j = add j 1
499end
500if gt sub j i 4000
501ofail t_unit t_plong
502ret false
503end
504pclr
505for q ulong sub j i bound 4096
506pc ulong get g_u add i q
507end
508if not bload
509psrcfail
510ret false
511end
512if not nl
513ofail t_unit t_big
514ret false
515end
516i = j
517end
518ret true
519end
520note compile the composed unit twice; true when both exit 0 with empty stderr and equal bytes
521fn build to bool
522psn t_om
523if not bsave false
524ofail t_unit t_write
525ret false
526end
527r1 = omc t_b1 t_e1
528r2 = omc t_b2 t_e2
529if or ne r1 0 ne r2 0
530ot t_fail
531ot t_unit
532onm
533oc 32
534ot t_cmp
535ot t_exit
536onum r1
537oc 47
538onum r2
539onl
540put g_n 11 add get g_n 11 1
541ret false
542end
543psn t_e1
544s1 = fsize
545psn t_e2
546s2 = fsize
547if or ne s1 0 ne s2 0
548ofail t_unit t_stderr
549ret false
550end
551psn t_b1
552if not fhash
553ofail t_unit t_big
554ret false
555end
556dsave
557psn t_b2
558if not fhash
559ofail t_unit t_big
560ret false
561end
562if not dsame
563ofail t_unit t_twice
564ret false
565end
566psn t_b1
567sz = fsize
568psn t_b1
569if not fhash
570ofail t_unit t_big
571ret false
572end
573ot t_ok
574ot t_unit
575onm
576ot t_bytes
577onum sz
578ot t_sha
579ohex
580onl
581put g_n 8 add get g_n 8 1
582ret true
583end
584note one table row [s, e): NAME then parts; # starts a comment line; an empty line is skipped
585fn urow s ulong e ulong to void
586if or eq s e eq get g_u s 35
587ret
588end
589put g_n 14 0
590k = s
591while uisnt k e 32 bound 1048576
592k = add k 1
593end
594if or ge k e or eq k s gt sub k s 249
595ofail t_unit t_row
596ret
597end
598for q ulong sub k s bound 256
599put g_nm q get g_u add s q
600end
601put g_n 14 sub k s
602put g_n 16 add get g_n 16 1
603if not nameok
604ofail t_unit t_badname
605ret
606end
607if not nameadd
608ofail t_unit t_dup
609ret
610end
611if compose k e
612z = build
613end
614end
615note a unit name uses only A-Z a-z 0-9 _ (A-Z a-z 0-9 _), so it names a file in SCRATCH
616fn nameok to bool
617for q ulong get g_n 14 bound 256
618c = get g_nm q
619d = and ge c 48 le c 57
620u = and ge c 65 le c 90
621l = and ge c 97 le c 122
622ok = or or d u or l eq c 95
623if not ok
624ret false
625end
626end
627ret true
628end
629note record the name in g_seen (names joined by newlines); false when it is already there or g_seen is full
630fn nameadd to bool
631n = get g_n 15
632m = get g_n 14
633p = ulong 0
634while lt p n bound 65536
635q = p
636while and lt q n ne get g_seen q 10 bound 65536
637q = add q 1
638end
639if nameat p q
640ret false
641end
642p = add q 1
643end
644if ge add add n m 1 len g_seen
645ret false
646end
647for j ulong m bound 256
648put g_seen add n j get g_nm j
649end
650put g_seen add n m 10
651put g_n 15 add add n m 1
652ret true
653end
654note g_seen[p, q) equals the current name
655fn nameat p ulong q ulong to bool
656if ne sub q p get g_n 14
657ret false
658end
659for j ulong sub q p bound 256
660if ne get g_seen add p j get g_nm j
661ret false
662end
663end
664ret true
665end
666fn table to bool
667pclr
668pt t_tab
669put g_n 6 0
670if not bload
671ret false
672end
673n = get g_n 6
674if ge n len g_u
675ret false
676end
677for q ulong n bound 1048576
678put g_u q get g_b q
679end
680put g_n 7 n
681s = ulong 0
682while lt s n bound 1048576
683e = s
684while uisnt e n 10 bound 1048576
685e = add e 1
686end
687urow s e
688s = add e 1
689end
690if eq get g_n 16 0
691nmset t_tab
692ofail t_unit t_empty
693end
694ret true
695end
696note ---- controls
697fn caught c bool what view uchar * to void
698put g_n 12 add get g_n 12 1
699nmset what
700if c
701ot t_ok
702ot t_ctl
703onm
704ot t_caught
705onl
706put g_n 13 add get g_n 13 1
707else
708ofail t_ctl t_esc
709end
710end
711note a compiler one byte longer must fail the digest check
712fn cwrong to void
713pclr
714for j ulong get g_n 1 bound 4096
715pc ulong get g_omc j
716end
717put g_n 6 0
718ok = bload
719n = get g_n 6
720put g_b n 0
721put g_n 6 add n 1
722psc t_plus
723ok = and ok bsave true
724psc t_plus
725c = and ok not pinok
726caught c t_plus
727end
728note ---- main
729fn ausage to int
730r = write 2 t_usage 0 len t_usage
731ret 2
732end
733fn main to int
734if ne argcnt 3
735ret ausage
736end
737r = argread 1 g_omc 0 4095
738if lt r 1
739ret ausage
740end
741put g_n 1 ulong r
742if not pinread
743r = write 2 t_nopin 0 len t_nopin
744ret 2
745end
746pclr
747for j ulong get g_n 1 bound 4096
748pc ulong get g_omc j
749end
750if not pinok
751r = write 2 t_refd 0 len t_refd
752ret 69
753end
754r = argread 2 g_scr 0 4094
755if lt r 1
756ret ausage
757end
758if gt r 1024
759r = write 2 t_long 0 len t_long
760ret 2
761end
762n = ulong r
763x = makedir g_scr 0 n 448
764if ne x 0
765r = write 2 t_noscr 0 len t_noscr
766ret 2
767end
768put g_scr n 47
769put g_n 2 add n 1
770if not table
771r = write 2 t_notab 0 len t_notab
772ret 2
773end
774cwrong
775f = get g_n 11
776ot t_sum
777if eq f 0
778ot t_pass
779else
780ot t_fl
781end
782ot t_su
783onum get g_n 8
784ot t_sc
785onum get g_n 12
786ot t_sk
787onum get g_n 13
788ot t_hw
789oflush
790if ne f 0
791ret 1
792end
793if ne get g_n 12 get g_n 13
794ret 1
795end
796ret 0
797end