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 / replay / gate_replay.om

1267 lines · 25247 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/replay/gate_replay.om: Standalone Pure-OM-2 Verification Gate for Stage 2
3note (Pillar 4: Reversible & Counterfactual Time-Travel Execution).
4note
5note Link order (program mode; ir.om provides om2 at line 1):
6note cat seed/ng/src/ir.om seed/ng/sha256.om \
7note seed/ng/replay/tok_journal.om seed/ng/replay/slab_ckpt.om \
8note seed/ng/replay/cf_fork.om seed/ng/replay/state_stream.om \
9note seed/ng/replay/gate_replay.om | omc > gate_replay
10note
11note Usage:
12note gate_replay [NGRUN_BIN SCRATCH_DIR]
13
14global gr_dig uchar 24
15global gr_cnt ulong 4
16note gr_cnt: 0 checks, 1 neg_controls, 2 live_checks, 3 live_negs
17
18global gr_ngp uchar 2048
19global gr_ngl ulong 1
20global gr_scp uchar 2048
21global gr_scl ulong 1
22global gr_pa uchar 4096
23global gr_pl ulong 1
24global gr_pa2 uchar 4096
25global gr_pl2 ulong 1
26global gr_av uchar 16384
27global gr_an ulong 2
28global gr_st uchar 4
29global gr_out uchar 65536
30global gr_err uchar 65536
31global gr_olen ulong 2
32global gr_ref_dig uchar 128
33global gr_saved_j uchar 4096
34global gr_h uint 32
35
36text gr_m_hdr "=== OMEGA NG STAGE 2 (REVERSIBLE & COUNTERFACTUAL REPLAY) GATE ===\n"
37text gr_m_s1 " [Stage 1] OTKJ v1 tok-journal + streaming + 6 neg controls: PASS (entries="
38text gr_m_s2 " [Stage 2] SLAB checkpoint rewind + bit-exact parity + 2 neg controls: PASS (ckpts="
39text gr_m_s3 " [Stage 3] Counterfactual 18-branch fork matrix: PASS (branches=18 diverged="
40text gr_m_s4 " [Stage 4] Semantic state-stream delta codec + 3 neg controls: PASS (raw="
41text gr_m_s5 " [Stage 5] Live ngrun trap-to-cause rewind + OTKJ verify + regression: PASS (live="
42text gr_m_pass "GATE-REPLAY PASS checks="
43text gr_m_neg " neg_controls="
44text gr_m_live " live_checks="
45text gr_m_lneg " live_neg="
46text gr_m_fail "GATE-REPLAY FAIL code="
47text gr_m_nl "\n"
48text gr_m_cp ")\n"
49text gr_m_bytes " bytes="
50text gr_m_ratio "x)\n"
51text gr_m_stream " stream="
52text gr_m_comp " ratio="
53
54text gr_f_j1 "/stage1_mem.otkj"
55text gr_f_j2 "/stage1_stream.otkj"
56text gr_f_b "/prog_bounds.ir"
57text gr_f_d "/prog_divz.ir"
58text gr_f_a "/prog_addovf.ir"
59text gr_f_t "/prog_trap.ir"
60text gr_f_ok "/prog_ok.ir"
61text gr_f_jb "/bounds.otkj"
62text gr_f_jd "/divz.otkj"
63text gr_f_ja "/addovf.otkj"
64text gr_f_jt "/trap.otkj"
65text gr_f_jok "/ok.otkj"
66text gr_f_out "/child.out"
67text gr_f_err "/child.err"
68text gr_f_in "/child.in"
69
70text gr_a_rw "--rewind"
71text gr_a_ce "--ckpt-every"
72text gr_a_4 "4"
73text gr_a_jn "--journal"
74
75text gr_k_pfx "ngrun-rewind: trap="
76text gr_k_t3 "trap=3 "
77text gr_k_t2 "trap=2 "
78text gr_k_t1 "trap=1 "
79text gr_k_t9 "trap=9 "
80text gr_k_par "replay_parity=EXACT"
81text gr_k_op43 "cause_op=43 "
82text gr_k_v99 "cause_val=99 "
83text gr_k_v0 "cause_val=0 "
84text gr_k_vmax "cause_val=2147483647 "
85text gr_k_v5000 "cause_val=5000 "
86text gr_k_ck0 "ckpt_step=0 "
87
88fn gr_wrt s view uchar * to void
89r = write 1 s 0 len s
90end
91
92fn gr_num v ulong to void
93x = v
94p = ulong 24
95for i ulong 21
96p = sub p 1
97put gr_dig p uchar add 48 rem x 10
98x = div x 10
99if eq x 0
100r = write 1 gr_dig p sub 24 p
101ret
102end
103end
104end
105
106fn gr_fail code ulong to int
107gr_wrt gr_m_fail
108gr_num code
109gr_wrt gr_m_nl
110ret int code
111end
112
113fn gr_chk n ulong to void
114put gr_cnt 0 add get gr_cnt 0 n
115end
116
117fn gr_neg to void
118put gr_cnt 1 add get gr_cnt 1 1
119end
120
121fn gr_spath suf view uchar * dst ulong to void
122n = get gr_scl 0
123m = len suf
124if eq dst 1
125for i ulong n bound 2048
126put gr_pa i get gr_scp i
127end
128for j ulong m bound 128
129put gr_pa add n j get suf j
130end
131put gr_pl 0 add n m
132else
133if eq dst 2
134for i ulong n bound 2048
135put gr_pa2 i get gr_scp i
136end
137for j ulong m bound 128
138put gr_pa2 add n j get suf j
139end
140put gr_pl2 0 add n m
141else
142for i ulong n bound 2048
143put tj_path i get gr_scp i
144end
145for j ulong m bound 128
146put tj_path add n j get suf j
147end
148put tj_plen 0 add n m
149end
150end
151end
152
153note ---- Stage 1: OTKJ v1 Hash-Chained tok Journal + Growable Pool + Streaming + 6 Neg Controls ----
154fn gr_stage1_disk to int
155gr_spath gr_f_j1 3
156if ne tkj_write_path TKJ_OK
157ret gr_fail 17
158end
159if ne tkj_read_path TKJ_OK
160ret gr_fail 18
161end
162if or ne tkj_count 1200 ne tkj_entry_u64 777 1 mul ulong 777 17
163ret gr_fail 19
164end
165tkj_set_alarm_bytes 4096
166gr_spath gr_f_j2 3
167if ne tkj_stream_open_path TKJ_OK
168ret gr_fail 20
169end
170for i ulong 64
171tkj_pack_u64x4 i add 9000 i 42 7
172if ne tkj_stream_append TKJ_OP_HOST 1 i 32 TKJ_OK
173ret gr_fail 21
174end
175end
176if ne tkj_stream_close TKJ_OK
177ret gr_fail 22
178end
179if ne tkj_alarm_tripped 1
180ret gr_fail 23
181end
182if ne tkj_read_path TKJ_OK
183ret gr_fail 24
184end
185if or ne tkj_count 64 ne tkj_entry_u64 63 1 9063
186ret gr_fail 25
187end
188gr_chk 5
189ret 0
190end
191
192fn gr_stage1_negs to int
193if ne tkj_reset TKJ_OK
194ret gr_fail 26
195end
196for i ulong 8
197tkj_pack_u64x4 i add 100 i 55 99
198if ne tkj_append TKJ_OP_STORE 2 i 32 TKJ_OK
199ret gr_fail 27
200end
201end
202sz = tkj_bytes
203if ne tkj_verify_buf sz TKJ_OK
204ret gr_fail 28
205end
206for j ulong sz bound 4096
207b = get tj_buf j
208put gr_saved_j j b
209end
210
211note NC-J1: Truncated frame -> TKJ_ERR_TRUNC (3)
212if ne tkj_verify_buf sub sz 1 TKJ_ERR_TRUNC
213ret gr_fail 29
214end
215gr_neg
216
217note NC-J2: 1-bit flip in payload_bytes of entry 3 -> TKJ_ERR_HASH (8)
218po = get tj_epoff 3
219put tj_buf po xor get tj_buf po uchar 1
220if ne tkj_verify_buf sz TKJ_ERR_HASH
221ret gr_fail 30
222end
223put tj_buf po get gr_saved_j po
224gr_neg
225
226note NC-J3: 1-bit flip in entry_hash of entry 4 -> TKJ_ERR_HASH (8)
227eh = add get tj_eoff 4 85
228put tj_buf eh xor get tj_buf eh uchar 1
229if ne tkj_verify_buf sz TKJ_ERR_HASH
230ret gr_fail 31
231end
232put tj_buf eh get gr_saved_j eh
233gr_neg
234
235note NC-J4: 1-bit flip in prev_hash chain link of entry 5 -> TKJ_ERR_CHAIN (7)
236ph = add get tj_eoff 5 53
237put tj_buf ph xor get tj_buf ph uchar 1
238if ne tkj_verify_buf sz TKJ_ERR_CHAIN
239ret gr_fail 32
240end
241put tj_buf ph get gr_saved_j ph
242gr_neg
243
244note NC-J5: Duplicate/reordered seq in entry 2 -> TKJ_ERR_SEQ (5)
245so = get tj_eoff 2
246put tj_buf so uchar 1
247if ne tkj_verify_buf sz TKJ_ERR_SEQ
248ret gr_fail 33
249end
250put tj_buf so get gr_saved_j so
251gr_neg
252
253note NC-J6: Bad magic / trailing byte -> TKJ_ERR_MAGIC (1) & TKJ_ERR_TRAILING (4)
254put tj_buf 0 uchar 88
255if ne tkj_verify_buf sz TKJ_ERR_MAGIC
256ret gr_fail 34
257end
258put tj_buf 0 get gr_saved_j 0
259if ne tkj_ensure_buf add sz 1 TKJ_OK
260ret gr_fail 35
261end
262put tj_buf sz uchar 0
263if ne tkj_verify_buf add sz 1 TKJ_ERR_TRAILING
264ret gr_fail 36
265end
266gr_neg
267gr_chk 6
268ret 0
269end
270
271fn gr_stage1 has_scr bool to int
272if ne tkj_reset TKJ_OK
273ret gr_fail 10
274end
275tkj_set_alarm_bytes 65536
276for i ulong 1200
277k = add rem i 12 1
278tkj_pack_u64x4 i mul i 17 add i 1000 xor i 0x55aa
279if ne tkj_append k rem i 8 rem i 16 32 TKJ_OK
280ret gr_fail 11
281end
282end
283if ne tkj_count 1200
284ret gr_fail 12
285end
286if ne tkj_alarm_tripped 1
287ret gr_fail 13
288end
289tot = tkj_bytes
290if lt tot 140000
291ret gr_fail 14
292end
293if ne tkj_verify_buf tot TKJ_OK
294ret gr_fail 15
295end
296if ne tkj_entry_u64 1199 1 mul ulong 1199 17
297ret gr_fail 16
298end
299gr_chk 7
300if has_scr
301rd = gr_stage1_disk
302if ne rd 0
303ret rd
304end
305end
306rn = gr_stage1_negs
307if ne rn 0
308ret rn
309end
310gr_wrt gr_m_s1
311gr_num 1200
312gr_wrt gr_m_bytes
313gr_num tot
314gr_wrt gr_m_cp
315ret 0
316end
317
318note ---- Stage 2: Periodic SLAB Checkpoints + Bit-Exact Forward vs. Backward Rewind Parity ----
319fn gr_s2_write_slot off ulong r0 ulong r1 ulong r2 ulong step ulong to void
320for j ulong 8
321s = mul j 8
322put sc_slab add off j uchar and shr r0 s 255
323put sc_slab add add off 8 j uchar and shr r1 s 255
324put sc_slab add add off 16 j uchar and shr r2 s 255
325put sc_slab add add off 24 j uchar and shr step s 255
326end
327end
328
329fn gr_s2_step step ulong to void
330put sc_vm 0 step
331put sc_vm 1 rem step 4
332put sc_vm 2 rem step 7
333put sc_vm 3 add get sc_vm 3 1
334r0 = add get sc_regs 0 add mul step 13 7
335r1 = xor get sc_regs 1 mul step 2654435761
336r2 = add get sc_regs 2 rem r1 97
337put sc_regs 0 r0
338put sc_regs 1 r1
339put sc_regs 2 r2
340put sc_regs 3 step
341gr_s2_write_slot mul rem step 128 64 r0 r1 r2 step
342end
343
344fn gr_s2_save_ref slot ulong to void
345off = mul slot 32
346for j ulong 32
347put gr_ref_dig add off j get sh_dig j
348end
349end
350
351fn gr_s2_chk_ref slot ulong to bool
352off = mul slot 32
353for j ulong 32
354if ne get sh_dig j get gr_ref_dig add off j
355ret false
356end
357end
358ret true
359end
360
361fn gr_s2_rewind_check target ulong exp_ck_step ulong ref_slot ulong to bool
362if ne sck_restore_nearest target SCK_OK
363ret false
364end
365if ne sck_last_restored_step exp_ck_step
366ret false
367end
368s = add exp_ck_step 1
369while le s target bound 64
370gr_s2_step s
371s = add s 1
372end
373if ne sck_hash_live SCK_OK
374ret false
375end
376ret gr_s2_chk_ref ref_slot
377end
378
379fn gr_stage2 to int
380if ne sck_init 8 8192 4 4 SCK_OK
381ret gr_fail 40
382end
383if ne sck_take SCK_OK
384ret gr_fail 41
385end
386for i ulong 64
387step = add i 1
388gr_s2_step step
389if sck_should_take step
390if ne sck_take SCK_OK
391ret gr_fail 42
392end
393end
394if or eq step 23 or eq step 39 or eq step 55 eq step 64
395if ne sck_hash_live SCK_OK
396ret gr_fail 43
397end
398if eq step 23
399gr_s2_save_ref 0
400end
401if eq step 39
402gr_s2_save_ref 1
403end
404if eq step 55
405gr_s2_save_ref 2
406end
407if eq step 64
408gr_s2_save_ref 3
409end
410end
411end
412if ne sck_count 17
413ret gr_fail 44
414end
415if ge sck_data_bytes sck_raw_bytes
416ret gr_fail 45
417end
418if not gr_s2_rewind_check 55 52 2
419ret gr_fail 46
420end
421if not gr_s2_rewind_check 39 36 1
422ret gr_fail 47
423end
424if not gr_s2_rewind_check 23 20 0
425ret gr_fail 48
426end
427if not gr_s2_rewind_check 64 64 3
428ret gr_fail 49
429end
430gr_chk 7
431
432note NC-CK1: 1-bit tamper in checkpoint 5 digest -> SCK_ERR_DIGEST (3)
433sck_tamper_digest 5 7
434if ne sck_restore_id 5 SCK_ERR_DIGEST
435ret gr_fail 50
436end
437sck_tamper_digest 5 7
438if ne sck_restore_id 5 SCK_OK
439ret gr_fail 51
440end
441gr_neg
442
443note NC-CK2: 1-bit tamper in checkpoint 6 payload data -> SCK_ERR_DIGEST (3)
444sck_tamper_data 6 0
445if ne sck_restore_id 6 SCK_ERR_DIGEST
446ret gr_fail 52
447end
448sck_tamper_data 6 0
449gr_neg
450gr_chk 2
451
452gr_wrt gr_m_s2
453gr_num sck_count
454gr_wrt gr_m_bytes
455gr_num sck_data_bytes
456gr_wrt gr_m_cp
457ret 0
458end
459
460note ---- Stage 3: >= 16-Branch Counterfactual Fork Matrix ----
461fn gr_stage3 to int
462if ne cff_run_matrix18 CF_OK
463ret gr_fail 60
464end
465if ne get cf_br_cnt 0 18
466ret gr_fail 61
467end
468if or ne cff_rep_get 0 0 0 or ne cff_rep_get 0 15 1 ne cff_rep_get 0 9 8
469ret gr_fail 62
470end
471if or ne cff_rep_get 1 0 1 or ne cff_rep_get 1 1 12 ne cff_rep_get 1 2 1
472ret gr_fail 63
473end
474if or ne cff_rep_get 1 6 45000 or ne cff_rep_get 1 7 12000 ne cff_rep_get 1 15 0
475ret gr_fail 64
476end
477if or ne cff_rep_get 6 1 24 ne cff_rep_get 6 12 2
478ret gr_fail 65
479end
480for b ulong 5
481br = add b 7
482if or ne cff_rep_get br 0 1 or ne cff_rep_get br 1 12 ne cff_rep_get br 2 2
483ret gr_fail 66
484end
485if ne cff_rep_get br 9 8
486ret gr_fail 67
487end
488end
489if or ne cff_rep_get 14 1 22 or ne cff_rep_get 14 2 3 ne cff_rep_get 14 13 9
490ret gr_fail 68
491end
492if or ne cff_rep_get 17 1 28 or ne cff_rep_get 17 2 4 ne cff_rep_get 17 8 777
493ret gr_fail 69
494end
495div_cnt = ulong 0
496for br ulong 18
497if ne br 0
498if and eq cff_rep_get br 0 1 eq cff_rep_get br 15 0
499div_cnt = add div_cnt 1
500end
501end
502end
503if lt div_cnt 16
504ret gr_fail 70
505end
506if ne tkj_verify_buf tkj_bytes TKJ_OK
507ret gr_fail 71
508end
509gr_chk 18
510
511gr_wrt gr_m_s3
512gr_num div_cnt
513gr_wrt gr_m_cp
514ret 0
515end
516
517note ---- Stage 4: Compact Semantic State-Stream Delta Codec + 3 Fail-Closed Neg Controls ----
518fn gr_s4_fill_frame f ulong to void
519put ss_cur_regs 0 add f 1
520put ss_cur_regs 1 mul add f 1 1009
521put ss_cur_regs 2 xor get ss_cur_regs 2 mul add f 1 31337
522base_off = mul f 1024
523for j ulong 32
524v = uchar and add mul f 17 add j 3 255
525put ss_cur_slab add base_off j v
526end
527end
528
529fn gr_stage4 to int
530if ne sst_enc_init 16 65536 SST_OK
531ret gr_fail 80
532end
533for f ulong 64
534gr_s4_fill_frame f
535if ne sst_emit_frame mul f 4 SST_OK
536ret gr_fail 81
537end
538end
539s_bytes = sst_stream_bytes
540r_bytes = sst_raw_bytes
541if or ne sst_frame_count 64 lt r_bytes 4000000
542ret gr_fail 82
543end
544ratio = div r_bytes s_bytes
545if or gt s_bytes 16384 lt ratio 250
546ret gr_fail 83
547end
548if ne sst_decode_verify s_bytes SST_OK
549ret gr_fail 84
550end
551if ne sst_decoded_frames 64
552ret gr_fail 85
553end
554if not sst_verify_exact_match
555ret gr_fail 86
556end
557gr_chk 7
558
559note NC-SS1: Truncated OSTM frame -> SST_ERR_TRUNC (3)
560if ne sst_decode_verify sub s_bytes 1 SST_ERR_TRUNC
561ret gr_fail 87
562end
563gr_neg
564
565note NC-SS2: 1-bit tamper in delta payload byte -> SST_ERR_HASH (7)
566tpos = ulong 44
567orig = get ss_buf tpos
568put ss_buf tpos xor orig uchar 1
569if ne sst_decode_verify s_bytes SST_ERR_HASH
570ret gr_fail 88
571end
572put ss_buf tpos orig
573gr_neg
574
575note NC-SS3: Bad magic -> SST_ERR_MAGIC (1)
576m0 = get ss_buf 0
577put ss_buf 0 uchar 65
578if ne sst_decode_verify s_bytes SST_ERR_MAGIC
579ret gr_fail 89
580end
581put ss_buf 0 m0
582gr_neg
583gr_chk 3
584
585gr_wrt gr_m_s4
586gr_num r_bytes
587gr_wrt gr_m_stream
588gr_num s_bytes
589gr_wrt gr_m_comp
590gr_num ratio
591gr_wrt gr_m_ratio
592ret 0
593end
594
595note ---- Stage 5: Live ngrun Subprocess Trap-to-Cause Backwards Stepping & Regression ----
596fn gr_write_ir suf view uchar * to bool
597n = ir_wr
598gr_spath suf 1
599fd = fopen gr_pa 0 get gr_pl 0 577 420
600if lt fd 0
601ret false
602end
603ok = ar_out ulong fd 1 0 n
604r = fclose int fd
605ret eq ok 1
606end
607
608note Helper: append standard 12-iteration loop from gr_h[0]=f, gr_h[1]=b0, gr_h[2]=entry_tok,
609note gr_h[3]=c0, gr_h[4]=c1, gr_h[5]=n_iters; writes exit block into gr_h[6].
610fn gr_emit_loop to void
611f = get gr_h 0
612b0 = get gr_h 1
613b1 = ir_blk f
614bi = ir_bpar b1 5
615b2 = ir_blk f
616b3 = ir_blk f
617o = ir_op b0 60 0 0 3
618ir_set o 0 b1
619ir_set o 1 get gr_h 2
620ir_set o 2 get gr_h 3
621o = ir_op b1 32 1 0 2
622ir_set o 0 bi
623ir_set o 1 get gr_h 5
624lt1 = ir_res o
625o = ir_op b1 61 0 0 4
626ir_set o 0 ir_btok b1
627ir_set o 1 lt1
628ir_set o 2 b2
629ir_set o 3 b3
630c32 = ir_res ir_op b2 1 5 32 0
631o = ir_op b2 81 0 4 3
632ir_set o 0 ir_btok b2
633ir_set o 1 bi
634ir_set o 2 c32
635tk = ir_otok o
636o = ir_op b2 10 5 0 2
637ir_set o 0 bi
638ir_set o 1 get gr_h 4
639bi2 = ir_res o
640o = ir_op b2 60 0 0 3
641ir_set o 0 b1
642ir_set o 1 tk
643ir_set o 2 bi2
644put gr_h 6 b3
645end
646
647note Program 1 (chk.bounds): store 99 at @g0[3], loop 12x, load @g0[3], add 1, chk.bounds -> exit 103
648fn gr_build_prog_bounds to void
649ir_init
650ta = ir_tarr 5 8
651tr = ir_tref ta 1 0
652g0 = ir_glob ta 0
653f = ir_fnew 2 0 8
654b0 = ir_blk f
655put gr_h 0 f
656put gr_h 1 b0
657ga = ir_res ir_op b0 52 tr ulong g0 0
658put gr_h 7 ga
659put gr_h 3 ir_res ir_op b0 1 5 0 0
660put gr_h 4 ir_res ir_op b0 1 5 1 0
661c3 = ir_res ir_op b0 1 5 3 0
662put gr_h 8 c3
663put gr_h 5 ir_res ir_op b0 1 5 12 0
664c99 = ir_res ir_op b0 1 5 99 0
665o = ir_op b0 79 0 3 4
666ir_set o 0 ir_ftok f
667ir_set o 1 ga
668ir_set o 2 c3
669ir_set o 3 get gr_h 4
670tk = ir_otok o
671o = ir_op b0 43 0 0 4
672ir_set o 0 tk
673ir_set o 1 ga
674ir_set o 2 c3
675ir_set o 3 c99
676put gr_h 2 ir_otok o
677gr_emit_loop
678b3 = get gr_h 6
679o = ir_op b3 79 0 3 4
680ir_set o 0 ir_btok b3
681ir_set o 1 get gr_h 7
682ir_set o 2 get gr_h 8
683ir_set o 3 get gr_h 4
684tk = ir_otok o
685o = ir_op b3 42 5 0 3
686ir_set o 0 tk
687ir_set o 1 get gr_h 7
688ir_set o 2 get gr_h 8
689ld = ir_res o
690o = ir_op b3 10 5 0 2
691ir_set o 0 ld
692ir_set o 1 get gr_h 4
693bad = ir_res o
694o = ir_op b3 79 0 3 4
695ir_set o 0 tk
696ir_set o 1 get gr_h 7
697ir_set o 2 bad
698ir_set o 3 get gr_h 4
699tk = ir_otok o
700z = ir_res ir_op b3 1 8 0 0
701o = ir_op b3 63 0 0 2
702ir_set o 0 tk
703ir_set o 1 z
704end
705
706fn gr_build_divz_helper to void
707fh = ir_fnew 0 0 8
708pd = ir_fpar fh 9
709bh = ir_blk fh
710o = ir_op bh 74 0 2 2
711ir_set o 0 ir_ftok fh
712ir_set o 1 pd
713tk = ir_otok o
714zh = ir_res ir_op bh 1 8 0 0
715o = ir_op bh 63 0 0 2
716ir_set o 0 tk
717ir_set o 1 zh
718put gr_h 9 fh
719end
720
721fn gr_build_divz_tail to void
722b3 = get gr_h 6
723o = ir_op b3 79 0 3 4
724ir_set o 0 ir_btok b3
725ir_set o 1 get gr_h 7
726ir_set o 2 get gr_h 4
727ir_set o 3 get gr_h 4
728tk = ir_otok o
729o = ir_op b3 42 9 0 3
730ir_set o 0 tk
731ir_set o 1 get gr_h 7
732ir_set o 2 get gr_h 4
733ld = ir_res o
734o = ir_op b3 64 8 0 3
735ir_set o 0 tk
736ir_set o 1 get gr_h 9
737ir_set o 2 ld
738tk = ir_otok o
739res = ir_res o
740o = ir_op b3 63 0 0 2
741ir_set o 0 tk
742ir_set o 1 res
743end
744
745note Program 2 (chk.divz across call): store 0 at @g0[1], loop 10x, load @g0[1], call f0(v) -> exit 102
746fn gr_build_prog_divz to void
747ir_init
748ta = ir_tarr 9 4
749tr = ir_tref ta 1 0
750g0 = ir_glob ta 0
751gr_build_divz_helper
752fm = ir_fnew 2 0 8
753b0 = ir_blk fm
754put gr_h 0 fm
755put gr_h 1 b0
756ga = ir_res ir_op b0 52 tr ulong g0 0
757put gr_h 7 ga
758put gr_h 3 ir_res ir_op b0 1 5 0 0
759put gr_h 4 ir_res ir_op b0 1 5 1 0
760put gr_h 5 ir_res ir_op b0 1 5 10 0
761z64 = ir_res ir_op b0 1 9 0 0
762o = ir_op b0 79 0 3 4
763ir_set o 0 ir_ftok fm
764ir_set o 1 ga
765ir_set o 2 get gr_h 4
766ir_set o 3 get gr_h 4
767tk = ir_otok o
768o = ir_op b0 43 0 0 4
769ir_set o 0 tk
770ir_set o 1 ga
771ir_set o 2 get gr_h 4
772ir_set o 3 z64
773put gr_h 2 ir_otok o
774gr_emit_loop
775gr_build_divz_tail
776end
777
778fn gr_build_addovf_tail to void
779b3 = get gr_h 6
780o = ir_op b3 79 0 3 4
781ir_set o 0 ir_btok b3
782ir_set o 1 get gr_h 7
783ir_set o 2 get gr_h 8
784ir_set o 3 get gr_h 4
785tk = ir_otok o
786o = ir_op b3 42 8 0 3
787ir_set o 0 tk
788ir_set o 1 get gr_h 7
789ir_set o 2 get gr_h 8
790ld = ir_res o
791i1 = ir_res ir_op b3 1 8 1 0
792o = ir_op b3 70 0 1 3
793ir_set o 0 tk
794ir_set o 1 ld
795ir_set o 2 i1
796tk = ir_otok o
797o = ir_op b3 63 0 0 2
798ir_set o 0 tk
799ir_set o 1 ld
800end
801
802note Program 3 (chk.add overflow): store 2147483647 at @g0[2], loop 10x, load, chk.add +1 -> exit 101
803fn gr_build_prog_addovf to void
804ir_init
805ta = ir_tarr 8 4
806tr = ir_tref ta 1 0
807g0 = ir_glob ta 0
808fm = ir_fnew 2 0 8
809b0 = ir_blk fm
810put gr_h 0 fm
811put gr_h 1 b0
812ga = ir_res ir_op b0 52 tr ulong g0 0
813put gr_h 7 ga
814put gr_h 3 ir_res ir_op b0 1 5 0 0
815put gr_h 4 ir_res ir_op b0 1 5 1 0
816c2 = ir_res ir_op b0 1 5 2 0
817put gr_h 8 c2
818put gr_h 5 ir_res ir_op b0 1 5 10 0
819cmax = ir_res ir_op b0 1 8 2147483647 0
820o = ir_op b0 79 0 3 4
821ir_set o 0 ir_ftok fm
822ir_set o 1 ga
823ir_set o 2 c2
824ir_set o 3 get gr_h 4
825tk = ir_otok o
826o = ir_op b0 43 0 0 4
827ir_set o 0 tk
828ir_set o 1 ga
829ir_set o 2 c2
830ir_set o 3 cmax
831put gr_h 2 ir_otok o
832gr_emit_loop
833gr_build_addovf_tail
834end
835
836note Program 4 (explicit trap 9 via cbr): store 5000 at @g0[0], loop 10x, load, gt 3000 -> cbr -> trap 9 (exit 109)
837fn gr_build_trap_tail to void
838f = get gr_h 0
839b3 = get gr_h 6
840bt = ir_blk f
841bf = ir_blk f
842o = ir_op b3 79 0 3 4
843ir_set o 0 ir_btok b3
844ir_set o 1 get gr_h 7
845ir_set o 2 get gr_h 3
846ir_set o 3 get gr_h 4
847tk = ir_otok o
848o = ir_op b3 42 5 0 3
849ir_set o 0 tk
850ir_set o 1 get gr_h 7
851ir_set o 2 get gr_h 3
852ld = ir_res o
853c3000 = ir_res ir_op b3 1 5 3000 0
854o = ir_op b3 34 1 0 2
855ir_set o 0 ld
856ir_set o 1 c3000
857gt1 = ir_res o
858o = ir_op b3 61 0 0 4
859ir_set o 0 tk
860ir_set o 1 gt1
861ir_set o 2 bt
862ir_set o 3 bf
863o = ir_op bt 65 0 9 1
864ir_set o 0 ir_btok bt
865z = ir_res ir_op bf 1 8 0 0
866o = ir_op bf 63 0 0 2
867ir_set o 0 ir_btok bf
868ir_set o 1 z
869end
870
871fn gr_build_prog_trap to void
872ir_init
873ta = ir_tarr 5 4
874tr = ir_tref ta 1 0
875g0 = ir_glob ta 0
876fm = ir_fnew 2 0 8
877b0 = ir_blk fm
878put gr_h 0 fm
879put gr_h 1 b0
880ga = ir_res ir_op b0 52 tr ulong g0 0
881put gr_h 7 ga
882put gr_h 3 ir_res ir_op b0 1 5 0 0
883put gr_h 4 ir_res ir_op b0 1 5 1 0
884put gr_h 5 ir_res ir_op b0 1 5 10 0
885c5000 = ir_res ir_op b0 1 5 5000 0
886o = ir_op b0 79 0 3 4
887ir_set o 0 ir_ftok fm
888ir_set o 1 ga
889ir_set o 2 get gr_h 3
890ir_set o 3 get gr_h 4
891tk = ir_otok o
892o = ir_op b0 43 0 0 4
893ir_set o 0 tk
894ir_set o 1 ga
895ir_set o 2 get gr_h 3
896ir_set o 3 c5000
897put gr_h 2 ir_otok o
898gr_emit_loop
899gr_build_trap_tail
900end
901
902note Program 5 (clean non-trapping program returning 42)
903fn gr_build_prog_ok to void
904ir_init
905f = ir_fnew 2 0 8
906b = ir_blk f
907c42 = ir_res ir_op b 1 8 42 0
908o = ir_op b 63 0 0 2
909ir_set o 0 ir_ftok f
910ir_set o 1 c42
911end
912
913note Argv packet builder for execprog (u16 count, then u16 len + bytes per arg)
914fn gr_av_reset to void
915put gr_av 0 0
916put gr_av 1 0
917put gr_an 0 2
918put gr_an 1 0
919end
920
921fn gr_av_commit n ulong to void
922put gr_an 0 add add get gr_an 0 2 n
923c = add get gr_an 1 1
924put gr_an 1 c
925put gr_av 0 uchar and c 255
926put gr_av 1 uchar and shr c 8 255
927end
928
929fn gr_av_push s view uchar * to void
930p = get gr_an 0
931n = len s
932put gr_av p uchar and n 255
933put gr_av add p 1 uchar and shr n 8 255
934for j ulong n bound 4096
935put gr_av add add p 2 j get s j
936end
937gr_av_commit n
938end
939
940fn gr_av_push_ng to void
941p = get gr_an 0
942n = get gr_ngl 0
943put gr_av p uchar and n 255
944put gr_av add p 1 uchar and shr n 8 255
945for j ulong n bound 2048
946put gr_av add add p 2 j get gr_ngp j
947end
948gr_av_commit n
949end
950
951fn gr_av_push_pa dst ulong to void
952p = get gr_an 0
953if eq dst 1
954n = get gr_pl 0
955put gr_av p uchar and n 255
956put gr_av add p 1 uchar and shr n 8 255
957for j ulong n bound 4096
958put gr_av add add p 2 j get gr_pa j
959end
960gr_av_commit n
961else
962n = get gr_pl2 0
963put gr_av p uchar and n 255
964put gr_av add p 1 uchar and shr n 8 255
965for j ulong n bound 4096
966put gr_av add add p 2 j get gr_pa2 j
967end
968gr_av_commit n
969end
970end
971
972fn gr_read_cap suf view uchar * is_err bool to ulong
973gr_spath suf 1
974fd = fopen gr_pa 0 get gr_pl 0 0 0
975if lt fd 0
976ret 0
977end
978n = ulong 0
979while lt n 65536 bound 1024
980k = long 0
981if is_err
982k = read int fd gr_err n sub 65536 n
983else
984k = read int fd gr_out n sub 65536 n
985end
986if le k 0
987break
988end
989n = add n ulong k
990end
991r = fclose int fd
992ret n
993end
994
995fn gr_spawn to ulong
996gr_spath gr_f_in 1
997fi = fopen gr_pa 0 get gr_pl 0 577 420
998r = fclose int fi
999fi = fopen gr_pa 0 get gr_pl 0 0 0
1000gr_spath gr_f_out 1
1001fo = fopen gr_pa 0 get gr_pl 0 577 420
1002gr_spath gr_f_err 1
1003fe = fopen gr_pa 0 get gr_pl 0 577 420
1004pid = fork
1005if eq pid 0
1006d0 = dupfd int fi 0 0
1007d1 = dupfd int fo 1 0
1008d2 = dupfd int fe 2 0
1009c0 = fclose int fi
1010c1 = fclose int fo
1011c2 = fclose int fe
1012ex = execprog gr_av 0 get gr_an 0
1013exit 127
1014end
1015c0 = fclose int fi
1016c1 = fclose int fo
1017c2 = fclose int fe
1018w = waitpid int pid gr_st 0 4 0
1019put gr_olen 0 gr_read_cap gr_f_out false
1020put gr_olen 1 gr_read_cap gr_f_err true
1021if ne get gr_st 0 0
1022ret 255
1023end
1024ret ulong get gr_st 1
1025end
1026
1027fn gr_err_has needle view uchar * to bool
1028el = get gr_olen 1
1029nl = len needle
1030if gt nl el
1031ret false
1032end
1033lim = add sub el nl 1
1034for i ulong lim bound 65536
1035ok = true
1036for j ulong nl bound 256
1037if ne get gr_err add i j get needle j
1038ok = false
1039break
1040end
1041end
1042if ok
1043ret true
1044end
1045end
1046ret false
1047end
1048
1049fn gr_run_ngrun ir_suf view uchar * j_suf view uchar * do_rw bool do_j bool to ulong
1050gr_av_reset
1051gr_av_push_ng
1052gr_spath ir_suf 1
1053gr_av_push_pa 1
1054if do_rw
1055gr_av_push gr_a_rw
1056gr_av_push gr_a_ce
1057gr_av_push gr_a_4
1058end
1059if do_j
1060gr_av_push gr_a_jn
1061gr_spath j_suf 2
1062gr_av_push_pa 2
1063end
1064ret gr_spawn
1065end
1066
1067fn gr_stage5_traps to int
1068note 5a. chk.bounds (exit 103)
1069ex = gr_run_ngrun gr_f_b gr_f_jb true true
1070if ne ex 103
1071ret gr_fail 104
1072end
1073if not gr_err_has gr_k_pfx
1074ret gr_fail 105
1075end
1076if or not gr_err_has gr_k_t3 not gr_err_has gr_k_par
1077ret gr_fail 106
1078end
1079if or not gr_err_has gr_k_op43 not gr_err_has gr_k_v99
1080ret gr_fail 107
1081end
1082if gr_err_has gr_k_ck0
1083ret gr_fail 108
1084end
1085gr_spath gr_f_jb 3
1086if ne tkj_read_path TKJ_OK
1087ret gr_fail 109
1088end
1089if lt tkj_count 10
1090ret gr_fail 110
1091end
1092put gr_cnt 2 add get gr_cnt 2 4
1093
1094note 5b. chk.divz across function call (exit 102)
1095ex = gr_run_ngrun gr_f_d gr_f_jd true true
1096if ne ex 102
1097ret gr_fail 111
1098end
1099if or not gr_err_has gr_k_t2 not gr_err_has gr_k_par
1100ret gr_fail 112
1101end
1102if or not gr_err_has gr_k_op43 not gr_err_has gr_k_v0
1103ret gr_fail 113
1104end
1105if gr_err_has gr_k_ck0
1106ret gr_fail 114
1107end
1108gr_spath gr_f_jd 3
1109if ne tkj_read_path TKJ_OK
1110ret gr_fail 115
1111end
1112put gr_cnt 2 add get gr_cnt 2 3
1113
1114note 5c. chk.add overflow (exit 101)
1115ex = gr_run_ngrun gr_f_a gr_f_ja true true
1116if ne ex 101
1117ret gr_fail 116
1118end
1119if or not gr_err_has gr_k_t1 not gr_err_has gr_k_par
1120ret gr_fail 117
1121end
1122if or not gr_err_has gr_k_op43 not gr_err_has gr_k_vmax
1123ret gr_fail 118
1124end
1125if gr_err_has gr_k_ck0
1126ret gr_fail 119
1127end
1128gr_spath gr_f_ja 3
1129if ne tkj_read_path TKJ_OK
1130ret gr_fail 120
1131end
1132put gr_cnt 2 add get gr_cnt 2 3
1133
1134note 5c2. Explicit trap 9 via cbr (exit 109)
1135ex = gr_run_ngrun gr_f_t gr_f_jt true true
1136if ne ex 109
1137ret gr_fail 128
1138end
1139if or not gr_err_has gr_k_t9 not gr_err_has gr_k_par
1140ret gr_fail 129
1141end
1142if or not gr_err_has gr_k_op43 not gr_err_has gr_k_v5000
1143ret gr_fail 130
1144end
1145if gr_err_has gr_k_ck0
1146ret gr_fail 131
1147end
1148gr_spath gr_f_jt 3
1149if ne tkj_read_path TKJ_OK
1150ret gr_fail 132
1151end
1152put gr_cnt 2 add get gr_cnt 2 3
1153ret 0
1154end
1155
1156fn gr_stage5 to int
1157gr_build_prog_bounds
1158if not gr_write_ir gr_f_b
1159ret gr_fail 100
1160end
1161gr_build_prog_divz
1162if not gr_write_ir gr_f_d
1163ret gr_fail 101
1164end
1165gr_build_prog_addovf
1166if not gr_write_ir gr_f_a
1167ret gr_fail 102
1168end
1169gr_build_prog_trap
1170if not gr_write_ir gr_f_t
1171ret gr_fail 133
1172end
1173gr_build_prog_ok
1174if not gr_write_ir gr_f_ok
1175ret gr_fail 103
1176end
1177rt = gr_stage5_traps
1178if ne rt 0
1179ret rt
1180end
1181
1182note 5d. Live NC: 1-bit tamper on ngrun-produced bounds.otkj on disk MUST be REFUSED!
1183gr_spath gr_f_jb 3
1184if ne tkj_read_path TKJ_OK
1185ret gr_fail 121
1186end
1187bpos = ulong 40
1188put tj_buf bpos xor get tj_buf bpos uchar 1
1189if ne tkj_write_path TKJ_OK
1190ret gr_fail 122
1191end
1192rc = tkj_read_path
1193if eq rc TKJ_OK
1194ret gr_fail 123
1195end
1196put gr_cnt 3 add get gr_cnt 3 1
1197
1198note 5e. Legacy ngrun regression check (no --rewind, no --journal)
1199ex = gr_run_ngrun gr_f_ok gr_f_jok false false
1200if or ne ex 42 ne get gr_olen 1 0
1201ret gr_fail 124
1202end
1203ex = gr_run_ngrun gr_f_b gr_f_jb false false
1204if or ne ex 103 ne get gr_olen 1 0
1205ret gr_fail 125
1206end
1207ex = gr_run_ngrun gr_f_ok gr_f_jok false true
1208if or ne ex 42 ne get gr_olen 1 0
1209ret gr_fail 126
1210end
1211gr_spath gr_f_jok 3
1212if ne tkj_read_path TKJ_OK
1213ret gr_fail 127
1214end
1215put gr_cnt 2 add get gr_cnt 2 4
1216
1217gr_wrt gr_m_s5
1218gr_num get gr_cnt 2
1219gr_wrt gr_m_cp
1220ret 0
1221end
1222
1223fn main to int
1224gr_wrt gr_m_hdr
1225has_live = false
1226if ge argcnt 3
1227m1 = argread 1 gr_ngp 0 len gr_ngp
1228m2 = argread 2 gr_scp 0 len gr_scp
1229if and gt m1 0 gt m2 0
1230put gr_ngl 0 ulong m1
1231put gr_scl 0 ulong m2
1232has_live = true
1233end
1234end
1235r1 = gr_stage1 has_live
1236if ne r1 0
1237ret r1
1238end
1239r2 = gr_stage2
1240if ne r2 0
1241ret r2
1242end
1243r3 = gr_stage3
1244if ne r3 0
1245ret r3
1246end
1247r4 = gr_stage4
1248if ne r4 0
1249ret r4
1250end
1251if has_live
1252r5 = gr_stage5
1253if ne r5 0
1254ret r5
1255end
1256end
1257gr_wrt gr_m_pass
1258gr_num get gr_cnt 0
1259gr_wrt gr_m_neg
1260gr_num get gr_cnt 1
1261gr_wrt gr_m_live
1262gr_num get gr_cnt 2
1263gr_wrt gr_m_lneg
1264gr_num get gr_cnt 3
1265gr_wrt gr_m_nl
1266ret 0
1267end