interplane.Get Omega
Read in your language

omtls

omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

omtls / tools / boot.om

677 lines · 12719 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note boot.om: build one omtls unit with the pinned compiler. It is the only program built by hand:
4note mkdir -p build && ./omc < tools/boot.om > build/boot && chmod 755 build/boot
5note usage: build/boot UNIT [MUTANTS-FILE MUTANT-NAME]
6note Reads units/UNIT.list (one source path per line, blank lines ignored) and writes build/UNIT.om: the line
7note "om2", then every listed file with its own "om2" lines dropped and a leading "export fn " read as "fn ".
8note Runs ./omc on it (stdin build/UNIT.om, stdout build/UNIT.new, stderr build/UNIT.err) and on success
9note renames build/UNIT.new to build/UNIT (mode 755). With a mutant, the mutant line
10note "NAME|FILE|ORIGINAL LINE|REPLACEMENT LINE" must match exactly one line of FILE in the unit; the output is
11note build/UNIT.mut instead.
12note Exit: 0 built, 1 the compiler refused (its stderr is copied to ours), 2 usage or I/O, 3 the mutant does not
13note apply exactly once.
14note Boundaries: the unit is at most 8388607 bytes, the compiler's own input boundary (OMEGESE 9.6); a larger
15note unit is refused (exit 2) with that reason. The pin of ./omc is checked by the gate, not here.
16const BT_MAX ulong 8388607
17global bt_out uchar 8388608
18global bt_in uchar 8388608
19global bt_list uchar 8388608
20global bt_mut uchar 8388608
21global bt_v ulong 32
22global bt_unit uchar 4096
23global bt_path uchar 8192
24global bt_cur uchar 8192
25global bt_mname uchar 8192
26global bt_mfile uchar 8192
27global bt_mold uchar 1048576
28global bt_mnew uchar 1048576
29global bt_pk uchar 20000
30text bt_t_units "units/"
31text bt_t_list ".list"
32text bt_t_build "build/"
33text bt_t_om ".om"
34text bt_t_new ".new"
35text bt_t_err ".err"
36text bt_t_mut ".mut"
37text bt_t_omc "./omc"
38text bt_t_om2 "om2"
39text bt_t_export "export fn "
40text bt_t_fn "fn "
41text bt_t_nl "\n"
42text bt_t_usage "usage: build/boot UNIT [MUTANTS-FILE MUTANT-NAME]\n"
43text bt_t_badname "boot: UNIT must be letters, digits, '_', '-' or '.'\n"
44text bt_t_io "boot: cannot read or write "
45text bt_t_big "boot: the unit is larger than 8388607 bytes, the compiler's input boundary\n"
46text bt_t_refused "boot: omc refused build/"
47text bt_t_exit ".om, exit "
48text bt_t_mutapply "boot: the mutant does not match exactly one line, matches="
49text bt_t_mutnone "boot: no such mutant: "
50text bt_t_mutbad "boot: malformed mutant line (want NAME|FILE|ORIGINAL|REPLACEMENT): "
51text bt_t_built "boot: built build/"
52text bt_t_bytes " bytes of source\n"
53
54note ---- output to stderr ----
55fn bt_say t view uchar * to void
56k = write 2 t 0 len t
57end
58fn bt_num n ulong to void
59d = new uchar 24 0
60i = ulong 24
61v = n
62while true bound 30
63i = sub i 1
64put d i uchar add 48 rem v 10
65v = div v 10
66if eq v 0
67break
68end
69end
70k = write 2 d i sub 24 i
71end
72fn bt_saypath to void
73k = write 2 bt_path 0 get bt_v 6
74k = write 2 bt_t_nl 0 1
75end
76
77note ---- path in bt_path, its length in bt_v[6] ----
78fn bt_pclear to void
79put bt_v 6 0
80end
81fn bt_padd t view uchar * to void
82p = get bt_v 6
83for i ulong len t
84put bt_path add p i get t i
85end
86put bt_v 6 add p len t
87end
88fn bt_paddunit to void
89p = get bt_v 6
90for i ulong get bt_v 7
91put bt_path add p i get bt_unit i
92end
93put bt_v 6 add p get bt_v 7
94end
95
96note ---- read the file named by bt_path into bt_in (length bt_v[1]); false when it cannot be read whole ----
97fn bt_load to bool
98fd = fopen bt_path 0 get bt_v 6 0 0
99if lt fd 0
100ret false
101end
102n = ulong 0
103ok = true
104while true bound 9223372036854775807
105if ge n BT_MAX
106ok = false
107break
108end
109k = read int fd bt_in n sub BT_MAX n
110if eq k -4
111continue
112end
113if lt k 0
114ok = false
115break
116end
117if eq k 0
118break
119end
120n = add n ulong k
121end
122z = fclose int fd
123put bt_v 1 n
124ret ok
125end
126
127note ---- append to bt_out (length bt_v[0]); false when the unit would pass the compiler boundary ----
128fn bt_room n ulong to bool
129if gt add get bt_v 0 n BT_MAX
130put bt_v 9 1
131ret false
132end
133ret true
134end
135fn bt_emitt t view uchar * to void
136if not bt_room len t
137ret
138end
139p = get bt_v 0
140for i ulong len t
141put bt_out add p i get t i
142end
143put bt_v 0 add p len t
144end
145fn bt_emitin s ulong e ulong to void
146if not bt_room sub e s
147ret
148end
149p = get bt_v 0
150for i ulong sub e s
151put bt_out add p i get bt_in add s i
152end
153put bt_v 0 add p sub e s
154end
155fn bt_emitnew to void
156n = get bt_v 13
157if not bt_room n
158ret
159end
160p = get bt_v 0
161for i ulong n
162put bt_out add p i get bt_mnew i
163end
164put bt_v 0 add p n
165end
166
167note ---- comparisons against globals (globals cannot be passed, so each comparison names its arrays) ----
168fn bt_inist s ulong n ulong t view uchar * to bool
169if ne n len t
170ret false
171end
172for i ulong n
173if ne get bt_in add s i get t i
174ret false
175end
176end
177ret true
178end
179fn bt_inprefix s ulong e ulong t view uchar * to bool
180if lt sub e s len t
181ret false
182end
183ret bt_inist s len t t
184end
185fn bt_inisold s ulong n ulong to bool
186if ne n get bt_v 12
187ret false
188end
189for i ulong n
190if ne get bt_in add s i get bt_mold i
191ret false
192end
193end
194ret true
195end
196fn bt_curismfile to bool
197if ne get bt_v 8 get bt_v 11
198ret false
199end
200for i ulong get bt_v 8
201if ne get bt_cur i get bt_mfile i
202ret false
203end
204end
205ret true
206end
207
208note ---- one source file (already in bt_in) into bt_out ----
209fn bt_source to void
210n = get bt_v 1
211mf = false
212if eq get bt_v 4 1
213mf = bt_curismfile
214end
215p = ulong 0
216while lt p n bound 9223372036854775807
217q = p
218while lt q n bound 9223372036854775807
219if eq get bt_in q 10
220break
221end
222q = add q 1
223end
224if bt_inist p sub q p bt_t_om2
225p = add q 1
226continue
227end
228done = false
229if mf
230if bt_inisold p sub q p
231put bt_v 5 add get bt_v 5 1
232bt_emitnew
233done = true
234end
235end
236if not done
237if bt_inprefix p q bt_t_export
238bt_emitt bt_t_fn
239bt_emitin add p len bt_t_export q
240else
241bt_emitin p q
242end
243end
244bt_emitt bt_t_nl
245p = add q 1
246end
247end
248
249note ---- the mutant: find NAME in the mutants file (in bt_in) and split its fields into globals ----
250fn bt_mutfind to bool
251n = get bt_v 1
252p = ulong 0
253while lt p n bound 9223372036854775807
254q = p
255while lt q n bound 9223372036854775807
256if eq get bt_in q 10
257break
258end
259q = add q 1
260end
261m = get bt_v 10
262hit = false
263if gt sub q p m
264if eq get bt_in add p m 124
265hit = true
266for i ulong m
267if ne get bt_in add p i get bt_mname i
268hit = false
269break
270end
271end
272end
273end
274if hit
275ret bt_mutsplit add add p m 1 q
276end
277p = add q 1
278end
279bt_say bt_t_mutnone
280k = write 2 bt_mname 0 get bt_v 10
281k = write 2 bt_t_nl 0 1
282exit 3
283end
284fn bt_mutsplit s ulong e ulong to bool
285note fields: FILE | ORIGINAL | REPLACEMENT (the replacement is the rest of the line)
286a = s
287while lt a e bound 9223372036854775807
288if eq get bt_in a 124
289break
290end
291a = add a 1
292end
293b = add a 1
294while lt b e bound 9223372036854775807
295if eq get bt_in b 124
296break
297end
298b = add b 1
299end
300if or ge a e ge b e
301ret false
302end
303if gt sub a s 8192
304ret false
305end
306if gt sub b add a 1 1048576
307ret false
308end
309if gt sub e add b 1 1048576
310ret false
311end
312for i ulong sub a s
313put bt_mfile i get bt_in add s i
314end
315put bt_v 11 sub a s
316for i ulong sub b add a 1
317put bt_mold i get bt_in add add a 1 i
318end
319put bt_v 12 sub b add a 1
320for i ulong sub e add b 1
321put bt_mnew i get bt_in add add b 1 i
322end
323put bt_v 13 sub e add b 1
324ret true
325end
326
327note ---- write bt_out to the file named by bt_path ----
328fn bt_store to bool
329fd = fopen bt_path 0 get bt_v 6 577 420
330if lt fd 0
331ret false
332end
333p = ulong 0
334n = get bt_v 0
335while lt p n bound 9223372036854775807
336k = write int fd bt_out p sub n p
337if eq k -4
338continue
339end
340if le k 0
341z = fclose int fd
342ret false
343end
344p = add p ulong k
345end
346z = fclose int fd
347ret true
348end
349
350note ---- packed lists in bt_pk (length bt_v[14]) ----
351fn bt_pkstart count ulong to void
352put bt_pk 0 uchar and count 255
353put bt_pk 1 uchar shr count 8
354put bt_v 14 2
355end
356fn bt_pkpath to void
357note append the path in bt_path as one record
358n = get bt_v 6
359p = get bt_v 14
360put bt_pk p uchar and n 255
361put bt_pk add p 1 uchar shr n 8
362for i ulong n
363put bt_pk add add p 2 i get bt_path i
364end
365put bt_v 14 add add p 2 n
366end
367
368note ---- the output path: build/UNIT then the suffix ----
369fn bt_outpath sfx view uchar * to void
370bt_pclear
371bt_padd bt_t_build
372bt_paddunit
373bt_padd sfx
374end
375fn bt_outfinal to void
376bt_pclear
377bt_padd bt_t_build
378bt_paddunit
379note a mutant build already has ".mut" in its unit name
380end
381
382note ---- run ./omc with redirections; returns its exit status (or 300+ for a signal, 400 for I/O) ----
383fn bt_omc to ulong
384bt_outpath bt_t_om
385fi = fopen bt_path 0 get bt_v 6 0 0
386bt_outpath bt_t_new
387z = unlink bt_path 0 get bt_v 6
388fo = fopen bt_path 0 get bt_v 6 577 493
389bt_outpath bt_t_err
390fe = fopen bt_path 0 get bt_v 6 577 420
391if or or lt fi 0 lt fo 0 lt fe 0
392ret 400
393end
394bt_pclear
395bt_padd bt_t_omc
396bt_pkstart 1
397bt_pkpath
398pid = fork
399if lt pid 0
400ret 400
401end
402if eq pid 0
403if lt dupfd int fi 0 0 0
404exit 126
405end
406if lt dupfd int fo 1 0 0
407exit 126
408end
409if lt dupfd int fe 2 0 0
410exit 126
411end
412r = execprog bt_pk 0 get bt_v 14
413exit 127
414end
415z = fclose int fi
416z = fclose int fo
417z = fclose int fe
418b = new uchar 4 0
419while true bound 9223372036854775807
420w = waitpid int pid b 0 4 0
421if eq w pid
422break
423end
424if ne w -4
425ret 400
426end
427end
428st = or or ulong get b 0 shl ulong get b 1 8 or shl ulong get b 2 16 shl ulong get b 3 24
429if eq and st 127 0
430ret and shr st 8 255
431end
432ret add 300 and st 127
433end
434
435fn bt_copyerr to void
436bt_outpath bt_t_err
437if bt_load
438k = write 2 bt_in 0 get bt_v 1
439end
440end
441
442fn bt_ioerr to void
443bt_say bt_t_io
444bt_saypath
445exit 2
446end
447
448note ---- the unit list into bt_list (length bt_v[2]); a line "@PATH" is replaced by the lines of list file PATH
449note (one level: an included list does not include others) ----
450fn bt_expand to bool
451n = get bt_v 1
452for i ulong n
453put bt_mut i get bt_in i
454end
455put bt_v 2 0
456p = ulong 0
457while lt p n bound 9223372036854775807
458q = p
459while lt q n bound 9223372036854775807
460if eq get bt_mut q 10
461break
462end
463q = add q 1
464end
465inc = false
466if gt q p
467inc = eq get bt_mut p 64
468end
469if inc
470if not bt_include add p 1 q
471ret false
472end
473else
474bt_listadd p q
475end
476p = add q 1
477end
478ret true
479end
480fn bt_listadd s ulong e ulong to void
481o = get bt_v 2
482for i ulong sub e s
483put bt_list add o i get bt_mut add s i
484end
485put bt_list add o sub e s 10
486put bt_v 2 add add o sub e s 1
487end
488fn bt_include s ulong e ulong to bool
489bt_pclear
490m = e
491while gt m s bound 9223372036854775807
492lc = get bt_mut sub m 1
493if and ne lc 32 ne lc 13
494break
495end
496m = sub m 1
497end
498for i ulong sub m s
499put bt_path i get bt_mut add s i
500end
501put bt_v 6 sub m s
502if not bt_load
503ret false
504end
505o = get bt_v 2
506k = get bt_v 1
507for i ulong k
508put bt_list add o i get bt_in i
509end
510put bt_list add o k 10
511put bt_v 2 add add o k 1
512ret true
513end
514
515fn bt_nameok to bool
516for i ulong get bt_v 7
517ch = get bt_unit i
518lo = and ge ch 97 le ch 122
519dg = and ge ch 48 le ch 57
520pu = or eq ch 95 or eq ch 45 eq ch 46
521if not or or lo dg pu
522ret false
523end
524if and eq i 0 eq ch 46
525ret false
526end
527end
528ret true
529end
530
531fn main to int
532c = ulong argcnt
533if and ne c 2 ne c 4
534bt_say bt_t_usage
535ret 2
536end
537k = argread 1 bt_unit 0 4096
538if or le k 0 gt k 200
539bt_say bt_t_usage
540ret 2
541end
542put bt_v 7 ulong k
543if not bt_nameok
544bt_say bt_t_badname
545ret 2
546end
547if eq c 4
548k = argread 3 bt_mname 0 8192
549if le k 0
550bt_say bt_t_usage
551ret 2
552end
553put bt_v 10 ulong k
554k = argread 2 bt_path 0 8192
555if le k 0
556bt_say bt_t_usage
557ret 2
558end
559put bt_v 6 ulong k
560if not bt_load
561bt_ioerr
562end
563if not bt_mutfind
564bt_say bt_t_mutbad
565k = write 2 bt_mname 0 get bt_v 10
566k = write 2 bt_t_nl 0 1
567ret 3
568end
569put bt_v 4 1
570end
571note the unit list
572bt_pclear
573bt_padd bt_t_units
574bt_paddunit
575bt_padd bt_t_list
576if not bt_load
577bt_ioerr
578end
579if not bt_expand
580bt_ioerr
581end
582ln = get bt_v 2
583if eq get bt_v 4 1
584u = get bt_v 7
585for i ulong 4
586put bt_unit add u i get bt_t_mut i
587end
588put bt_v 7 add u 4
589end
590put bt_v 0 0
591bt_emitt bt_t_om2
592bt_emitt bt_t_nl
593p = ulong 0
594while lt p ln bound 9223372036854775807
595q = p
596while lt q ln bound 9223372036854775807
597if eq get bt_list q 10
598break
599end
600q = add q 1
601end
602s = p
603e = q
604while lt s e bound 9223372036854775807
605if ne get bt_list s 32
606break
607end
608s = add s 1
609end
610while gt e s bound 9223372036854775807
611lc = get bt_list sub e 1
612if and ne lc 32 ne lc 13
613break
614end
615e = sub e 1
616end
617if gt e s
618if gt sub e s 8000
619bt_say bt_t_usage
620ret 2
621end
622bt_pclear
623for i ulong sub e s
624put bt_path i get bt_list add s i
625put bt_cur i get bt_list add s i
626end
627put bt_v 6 sub e s
628put bt_v 8 sub e s
629if not bt_load
630bt_ioerr
631end
632bt_source
633end
634p = add q 1
635end
636if eq get bt_v 9 1
637bt_say bt_t_big
638ret 2
639end
640if eq get bt_v 4 1
641if ne get bt_v 5 1
642bt_say bt_t_mutapply
643bt_num get bt_v 5
644bt_say bt_t_nl
645ret 3
646end
647end
648bt_outpath bt_t_om
649if not bt_store
650bt_ioerr
651end
652st = bt_omc
653if ne st 0
654bt_copyerr
655bt_say bt_t_refused
656k = write 2 bt_unit 0 get bt_v 7
657bt_say bt_t_exit
658bt_num st
659bt_say bt_t_nl
660ret 1
661end
662note rename build/UNIT.new to build/UNIT (or build/UNIT.mut)
663bt_pkstart 2
664bt_outpath bt_t_new
665bt_pkpath
666bt_outfinal
667bt_pkpath
668r = renames bt_pk 0 get bt_v 14 0
669if lt r 0
670bt_ioerr
671end
672bt_say bt_t_built
673k = write 2 bt_unit 0 get bt_v 7
674note (the unit name already ends in .mut for a mutant)
675bt_say bt_t_nl
676ret 0
677end