interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / ng / ngrun.om

2250 lines · 43018 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/ngrun.om: the ΩIR reference interpreter (lane ng-ir). Build:
3note cat seed/ng/src/ir.om seed/ng/ngrun.om | seed/out/omc > ngrun
4note Usage: ngrun FILE.ir [STEPS]. It runs the program entry (IR.md s6.1) of a unit that ngcheck accepts, with
5note the meaning of IR.md s7, one op at a time: host words reach the real process (stdout, stderr,
6note stdin), the exit status is the entry's i32 result, `exit v` exits v, a failing check or `trap`
7note exits 100 + code. Call depth past 65535 frames, more than 4194304 live values or more than
8note 16 MiB of memory (globals plus alloca) trap 6 (IR.md s7.9: call and alloca may trap 6).
9note It is the oracle of the differential gate (D9): no optimization, nothing shared with a back end;
10note it shares only ir.om's reader, which the ΩIR gate checks byte for byte.
11note Outside the supported subset it prints one stderr line "ngrun: unsupported op N at OFFSET" (or
12note "unsupported host word N") and exits 125; OFFSET is the op's byte offset in the file.
13note A file over 16777216 bytes is "ngrun: refused class 23 at 16777216" and exit 23; an unreadable one
14note "ngrun: cannot read input" and exit 27.
15note Supported: const select, integer and f32/f64 arithmetic, compare, conv, bitcast (10..37), every
16note check (70..82), facts (90..93, no effect), load store atom atomcas alloca fill gaddr len slice
17note fload fstore fref on scalar elements, br cbr ret retv call trap exit, host read write monons
18note realns cpufence cpurelax; fused f32/f64 fma (46); every f32/f64 conv rounding/saturation mode;
19note packed 16-byte float vectors vec f32 4 and vec f64 2 on vload vstore vsplat vextract add sub mul fma.
20note Not yet: other vectors and tiles (102..106), kernels (40, 41, 44, 45, 67), fmath (28),
21note f16/bf16/fp8.
22note Its own limits never exit 100..114 (those are the IR's traps): after STEPS executed ops (default 2^36 =
23note 68719476736, about an hour on the Spark, above the 7.8e9 of the sort kernel; a loop without chk.fuel
24note may run forever) it prints "ngrun: step limit at OFFSET" (the next
25note op) and exits 124; an access whose element lies outside the object its REF or SLICE names (only an
26note input ngcheck refuses, UNCHECKED, can reach one) prints "ngrun: access outside storage at OFFSET" and
27note exits 126. Other input that ngcheck refuses has no meaning here: ngrun reads it with ir.om and its
28note exit status is not an IR result; judge a run only on a unit ngcheck accepts.
29note ---- state ------------------------------------------------------------------------------------
30note r_s: 0 slot offset (slot = r_s0 + value handle), 1 current function, 2 memory top, 3 call
31note depth, 4 value stack top, 5 step budget, 6 steps run
32global r_s ulong 8
33global r_v ulong 4194304
34global r_w ulong 4194304
35global r_m uchar 16777216
36global r_pm uchar 16777216
37global r_pb ulong 65537
38global r_pl ulong 65537
39global r_ps ulong 1
40global r_ga ulong 65537
41global r_cf uint 65536
42global r_co uint 65536
43global r_cs ulong 65536
44global r_cm ulong 65536
45global r_ct ulong 65536
46global r_tv ulong 64
47global r_tw ulong 64
48global r_dg uchar 24
49global r_pa uchar 4096
50text r_msg "ngrun: unsupported op \n at \nngrun: unsupported host word \nngrun: refused class \nngrun: usage: ngrun FILE.ir [STEPS]\nngrun: cannot read input\nngrun: step limit at \nngrun: access outside storage at \n"
51note ---- messages ---------------------------------------------------------------------------------
52fn rn_say a ulong b ulong to void
53k = write 2 r_msg a sub b a
54end
55fn rn_num v ulong to void
56x = v
57i = ulong 24
58for j ulong 21
59i = sub i 1
60put r_dg i uchar add 48 rem x 10
61x = div x 10
62if eq x 0
63k = write 2 r_dg i sub 24 i
64ret
65end
66end
67end
68fn rn_nl to void
69put r_dg 0 10
70k = write 2 r_dg 0 1
71end
72note one stderr line, message r_msg[a, b) then the offset of op o
73fn rn_lim a ulong b ulong o uint to void
74rn_say a b
75rn_num ulong get i_oof o
76rn_nl
77end
78note an access outside its object (input ngcheck refuses): one line, exit 126
79fn rn_oob o uint to void
80rn_lim 163 196 o
81exit 126
82end
83note elements [off, off + n) of operand slot k lie inside its object, else rn_oob
84fn rn_ext o uint k uint off ulong n ulong to void
85l = rn_len o k
86if gt off l
87rn_oob o
88end
89if gt n sub l off
90rn_oob o
91end
92end
93note an op outside the supported subset: one line, exit 125
94fn rn_uns o uint to void
95op = ulong get i_oop o
96if eq op 110
97rn_say 28 57
98rn_num get i_oim o
99else
100rn_say 0 22
101rn_num op
102end
103rn_say 23 27
104rn_num ulong get i_oof o
105rn_nl
106exit 125
107end
108note ---- values -----------------------------------------------------------------------------------
109fn rn_g h uint to ulong
110ret get r_v add get r_s 0 ulong h
111end
112fn rn_gw h uint to ulong
113ret get r_w add get r_s 0 ulong h
114end
115fn rn_p h uint x ulong to void
116put r_v add get r_s 0 ulong h x
117end
118fn rn_pw h uint x ulong to void
119put r_w add get r_s 0 ulong h x
120end
121note value and type of operand slot k of op o
122fn rn_a o uint k uint to ulong
123ret rn_g ir_arg o k
124end
125fn rn_aw o uint k uint to ulong
126ret rn_gw ir_arg o k
127end
128fn rn_at o uint k uint to uint
129ret get i_vty ir_arg o k
130end
131note set op o's result (both words)
132fn rn_r o uint x ulong to void
133h = get i_ors o
134if ne h 0
135rn_p h x
136rn_pw h 0
137end
138end
139fn rn_rw o uint y ulong to void
140h = get i_ors o
141if ne h 0
142rn_pw h y
143end
144end
145note ---- scalar types -----------------------------------------------------------------------------
146fn rn_w t uint to ulong
147if eq t 1
148ret 1
149end
150if or eq t 2 eq t 6
151ret 8
152end
153if or eq t 3 eq t 7
154ret 16
155end
156if or eq t 4 or eq t 8 eq t 13
157ret 32
158end
159ret 64
160end
161fn rn_sg t uint to bool
162ret and ge t 6 le t 9
163end
164fn rn_isi t uint to bool
165ret and ge t 2 le t 9
166end
167fn rn_isf t uint to bool
168ret or eq t 13 eq t 14
169end
170note the canonical form of x as type t: zero-extended when unsigned, sign-extended when signed
171fn rn_cn t uint x ulong to ulong
172w = rn_w t
173if eq w 64
174ret x
175end
176y = and x sub shl ulong 1 w 1
177if rn_sg t
178s = shl ulong 1 sub w 1
179ret sub xor y s s
180end
181ret y
182end
183fn rn_min t uint to ulong
184ret rn_cn t shl ulong 1 sub rn_w t 1
185end
186fn rn_lt t uint a ulong b ulong to bool
187if rn_sg t
188ret lt bitcast long a bitcast long b
189end
190ret lt a b
191end
192note ---- integer arithmetic (IR.md s7.3: total) -----------------------------------------------------
193fn rn_div t uint a ulong b ulong to ulong
194if eq b 0
195ret 0
196end
197if rn_sg t
198if and eq a rn_min t eq b sub ulong 0 1
199ret a
200end
201ret rn_cn t bitcast ulong div bitcast long a bitcast long b
202end
203ret div a b
204end
205fn rn_rem t uint a ulong b ulong to ulong
206if eq b 0
207ret 0
208end
209if rn_sg t
210if eq b sub ulong 0 1
211ret 0
212end
213ret rn_cn t bitcast ulong rem bitcast long a bitcast long b
214end
215ret rem a b
216end
217fn rn_rot t uint a ulong b ulong l bool to ulong
218w = rn_w t
219s = and b sub w 1
220if eq s 0
221ret a
222end
223if not l
224s = sub w s
225end
226ret rn_cn t or shl a s shr a sub w s
227end
228fn rn_ia op uint t uint a ulong b ulong to ulong
229if eq op 10
230ret rn_cn t add a b
231end
232if eq op 11
233ret rn_cn t sub a b
234end
235if eq op 12
236ret rn_cn t mul a b
237end
238if eq op 13
239ret rn_div t a b
240end
241if eq op 14
242ret rn_rem t a b
243end
244if eq op 15
245ret rn_cn t sub ulong 0 a
246end
247if eq op 16
248ret and a b
249end
250if eq op 17
251ret or a b
252end
253if eq op 18
254ret xor a b
255end
256if eq op 19
257ret rn_cn t not a
258end
259s = and b sub rn_w t 1
260if eq op 20
261ret rn_cn t shl a s
262end
263if eq op 21
264if rn_sg t
265ret bitcast ulong shr bitcast long a bitcast long s
266end
267ret shr a s
268end
269if eq op 22
270ret rn_rot t a b true
271end
272if eq op 23
273ret rn_rot t a b false
274end
275if eq op 24
276if rn_lt t b a
277ret b
278end
279ret a
280end
281if rn_lt t a b
282ret b
283end
284ret a
285end
286fn rn_icmp op uint t uint a ulong b ulong to bool
287if eq op 30
288ret eq a b
289end
290if eq op 31
291ret ne a b
292end
293if eq op 32
294ret rn_lt t a b
295end
296if eq op 33
297ret not rn_lt t b a
298end
299if eq op 34
300ret rn_lt t b a
301end
302ret not rn_lt t a b
303end
304note ---- floats: raw bits in the low 32 (f32) or 64 (f64) bits of a value ----------------------------
305fn rn_tof t uint a ulong to f64
306if eq t 13
307ret f64 bitcast f32 uint a
308end
309ret bitcast f64 a
310end
311fn rn_f64 op uint a ulong b ulong to ulong
312x = bitcast f64 a
313y = bitcast f64 b
314if eq op 10
315ret bitcast ulong add x y
316end
317if eq op 11
318ret bitcast ulong sub x y
319end
320if eq op 12
321ret bitcast ulong mul x y
322end
323if eq op 13
324ret bitcast ulong div x y
325end
326if eq op 26
327ret bitcast ulong fsqrt x
328end
329ret 0
330end
331fn rn_f32 op uint a ulong b ulong to ulong
332x = bitcast f32 uint a
333y = bitcast f32 uint b
334if eq op 10
335ret ulong bitcast uint add x y
336end
337if eq op 11
338ret ulong bitcast uint sub x y
339end
340if eq op 12
341ret ulong bitcast uint mul x y
342end
343if eq op 13
344ret ulong bitcast uint div x y
345end
346if eq op 26
347ret ulong bitcast uint fsqrt x
348end
349ret 0
350end
351note min and max (IR.md s7.3, OMEGESE fmin/fmax, FMINNM/FMAXNM): one quiet NaN gives the other operand;
352note a signaling NaN in either operand gives that operand quieted (a first); two quiet NaNs give a; -0 < +0
353fn rn_fmm op uint t uint a ulong b ulong to ulong
354x = rn_tof t a
355y = rn_tof t b
356q = ulong 0x0008000000000000
357if eq t 13
358q = 0x00400000
359end
360na = ne x x
361nb = ne y y
362if or na nb
363sa = and na eq and a q 0
364sb = and nb eq and b q 0
365if sa
366ret or a q
367end
368if sb
369ret or b q
370end
371if and na nb
372ret or a q
373end
374if na
375ret b
376end
377ret a
378end
379if eq op 24
380if lt x y
381ret a
382end
383if lt y x
384ret b
385end
386ret or a b
387end
388if gt x y
389ret a
390end
391if gt y x
392ret b
393end
394ret and a b
395end
396fn rn_fa op uint t uint a ulong b ulong to ulong
397sb = shl ulong 1 sub rn_w t 1
398if eq op 15
399ret xor a sb
400end
401if eq op 27
402ret and a sub sb 1
403end
404if or eq op 24 eq op 25
405ret rn_fmm op t a b
406end
407if eq t 13
408ret rn_f32 op a b
409end
410ret rn_f64 op a b
411end
412fn rn_fcmp op uint t uint a ulong b ulong to bool
413x = rn_tof t a
414y = rn_tof t b
415if eq op 30
416ret eq x y
417end
418if eq op 31
419ret ne x y
420end
421if eq op 32
422ret lt x y
423end
424if eq op 33
425ret le x y
426end
427if eq op 34
428ret gt x y
429end
430ret ge x y
431end
432fn rn_b2u c bool to ulong
433if c
434ret 1
435end
436ret 0
437end
438note ---- conv and bitcast (IR.md s7.3) -------------------------------------------------------------
439note float x to int type t: truncate toward zero, saturate, NaN 0 (omc's conversion has that meaning)
440fn rn_f2i t uint x f64 to ulong
441if eq t 2
442ret ulong uchar x
443end
444if eq t 3
445ret ulong ushort x
446end
447if eq t 4
448ret ulong uint x
449end
450if eq t 5
451ret ulong x
452end
453if eq t 6
454ret bitcast ulong long byte x
455end
456if eq t 7
457ret bitcast ulong long short x
458end
459if eq t 8
460ret bitcast ulong long int x
461end
462ret bitcast ulong long x
463end
464fn rn_i2f t uint s uint a ulong to ulong
465if eq t 14
466if rn_sg s
467ret bitcast ulong f64 bitcast long a
468end
469ret bitcast ulong f64 a
470end
471if rn_sg s
472ret ulong bitcast uint f32 bitcast long a
473end
474ret ulong bitcast uint f32 a
475end
476note Integer to float toward zero: retain the leading precision bits, never round upward.
477note No intermediate float, so u64 and i64 cannot double-round through f64.
478fn rn_i2z t uint s uint a ulong to ulong
479v = a
480sg = ulong 0
481if rn_sg s
482if lt bitcast long a 0
483v = sub ulong 0 a
484sg = shl ulong 1 sub rn_w t 1
485end
486end
487if eq v 0
488ret 0
489end
490p = ulong 23
491bias = ulong 127
492if eq t 14
493p = 52
494bias = 1023
495end
496e = ulong 0
497for j ulong 64
498if ne shr v j 0
499e = j
500end
501end
502m = v
503if gt e p
504m = shr v sub e p
505else
506m = shl v sub p e
507end
508ret or sg or shl add e bias p and m sub shl ulong 1 p 1
509end
510fn rn_conv o uint to void
511t = get i_oty o
512s = rn_at o 0
513a = rn_a o 0
514im = get i_oim o
515if and rn_isi t rn_isi s
516rn_r o rn_cn t a
517ret
518end
519
520if or not or rn_isi t rn_isf t not or rn_isi s rn_isf s
521rn_uns o
522end
523if rn_isi s
524if eq and im 3 1
525rn_r o rn_i2z t s a
526ret
527end
528rn_r o rn_i2f t s a
529ret
530end
531x = rn_tof s a
532if rn_isi t
533rn_r o rn_f2i t x
534ret
535end
536if eq t 14
537rn_r o bitcast ulong x
538ret
539end
540y = f32 x
541inf = div 1.0f64 0.0f64
542note The nearest result beyond the exact magnitude has the toward-zero predecessor.
543if eq and im 3 1
544if gt fabs f64 y fabs x
545bits = sub ulong bitcast uint y 1
546y = bitcast f32 uint bits
547end
548end
549sat = ne and im 4 0
550yi = eq fabs f64 y inf
551xf = ne fabs x inf
552if and sat and yi xf
553mx = bitcast f32 uint 2139095039
554y = mx
555if lt x 0.0f64
556y = neg mx
557end
558end
559rn_r o ulong bitcast uint y
560end
561fn rn_bcast o uint to void
562t = get i_oty o
563a = rn_a o 0
564if rn_isf t
565rn_r o rn_cn t a
566ret
567end
568rn_r o rn_cn t a
569end
570note ---- pure ops (1..37, 46) -----------------------------------------------------------------------
571note FMA is one native operation at the declared precision, never widened or split.
572fn rn_fused t uint a ulong b ulong c ulong to ulong
573if eq t 13
574x = bitcast f32 uint a
575y = bitcast f32 uint b
576z = bitcast f32 uint c
577ret ulong bitcast uint fma x y z
578end
579x = bitcast f64 a
580y = bitcast f64 b
581z = bitcast f64 c
582ret bitcast ulong fma x y z
583end
584fn rn_isvf t uint to bool
585if ne get i_tk t 1
586ret false
587end
588e = get i_t1 t
589l = get i_t2 t
590if and eq e 13 eq l 4
591ret true
592end
593ret and eq e 14 eq l 2
594end
595fn rn_vget t uint lo ulong hi ulong k ulong to ulong
596if eq get i_t1 t 14
597if eq k 0
598ret lo
599end
600ret hi
601end
602w = lo
603if ge k 2
604w = hi
605end
606if ne and k 1 0
607ret shr w 32
608end
609ret and w 4294967295
610end
611fn rn_vf32w a ulong b ulong c ulong to ulong
612r0 = rn_fused 13 and a 4294967295 and b 4294967295 and c 4294967295
613r1 = rn_fused 13 shr a 32 shr b 32 shr c 32
614ret or r0 shl r1 32
615end
616fn rn_vfma o uint t uint to void
617if eq get i_t1 t 14
618rn_r o rn_fused 14 rn_a o 0 rn_a o 1 rn_a o 2
619rn_rw o rn_fused 14 rn_aw o 0 rn_aw o 1 rn_aw o 2
620ret
621end
622rn_r o rn_vf32w rn_a o 0 rn_a o 1 rn_a o 2
623rn_rw o rn_vf32w rn_aw o 0 rn_aw o 1 rn_aw o 2
624end
625fn rn_vb32w op uint a ulong b ulong to ulong
626r0 = rn_fa op 13 and a 4294967295 and b 4294967295
627r1 = rn_fa op 13 shr a 32 shr b 32
628ret or r0 shl r1 32
629end
630fn rn_vbin o uint op uint t uint to void
631if or lt op 10 gt op 12
632rn_uns o
633end
634if eq get i_t1 t 14
635rn_r o rn_fa op 14 rn_a o 0 rn_a o 1
636rn_rw o rn_fa op 14 rn_aw o 0 rn_aw o 1
637ret
638end
639rn_r o rn_vb32w op rn_a o 0 rn_a o 1
640rn_rw o rn_vb32w op rn_aw o 0 rn_aw o 1
641end
642fn rn_vec o uint op uint to void
643if eq op 100
644t = get i_oty o
645if not rn_isvf t
646rn_uns o
647end
648x = rn_a o 0
649if eq get i_t1 t 14
650rn_r o x
651rn_rw o x
652ret
653end
654w = or and x 4294967295 shl and x 4294967295 32
655rn_r o w
656rn_rw o w
657ret
658end
659vt = rn_at o 0
660if not rn_isvf vt
661rn_uns o
662end
663m = get i_oim o
664if ge m ulong get i_t2 vt
665rn_uns o
666end
667rn_r o rn_vget vt rn_a o 0 rn_aw o 0 m
668end
669fn rn_pure o uint op uint to void
670t = get i_oty o
671if eq op 46
672if rn_isvf t
673rn_vfma o t
674ret
675end
676if not rn_isf t
677rn_uns o
678end
679rn_r o rn_fused t rn_a o 0 rn_a o 1 rn_a o 2
680ret
681end
682if eq op 1
683rn_r o get i_oim o
684ret
685end
686if eq op 2
687h = ir_arg o 2
688if ne rn_a o 0 0
689h = ir_arg o 1
690end
691rn_r o rn_g h
692rn_rw o rn_gw h
693ret
694end
695if eq op 36
696rn_conv o
697ret
698end
699if eq op 37
700rn_bcast o
701ret
702end
703s = rn_at o 0
704a = rn_a o 0
705b = ulong 0
706if gt get i_ona o 1
707b = rn_a o 1
708end
709if and ge op 30 le op 35
710if rn_isf s
711rn_r o rn_b2u rn_fcmp op s a b
712ret
713end
714if gt s 9
715rn_uns o
716end
717rn_r o rn_b2u rn_icmp op s a b
718ret
719end
720if rn_isvf t
721rn_vbin o op t
722ret
723end
724if or eq op 28 gt t 14
725rn_uns o
726end
727if rn_isf t
728rn_r o rn_fa op t a b
729ret
730end
731if or eq op 26 or eq op 27 gt t 9
732rn_uns o
733end
734if eq t 1
735rn_r o and rn_ia op 5 a b 1
736ret
737end
738rn_r o rn_ia op t a b
739end
740note ---- checks (70..82): IR.md s7.9 ---------------------------------------------------------------
741fn rn_ckadd t uint a ulong b ulong to bool
742r = add a b
743if lt rn_w t 64
744ret ne rn_cn t r r
745end
746if rn_sg t
747ret ne and and xor a r xor b r shl ulong 1 63 0
748end
749ret lt r a
750end
751fn rn_cksub t uint a ulong b ulong to bool
752r = sub a b
753if lt rn_w t 64
754ret ne rn_cn t r r
755end
756if rn_sg t
757ret ne and and xor a b xor a r shl ulong 1 63 0
758end
759ret lt a b
760end
761fn rn_ckmul t uint a ulong b ulong to bool
762r = mul a b
763if lt rn_w t 64
764ret ne rn_cn t r r
765end
766if eq a 0
767ret false
768end
769if not rn_sg t
770ret ne div r a b
771end
772m = shl ulong 1 63
773if eq a sub ulong 0 1
774ret eq b m
775end
776if eq b sub ulong 0 1
777ret eq a m
778end
779ret ne div bitcast long r bitcast long a bitcast long b
780end
781note the exact a x 2^b fits the signed type t when MIN >> b <= a <= MAX >> b
782fn rn_ckshl t uint a ulong b ulong to bool
783w = rn_w t
784if ge b w
785ret ne a 0
786end
787mn = bitcast long rn_min t
788mx = long 9223372036854775807
789if lt w 64
790mx = sub neg mn 1
791end
792sa = bitcast long a
793sb = bitcast long b
794if lt sa shr mn sb
795ret true
796end
797ret gt sa shr mx sb
798end
799note v (type s) outside int type t, float v truncated toward zero first, NaN outside. An unsigned v into a
800note signed t of any width is outside from 2^(w-1) up: the canonical test alone would take 2^64 - 1 into i32
801note as -1 (bug hunt, ledger L1060; tests/ir x-convu)
802fn rn_ckcv t uint s uint v ulong to bool
803w = rn_w t
804if rn_isf s
805x = rn_tof s v
806if ne x x
807ret true
808end
809hi = mul f64 shl ulong 1 sub w 1 2.0f64
810lo = f64 -1.0
811if rn_sg t
812hi = f64 shl ulong 1 sub w 1
813lo = sub neg hi 1.0
814if eq w 64
815ret or lt x neg hi ge x hi
816end
817end
818ret or le x lo ge x hi
819end
820if rn_sg s
821sv = bitcast long v
822if rn_sg t
823ret ne rn_cn t v v
824end
825if lt sv 0
826ret true
827end
828end
829if rn_sg t
830ret ge v shl ulong 1 sub w 1
831end
832ret ne rn_cn t v v
833end
834fn rn_len o uint k uint to ulong
835h = ir_arg o k
836rt = get i_vty h
837if eq get i_tk rt 5
838ret rn_gw h
839end
840tt = get i_t1 rt
841ret ulong get i_t2 tt
842end
843fn rn_ckfail o uint op uint to bool
844t = rn_at o 1
845a = rn_a o 1
846b = ulong 0
847if gt get i_ona o 2
848b = rn_a o 2
849end
850if eq op 70
851ret rn_ckadd t a b
852end
853if eq op 71
854ret rn_cksub t a b
855end
856if eq op 72
857ret rn_ckmul t a b
858end
859if eq op 73
860if rn_sg t
861ret eq a rn_min t
862end
863ret ne a 0
864end
865if eq op 74
866ret eq a 0
867end
868if eq op 75
869ret and eq a rn_min t eq b sub ulong 0 1
870end
871if eq op 76
872ret rn_ckshl t a b
873end
874if eq op 77
875ret ge a rn_w t
876end
877if eq op 78
878ret rn_ckcv get i_oty o t a
879end
880if eq op 81
881ret ge a b
882end
883if eq op 82
884ret eq a 0
885end
886n = rn_len o 1
887x = rn_a o 3
888if eq op 79
889ret or eq x 0 or gt b n gt x sub n b
890end
891ret or gt b n gt x sub n b
892end
893fn rn_check o uint to void
894if rn_ckfail o uint get i_oop o
895exit int add 100 get i_oim o
896end
897end
898note ---- memory ------------------------------------------------------------------------------------
899fn rn_ld p ulong n ulong be bool to ulong
900x = ulong 0
901for j ulong n bound 8
902c = ulong get r_m add p j
903if be
904x = or shl x 8 c
905else
906x = or x shl c mul j 8
907end
908end
909ret x
910end
911fn rn_st p ulong n ulong x ulong be bool to void
912for j ulong n bound 8
913s = mul j 8
914if be
915s = mul sub sub n 1 j 8
916end
917put r_m add p j uchar and shr x s 255
918end
919end
920note element type of a REF to ARRAY or a SLICE
921fn rn_elt rt uint to uint
922if eq get i_tk rt 5
923ret get i_t1 rt
924end
925ret get i_t1 get i_t1 rt
926end
927note address of element i of operand slot k of op o; refuses non-scalar elements and an i outside the
928note object (rn_oob)
929fn rn_ea o uint k uint i ulong to ulong
930if ge i rn_len o k
931rn_oob o
932end
933ret rn_eau o k i
934end
935note element i's address without the extent test (i may be the object's end)
936fn rn_eau o uint k uint i ulong to ulong
937et = rn_elt rn_at o k
938if ge et 32
939rn_uns o
940end
941ret add rn_a o k mul i ir_tsize et
942end
943fn rn_ldv t uint p ulong be bool to ulong
944x = rn_ld p ir_tsize t be
945if rn_isi t
946ret rn_cn t x
947end
948ret x
949end
950note memory top aligned to 8, then n zero bytes; trap 6 past 16 MiB
951fn rn_alloc n ulong to ulong
952p = and add get r_s 2 7 not ulong 7
953if gt p sub 16777216 get r_ps 0
954exit 106
955end
956if gt n sub sub 16777216 p get r_ps 0
957exit 106
958end
959for j ulong n bound 16777216
960put r_m add p j 0
961end
962put r_s 2 add p n
963ret p
964end
965note field slot of field number k of the STRUCT that REF type rt targets
966fn rn_fld rt uint k ulong to ulong
967ret add ulong get i_t3 get i_t1 rt k
968end
969fn rn_atom o uint im ulong t uint old ulong v ulong to ulong
970f = and im 255
971if eq f 0
972ret rn_cn t add old v
973end
974if eq f 1
975ret rn_ia 24 t old v
976end
977if eq f 2
978ret rn_ia 25 t old v
979end
980if eq f 3
981ret and old v
982end
983if eq f 4
984ret or old v
985end
986if eq f 5
987ret xor old v
988end
989ret v
990end
991fn rn_mem o uint op uint to void
992if eq op 52
993rn_r o get r_ga get i_oim o
994ret
995end
996if eq op 50
997rn_r o rn_alloc ir_tsize get i_t1 get i_oty o
998ret
999end
1000if eq op 53
1001rn_r o rn_len o 0
1002ret
1003end
1004if eq op 42
1005p = rn_ea o 1 rn_a o 2
1006rn_r o rn_ldv get i_oty o p false
1007ret
1008end
1009if eq op 43
1010p = rn_ea o 1 rn_a o 2
1011rn_st p ir_tsize rn_elt rn_at o 1 rn_a o 3 false
1012ret
1013end
1014if eq op 54
1015rn_ext o 1 rn_a o 2 rn_a o 3
1016p = rn_eau o 1 rn_a o 2
1017rn_r o p
1018rn_rw o rn_a o 3
1019ret
1020end
1021if eq op 51
1022n = rn_len o 1
1023et = rn_elt rn_at o 1
1024v = rn_a o 2
1025p = rn_eau o 1 0
1026for j ulong n bound 16777216
1027rn_st add p mul j ir_tsize et ir_tsize et v false
1028end
1029ret
1030end
1031if or eq op 47 eq op 48
1032t = get i_oty o
1033if not rn_isi t
1034rn_uns o
1035end
1036p = rn_ea o 1 rn_a o 2
1037sz = ir_tsize t
1038old = rn_ldv t p false
1039v = rn_a o 3
1040nv = rn_atom o get i_oim o t old v
1041if eq op 48
1042nv = old
1043if eq old v
1044nv = rn_a o 4
1045end
1046end
1047rn_st p sz nv false
1048rn_r o old
1049ret
1050end
1051if and ge op 55 le op 57
1052k = uint 1
1053if eq op 57
1054k = 0
1055end
1056rt = rn_at o k
1057fx = rn_fld rt get i_oim o
1058ft = get i_xty fx
1059p = add rn_a o k ulong get i_xof fx
1060be = eq get i_xor fx 1
1061if eq op 57
1062rn_r o p
1063ret
1064end
1065if ge ft 32
1066rn_uns o
1067end
1068if eq op 55
1069rn_r o rn_ldv ft p be
1070ret
1071end
1072rn_st p ir_tsize ft rn_a o 2 be
1073ret
1074end
1075if eq op 58
1076t = get i_oty o
1077if not rn_isvf t
1078rn_uns o
1079end
1080rn_ext o 1 rn_a o 2 ulong get i_t2 t
1081p = rn_eau o 1 rn_a o 2
1082rn_r o rn_ld p 8 false
1083rn_rw o rn_ld add p 8 8 false
1084ret
1085end
1086if eq op 59
1087vt = rn_at o 3
1088if not rn_isvf vt
1089rn_uns o
1090end
1091rn_ext o 1 rn_a o 2 ulong get i_t2 vt
1092p = rn_eau o 1 rn_a o 2
1093rn_st p 8 rn_a o 3 false
1094rn_st add p 8 8 rn_aw o 3 false
1095ret
1096end
1097rn_uns o
1098end
1099note ---- host words (IR.md s7.11) --------------------------------------------------------------------
1100note slot base of a host op's 3-operand buffer argument starting at slot k: address of A + off
1101fn rn_buf o uint k uint to ulong
1102ret add rn_a o k rn_a o add k 1
1103end
1104fn rn_host o uint to void
1105rn_safehost o
1106end
1107note ---- control (60..67) ---------------------------------------------------------------------------
1108note values handed to a list of parameter values starting at v (block or function parameters)
1109fn rn_pars v0 uint n ulong to void
1110v = v0
1111for k ulong n bound 64
1112if eq v 0
1113ret
1114end
1115rn_p v get r_tv k
1116rn_pw v get r_tw k
1117v = get i_vnx v
1118end
1119end
1120fn rn_take o uint k0 uint to ulong
1121n = sub ulong get i_ona o ulong k0
1122for k ulong n bound 64
1123h = ir_arg o uint add ulong k0 k
1124put r_tv k rn_g h
1125put r_tw k rn_gw h
1126end
1127ret n
1128end
1129note values of function f (they are contiguous handles from its entry token)
1130fn rn_nv f uint to ulong
1131if lt ulong f get i_n 3
1132ret sub ulong get i_ftk add f 1 ulong get i_ftk f
1133end
1134ret sub add get i_n 6 1 ulong get i_ftk f
1135end
1136note enter function f with r_tv[0..n) as its parameters; returns its first op
1137fn rn_enter f uint n ulong to uint
1138b = get r_s 4
1139nv = rn_nv f
1140if gt nv sub 4194304 b
1141exit 106
1142end
1143put r_s 4 add b nv
1144put r_s 0 sub b ulong get i_ftk f
1145put r_s 1 ulong f
1146rn_pars get i_fp1 f n
1147ret get i_bo1 get i_fb1 f
1148end
1149fn rn_call o uint to uint
1150n = rn_take o 2
1151d = get r_s 3
1152if ge d 65535
1153exit 106
1154end
1155put r_cf d uint get r_s 1
1156put r_co d o
1157put r_cs d get r_s 0
1158put r_cm d get r_s 2
1159put r_ct d get r_s 4
1160put r_s 3 add d 1
1161ret rn_enter ir_arg o 1 n
1162end
1163fn rn_ret x ulong y ulong to uint
1164d = get r_s 3
1165if eq d 0
1166exit int bitcast long x
1167end
1168d = sub d 1
1169put r_s 3 d
1170c = get r_co d
1171put r_s 0 get r_cs d
1172put r_s 1 ulong get r_cf d
1173put r_s 2 get r_cm d
1174put r_s 4 get r_ct d
1175rn_r c x
1176rn_rw c y
1177ret get i_onx c
1178end
1179fn rn_ctl o uint op uint to uint
1180if eq op 60
1181b = ir_arg o 0
1182n = rn_take o 1
1183rn_pars get i_bp1 b n
1184ret get i_bo1 b
1185end
1186if eq op 61
1187b = ir_arg o 3
1188if ne rn_a o 1 0
1189b = ir_arg o 2
1190end
1191ret get i_bo1 b
1192end
1193if eq op 62
1194ret rn_ret 0 0
1195end
1196if eq op 63
1197ret rn_ret rn_a o 1 rn_aw o 1
1198end
1199if eq op 64
1200ret rn_call o
1201end
1202if eq op 65
1203exit int add 100 get i_oim o
1204end
1205if eq op 66
1206exit int bitcast long rn_a o 1
1207end
1208rn_uns o
1209ret 0
1210end
1211note ---- one op: returns the next op -----------------------------------------------------------------
1212fn rn_step o uint to uint
1213op = uint get i_oop o
1214if or lt op 38 eq op 46
1215rn_pure o op
1216ret get i_onx o
1217end
1218if and ge op 70 le op 82
1219rn_check o
1220ret get i_onx o
1221end
1222if and ge op 90 le op 93
1223ret get i_onx o
1224end
1225if and ge op 60 le op 67
1226ret rn_ctl o op
1227end
1228if eq op 110
1229rn_host o
1230ret get i_onx o
1231end
1232if and ge op 111 le op 116
1233rn_pool o op
1234ret get i_onx o
1235end
1236if or eq op 42 or eq op 43 or eq op 47 eq op 48
1237rn_mem o op
1238ret get i_onx o
1239end
1240if and ge op 50 le op 59
1241rn_mem o op
1242ret get i_onx o
1243end
1244if or eq op 100 eq op 101
1245rn_vec o op
1246ret get i_onx o
1247end
1248rn_uns o
1249ret 0
1250end
1251note ---- start --------------------------------------------------------------------------------------
1252note globals at 8-aligned addresses from 8 (address 0 is never an object); class 1 copies its bytes
1253fn rn_globs to void
1254put r_s 2 8
1255note ngrun keeps its own address table of IR.md s12's 65536 globals (r_ga), as ngcheck does; src/ir.om reads
1256note more since the no-caps arena, so a unit with more is LIMIT here (class 23 at offset 0), never a trap
1257if gt get i_n 2 65536
1258rn_say 58 79
1259rn_num 23
1260rn_say 23 27
1261rn_num 0
1262rn_nl
1263exit 23
1264end
1265for g ulong get i_n 2 bound 65536
1266h = add g 1
1267sz = ulong 0
1268if ne ar_c 88 h 2
1269sz = ir_tsize ar_u 87 h
1270end
1271p = rn_alloc sz
1272put r_ga h p
1273if eq uchar get ar_m add get ar_rb 88 h 1
1274d0 = get ar_m add get ar_rb 89 h
1275for j ulong sz bound 16777216
1276c = ar_bg 2 add d0 j
1277put r_m add p j c
1278end
1279end
1280end
1281end
1282note the program entry: the export with no parameters and result i32 (IR.md s6.1)
1283fn rn_entry to uint
1284for i ulong get i_n 3 bound 65536
1285f = add i 1
1286ok = and eq get i_fk f 2 eq get i_fnp f 0
1287if and ok eq get i_frs f 8
1288ret uint f
1289end
1290end
1291ret 0
1292end
1293note the STEPS argument: 1 to 19 decimal digits, else usage and exit 2
1294fn rn_steps to ulong
1295m = argread 2 r_pa 0 4096
1296if or le m 0 gt m 19
1297rn_say 80 116
1298exit 2
1299end
1300x = ulong 0
1301for j ulong ulong m bound 19
1302c = ulong get r_pa j
1303if or lt c 48 gt c 57
1304rn_say 80 116
1305exit 2
1306end
1307x = add mul x 10 sub c 48
1308end
1309ret x
1310end
1311fn rn_load to ulong
1312m = argread 1 r_pa 0 4096
1313if or le m 0 ge m 4096
1314rn_say 80 116
1315exit 2
1316end
1317fd = fopen r_pa 0 ulong m 524288 0
1318if lt fd 0
1319rn_say 116 141
1320exit 27
1321end
1322note the unit into region 0 of the arena (src/ir.om ar_rdin; no-caps 2026-11-11): a read error is exit 27, a
1323note unit the arena cannot hold is class 23 at the bytes read
1324x = ar_rdin int fd
1325if eq x 1
1326rn_say 116 141
1327exit 27
1328end
1329n = get ar_inl 0
1330if eq x 2
1331rn_say 58 79
1332rn_num 23
1333rn_say 23 27
1334rn_num n
1335rn_nl
1336exit 23
1337end
1338r = fclose int fd
1339ret n
1340end
1341fn main to int
1342rn_opts
1343n = rn_load
1344c = ir_read 0 n
1345if ne c 0
1346rn_say 58 79
1347rn_num c
1348rn_say 23 27
1349rn_num ir_badat
1350rn_nl
1351ret int c
1352end
1353f = rn_entry
1354if eq f 0
1355rn_say 58 79
1356rn_num 30
1357rn_nl
1358ret 30
1359end
1360rn_setup
1361rn_globs
1362o = rn_enter f 0
1363while true bound 9223372036854775807
1364if ge get r_s 6 get r_s 5
1365rn_lim 141 162 o
1366exit 124
1367end
1368put r_s 6 add get r_s 6 1
1369o = rn_step o
1370end
1371ret 0
1372end
1373note ---- confined host-word execution ---------------------------------------------------------------
1374note r_cfg: 0 scratch option argument, 1 first program argument, 2 scratch enabled.
1375note Virtual descriptors expose only stdio and descriptors created by an interpreted host word.
1376global r_cfg ulong 3
1377global r_fd long 256
1378global r_own uchar 256
1379global r_root uchar 4096
1380note r_pst: the stat structure of a name opened without following a link; r_pp: /proc/self/fd/N for it.
1381global r_pst uchar 128
1382global r_pp uchar 32
1383text r_ppre "/proc/self/fd/"
1384text r_hmsg "ngrun: refused host class "
1385text r_hword " word "
1386text r_hat " at "
1387text r_help "ngrun: usage: ngrun FILE.ir [STEPS] [--scratch NEW-DIR] [-- ARGS...]\n"
1388text r_opt0 "--scratch"
1389text r_opt1 "--"
1390fn rn_href o uint code int to void
1391k = write 2 r_hmsg 0 len r_hmsg
1392rn_num ulong code
1393k = write 2 r_hword 0 len r_hword
1394word = get i_oim o
1395if or eq get i_oop o 115 eq get i_oop o 116
1396word = sub ulong get i_oop o 62
1397end
1398rn_num word
1399k = write 2 r_hat 0 len r_hat
1400rn_num ulong get i_oof o
1401rn_nl
1402exit code
1403end
1404fn rn_usage to void
1405k = write 2 r_help 0 len r_help
1406exit 2
1407end
1408fn rn_argis idx ulong word view uchar * to bool
1409n = argread idx r_pa 0 len r_pa
1410if ne n long len word
1411ret false
1412end
1413for j ulong len word bound 64
1414if ne get r_pa j get word j
1415ret false
1416end
1417end
1418ret true
1419end
1420fn rn_opts to void
1421for j ulong 256
1422put r_fd j -1
1423end
1424put r_fd 0 0
1425put r_fd 1 1
1426put r_fd 2 2
1427put r_s 5 68719476736
1428put r_cfg 1 ulong argcnt
1429if or lt argcnt 2 gt argcnt 4096
1430rn_usage
1431end
1432i = ulong 2
1433while lt i ulong argcnt bound 4096
1434if rn_argis i r_opt1
1435put r_cfg 1 add i 1
1436ret
1437end
1438if rn_argis i r_opt0
1439if ne get r_cfg 0 0
1440rn_usage
1441end
1442i = add i 1
1443if ge i ulong argcnt
1444rn_usage
1445end
1446put r_cfg 0 i
1447else
1448if ne i 2
1449rn_usage
1450end
1451put r_s 5 rn_steps
1452end
1453i = add i 1
1454end
1455end
1456fn rn_setup to void
1457idx = get r_cfg 0
1458if eq idx 0
1459ret
1460end
1461n = argread idx r_root 0 len r_root
1462if or le n 0 ge n long len r_root
1463rn_usage
1464end
1465note A fresh private directory is required. Existing trees, hard links and preplanted symlinks are not admitted.
1466k = makedir r_root 0 ulong n 448
1467if ne k 0
1468rn_usage
1469end
1470note AArch64 Linux: O_DIRECTORY 16384, O_NOFOLLOW 32768, O_CLOEXEC 524288.
1471flags = or int 16384 or int 32768 int 524288
1472fd = fopen r_root 0 ulong n flags 0
1473if lt fd 0
1474rn_usage
1475end
1476k = fchdir int fd
1477cl = fclose int fd
1478if ne k 0
1479rn_usage
1480end
1481put r_cfg 2 1
1482end
1483fn rn_hr o uint value long to void
1484rn_r o bitcast ulong value
1485end
1486fn rn_fget o uint slot uint to int
1487v = bitcast long rn_a o slot
1488if or lt v 0 ge v 256
1489ret -1
1490end
1491ret int get r_fd ulong v
1492end
1493fn rn_fadd o uint actual long to long
1494if lt actual 0
1495ret actual
1496end
1497for j ulong 256
1498if lt get r_fd j 0
1499put r_fd j actual
1500put r_own j 1
1501ret long j
1502end
1503end
1504k = fclose int actual
1505rn_href o 123
1506ret 0
1507end
1508note A path over 4095 bytes or holding a zero byte traps 3 natively before any kernel call (OMEGESE s10),
1509note with or without --scratch, so ngrun gives that trap before any confinement refusal.
1510fn rn_pathspan o uint p ulong n ulong to void
1511if gt n 4095
1512exit 103
1513end
1514for i ulong n bound 4095
1515if eq get r_m add p i 0
1516exit 103
1517end
1518end
1519if eq get r_cfg 2 0
1520rn_href o 119
1521end
1522if eq n 0
1523rn_href o 119
1524end
1525for j ulong n bound 4095
1526c = get r_m add p j
1527if or eq c 0 eq c 47
1528rn_href o 119
1529end
1530end
1531if eq get r_m p 46
1532if eq n 1
1533rn_href o 119
1534end
1535if eq n 2
1536if eq get r_m add p 1 46
1537rn_href o 119
1538end
1539end
1540end
1541end
1542fn rn_path o uint slot uint to ulong
1543rn_ext o slot rn_a o add slot 1 rn_a o add slot 2
1544p = rn_buf o slot
1545rn_pathspan o p rn_a o add slot 2
1546ret p
1547end
1548note A packed pair contains exactly two confined component names. No symlink creation is allowed.
1549note The whole packet is checked first as the native words check it (count 2, records inside the packet, no
1550note trailing bytes, no empty record, no zero byte: trap 3); only then is each name confined.
1551fn rn_pair o uint to ulong
1552rn_ext o 1 rn_a o 2 rn_a o 3
1553p = rn_buf o 1
1554n = rn_a o 3
1555if or lt n 6 gt n 4096
1556exit 103
1557end
1558if ne rn_ld p 2 false 2
1559exit 103
1560end
1561cur = add p 2
1562stop = add p n
1563for j ulong 2
1564if gt add cur 2 stop
1565exit 103
1566end
1567m = rn_ld cur 2 false
1568cur = add cur 2
1569if gt m sub stop cur
1570exit 103
1571end
1572if eq m 0
1573exit 103
1574end
1575for i ulong m bound 4096
1576if eq get r_m add cur i 0
1577exit 103
1578end
1579end
1580cur = add cur m
1581end
1582if ne cur stop
1583exit 103
1584end
1585cur = add p 2
1586for j ulong 2
1587m = rn_ld cur 2 false
1588cur = add cur 2
1589rn_pathspan o cur m
1590cur = add cur m
1591end
1592ret p
1593end
1594note rn_pkt: the native packed-list check (OMEGESE s10, omc hpack) of the packet at operand slot k. Kind 0
1595note (an exec argument packet: execprog, execpure, execenv's program) is 2..65536 bytes with count 1..1024,
1596note as omc since the exec argv limits (ee5fc83e; pinned omc 564c5835); kinds 1 and 2 are 2..4096 bytes with
1597note count 1..64 (exactly 2 for kind 1). Every record lies inside it, no bytes trail the last record and no
1598note record holds a zero byte. Kind 0 needs a nonempty first record; kind 1 (symlnk) and kind 2 (execenv's
1599note environment) need every record nonempty.
1600note A violation traps 3, as natively, before ngrun's refusal of the word.
1601fn rn_pkt o uint k uint kind ulong to void
1602rn_ext o k rn_a o add k 1 rn_a o add k 2
1603p = rn_buf o k
1604n = rn_a o add k 2
1605lim = ulong 4096
1606if eq kind 0
1607lim = ulong 65536
1608end
1609if or lt n 2 gt n lim
1610exit 103
1611end
1612rn_pkrec p n kind
1613end
1614note rn_pkrec: the count and records of the packet of n bytes at p (rn_pkt); rn_pknul: no zero byte in m bytes.
1615fn rn_pknul q ulong m ulong to void
1616for i ulong m bound 65536
1617if eq get r_m add q i 0
1618exit 103
1619end
1620end
1621end
1622fn rn_pkrec p ulong n ulong kind ulong to void
1623c = rn_ld p 2 false
1624cl = ulong 64
1625if eq kind 0
1626cl = ulong 1024
1627end
1628if or eq c 0 gt c cl
1629exit 103
1630end
1631if and eq kind 1 ne c 2
1632exit 103
1633end
1634cur = add p 2
1635stop = add p n
1636for j ulong c bound 1024
1637if gt add cur 2 stop
1638exit 103
1639end
1640m = rn_ld cur 2 false
1641cur = add cur 2
1642if gt m sub stop cur
1643exit 103
1644end
1645if eq m 0
1646if or ne kind 0 eq j 0
1647exit 103
1648end
1649end
1650rn_pknul cur m
1651cur = add cur m
1652end
1653if ne cur stop
1654exit 103
1655end
1656end
1657note rn_htrap: native trap 3 checks that precede any kernel call of a word ngrun refuses (OMEGESE s10; omc
1658note hwaitio, hpeercr, hpollfds, hbuffer, hpack and the pipefds lowering): waitio events outside 0..65535,
1659note a peercred output under 12 bytes, a pollfds length not a multiple of 8 or over 8192, a waitpid output
1660note under 4 bytes, a pipefds output under 8 bytes and a malformed execprog, execpure, execenv or symlnk
1661note packet. ngrun gives that trap before its refusal, so the program result equals the native one.
1662fn rn_htrap o uint w ulong to void
1663if eq w 123
1664e = bitcast long rn_a o 2
1665if or lt e 0 gt e 65535
1666exit 103
1667end
1668end
1669if eq w 220
1670if lt rn_a o 4 12
1671exit 103
1672end
1673end
1674if eq w 221
1675n = rn_a o 3
1676if or ne and n 7 0 gt n 8192
1677exit 103
1678end
1679end
1680if eq w 116
1681if lt rn_a o 4 4
1682exit 103
1683end
1684end
1685if eq w 128
1686if lt rn_a o 3 8
1687exit 103
1688end
1689end
1690if or eq w 124 eq w 198
1691rn_pkt o 1 0
1692end
1693if eq w 233
1694rn_pkt o 1 0
1695rn_pkt o 4 2
1696end
1697if eq w 142
1698rn_pkt o 1 1
1699end
1700end
1701fn rn_hargs o uint w ulong to void
1702base = get r_cfg 1
1703count = add sub ulong argcnt base 1
1704if eq w 121
1705rn_hr o long count
1706ret
1707end
1708if eq w 136
1709rn_hr o envcnt
1710ret
1711end
1712rn_ext o 2 rn_a o 3 rn_a o 4
1713idx = rn_a o 1
1714p = rn_buf o 2
1715n = rn_a o 4
1716if eq w 137
1717rn_hr o envread idx r_m p n
1718ret
1719end
1720if ge idx count
1721exit 103
1722end
1723real = ulong 1
1724if ne idx 0
1725real = add base sub idx 1
1726end
1727rn_hr o argread real r_m p n
1728end
1729fn rn_hio o uint w ulong to void
1730fd = rn_fget o 1
1731rn_ext o 2 rn_a o 3 rn_a o 4
1732if lt fd 0
1733rn_hr o -9
1734ret
1735end
1736vfd = rn_a o 1
1737if eq get r_own vfd 0
1738if eq w 53
1739if ne vfd 0
1740rn_href o 119
1741end
1742else
1743if eq w 54
1744if and ne vfd 1 ne vfd 2
1745rn_href o 119
1746end
1747else
1748rn_href o 119
1749end
1750end
1751end
1752p = rn_buf o 2
1753n = rn_a o 4
1754if eq w 53
1755rn_hr o read fd r_m p n
1756ret
1757end
1758if eq w 54
1759rn_hr o write fd r_m p n
1760ret
1761end
1762pos = bitcast long rn_a o 5
1763if eq w 161
1764rn_hr o pread fd r_m p n pos
1765ret
1766end
1767rn_hr o pwrite fd r_m p n pos
1768end
1769fn rn_hfd o uint w ulong to void
1770if eq w 117
1771note fstat traps 3 natively on an output under 128 bytes before any kernel call, so ngrun gives that
1772note trap before its descriptor checks.
1773if lt rn_a o 4 128
1774exit 103
1775end
1776end
1777fd = rn_fget o 1
1778if lt fd 0
1779rn_hr o -9
1780ret
1781end
1782v = rn_a o 1
1783if eq w 100
1784put r_fd v -1
1785put r_own v 0
1786rn_hr o fclose fd
1787ret
1788end
1789if eq get r_own v 0
1790rn_href o 119
1791end
1792if eq w 101
1793rn_hr o fsync fd
1794ret
1795end
1796if eq w 156
1797rn_hr o fdsync fd
1798ret
1799end
1800if eq w 102
1801mode = int bitcast long rn_a o 2
1802rn_hr o fchmod fd mode
1803ret
1804end
1805if eq w 103
1806pos = bitcast long rn_a o 2
1807whence = int bitcast long rn_a o 3
1808rn_hr o fseek fd pos whence
1809ret
1810end
1811if eq w 139
1812rn_hr o ftrunc fd bitcast long rn_a o 2
1813ret
1814end
1815if eq w 153
1816rn_hr o fchdir fd
1817ret
1818end
1819if eq w 197
1820op = int bitcast long rn_a o 2
1821if and ne op 8 eq and op 4 0
1822rn_href o 121
1823end
1824rn_hr o flock fd op
1825ret
1826end
1827rn_ext o 2 rn_a o 3 rn_a o 4
1828p = rn_buf o 2
1829n = rn_a o 4
1830if eq w 113
1831rn_hr o getdnts fd r_m p n
1832ret
1833end
1834rn_hr o fstat fd r_m p n
1835end
1836note chmod, faccess, stat, truncate and chown follow a final link natively, so a link another process
1837note plants in the scratch tree would take them outside it. ngrun opens the name O_PATH|O_NOFOLLOW|O_CLOEXEC
1838note (AArch64 2097152, 32768, 524288), answers a link with -40 (ELOOP) as a NOFOLLOW open does, and gives
1839note the native word /proc/self/fd/N, which names the inode that open found. rn_nofol returns that
1840note descriptor or -errno; rn_ppath writes /proc/self/fd/N into r_pp and returns its length.
1841fn rn_nofol p ulong n ulong to long
1842flags = or int 2097152 or int 32768 int 524288
1843fd = fopen r_m p n flags 0
1844if lt fd 0
1845ret fd
1846end
1847k = fstat int fd r_pst 0 len r_pst
1848if lt k 0
1849cl = fclose int fd
1850ret k
1851end
1852note st_mode is bytes 16..19 of the AArch64 stat structure; S_IFMT 0xF000 and S_IFLNK 0xA000 are the
1853note high nibble of byte 17.
1854if eq and get r_pst 17 240 160
1855cl = fclose int fd
1856ret -40
1857end
1858ret fd
1859end
1860fn rn_ppath fd long to ulong
1861for j ulong len r_ppre bound 16
1862put r_pp j get r_ppre j
1863end
1864i = ulong len r_ppre
1865v = ulong fd
1866d = ulong 1
1867while ge div v d 10 bound 20
1868d = mul d 10
1869end
1870while gt d 0 bound 20
1871put r_pp i uchar add 48 rem div v d 10
1872i = add i 1
1873d = div d 10
1874end
1875ret i
1876end
1877note The kernel's EINVAL checks that precede the lookup (faccess mode outside 0..7, truncate length below
1878note 0) come first, as natively; then the name is opened without following a link (rn_nofol).
1879fn rn_hnofol o uint w ulong p ulong n ulong to void
1880a = bitcast long rn_a o 4
1881if and eq w 144 or lt a 0 gt a 7
1882rn_hr o -22
1883ret
1884end
1885if and eq w 150 lt a 0
1886rn_hr o -22
1887ret
1888end
1889outp = ulong 0
1890if eq w 140
1891rn_ext o 4 rn_a o 5 rn_a o 6
1892outp = rn_buf o 4
1893end
1894fd = rn_nofol p n
1895if lt fd 0
1896rn_hr o fd
1897ret
1898end
1899m = rn_ppath fd
1900k = long 0
1901if eq w 140
1902k = stat r_pp 0 m r_m outp rn_a o 6
1903end
1904if eq w 143
1905k = chmod r_pp 0 m int a
1906end
1907if eq w 144
1908k = faccess r_pp 0 m int a
1909end
1910if eq w 150
1911k = truncate r_pp 0 m a
1912end
1913if eq w 151
1914k = chown r_pp 0 m int a int bitcast long rn_a o 5
1915end
1916cl = fclose int fd
1917rn_hr o k
1918end
1919fn rn_hpath o uint w ulong to void
1920if or or eq w 133 eq w 140 eq w 149
1921note stat, lstat and readlnk trap 3 natively on an empty path, and stat and lstat on an output under
1922note 128 bytes, before any kernel call and with or without --scratch, so ngrun gives that trap first.
1923if eq rn_a o 3 0
1924exit 103
1925end
1926if and ne w 133 lt rn_a o 6 128
1927exit 103
1928end
1929end
1930p = rn_path o 1
1931n = rn_a o 3
1932if eq w 110
1933flags = int bitcast long rn_a o 4
1934mode = int bitcast long rn_a o 5
1935note NOFOLLOW prevents an open through a link even if another same-user process plants one.
1936flags = or flags 32768
1937rn_hr o rn_fadd o fopen r_m p n flags mode
1938ret
1939end
1940if eq w 111
1941rn_hr o unlink r_m p n
1942ret
1943end
1944if eq w 112
1945mode = int bitcast long rn_a o 4
1946rn_hr o makedir r_m p n mode
1947ret
1948end
1949if eq w 126
1950rn_hr o rmdir r_m p n
1951ret
1952end
1953if eq w 138
1954note chdir through an O_PATH|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC descriptor (AArch64 2097152, 16384, 32768,
1955note 524288): a final link another process plants is not followed, so the working directory stays inside
1956note the scratch tree. Search permission is checked by fchdir as chdir checks it.
1957flags = or int 2097152 or int 16384 or int 32768 int 524288
1958fd = fopen r_m p n flags 0
1959if lt fd 0
1960rn_hr o fd
1961ret
1962end
1963k = fchdir int fd
1964cl = fclose int fd
1965rn_hr o k
1966ret
1967end
1968if or or or eq w 140 eq w 143 or eq w 144 eq w 150 eq w 151
1969rn_hnofol o w p n
1970ret
1971end
1972rn_ext o 4 rn_a o 5 rn_a o 6
1973outp = rn_buf o 4
1974cap = rn_a o 6
1975if eq w 133
1976rn_hr o readlnk r_m p n r_m outp cap
1977ret
1978end
1979rn_hr o lstat r_m p n r_m outp cap
1980end
1981fn rn_safehost o uint to void
1982w = get i_oim o
1983rn_htrap o w
1984if or eq w 199 eq w 200
1985ret
1986end
1987if eq w 109
1988rn_hr o monons
1989ret
1990end
1991if eq w 130
1992rn_hr o realns
1993ret
1994end
1995if or or eq w 121 eq w 122 or eq w 136 eq w 137
1996rn_hargs o w
1997ret
1998end
1999if or or eq w 53 eq w 54 or eq w 161 eq w 162
2000rn_hio o w
2001ret
2002end
2003if or eq w 145 eq w 146
2004if eq w 145
2005rn_hr o getuid
2006else
2007rn_hr o geteuid
2008end
2009ret
2010end
2011if or eq w 147 eq w 148
2012if eq w 147
2013rn_hr o getgid
2014else
2015rn_hr o getegid
2016end
2017ret
2018end
2019if eq w 235
2020rn_href o 116
2021end
2022if or and ge w 166 le w 177 and ge w 228 le w 232
2023rn_href o 117
2024end
2025if or and ge w 104 le w 106 or eq w 114 eq w 115
2026rn_href o 118
2027end
2028if or or eq w 118 eq w 119 or eq w 123 eq w 131
2029rn_href o 118
2030end
2031if or eq w 134 or eq w 165 or and ge w 220 le w 222 eq w 224
2032rn_href o 118
2033end
2034if or eq w 135 eq w 120
2035if eq w 120
2036note sleepns traps 3 natively on a negative interval (OMEGESE s10): a deterministic result, not a wait.
2037if lt bitcast long rn_a o 1 0
2038exit 103
2039end
2040if eq rn_a o 1 0
2041rn_hr o sleepns 0
2042ret
2043end
2044end
2045rn_href o 120
2046end
2047note the library words of `target macos` call code outside the program: refused as class 117
2048if and ge w 241 le w 243
2049rn_href o 117
2050end
2051if or or eq w 107 eq w 108 or eq w 116 eq w 124
2052rn_href o 115
2053end
2054if or or eq w 127 eq w 128 or eq w 129 eq w 154
2055rn_href o 115
2056end
2057if or and ge w 158 le w 160 or eq w 163 or eq w 198 eq w 233
2058rn_href o 115
2059end
2060note cpuset, cpuget, setprio and getprio read or change the scheduling state of the process (115);
2061note cpunow gives the CPU the thread ran on, which may change between calls (120). IR.md s7.11.
2062if or and ge w 236 le w 237 or eq w 239 eq w 240
2063rn_href o 115
2064end
2065if eq w 238
2066rn_href o 120
2067end
2068if or eq w 142 eq w 164
2069rn_href o 121
2070end
2071if or or eq w 100 eq w 101 or eq w 102 eq w 103
2072rn_hfd o w
2073ret
2074end
2075if or or eq w 113 eq w 117 or eq w 139 eq w 153
2076rn_hfd o w
2077ret
2078end
2079if or eq w 156 eq w 197
2080rn_hfd o w
2081ret
2082end
2083if or and ge w 110 le w 112 or eq w 126 eq w 138
2084rn_hpath o w
2085ret
2086end
2087if or or eq w 133 eq w 140 or eq w 143 eq w 144
2088rn_hpath o w
2089ret
2090end
2091if or eq w 149 or eq w 150 eq w 151
2092rn_hpath o w
2093ret
2094end
2095if eq w 132
2096if eq get r_cfg 2 0
2097rn_href o 119
2098end
2099rn_ext o 1 rn_a o 2 rn_a o 3
2100rn_hr o cwd r_m rn_buf o 1 rn_a o 3
2101ret
2102end
2103if eq w 155
2104if eq get r_cfg 2 0
2105rn_href o 119
2106end
2107rn_hr o umask int bitcast long rn_a o 1
2108ret
2109end
2110if eq w 157
2111rn_ext o 1 rn_a o 2 rn_a o 3
2112p = rn_buf o 1
2113flags = int bitcast long rn_a o 4
2114rn_hr o rn_fadd o memfd r_m p rn_a o 3 flags
2115ret
2116end
2117if or eq w 125 eq w 141
2118p = rn_pair o
2119if eq w 125
2120flags = int bitcast long rn_a o 4
2121rn_hr o renames r_m p rn_a o 3 flags
2122else
2123rn_hr o link r_m p rn_a o 3
2124end
2125ret
2126end
2127rn_href o 122
2128end
2129
2130
2131note Pool storage is separate from invocation frames. The combined live byte budget stays 16 MiB.
2132note References never escape a pool op; compaction after growth cannot invalidate an IR value.
2133fn rn_pgrow g ulong n ulong to long
2134old = get r_pl g
2135if le n old
2136ret 0
2137end
2138sz = ir_tsize ar_u 87 g
2139if gt n div 9223372036854775807 sz
2140ret -22
2141end
2142delta = mul sub n old sz
2143used = get r_ps 0
2144if gt delta sub sub 16777216 get r_s 2 used
2145ret -12
2146end
2147cut = add get r_pb g mul old sz
2148rn_pmove g cut delta
2149put r_pl g n
2150put r_ps 0 add used delta
2151ret 0
2152end
2153
2154fn rn_pio o uint op uint g ulong to void
2155off = rn_a o 2
2156n = rn_a o 3
2157ln = get r_pl g
2158if gt off ln
2159exit 103
2160end
2161if gt n sub ln off
2162exit 103
2163end
2164fd = rn_fget o 1
2165if lt fd 0
2166rn_hr o -9
2167ret
2168end
2169vfd = rn_a o 1
2170if eq get r_own vfd 0
2171if eq op 115
2172if ne vfd 0
2173rn_href o 119
2174end
2175else
2176if and ne vfd 1 ne vfd 2
2177rn_href o 119
2178end
2179end
2180end
2181p = add get r_pb g off
2182if eq op 115
2183rn_hr o read fd r_pm p n
2184else
2185rn_hr o write fd r_pm p n
2186end
2187end
2188
2189fn rn_pool o uint op uint to void
2190g = get i_oim o
2191if eq op 111
2192rn_hr o rn_pgrow g rn_a o 1
2193ret
2194end
2195if eq op 112
2196rn_r o get r_pl g
2197ret
2198end
2199if ge op 115
2200rn_pio o op g
2201ret
2202end
2203ix = rn_a o 1
2204if ge ix get r_pl g
2205exit 103
2206end
2207t = ar_u 87 g
2208sz = ir_tsize t
2209p = add get r_pb g mul ix sz
2210if eq op 114
2211rn_pst p sz rn_a o 2
2212ret
2213end
2214v = rn_pld p sz
2215if rn_isi t
2216v = rn_cn t v
2217end
2218rn_r o v
2219end
2220
2221fn rn_pmove g ulong cut ulong delta ulong to void
2222used = get r_ps 0
2223for j ulong sub used cut bound 16777216
2224src = sub sub used 1 j
2225put r_pm add src delta get r_pm src
2226end
2227for j ulong delta bound 16777216
2228put r_pm add cut j 0
2229end
2230for j ulong get i_n 2 bound 65536
2231h = add j 1
2232if and gt h g eq ar_c 88 h 2
2233put r_pb h add get r_pb h delta
2234end
2235end
2236end
2237
2238
2239fn rn_pst p ulong sz ulong v ulong to void
2240for j ulong sz bound 8
2241put r_pm add p j uchar and shr v mul j 8 255
2242end
2243end
2244fn rn_pld p ulong sz ulong to ulong
2245v = ulong 0
2246for j ulong sz bound 8
2247v = or v shl ulong get r_pm add p j mul j 8
2248end
2249ret v
2250end