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 / gpu / cuda-crack / check / mutate.om

499 lines · 9570 bytes Raw Blame History
1om2
2note gpu/cuda-crack/check/mutate.om: the four planted bugs of check/check.sh, in pure seeded Omega.
3note
4note check.sh plants four bugs and requires each to be caught. It makes them with awk, od and
5note printf. This program makes the same four inputs, byte for byte; build.sh compares its output
6note with those one-liners on every build, on both targets.
7note Build with the seed-born compiler:
8note omc < check/mutate.om > mutate && chmod +x mutate
9note { printf 'target x86linux\n'; cat check/mutate.om; } | omc-x86 > mutate (native x86-64)
10note Run:
11note mutate MODE < IN > OUT
12note MODE 1 IN is isa/sm121-forms.txt. Let K be fields 5..10 of the F record whose field 3 is
13note 255 (the NOP). In every F record whose fields 5..10 are K, field 5 (fix0) has its
14note bit 0 flipped. If there is not exactly one such NOP record, nothing changes (as the
15note awk key then never matches).
16note MODE 2 IN is a kernel's machine code; bit 0 of its first byte is flipped.
17note MODE 3 IN is a table; the first D record whose field 8 (width) is over 1 gets width - 1.
18note MODE 4 IN is a table; in the first D record whose width is over 0, the mask word holding
19note bit "start" (field 7) loses that bit: field 4 + start / 32 becomes itself minus
20note 2 ^ (start mod 32), printed with a sign when it goes below zero.
21note Text modes read lines split at "\n" and fields split at runs of spaces and tabs, as awk's
22note default field splitting does. A changed record is written as its fields joined by single
23note spaces; every other line is written as it was. Every record ends with "\n" on output, also
24note a last line that had none. A field these rules must read as a number is decimal digits
25note (1 to 19 of them): a field 3, 7 or 8 of an F or D record that is not, or a changed field
26note that is not, is refused.
27note Refusals: one stderr line "mutate: ...", exit 1, nothing on stdout. Usage errors exit 2.
28note Input and output are each at most 4 MiB. No clock, no environment.
29
30note standard input and output
31global inp uchar 4194304
32global inn ulong 1
33global ob uchar 4194304
34global obn ulong 1
35note the fields of the current line: offset in inp and length; nf is their count, 65 when over 64
36global fo ulong 64
37global fl ulong 64
38global nf ulong 1
39note mode 1: fields 5..10 of the NOP record (offset, length pairs) and how many NOP records there are
40global ky ulong 12
41global nky ulong 1
42note the mode, and for modes 3 and 4 whether a D record was changed already
43global md ulong 1
44global dn ulong 1
45
46text tpre "mutate: "
47text tnl "\n"
48text tusage "usage: mutate 1|2|3|4 < IN > OUT (see the notes in check/mutate.om)\n"
49text tbig "input over the 4 MiB buffer"
50text tread "cannot read standard input"
51text toverf "output is over the 4 MiB buffer"
52text twrite "cannot write standard output"
53text tempty "mode 2 needs at least one byte of input"
54text tnum "a field that must be a decimal number is not, on the line: "
55text tmany "a record to change has more than 64 fields: "
56text tnof "the field to change does not exist, on the line: "
57text tF "F"
58text tD "D"
59
60note die: report m and up to 200 bytes of inp from o on stderr, then exit 1
61fn die m view uchar * o ulong n ulong to void
62k = n
63if gt k 200
64k = 200
65end
66w = write 2 tpre 0 len tpre
67w = write 2 m 0 len m
68if gt k 0
69w = write 2 inp o k
70end
71w = write 2 tnl 0 len tnl
72exit 1
73end
74
75note emb: append one byte to ob
76fn emb c uchar to void
77p = get obn 0
78if ge p 4194304
79die toverf 0 0
80end
81put ob p c
82put obn 0 add p 1
83end
84
85note emi: append n bytes of inp from o to ob
86fn emi o ulong n ulong to void
87p = get obn 0
88if gt n sub 4194304 p
89die toverf 0 0
90end
91for i ulong n bound 4194304
92put ob add p i get inp add o i
93end
94put obn 0 add p n
95end
96
97note emu: append v in decimal to ob
98fn emu v ulong to void
99d = new uchar 20 0
100x = v
101put d 0 uchar add rem x 10 48
102k = ulong 1
103x = div x 10
104while gt x 0 bound 20
105put d k uchar add rem x 10 48
106k = add k 1
107x = div x 10
108end
109while gt k 0 bound 20
110k = sub k 1
111emb get d k
112end
113end
114
115note wout: write ob to standard output
116fn wout to void
117n = get obn 0
118p = ulong 0
119while lt p n bound 4194305
120k = write 1 ob p sub n p
121if le k 0
122die twrite 0 0
123end
124p = add p ulong k
125end
126end
127
128note loadin: read all of standard input into inp
129fn loadin to void
130t = new uchar 1 0
131p = ulong 0
132going = true
133note every pass but the last reads at least one byte of a 4 MiB buffer
134while going bound 4194306
135k = read 0 inp p sub 4194304 p
136if lt k 0
137die tread 0 0
138end
139if eq k 0
140going = false
141else
142p = add p ulong k
143if eq p 4194304
144going = false
145end
146end
147end
148if eq p 4194304
149k = read 0 t 0 1
150if ne k 0
151die tbig 0 0
152end
153end
154put inn 0 p
155end
156
157note eol: the end of the line that starts at p (its newline, or the end of the input)
158fn eol p ulong to ulong
159e = get inn 0
160q = p
161while lt q e bound 4194305
162if eq get inp q 10
163ret q
164end
165q = add q 1
166end
167ret e
168end
169
170note isws: is c a field separator (space or tab)
171fn isws c uchar to bool
172ret or eq c 32 eq c 9
173end
174
175note split: the fields of the line inp[s, e) into fo, fl and nf
176fn split s ulong e ulong to void
177n = ulong 0
178p = s
179while lt p e bound 4194305
180c = get inp p
181if isws c
182p = add p 1
183else
184q = p
185going = true
186while going bound 4194305
187q = add q 1
188if ge q e
189going = false
190else
191if isws get inp q
192going = false
193end
194end
195end
196if lt n 64
197put fo n p
198put fl n sub q p
199end
200n = add n 1
201p = q
202end
203end
204if gt n 64
205n = 65
206end
207put nf 0 n
208end
209
210note fhas: does field i (1-based) exist on the current line
211fn fhas i ulong to bool
212n = get nf 0
213if eq i 0
214ret false
215end
216if gt i 64
217ret false
218end
219ret le i n
220end
221
222note fis: is field i exactly the text t
223fn fis i ulong t view uchar * to bool
224if not fhas i
225ret false
226end
227o = get fo sub i 1
228n = get fl sub i 1
229if ne n len t
230ret false
231end
232for j ulong n bound 64
233if ne get inp add o j get t j
234ret false
235end
236end
237ret true
238end
239
240note fdig: is field i 1 to 19 decimal digits
241fn fdig i ulong to bool
242if not fhas i
243ret false
244end
245o = get fo sub i 1
246n = get fl sub i 1
247if eq n 0
248ret false
249end
250if gt n 19
251ret false
252end
253for j ulong n bound 19
254c = get inp add o j
255if lt c 48
256ret false
257end
258if gt c 57
259ret false
260end
261end
262ret true
263end
264
265note fnum: field i as a number; s, e is the line, named in the refusal
266fn fnum i ulong s ulong e ulong to ulong
267if not fdig i
268die tnum s sub e s
269end
270o = get fo sub i 1
271n = get fl sub i 1
272v = ulong 0
273for j ulong n bound 19
274v = add mul v 10 ulong sub get inp add o j 48
275end
276ret v
277end
278
279note fsame: is field i the same bytes as the saved key slice k (0..5) of ky
280fn fsame i ulong k ulong to bool
281ko = get ky mul k 2
282kn = get ky add mul k 2 1
283if not fhas i
284ret eq kn 0
285end
286o = get fo sub i 1
287n = get fl sub i 1
288if ne n kn
289ret false
290end
291for j ulong n bound 4194304
292if ne get inp add o j get inp add ko j
293ret false
294end
295end
296ret true
297end
298
299note emrec: write the current record's fields joined by single spaces, field ci replaced by the
300note value neg/mag (a minus sign first when neg), then "\n"; s, e is the line, for refusals
301fn emrec ci ulong ng bool mg ulong s ulong e ulong to void
302n = get nf 0
303if gt n 64
304die tmany s sub e s
305end
306for i ulong n bound 64
307if gt i 0
308emb uchar 32
309end
310if eq add i 1 ci
311if ng
312emb uchar 45
313end
314emu mg
315else
316emi get fo i get fl i
317end
318end
319emb uchar 10
320end
321
322note keep: write the line s, e as it was, then "\n"
323fn keep s ulong e ulong to void
324emi s sub e s
325emb uchar 10
326end
327
328note nopscan: mode 1, first pass: count the F records whose field 3 is 255 and keep the first one's
329note fields 5..10
330fn nopscan to void
331e = get inn 0
332p = ulong 0
333while lt p e bound 4194305
334q = eol p
335split p q
336if fis 1 tF
337if fdig 3
338if eq fnum 3 p q 255
339k = get nky 0
340if eq k 0
341savekey
342end
343put nky 0 add k 1
344end
345end
346end
347p = add q 1
348end
349end
350
351note savekey: keep fields 5..10 of the current line in ky (a missing field as length 0)
352fn savekey to void
353for k ulong 6
354i = add k 5
355if fhas i
356put ky mul k 2 get fo sub i 1
357put ky add mul k 2 1 get fl sub i 1
358else
359put ky mul k 2 0
360put ky add mul k 2 1 0
361end
362end
363end
364
365note iskey: are fields 5..10 of the current line the NOP's
366fn iskey to bool
367for k ulong 6
368if not fsame add k 5 k
369ret false
370end
371end
372ret true
373end
374
375note line1: mode 1, one line
376fn line1 s ulong e ulong to void
377if fis 1 tF
378if eq get nky 0 1
379if iskey
380v = fnum 5 s e
381emrec 5 false xor v 1 s e
382ret
383end
384end
385end
386keep s e
387end
388
389note line3: mode 3, one line
390fn line3 s ulong e ulong to void
391if eq get dn 0 0
392if fis 1 tD
393if fhas 8
394w = fnum 8 s e
395if gt w 1
396put dn 0 1
397emrec 8 false sub w 1 s e
398ret
399end
400end
401end
402end
403keep s e
404end
405
406note line4: mode 4, one line
407fn line4 s ulong e ulong to void
408if eq get dn 0 0
409if fis 1 tD
410if fhas 8
411w = fnum 8 s e
412if gt w 0
413put dn 0 1
414st = fnum 7 s e
415ci = add 4 div st 32
416if not fhas ci
417die tnof s sub e s
418end
419v = fnum ci s e
420b = shl ulong 1 rem st 32
421if ge v b
422emrec ci false sub v b s e
423else
424emrec ci true sub b v s e
425end
426ret
427end
428end
429end
430end
431keep s e
432end
433
434note lines: run the mode's line rule over every line of the input
435fn lines to void
436e = get inn 0
437m = get md 0
438p = ulong 0
439while lt p e bound 4194305
440q = eol p
441split p q
442if eq m 1
443line1 p q
444end
445if eq m 3
446line3 p q
447end
448if eq m 4
449line4 p q
450end
451p = add q 1
452end
453end
454
455note getmode: the mode from the one argument, 0 when it is not 1, 2, 3 or 4
456fn getmode to ulong
457a = new uchar 8 0
458n = argread 1 a 0 8
459if ne n 1
460ret 0
461end
462c = get a 0
463if lt c 49
464ret 0
465end
466if gt c 52
467ret 0
468end
469ret ulong sub c 48
470end
471
472fn main to int
473if ne argcnt 2
474w = write 2 tusage 0 len tusage
475ret 2
476end
477m = getmode
478if eq m 0
479w = write 2 tusage 0 len tusage
480ret 2
481end
482put md 0 m
483loadin
484if eq m 2
485n = get inn 0
486if eq n 0
487die tempty 0 0
488end
489emi 0 n
490put ob 0 xor get ob 0 1
491else
492if eq m 1
493nopscan
494end
495lines
496end
497wout
498ret 0
499end