interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tools / pincheck.om

379 lines · 6385 bytes Raw Blame History
1note pincheck: the compiler pin is written in one place, PIN. build.sh compiles this unit with the
2note pinned omc and runs it in the repository root as `git ls-files | build/pincheck`; a finding
3note fails the build (pattern: omega-mcp gates/pincheck.om).
4note It reads PIN (lines `omega-mv <40 hex>` and `omc-sha256 <64 hex>`) and a list of tracked paths
5note on stdin, one per line, and reports every word in those files that restates the pin. A word is
6note a run of 0-9 A-Z a-z _ between other characters; it restates the pin when it has 7 to 64
7note characters and, with A-F read as a-f, equals that many first characters of the PIN revision or
8note of the omc sha256. The rule compares against the values in PIN and asks nothing else of the word:
9note a short revision made only of digits is found like any other, and a number that is not a prefix
10note of the pin (a date, a count) is not. 7 is the shortest prefix git prints; 8, 12, 16 and the full
11note 40 or 64 are the lengths written in practice. Such a copy goes stale when PIN moves; point at PIN
12note (or derive the value from it) instead. Other hashes are not judged here.
13note Not read: PIN itself, and records of past runs (evidence/), which name the compiler they ran with.
14note Files and lines of any size are streamed; no limit of its own.
15note Exit 0 no findings, 1 findings, 2 PIN malformed, a listed file unreadable or a write error.
16
17global pin uchar 4096
18global ib uchar 65536
19global fb uchar 65536
20global tok uchar 64
21global pth uchar 4096
22global ob uchar 32
23global rev uchar 40
24global sha uchar 64
25global st ulong 16
26
27note st cells: word length, hex only (after reading A-F as a-f), line, findings, path length, files read
28const q_len ulong 0
29const q_hex ulong 1
30const q_line ulong 2
31const q_bad ulong 3
32const q_pl ulong 4
33const q_nf ulong 5
34
35text kpinf "PIN"
36text kmvw "omega-mv "
37text kshaw "omc-sha256 "
38text m_pin "pincheck: PIN must hold `omega-mv <40 hex>` and `omc-sha256 <64 hex>`, one per line\n"
39text m_open "pincheck: cannot read "
40text m_long "pincheck: path longer than 4095 bytes\n"
41text m_rest ": restates PIN: "
42text m_hint "pincheck: the pin is written only in PIN; point at PIN (or derive the value from it) instead\n"
43text m_sum "pincheck: files "
44text m_fnd " findings "
45text nl "\n"
46text colon ":"
47text x_ev "evidence/"
48
49fn wt t view uchar * to void
50n = len t
51p = ulong 0
52for it ulong 4611686018427387904
53if ge p n
54ret
55end
56k = write 1 t p sub n p
57if le k 0
58exit 2
59end
60p = add p ulong k
61end
62end
63fn wbuf k ulong to void
64n = get st k
65p = ulong 0
66for it ulong 4611686018427387904
67if ge p n
68ret
69end
70x = long 0
71if eq k q_pl
72x = write 1 pth p sub n p
73else
74x = write 1 tok p sub n p
75end
76if le x 0
77exit 2
78end
79p = add p ulong x
80end
81end
82fn putn v ulong to void
83i = ulong 32
84n = v
85for it ulong 32
86i = sub i 1
87put ob i uchar add rem n 10 48
88n = div n 10
89if eq n 0
90break
91end
92end
93p = i
94for it ulong 4611686018427387904
95if ge p 32
96ret
97end
98k = write 1 ob p sub 32 p
99if le k 0
100exit 2
101end
102p = add p ulong k
103end
104end
105fn ishex c uchar to bool
106if and ge c 48 le c 57
107ret true
108end
109ret and ge c 97 le c 102
110end
111fn isword c uchar to bool
112if ishex c
113ret true
114end
115if and ge c 65 le c 90
116ret true
117end
118if and ge c 97 le c 122
119ret true
120end
121ret eq c 95
122end
123
124note ---- PIN ----
125note the hex value after word w at the start of a line of pin[0 .. n) into rev (sel 0) or sha (sel 1)
126fn pinval n ulong w view uchar * want ulong sel ulong to bool
127p = ulong 0
128for it ulong 4611686018427387904
129if ge p n
130ret false
131end
132ok = true
133for i ulong len w
134if ge add p i n
135ok = false
136break
137end
138if ne get pin add p i get w i
139ok = false
140break
141end
142end
143if ok
144a = add p len w
145if gt add a want n
146ret false
147end
148for i ulong want
149c = get pin add a i
150if not ishex c
151ret false
152end
153if eq sel 0
154put rev i c
155else
156put sha i c
157end
158end
159e = add a want
160if lt e n
161c = get pin e
162if and ne c 10 ne c 13
163ret false
164end
165end
166ret true
167end
168for it2 ulong 4611686018427387904
169if ge p n
170ret false
171end
172if eq get pin p 10
173p = add p 1
174break
175end
176p = add p 1
177end
178end
179ret false
180end
181fn readpin to bool
182fd = fopen kpinf 0 len kpinf 524288 0
183if lt fd 0
184ret false
185end
186n = read int fd pin 0 len pin
187x = fclose int fd
188if le n 0
189ret false
190end
191if ge ulong n len pin
192ret false
193end
194if not pinval ulong n kmvw 40 0
195ret false
196end
197ret pinval ulong n kshaw 64 1
198end
199
200note ---- words ----
201note the word in tok (its first min(len, 64) bytes, A-F stored as a-f) is a prefix of rev or sha
202fn restates to bool
203n = get st q_len
204if lt n 7
205ret false
206end
207if gt n 64
208ret false
209end
210if le n 40
211r = true
212for i ulong n
213if ne get tok i get rev i
214r = false
215break
216end
217end
218if r
219ret true
220end
221end
222for i ulong n
223if ne get tok i get sha i
224ret false
225end
226end
227ret true
228end
229fn endword to void
230n = get st q_len
231if eq n 0
232ret
233end
234if eq get st q_hex 1
235if restates
236wbuf q_pl
237wt colon
238putn get st q_line
239wt m_rest
240wbuf q_len
241wt nl
242put st q_bad add get st q_bad 1
243end
244end
245put st q_len 0
246end
247fn onebyte c uchar to void
248if isword c
249n = get st q_len
250if eq n 0
251put st q_hex 1
252end
253d = c
254if and ge c 65 le c 70
255d = add c 32
256end
257if lt n 64
258put tok n d
259end
260put st q_len add n 1
261if not ishex d
262put st q_hex 0
263end
264ret
265end
266endword
267if eq c 10
268put st q_line add get st q_line 1
269end
270end
271
272note ---- files ----
273fn skipped to bool
274n = get st q_pl
275if and eq n len kpinf eq get pth 0 80
276if and eq get pth 1 73 eq get pth 2 78
277ret true
278end
279end
280if lt n len x_ev
281ret false
282end
283for i ulong len x_ev
284if ne get pth i get x_ev i
285ret false
286end
287end
288ret true
289end
290fn scan to int
291if skipped
292ret 0
293end
294fd = fopen pth 0 get st q_pl 524288 0
295if lt fd 0
296wt m_open
297wbuf q_pl
298wt nl
299ret 2
300end
301put st q_line 1
302put st q_len 0
303for it ulong 4611686018427387904
304r = read int fd fb 0 len fb
305if lt r 0
306x = fclose int fd
307wt m_open
308wbuf q_pl
309wt nl
310ret 2
311end
312if eq r 0
313break
314end
315for i ulong ulong r
316onebyte get fb i
317end
318end
319endword
320x = fclose int fd
321put st q_nf add get st q_nf 1
322ret 0
323end
324fn main to int
325if not readpin
326wt m_pin
327ret 2
328end
329put st q_pl 0
330rc = int 0
331for it ulong 4611686018427387904
332r = read int 0 ib 0 len ib
333if lt r 0
334ret 2
335end
336if eq r 0
337break
338end
339for i ulong ulong r
340c = get ib i
341if eq c 10
342if gt get st q_pl 0
343s = scan
344if ne s 0
345rc = s
346end
347end
348put st q_pl 0
349else
350n = get st q_pl
351if ge n sub len pth 1
352wt m_long
353ret 2
354end
355put pth n c
356put st q_pl add n 1
357end
358end
359end
360if gt get st q_pl 0
361s = scan
362if ne s 0
363rc = s
364end
365end
366wt m_sum
367putn get st q_nf
368wt m_fnd
369putn get st q_bad
370wt nl
371if ne rc 0
372ret rc
373end
374if gt get st q_bad 0
375wt m_hint
376ret 1
377end
378ret 0
379end