interplane.Get Omega
Read in your language

spark-connector

aien-dev/spark-connector

Brought by drakestapleton

branch main Branches Tags History

spark-connector / runner / 30cell.om

369 lines · 8173 bytes Raw Blame History
1note runner/30cell.om -- the cell: every byte of brought code runs only through omega-mv execpure.
2note A stage forks a child that opens its three stage files onto descriptors 0..2 and calls
3note execpure on one program. execpure (OMEGESE.md, word 198) closes every descriptor at or above 3,
4note pins the program as descriptor 3, restricts the process with Landlock (read and execute of
5note that program only), PR_SET_NO_NEW_PRIVS, seven prlimits (CPU 1 s soft / 2 s hard, file size
6note 1 MiB, stack 8 MiB, no core, no new process, 4 descriptors, 128 MiB address space) and a
7note seccomp filter that allows only read(0), write(1|2), exit, exit_group and execveat of the
8note pinned program (everything else EPERM, a non-AArch64 call kills), then executes it with an
9note empty environment. The parent kills the stage's process group at the 3 s wall clock.
10note Both stages run in the cell: the compiler too.
11note When the cell cannot start (a stage file does not open, or execpure itself fails) the child
12note writes "E" and the errno to its stage stderr file and exits 125 (r_kfail, r_cfail), so "the
13note cell could not start" is never mistaken for a program's own exit status.
14
15text rtomc "bin/omc"
16text rtsrc "source.om"
17text rtcout "compiler.out"
18text rtcerr "compile.err"
19text rtelf "part.elf"
20text rtsin "stdin"
21text rtsout "stdout"
22text rtserr "stderr"
23const rwall ulong 3000000000
24
25note r_kidio: open the stage file nm onto descriptor t (t 0: read; 1, 2: new file 0600); on failure
26note report the errno as a cell failure (r_kfail)
27fn r_kidio nm view uchar * t int to void
28n = r_jpath nm
29fl = rfonew
30if eq t 0
31fl = rford
32end
33fd = fopen rph 0 n fl 384
34if lt fd 0
35r_kfio t fd
36end
37r = dupfd int fd t 0
38if lt r 0
39r_kfio t r
40end
41if ne fd long t
42x = fclose int fd
43end
44end
45
46note r_kfio: a stage file could not be set up. Without its stderr file the child writes nothing
47note (descriptor 2 is still the runner's own log) and exits 125; the parent then finds no stderr
48note file and reports the cell failure.
49fn r_kfio t int v long to void
50if eq t 2
51exit 125
52end
53r_kfail v
54end
55
56note r_kfail: the cell could not start. execpure closes every descriptor at or above 3 before any
57note step that can fail (close_range first), so the record goes to descriptor 2, the stage's stderr
58note file (opened first; seccomp, when it is already installed, still allows write(2)): "E" and
59note the errno in 7 bytes little endian, then exit 125. The parent counts it as a cell failure
60note only when the status is 125 and the stderr file is exactly such a record or does not exist;
61note a program that imitates it only makes its own result uncounted.
62fn r_kfail v long to void
63e = new uchar 8 0
64put e 0 69
65u = ulong neg v
66for i ulong 7
67put e add i 1 uchar and shr u mul i 8 255
68end
69x = write 2 e 0 8
70exit 125
71end
72
73note r_kid: the stage child. sg 0 compiles source.om with <state>/bin/omc, sg 1 runs part.elf.
74fn r_kid sg ulong to void
75pk = new uchar 4200 0
76r = setpgid 0 0
77n = ulong 0
78if eq sg 0
79r_kidio rtcerr 2
80r_kidio rtsrc 0
81r_kidio rtcout 1
82n = r_path rtomc
83else
84r_kidio rtserr 2
85r_kidio rtsin 0
86r_kidio rtsout 1
87n = r_jpath rtelf
88end
89put pk 0 1
90put pk 1 0
91put pk 2 uchar and n 255
92put pk 3 uchar shr n 8
93for i ulong n bound 4096
94put pk add 4 i get rph i
95end
96v = execpure pk 0 add 4 n
97r_kfail v
98end
99
100fn r_u32 b view uchar * o ulong to ulong
101ret or or ulong get b o shl ulong get b add o 1 8 or shl ulong get b add o 2 16 shl ulong get b add o 3 24
102end
103
104note r_cell: run stage sg; sets rgv rwhy (0 exited with rcode, 1 killed by signal rcode, 2 wall
105note clock, 3 the cell could not start: errno rcode). False when no stage could be started.
106fn r_cell sg ulong to bool
107pid = fork
108if lt pid 0
109ret false
110end
111if eq pid 0
112r_kid sg
113exit 125
114end
115r_cwait pid
116if and eq get rgv rwhy 0 eq get rgv rcode 125
117r_cfail sg
118end
119ret true
120end
121
122note r_cwait: wait for the stage until the wall clock, then kill its process group
123fn r_cwait pid long to void
124st = new uchar 4 0
125dl = add monons long rwall
126put rgv rwhy 9
127put rgv rcode 0
128while true bound 9223372036854775807
129w = waitpid int pid st 0 4 1
130if eq w pid
131r_cstat r_u32 st 0
132ret
133end
134if lt w 0
135ret
136end
137if gt monons dl
138x = killpid int neg pid 9
139x = killpid int pid 9
140w = waitpid int pid st 0 4 0
141put rgv rwhy 2
142ret
143end
144x = sleepns 2000000
145end
146end
147
148fn r_cstat s ulong to void
149if eq and s 127 0
150put rgv rwhy 0
151put rgv rcode and shr s 8 255
152else
153put rgv rwhy 1
154put rgv rcode and s 127
155end
156end
157
158note r_cfail: status 125: the stage's stderr file is absent (it could not be opened) or exactly the
159note 8-byte record "E" + errno that r_kfail writes, then the cell could not start
160fn r_cfail sg ulong to void
161eb = new uchar 9 0
162n = ulong 0
163if eq sg 0
164n = r_jpath rtcerr
165else
166n = r_jpath rtserr
167end
168r = r_rd eb
169if eq r -2
170put rgv rwhy 3
171put rgv rcode 2
172ret
173end
174if and eq r 8 eq get eb 0 69
175put rgv rwhy 3
176put rgv rcode r_u32 eb 1
177end
178end
179
180note r_seal: copy compiler.out to a new inode part.elf, mode 0500 (a fresh inode with no writer)
181fn r_seal to bool
182b = new uchar 65536 0
183p = new uchar 4100 0
184n = r_jpath rtcout
185pl = r_phcp p
186src = fopen p 0 pl rford 0
187if lt src 0
188ret false
189end
190n = r_jpath rtelf
191dst = fopen rph 0 n rfonew 384
192if lt dst 0
193x = fclose int src
194ret false
195end
196ok = false
197while true bound 9223372036854775807
198r = read int src b 0 65536
199if eq r 0
200ok = true
201break
202end
203if lt r 0
204break
205end
206o = ulong 0
207while lt o ulong r bound 65536
208w = write int dst b o sub ulong r o
209if le w 0
210break
211end
212o = add o ulong w
213end
214if lt o ulong r
215break
216end
217end
218x = fclose int src
219s = fsync int dst
220m = fchmod int dst 320
221x = fclose int dst
222ret and and ok eq s 0 eq m 0
223end
224
225note r_fsha: SHA-256 of the file at rph into h (32 bytes)
226fn r_fsha h edit uchar * to bool
227c = new ulong 40 0
228b = new uchar 65536 0
229fd = fopen rph 0 get rgv rphl rford 0
230if lt fd 0
231ret false
232end
233sha256_init c
234ok = false
235while true bound 9223372036854775807
236r = read int fd b 0 65536
237if eq r 0
238ok = true
239break
240end
241if lt r 0
242break
243end
244sha256_update c part b 0 ulong r
245end
246x = fclose int fd
247sha256_final c h
248ret ok
249end
250
251note r_jfhex: SHA-256 of the job file nm as hex into rjf at o
252fn r_jfhex nm view uchar * o ulong to bool
253h = new uchar 32 0
254x = new uchar 64 0
255n = r_jpath nm
256if not r_fsha h
257ret false
258end
259k = r_hex h x 0
260for i ulong 64
261put rjf add o i get x i
262end
263ret true
264end
265
266note r_jfzero: 64 zeros into rjf at o (no binary)
267fn r_jfzero o ulong to void
268for i ulong 64
269put rjf add o i 48
270end
271end
272
273note r_exset: the exit text pre + decimal v (no decimal when v is 2^64-1) into rjf at rfexit
274fn r_exset pre view uchar * v ulong to void
275d = new uchar 40 0
276k = r_cp d 0 pre
277if ne v 18446744073709551615
278k = r_dec d k v
279end
280for i ulong k bound 40
281put rjf add rfexit i get d i
282end
283put rgv rexl k
284end
285
286fn r_cp d edit uchar * o ulong s view uchar * to ulong
287for i ulong len s bound 16777216
288put d add o i get s i
289end
290ret add o len s
291end
292
293text rtxomc "omc"
294text rtxomcs "omcsignal"
295text rtxomct "omctimeout"
296text rtxsig "signal"
297text rtxtmo "timeout"
298text rtxcell "cell"
299text rtxnone ""
300
301note r_outcome: the exit text for the last stage (sg 0 compile, 1 run)
302fn r_outcome sg ulong to void
303why = get rgv rwhy
304c = get rgv rcode
305none = ulong 18446744073709551615
306if eq why 3
307r_exset rtxcell c
308ret
309end
310if eq sg 0
311if eq why 0
312r_exset rtxomc c
313end
314if eq why 1
315r_exset rtxomcs c
316end
317if eq why 2
318r_exset rtxomct none
319end
320ret
321end
322if eq why 0
323r_exset rtxnone c
324end
325if eq why 1
326r_exset rtxsig c
327end
328if eq why 2
329r_exset rtxtmo none
330end
331end
332
333note r_check: compile and run the job directory (source.om, stdin) in the cell; fills binary,
334note stdout and exit in rjf. False only when a stage could not be started at all.
335fn r_check to bool
336if not r_cell 0
337ret false
338end
339if or ne get rgv rwhy 0 ne get rgv rcode 0
340r_outcome 0
341r_jfzero rfbin
342r_jfnil rfout
343ret true
344end
345if not r_jfhex rtcout rfbin
346ret false
347end
348if not r_seal
349ret false
350end
351if not r_cell 1
352ret false
353end
354r_outcome 1
355note LIEPOINT: the gate's lying-runner mutant changes the stdout hash line below
356ret r_jfhex rtsout rfout
357end
358
359note r_jfnil: the SHA-256 of no bytes as hex into rjf at o
360fn r_jfnil o ulong to void
361h = new uchar 32 0
362x = new uchar 64 0
363e = new uchar 1 0
364r_sha part e 0 0 h
365k = r_hex h x 0
366for i ulong 64
367put rjf add o i get x i
368end
369end