interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / compiler / rebuild.om

1445 lines · 28280 bytes Raw Blame History
1om2
2note compiler/rebuild.om: after /si has fetched the projects, rebuild each layer of the stack from
3note source and compare it with what is published, layer by layer from the seed upward. Pure seeded
4note Omega. Table-driven: the layers, their repositories and their commands are the rows of
5note layers.tsv beside repos.tsv; this program holds no layer, repository or command of its own.
6note
7note Build with the seeded compiler (compiler/README.md names the pins):
8note omc < compiler/rebuild.om > rebuild && chmod +x rebuild
9note Run:
10note rebuild check LAYERS
11note rebuild run LAYERS REPOS DIR WORK [hw=yes] [wrap=WORDS]
12note LAYERS layers.tsv: the line "layers TAB N" (N from 2 to 999), the header line
13note "layer TAB repo TAB needs TAB command TAB artifact TAB compare", then one row per step:
14note layer 1..N, rows in layer order, every layer from 1 to N with at least one row
15note repo a project name; its commit is the head repos.tsv gives it
16note needs any; aarch64-linux; macos (this is a Linux program, so always NOT_RUN); or
17note pci:VVVV:DDDD, a hardware row: it runs only with hw=yes and a PCI device
18note VVVV:DDDD under /sys/bus/pci/devices, else NOT_RUN, never PASS
19note command the repository's own documented command, run by /bin/sh -c in its clone,
20note or - when it has none (NOT_RUN no documented rebuild command)
21note artifact the file the command builds, a path in the clone, or -
22note compare a path in the clone whose bytes at the commit the artifact must equal, or -
23note REPOS repos.tsv: each project's commit is its head there
24note DIR the directory /si fetched into, one clone per project named as in repos.tsv
25note WORK an existing directory: a log per row, layer-L-R.log (R the row number), and the
26note files git writes
27note check validates LAYERS and prints "rebuild: table ok: N layers, R rows". run validates it the
28note same way, then takes the rows in order and prints one line per row:
29note PASS | layer L | repo | commit C | rc 0 | sha256 S | log G | cmd COMMAND
30note FAIL <why> | layer L | ... a clone not at its commit, a command exiting nonzero, a
31note missing artifact, or an artifact whose sha256 differs from
32note the committed file's
33note NOT_RUN <why> | layer L | ... needs not met here, no documented command, not fetched
34note and last "REBUILD pass=P fail=F not_run=N rows=R layers=L". Exit 0 when no row FAILs and every
35note row of layers 1 and 2 PASSes, else 1. A refused table: exit 1, one stderr line starting
36note "rebuild: " and nothing run. A usage error: exit 2.
37note A command runs with stdin /dev/null and stdout and stderr in its log. Exec hands a child an
38note empty environment, so the command gets HOME, PATH, USER, LOGNAME, LANG, LC_ALL, TMPDIR,
39note CARGO_HOME and RUSTUP_HOME copied from this program's own, where set, through /usr/bin/env.
40note wrap=WORDS puts WORDS, split at spaces, between those and /bin/sh, for example a CPU pool:
41note wrap=flock /path/pool.lock taskset -c 4,5.
42note SHA-256 (fn kinit to fn finish) is copied unchanged from compiler/gate.om, which copied it
43note from omega-mv om/sha256/sha256-om2.om at f4df82394e35893dcf1354dda69a2fe028d94bc1
44note (Apache-2.0, FIPS 180-4).
45
46text tpre "rebuild: "
47text tnl "\n"
48text tusage "usage: rebuild check LAYERS | rebuild run LAYERS REPOS DIR WORK [hw=yes] [wrap=WORDS]\n"
49text tcheck "check"
50text trun "run"
51text thw "hw=yes"
52text twrap "wrap="
53text targ "cannot read a command-line argument"
54text topen "cannot open "
55text tbig "file over its buffer: "
56text tnodir "WORK is not a directory: "
57text tlay "layers\t"
58text thdr "layer\trepo\tneeds\tcommand\tartifact\tcompare"
59text mfirst "the first line must be layers TAB N, N from 2 to 999"
60text mhdr "the second line must be the header layer TAB repo TAB needs TAB command TAB artifact TAB compare"
61text mcols "a row needs 6 tab-separated fields: line "
62text mlayer "layer is not a number from 1 to the declared count: line "
63text morder "rows are not in layer order: line "
64text mnorow1 "layer "
65text mnorow2 " has no row"
66text mrepo "bad repo name (letters, digits, . _ -, not starting with .): line "
67text mneeds "needs is not any, aarch64-linux, macos or pci:VVVV:DDDD (lowercase hex): line "
68text mcmd "command is empty, over 1024 bytes or has a control byte: line "
69text mpath "artifact or compare is not - or a relative path without .. or control bytes: line "
70text mcmp "compare needs an artifact: line "
71text mmany "more than 4096 rows at line "
72text tok1 "rebuild: table ok: "
73text tok2 " layers, "
74text tok3 " rows\n"
75text nany "any"
76text na64 "aarch64-linux"
77text nmac "macos"
78text npci "pci:"
79text tdash "-"
80text rmac "macOS only; this runner is a Linux program"
81text ra64 "needs AArch64 Linux"
82text rhw "hardware row; runs only with hw=yes, in an authorized hardware window"
83text rchip "hardware row; no PCI device "
84text rchip2 " here"
85text rcmd "no documented rebuild command"
86text rlist "not in repos.tsv, so /si does not fetch it"
87text rbadh "the repos.tsv head is not 40 lowercase hex digits"
88text rfetch "not fetched: no clone in DIR"
89text rhead "clone is not at the repos.tsv commit; it is at "
90text runk "clone HEAD cannot be read"
91text rexit "command exited nonzero"
92text rio "cannot write its log in WORK"
93text rpkt "command, wrap and environment exceed one exec packet (4096 bytes, 64 records)"
94text rart "artifact missing: "
95text rcom "no committed file at the commit: "
96text rdiff "artifact differs from committed "
97text tsp " "
98text vpass "PASS"
99text vfail "FAIL "
100text vnrun "NOT_RUN "
101text bl " | layer "
102text br " | "
103text bc " | commit "
104text brc " | rc "
105text bsha " | sha256 "
106text blog " | log "
107text bcmd " | cmd "
108text sum1 "REBUILD pass="
109text sum2 " fail="
110text sum3 " not_run="
111text sum4 " rows="
112text sum5 " layers="
113text tself "/proc/self/exe"
114text tpcid "/sys/bus/pci/devices"
115text tvend "/vendor"
116text tdev "/device"
117text tnull "/dev/null"
118text tlogp "/layer-"
119text tlog ".log"
120text tcom ".committed"
121text tgit "/git.out"
122text xenv "/usr/bin/env"
123text xsh "/bin/sh"
124text xc "-c"
125text xgit "git"
126text xrp "rev-parse"
127text xhead "HEAD"
128text xcf "cat-file"
129text xblob "blob"
130text e1 "HOME="
131text e2 "PATH="
132text e3 "USER="
133text e4 "LOGNAME="
134text e5 "LANG="
135text e6 "LC_ALL="
136text e7 "TMPDIR="
137text e8 "CARGO_HOME="
138text e9 "RUSTUP_HOME="
139text hexd "0123456789abcdef"
140
141note tb: the layers table; rp: repos.tsv
142global tb uchar 1048576
143global tbn ulong 1
144global rp uchar 4194304
145global rpn ulong 1
146note rows, 12 cells each: layer, repo offset and length, needs (1 any, 2 aarch64-linux, 3 macos,
147note 4 pci), command, artifact and compare offsets and lengths (4..9), line number (10), needs
148note offset (11). nlay: the declared layer count; prevl: the layer of the row before
149global row ulong 49152
150global nrow ulong 1
151global nlay ulong 1
152global prevl ulong 1
153note pb: the path being built; dir: DIR; wk: WORK; wr: the wrap words; cd: the clone of the row
154global pb uchar 8192
155global pbn ulong 1
156global dir uchar 4096
157global dirn ulong 1
158global wk uchar 4096
159global wkn ulong 1
160global wr uchar 4096
161global wrn ulong 1
162global cd uchar 8192
163global cdn ulong 1
164note av: the packed argv of a child (u16 count, then u16 length and bytes per record); ovf is 1
165note when a record did not fit. ev: the environment records handed on, packed the same way
166global av uchar 8192
167global avp ulong 1
168global avc ulong 1
169global ovf ulong 1
170global ev uchar 4096
171global evn ulong 1
172global evc ulong 1
173note st: a child's wait status; gb: small file reads; db: directory entries
174global st uchar 4
175global gb uchar 4096
176global gbn ulong 1
177global db uchar 32768
178note the current row: its commit (cm, cmn 40 when known), exit status (crc, hrc 1 when run),
179note artifact, log and committed sha256 in hex (hx, hl, hc; hsa and hlg 1 when known)
180global cm uchar 40
181global cmn ulong 1
182global crc ulong 1
183global hrc ulong 1
184global hx uchar 64
185global hl uchar 64
186global hc uchar 64
187global hsa ulong 1
188global hlg ulong 1
189global dg uchar 32
190note tally: PASS, FAIL and NOT_RUN rows; l12: rows of layers 1 and 2 that did not PASS;
191note a64: 1 when this program is an AArch64 ELF; hw: 1 with hw=yes
192global tally ulong 3
193global l12 ulong 1
194global a64 ulong 1
195global hw ulong 1
196
197text tslash "/"
198text tcolon ":"
199text tempty ""
200
201note K[0..63]: the first 32 bits of the fractional parts of the cube roots of the first 64 primes.
202note Copied mechanically from seed/qualification/om2/fixtures/sha256-k.dec (tool-computed, never typed).
203fn kinit k edit uint 64 to void
204put k 0 0x428a2f98
205put k 1 0x71374491
206put k 2 0xb5c0fbcf
207put k 3 0xe9b5dba5
208put k 4 0x3956c25b
209put k 5 0x59f111f1
210put k 6 0x923f82a4
211put k 7 0xab1c5ed5
212put k 8 0xd807aa98
213put k 9 0x12835b01
214put k 10 0x243185be
215put k 11 0x550c7dc3
216put k 12 0x72be5d74
217put k 13 0x80deb1fe
218put k 14 0x9bdc06a7
219put k 15 0xc19bf174
220put k 16 0xe49b69c1
221put k 17 0xefbe4786
222put k 18 0x0fc19dc6
223put k 19 0x240ca1cc
224put k 20 0x2de92c6f
225put k 21 0x4a7484aa
226put k 22 0x5cb0a9dc
227put k 23 0x76f988da
228put k 24 0x983e5152
229put k 25 0xa831c66d
230put k 26 0xb00327c8
231put k 27 0xbf597fc7
232put k 28 0xc6e00bf3
233put k 29 0xd5a79147
234put k 30 0x06ca6351
235put k 31 0x14292967
236put k 32 0x27b70a85
237put k 33 0x2e1b2138
238put k 34 0x4d2c6dfc
239put k 35 0x53380d13
240put k 36 0x650a7354
241put k 37 0x766a0abb
242put k 38 0x81c2c92e
243put k 39 0x92722c85
244put k 40 0xa2bfe8a1
245put k 41 0xa81a664b
246put k 42 0xc24b8b70
247put k 43 0xc76c51a3
248put k 44 0xd192e819
249put k 45 0xd6990624
250put k 46 0xf40e3585
251put k 47 0x106aa070
252put k 48 0x19a4c116
253put k 49 0x1e376c08
254put k 50 0x2748774c
255put k 51 0x34b0bcb5
256put k 52 0x391c0cb3
257put k 53 0x4ed8aa4a
258put k 54 0x5b9cca4f
259put k 55 0x682e6ff3
260put k 56 0x748f82ee
261put k 57 0x78a5636f
262put k 58 0x84c87814
263put k 59 0x8cc70208
264put k 60 0x90befffa
265put k 61 0xa4506ceb
266put k 62 0xbef9a3f7
267put k 63 0xc67178f2
268end
269
270note H(0): the first 32 bits of the fractional parts of the square roots of the first 8 primes
271note (fixtures/sha256-iv.dec).
272fn iv s edit uint 8 to void
273put s 0 0x6a09e667
274put s 1 0xbb67ae85
275put s 2 0x3c6ef372
276put s 3 0xa54ff53a
277put s 4 0x510e527f
278put s 5 0x9b05688c
279put s 6 0x1f83d9ab
280put s 7 0x5be0cd19
281end
282
283note Message schedule of the block m[o .. o+64). w is fresh: its incoming contents are never read.
284note The compiler proves that every get of w reads an element written earlier in this function
285note (error 27 otherwise): w[0..16) are the block's big-endian words, w[16..64) the FIPS recurrence.
286fn sched w edit uint 64 fresh m view uchar * o ulong to void
287q = ulong 0
288x = uint 0
289y = uint 0
290for i ulong 16 unroll
291q = add o mul i 4
292x = uint get m q
293x = or shl x 8 uint get m add q 1
294x = or shl x 8 uint get m add q 2
295put w i or shl x 8 uint get m add q 3
296end
297for i ulong 48 unroll
298x = get w add i 1
299y = get w add i 14
300x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3
301put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10
302end
303end
304
305note The 64 rounds on the chaining value s. One round body, emitted 64 times by unroll; i is a constant
306note in each copy, so get kt i and get w i need no run-time check. uint arithmetic wraps mod 2^32.
307note Ch(e,f,g) = (e and f) xor (not e and g) is written g xor (e and (f xor g)); Maj(a,b,c) =
308note (a and b) xor (a and c) xor (b and c) is written (a and b) or (c and (a or b)). Both are bitwise
309note identities. h first becomes T1, then T1 + T2, which is the next a; the names then shift by one.
310note The sums are built a term per line and each Sigma in two lines through t: 12 names are live, and
311note omc hands out 16 registers with no spilling (error 12 under compiler-s1/s2 for longer lines).
312fn rounds s edit uint 8 w view uint 64 kt view uint 64 to void
313a = get s 0
314b = get s 1
315c = get s 2
316d = get s 3
317e = get s 4
318f = get s 5
319g = get s 6
320h = get s 7
321t = uint 0
322for i ulong 64 unroll
323h = add h get kt i
324h = add h get w i
325t = xor rotr e 6 rotr e 11
326t = xor t rotr e 25
327h = add h t
328h = add h xor g and e xor f g
329d = add d h
330t = xor rotr a 2 rotr a 13
331t = xor t rotr a 22
332h = add h t
333t = and c or a b
334h = add h or and a b t
335t = h
336h = g
337g = f
338f = e
339e = d
340d = c
341c = b
342b = a
343a = t
344end
345put s 0 add get s 0 a
346put s 1 add get s 1 b
347put s 2 add get s 2 c
348put s 3 add get s 3 d
349put s 4 add get s 4 e
350put s 5 add get s 5 f
351put s 6 add get s 6 g
352put s 7 add get s 7 h
353end
354
355note One block m[o .. o+64) into s. sched and rounds stay calls: inlined, their names would share
356note one 16-register frame with this function's parameters (no spilling, error 12).
357fn compress s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * o ulong to void
358sched w m o
359rounds s w kt
360end
361
362note The whole blocks of m (len m is a multiple of 64 and at most 2^25, so under 2^19 passes).
363fn run s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
364for j ulong shr len m 6
365compress s w kt m shl j 6
366end
367end
368
369note The whole blocks of m (len m a multiple of 64). A rolled loop traps 4 after 1000000 passes
370note (OMEGESE.md section 2; omc s_for), so m is cut into runs of 2^25 bytes (2^19 blocks):
371note len m = top * 2^44 + mid * 2^25 + rest, and no loop passes 2^20 times. top reaches 1000000
372note only above 1.7e19 bytes, which no AArch64 address space holds.
373fn blocks s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * to void
374q = ulong 0
375for a ulong shr len m 44
376for b ulong 0x80000
377run s w kt part m q 0x2000000
378q = add q 0x2000000
379end
380end
381for b ulong and shr len m 25 0x7ffff
382run s w kt part m q 0x2000000
383q = add q 0x2000000
384end
385run s w kt part m q sub len m q
386end
387
388note Pad the k pending bytes already in p (k < 64; p was all zero past them): 0x80, zeros, and the
389note 64-bit big-endian bit length of the n-byte message in the last 8 bytes of one or two blocks.
390fn finish s edit uint 8 w edit uint 64 kt view uint 64 p edit uchar 128 k ulong n ulong to void
391put p k 0x80
392e = ulong 64
393if ge k 56
394e = 128
395end
396bits = shl n 3
397for i ulong 8 unroll
398put p sub e add i 1 uchar and shr bits mul i 8 0xff
399end
400compress s w kt view p 0
401if eq e 128
402compress s w kt view p 64
403end
404end
405
406
407fn say t view uchar * to void
408w = write 1 t 0 len t
409end
410fn sayn v ulong to void
411d = new uchar 24 0
412x = v
413k = ulong 0
414while or gt x 0 eq k 0 bound 24
415put d k uchar add rem x 10 48
416k = add k 1
417x = div x 10
418end
419while gt k 0 bound 24
420k = sub k 1
421w = write 1 d k 1
422end
423end
424fn die t view uchar * to void
425w = write 2 tpre 0 len tpre
426w = write 2 t 0 len t
427w = write 2 tnl 0 1
428exit 1
429end
430fn badline t view uchar * ln ulong to void
431say tpre
432say t
433sayn ln
434say tnl
435exit 1
436end
437fn patharg a ulong to void
438n = argread a pb 0 8192
439if le n 0
440die targ
441end
442put pbn 0 ulong n
443end
444fn pc t view uchar * to void
445put pbn 0 0
446pa t
447end
448fn pa t view uchar * to void
449p = get pbn 0
450for i ulong len t
451put pb add p i get t i
452end
453put pbn 0 add p len t
454end
455fn pt o ulong n ulong to void
456p = get pbn 0
457for i ulong n
458put pb add p i get tb add o i
459end
460put pbn 0 add p n
461end
462fn pn v ulong to void
463d = new uchar 24 0
464x = v
465k = ulong 0
466while or gt x 0 eq k 0 bound 24
467put d k uchar add rem x 10 48
468k = add k 1
469x = div x 10
470end
471while gt k 0 bound 24
472k = sub k 1
473p = get pbn 0
474put pb p get d k
475put pbn 0 add p 1
476end
477end
478fn eqt o ulong n ulong t view uchar * to bool
479if ne n len t
480ret false
481end
482for i ulong n
483if ne get tb add o i get t i
484ret false
485end
486end
487ret true
488end
489fn num o ulong n ulong to ulong
490v = ulong 0
491if eq n 0
492ret 0
493end
494for i ulong n
495c = get tb add o i
496if or lt c 48 gt c 57
497ret 0
498end
499v = add mul v 10 ulong sub c 48
500if gt v 999
501ret 0
502end
503end
504ret v
505end
506fn nam o ulong n ulong to bool
507if eq n 0
508ret false
509end
510if eq get tb o 46
511ret false
512end
513for i ulong n
514c = get tb add o i
515ok = and ge c 65 le c 90
516ok = or ok and ge c 97 le c 122
517ok = or ok and ge c 48 le c 57
518ok = or ok or eq c 46 or eq c 45 eq c 95
519if not ok
520ret false
521end
522end
523ret true
524end
525fn rel o ulong n ulong to bool
526if eq n 0
527ret false
528end
529if eq get tb o 47
530ret false
531end
532for i ulong n
533c = get tb add o i
534if or lt c 33 gt c 126
535ret false
536end
537if eq c 46
538if lt add i 1 n
539if eq get tb add add o i 1 46
540ret false
541end
542end
543end
544end
545ret true
546end
547fn hexchar c uchar to bool
548ret or and ge c 48 le c 57 and ge c 97 le c 102
549end
550fn needs o ulong n ulong to ulong
551if eqt o n nany
552ret 1
553end
554if eqt o n na64
555ret 2
556end
557if eqt o n nmac
558ret 3
559end
560if ne n 13
561ret 0
562end
563if not eqt o 4 npci
564ret 0
565end
566if ne get tb add o 8 58
567ret 0
568end
569for i ulong 9
570if ne i 4
571if not hexchar get tb add add o 4 i
572ret 0
573end
574end
575end
576ret 4
577end
578fn parserow s ulong e ulong ln ulong to void
579fs = new ulong 6 0
580fnn = new ulong 6 0
581if true
582f = ulong 0
583q = s
584x = s
585while le q e bound 1048577
586endf = eq q e
587if not endf
588endf = eq get tb q 9
589end
590if endf
591if ge f 6
592badline mcols ln
593end
594put fs f x
595put fnn f sub q x
596f = add f 1
597x = add q 1
598end
599q = add q 1
600end
601if ne f 6
602badline mcols ln
603end
604end
605v = num get fs 0 get fnn 0
606if or eq v 0 gt v get nlay 0
607badline mlayer ln
608end
609if lt v get prevl 0
610badline morder ln
611end
612put prevl 0 v
613if not nam get fs 1 get fnn 1
614badline mrepo ln
615end
616k = needs get fs 2 get fnn 2
617if eq k 0
618badline mneeds ln
619end
620if true
621cn = get fnn 3
622if or eq cn 0 gt cn 1024
623badline mcmd ln
624end
625for j ulong cn
626c = get tb add get fs 3 j
627if or lt c 32 gt c 126
628badline mcmd ln
629end
630end
631if not rel get fs 4 get fnn 4
632badline mpath ln
633end
634if not rel get fs 5 get fnn 5
635badline mpath ln
636end
637if eqt get fs 4 get fnn 4 tdash
638if not eqt get fs 5 get fnn 5 tdash
639badline mcmp ln
640end
641end
642end
643r = get nrow 0
644if ge r 4096
645badline mmany ln
646end
647b = mul r 12
648put row b v
649put row add b 1 get fs 1
650put row add b 2 get fnn 1
651put row add b 3 k
652put row add b 4 get fs 3
653put row add b 5 get fnn 3
654put row add b 6 get fs 4
655put row add b 7 get fnn 4
656put row add b 8 get fs 5
657put row add b 9 get fnn 5
658put row add b 10 ln
659put row add b 11 get fs 2
660put nrow 0 add r 1
661end
662fn coverage to void
663for i ulong get nlay 0
664found = false
665for j ulong get nrow 0
666ly = get row mul j 12
667if eq ly add i 1
668found = true
669end
670end
671if not found
672say tpre
673say mnorow1
674sayn add i 1
675say mnorow2
676say tnl
677exit 1
678end
679end
680end
681fn parse to void
682n = get tbn 0
683p = ulong 0
684ln = ulong 0
685while lt p n bound 1048577
686s = p
687while lt p n bound 1048577
688if eq get tb p 10
689break
690end
691p = add p 1
692end
693e = p
694p = add p 1
695ln = add ln 1
696if eq ln 1
697if lt sub e s 8
698die mfirst
699end
700if not eqt s 7 tlay
701die mfirst
702end
703v = num add s 7 sub sub e s 7
704if lt v 2
705die mfirst
706end
707put nlay 0 v
708else
709if eq ln 2
710if not eqt s sub e s thdr
711die mhdr
712end
713else
714parserow s e ln
715end
716end
717end
718if lt ln 2
719die mhdr
720end
721coverage
722end
723fn loadtab to void
724fd = fopen pb 0 get pbn 0 524288 0
725if lt fd 0
726die topen
727end
728p = ulong 0
729while lt p 1048576 bound 1048577
730k = read int fd tb p sub 1048576 p
731if lt k 0
732die topen
733end
734if eq k 0
735break
736end
737p = add p ulong k
738end
739c = fclose int fd
740if ge p 1048576
741die tbig
742end
743put tbn 0 p
744parse
745end
746fn loadrepos to void
747fd = fopen pb 0 get pbn 0 524288 0
748if lt fd 0
749die topen
750end
751p = ulong 0
752while lt p 4194304 bound 4194305
753k = read int fd rp p sub 4194304 p
754if lt k 0
755die topen
756end
757if eq k 0
758break
759end
760p = add p ulong k
761end
762c = fclose int fd
763if ge p 4194304
764die tbig
765end
766put rpn 0 p
767end
768note locate the row's name and copy its manifest commit; false if absent or malformed.
769fn commit b ulong to bool
770put cmn 0 0
771p = ulong 0
772while lt p get rpn 0 bound 4194305
773s = p
774while lt p get rpn 0 bound 4194305
775if or eq get rp p 9 eq get rp p 10
776break
777end
778p = add p 1
779end
780same = eq sub p s get row add b 2
781if same
782for i ulong sub p s
783co = add get row add b 1 i
784cv = get tb co
785if ne get rp add s i cv
786same = false
787end
788end
789end
790if same
791p = add p 1
792while lt p get rpn 0 bound 4194305
793if eq get rp p 9
794break
795end
796p = add p 1
797end
798p = add p 1
799s = p
800while lt p get rpn 0 bound 4194305
801if or eq get rp p 9 eq get rp p 10
802break
803end
804p = add p 1
805end
806if ne sub p s 40
807ret false
808end
809for i ulong 40
810v = get rp add s i
811if not hexchar v
812ret false
813end
814put cm i v
815end
816put cmn 0 40
817ret true
818end
819while lt p get rpn 0 bound 4194305
820if eq get rp p 10
821break
822end
823p = add p 1
824end
825p = add p 1
826end
827ret false
828end
829fn aa t view uchar * to void
830n = len t
831p = get avp 0
832if or gt add add p 2 n 4096 ge get avc 0 64
833put ovf 0 1
834ret
835end
836put av p uchar and n 255
837put av add p 1 uchar shr n 8
838for i ulong n
839put av add add p 2 i get t i
840end
841put avp 0 add add p 2 n
842put avc 0 add get avc 0 1
843end
844fn ap to void
845n = get pbn 0
846p = get avp 0
847if or gt add add p 2 n 4096 ge get avc 0 64
848put ovf 0 1
849ret
850end
851put av p uchar and n 255
852put av add p 1 uchar shr n 8
853for i ulong n
854put av add add p 2 i get pb i
855end
856put avp 0 add add p 2 n
857put avc 0 add get avc 0 1
858end
859fn envkeep n ulong t view uchar * to bool
860if lt n len t
861ret false
862end
863for i ulong len t
864if ne get gb i get t i
865ret false
866end
867end
868ret true
869end
870fn envinit to void
871put evn 0 0
872put evc 0 0
873nenv = ulong envcnt
874for i ulong nenv
875k = envread i gb 0 4096
876if gt k 0
877n = ulong k
878keep = envkeep n e1
879keep = or keep envkeep n e2
880keep = or keep envkeep n e3
881keep = or keep envkeep n e4
882keep = or keep envkeep n e5
883keep = or keep envkeep n e6
884keep = or keep envkeep n e7
885keep = or keep envkeep n e8
886keep = or keep envkeep n e9
887if keep
888p = get evn 0
889if le add add p 2 n 4096
890put ev p uchar and n 255
891put ev add p 1 uchar shr n 8
892for j ulong n
893put ev add add p 2 j get gb j
894end
895put evn 0 add add p 2 n
896put evc 0 add get evc 0 1
897end
898end
899end
900end
901end
902fn start to void
903put avp 0 2
904put avc 0 0
905put ovf 0 0
906aa xenv
907p = ulong 0
908for i ulong get evc 0
909n = add ulong get ev p shl ulong get ev add p 1 8
910p = add p 2
911put pbn 0 n
912for j ulong n
913put pb j get ev add p j
914end
915ap
916p = add p n
917end
918end
919fn wrapadd to void
920p = ulong 0
921while lt p get wrn 0 bound 4097
922if eq get wr p 32
923p = add p 1
924else
925s = p
926while lt p get wrn 0 bound 4097
927if eq get wr p 32
928break
929end
930p = add p 1
931end
932put pbn 0 sub p s
933for i ulong sub p s
934put pb i get wr add s i
935end
936ap
937end
938end
939end
940fn spawn fd int inclone bool to ulong
941if eq get ovf 0 1
942c = fclose fd
943ret 512
944end
945put av 0 uchar get avc 0
946put av 1 uchar 0
947pid = fork
948if lt pid 0
949c = fclose fd
950ret 512
951end
952if eq pid 0
953if inclone
954v = chdir cd 0 get cdn 0
955if ne v 0
956exit 97
957end
958end
959v = dupfd fd 1 0
960v = dupfd fd 2 0
961v = fopen tnull 0 len tnull 0 0
962if ge v 0
963z = dupfd int v 0 0
964end
965v = execprog av 0 get avp 0
966exit 98
967end
968c = fclose fd
969v = waitpid int pid st 0 4 0
970if ne v pid
971ret 512
972end
973if ne get st 0 0
974ret add 256 ulong get st 0
975end
976ret ulong get st 1
977end
978fn workpath to void
979put pbn 0 get wkn 0
980for i ulong get wkn 0
981put pb i get wk i
982end
983end
984fn logpath b ulong to void
985workpath
986pa tlogp
987pn get row b
988pa tdash
989pn add div b 12 1
990pa tlog
991end
992fn clonepath b ulong to void
993put pbn 0 get dirn 0
994for i ulong get dirn 0
995put pb i get dir i
996end
997pa tslash
998pt get row add b 1 get row add b 2
999put cdn 0 get pbn 0
1000for i ulong get pbn 0
1001put cd i get pb i
1002end
1003end
1004fn cpbase to void
1005put pbn 0 get cdn 0
1006for i ulong get cdn 0
1007put pb i get cd i
1008end
1009pa tslash
1010end
1011fn smallread to long
1012fd = fopen pb 0 get pbn 0 0 0
1013if lt fd 0
1014ret -1
1015end
1016n = read int fd gb 0 4096
1017c = fclose int fd
1018ret n
1019end
1020fn outputfd to int
1021ret int fopen pb 0 get pbn 0 577 420
1022end
1023fn headok to bool
1024start
1025aa xgit
1026aa xrp
1027aa xhead
1028workpath
1029pa tgit
1030fd = outputfd
1031if lt fd 0
1032ret false
1033end
1034rc = spawn fd true
1035if ne rc 0
1036ret false
1037end
1038workpath
1039pa tgit
1040n = smallread
1041if lt n 40
1042ret false
1043end
1044for i ulong 40
1045if ne get gb i get cm i
1046ret false
1047end
1048end
1049ret true
1050end
1051fn hexstate s view uint 8 to void
1052for i ulong 8
1053for j ulong 4
1054v = get s i
1055v = shr v uint sub 24 mul j 8
1056by = uchar and v 255
1057ix = add mul i 8 mul j 2
1058put hx ix get hexd shr by 4
1059put hx add ix 1 get hexd and by 15
1060end
1061end
1062end
1063fn hashfile to bool
1064s = new uint 8 0
1065w = new uint 64 0
1066kt = new uint 64 0
1067pad = new uchar 128 0
1068buf = new uchar 64 0
1069fd = fopen pb 0 get pbn 0 0 0
1070if lt fd 0
1071ret false
1072end
1073kinit kt
1074iv s
1075total = ulong 0
1076pending = ulong 0
1077while true bound 9223372036854775807
1078n = read int fd buf pending sub 64 pending
1079if lt n 0
1080c = fclose int fd
1081ret false
1082end
1083if eq n 0
1084break
1085end
1086total = add total ulong n
1087pending = add pending ulong n
1088if eq pending 64
1089compress s w kt view buf 0
1090pending = 0
1091end
1092end
1093c = fclose int fd
1094for i ulong pending
1095put pad i get buf i
1096end
1097finish s w kt pad pending total
1098hexstate s
1099ret true
1100end
1101fn aarch to void
1102pc tself
1103n = smallread
1104if gt n 20
1105if and eq get gb 18 183 eq get gb 19 0
1106put a64 0 1
1107end
1108end
1109end
1110fn chipentry b ulong p ulong rn ulong to bool
1111if true
1112q = add p 19
1113e = q
1114while lt e add p rn bound 32769
1115if eq get db e 0
1116break
1117end
1118e = add e 1
1119end
1120pc tpcid
1121pa tslash
1122for i ulong sub e q
1123z = get pbn 0
1124put pb z get db add q i
1125put pbn 0 add z 1
1126end
1127end
1128base = get pbn 0
1129pa tvend
1130v = smallread
1131okv = ge v 6
1132if okv
1133for i ulong 4
1134co = add get row add b 11 add 4 i
1135by = get tb co
1136if ne get gb add i 2 by
1137okv = false
1138end
1139end
1140end
1141put pbn 0 base
1142pa tdev
1143v = smallread
1144okd = ge v 6
1145if okd
1146for i ulong 4
1147co = add get row add b 11 add 9 i
1148by = get tb co
1149if ne get gb add i 2 by
1150okd = false
1151end
1152end
1153end
1154ret and okv okd
1155end
1156fn chip b ulong to bool
1157fd = fopen tpcid 0 len tpcid 65536 0
1158if lt fd 0
1159ret false
1160end
1161found = false
1162while not found bound 9223372036854775807
1163n = getdnts int fd db 0 32768
1164if le n 0
1165break
1166end
1167p = ulong 0
1168while lt p ulong n bound 32769
1169rn = ulong get db add p 16
1170rn = add rn shl ulong get db add p 17 8
1171if lt rn 20
1172break
1173end
1174if chipentry b p rn
1175found = true
1176break
1177end
1178p = add p rn
1179end
1180end
1181c = fclose int fd
1182ret found
1183end
1184fn fields b ulong to void
1185say bl
1186sayn get row b
1187say br
1188w = write 1 tb get row add b 1 get row add b 2
1189say bc
1190if eq get cmn 0 40
1191w = write 1 cm 0 40
1192else
1193say tdash
1194end
1195say brc
1196if eq get hrc 0 1
1197sayn get crc 0
1198else
1199say tdash
1200end
1201say bsha
1202if eq get hsa 0 1
1203w = write 1 hx 0 64
1204else
1205say tdash
1206end
1207say blog
1208logpath b
1209w = write 1 pb 0 get pbn 0
1210say bcmd
1211w = write 1 tb get row add b 4 get row add b 5
1212say tnl
1213end
1214fn result b ulong status ulong why view uchar * to void
1215if eq status 0
1216say vpass
1217else
1218if eq status 1
1219say vfail
1220else
1221say vnrun
1222end
1223say why
1224if le get row b 2
1225put l12 0 1
1226end
1227end
1228put tally status add get tally status 1
1229fields b
1230end
1231fn executerow b ulong to void
1232put hsa 0 0
1233put hrc 0 0
1234put cmn 0 0
1235has = commit b
1236k = get row add b 3
1237if eq k 3
1238result b 2 rmac
1239ret
1240end
1241if not has
1242result b 2 rlist
1243ret
1244end
1245if eq k 2
1246if eq get a64 0 0
1247result b 2 ra64
1248ret
1249end
1250end
1251if eq k 4
1252if not chip b
1253result b 2 rchip
1254ret
1255end
1256if eq get hw 0 0
1257result b 2 rhw
1258ret
1259end
1260end
1261if eqt get row add b 4 get row add b 5 tdash
1262result b 2 rcmd
1263ret
1264end
1265clonepath b
1266if not headok
1267result b 1 rhead
1268ret
1269end
1270start
1271wrapadd
1272aa xsh
1273aa xc
1274put pbn 0 0
1275pt get row add b 4 get row add b 5
1276ap
1277logpath b
1278fd = outputfd
1279if lt fd 0
1280result b 1 rio
1281ret
1282end
1283rc = spawn fd true
1284put crc 0 rc
1285put hrc 0 1
1286if not eqt get row add b 6 get row add b 7 tdash
1287cpbase
1288pt get row add b 6 get row add b 7
1289if hashfile
1290put hsa 0 1
1291end
1292end
1293if ne rc 0
1294result b 1 rexit
1295ret
1296end
1297if not eqt get row add b 6 get row add b 7 tdash
1298if eq get hsa 0 0
1299result b 1 rart
1300ret
1301end
1302end
1303if not eqt get row add b 8 get row add b 9 tdash
1304for i ulong 64
1305put hc i get hx i
1306end
1307start
1308aa xgit
1309aa xcf
1310aa xblob
1311put pbn 0 40
1312for i ulong 40
1313put pb i get cm i
1314end
1315pa tcolon
1316pt get row add b 8 get row add b 9
1317ap
1318workpath
1319pa tcom
1320fd = outputfd
1321if lt fd 0
1322result b 1 rio
1323ret
1324end
1325rc = spawn fd true
1326if ne rc 0
1327result b 1 rcom
1328ret
1329end
1330workpath
1331pa tcom
1332if not hashfile
1333result b 1 rcom
1334ret
1335end
1336same = true
1337for i ulong 64
1338if ne get hc i get hx i
1339same = false
1340end
1341put hx i get hc i
1342end
1343if not same
1344result b 1 rdiff
1345ret
1346end
1347end
1348result b 0 tempty
1349end
1350fn summary to void
1351say sum1
1352sayn get tally 0
1353say sum2
1354sayn get tally 1
1355say sum3
1356sayn get tally 2
1357say sum4
1358sayn get nrow 0
1359say sum5
1360sayn get nlay 0
1361say tnl
1362end
1363fn main to int
1364if lt argcnt 3
1365say tusage
1366ret 2
1367end
1368patharg 1
1369checking = false
1370if eq get pbn 0 5
1371checking = true
1372for i ulong 5
1373if ne get pb i get tcheck i
1374checking = false
1375end
1376end
1377end
1378if not checking
1379if lt argcnt 6
1380say tusage
1381ret 2
1382end
1383end
1384patharg 2
1385loadtab
1386if checking
1387say tok1
1388sayn get nlay 0
1389say tok2
1390sayn get nrow 0
1391say tok3
1392ret 0
1393end
1394patharg 3
1395loadrepos
1396n = argread 4 dir 0 4096
1397if le n 0
1398die targ
1399end
1400put dirn 0 ulong n
1401n = argread 5 wk 0 4096
1402if le n 0
1403die targ
1404end
1405put wkn 0 ulong n
1406extra = sub ulong argcnt 6
1407for i ulong extra
1408patharg add i 6
1409if eq get pbn 0 6
1410same = true
1411for j ulong 6
1412if ne get pb j get thw j
1413same = false
1414end
1415end
1416if same
1417put hw 0 1
1418end
1419end
1420if ge get pbn 0 5
1421same = true
1422for j ulong 5
1423if ne get pb j get twrap j
1424same = false
1425end
1426end
1427if same
1428put wrn 0 sub get pbn 0 5
1429for j ulong get wrn 0
1430put wr j get pb add j 5
1431end
1432end
1433end
1434end
1435envinit
1436aarch
1437for i ulong get nrow 0
1438executerow mul i 12
1439end
1440summary
1441if or gt get tally 1 0 eq get l12 0 1
1442ret 1
1443end
1444ret 0
1445end