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 / memo / gate_memo.om

1831 lines · 35928 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/memo/gate_memo.om: Standalone Pure-OM-2 Verification Gate for Stage 1 OMEGA-MEMO
3note (Sovereign Intelligence 2.0 Pillar 2: Universal Content-Addressed Memoization).
4note
5note Link order (program mode; ir.om provides om2 at line 1):
6note cat seed/ng/src/ir.om seed/ng/sha256.om seed/ng/target/target_desc.om \
7note seed/ng/pack/sem_pack.om seed/ng/memo/memo_policy.om \
8note seed/ng/memo/memo_store.om seed/ng/memo/gate_memo.om | seed/out/omc > gate_memo
9note
10note Usage:
11note ./gate_memo [SCRATCH_DIR [NG_BIN NGCHECK_BIN]]
12note
13note Stages verified:
14note Stage 1: Default root resolution (OMEGA_MEMO_DIR, $HOME/.local/share/omega-memo, empty-env refusal)
15note Stage 2: ΩIR v2 SEMANTIC_ID + OMPK v2 purity admission & fail-closed impure/contract refusals
16note Stage 3: Cross-process persistence via fork + execprog (/proc/self/exe): Process A stores,
17note fresh Process B hits with 0 recompute & exact output bytes + cross-PID provenance
18note Stage 4: One-way precision dominance (@exact->@tol PASS, @tol(100)->@tol(1000) PASS,
19note @tol->@exact REFUSED in-proc & cross-proc, looser->tighter REFUSED, monotonic upgrade,
20note downgrade refusal, conflicting @exact nondeterminism refusal)
21note Stage 5: Fail-closed tamper & corruption controls (1-bit output flip, 1-bit header/tier forge,
22note truncated body, truncated header, trailing byte, bad magic/version, key substitution)
23note Stage 6: Profitability policy (monons-timed trivial op skip, precheck skip, heavy op admit,
24note explicit MP_OVR_FORCE test override)
25note Stage 7: Uncapped scaling (128+ entries > 32-slot cap), loud disk-floor alarm without silent
26note truncation (both store_bytes and free_bytes thresholds), and explicit oldest-first mm_prune
27note Stage 8 (optional with NG_BIN NGCHECK_BIN): live ng @ng front-ir / @ng sem-id + ngcheck ck_pure
28
29global gm_dig uchar 24
30global gm_chk ulong 1
31global gm_neg ulong 1
32global gm_xproc ulong 1
33global gm_ent ulong 1
34global gm_cli_chk ulong 1
35global gm_cli_neg ulong 1
36
37global gm_scp uchar 1024
38global gm_scl ulong 1
39global gm_ngp uchar 1024
40global gm_ngl ulong 1
41global gm_ckp uchar 1024
42global gm_ckl ulong 1
43
44global gm_root_a uchar 1024
45global gm_root_al ulong 1
46global gm_pa uchar 2048
47global gm_pl ulong 1
48global gm_pb uchar 2048
49global gm_pbl ulong 1
50
51global gm_fid_ex uchar 32
52global gm_fid_t1 uchar 32
53global gm_fid_trv uchar 32
54global gm_fid_hvy uchar 32
55global gm_fid_cli uchar 32
56
57global gm_av uchar 4096
58global gm_an ulong 1
59global gm_ev uchar 4096
60global gm_en ulong 1
61global gm_st uchar 4
62global gm_arg uchar 1024
63global gm_io uchar 65536
64global gm_io2 uchar 65536
65global gm_bkp uchar 4096
66global gm_bkpl ulong 1
67
68text gm_m_hdr "=== OMEGA NG STAGE 1 (OMEGA-MEMO) VERIFICATION GATE ===\n"
69text gm_m_pass "GATE-MEMO PASS checks="
70text gm_m_neg " neg_controls="
71text gm_m_xp " xproc="
72text gm_m_ent " entries="
73text gm_m_cli " cli_checks="
74text gm_m_cneg " cli_neg="
75text gm_m_nl "\n"
76text gm_m_fail "GATE-MEMO FAIL check="
77
78text gm_self "/proc/self/exe"
79text gm_def_pfx "/tmp/omega-memo-gate-"
80text gm_w_store "--worker-store"
81text gm_w_look "--worker-lookup"
82text gm_w_ref "--worker-refuse"
83text gm_w_env "--worker-env"
84
85text gm_s_env1 "/env-explicit"
86text gm_s_env2 "/env-home"
87text gm_s_home_suf "/.local/share/omega-memo"
88text gm_s_main "/cas-main"
89text gm_s_scale "/cas-scale"
90text gm_s_cli "/cas-cli"
91text gm_e_dir "OMEGA_MEMO_DIR="
92text gm_e_home "HOME="
93text gm_e_other "UNRELATED_VAR=1"
94
95text gm_al_want "OMEGA-MEMO ALARM disk_floor "
96text gm_f_in "/in.bin"
97text gm_f_out "/out.bin"
98text gm_f_err "/err.bin"
99text gm_h_fi "@ng front-ir\n"
100text gm_h_sd "@ng sem-id\n"
101text gm_pfx_acc "ACCEPT "
102text gm_pfx_fn0 "fn 0 "
103
104text gm_src_pure "om2\n@pure\n@exact\nfn calc x ulong to ulong\nret add mul x 7 13\nend\nfn main to int\nret int and calc 5 255\nend\n"
105text gm_src_imp "om2\nglobal g_bad ulong 4\n@pure\nfn bad x ulong to ulong\nput g_bad 0 x\nret x\nend\nfn main to int\nret int and bad 5 255\nend\n"
106
107note ---- Output & Counting Helpers ----
108
109fn gm_wrt s view uchar * to void
110r = write 1 s 0 len s
111end
112
113fn gm_num v ulong to void
114x = v
115p = ulong 24
116for i ulong 21
117p = sub p 1
118put gm_dig p uchar add 48 rem x 10
119x = div x 10
120if eq x 0
121r = write 1 gm_dig p sub 24 p
122ret
123end
124end
125end
126
127fn gm_fail code ulong to int
128gm_wrt gm_m_fail
129gm_num code
130gm_wrt gm_m_nl
131ret int add 1 rem code 95
132end
133
134fn gm_add_chk n ulong to void
135put gm_chk 0 add get gm_chk 0 n
136end
137
138fn gm_add_neg to void
139put gm_neg 0 add get gm_neg 0 1
140end
141
142fn gm_put_u64_val v ulong to void
143for j ulong 8
144put mm_val_buf j uchar and shr v mul j 8 255
145end
146put mm_val_len 0 8
147end
148
149fn gm_get_u64_val to ulong
150v = ulong 0
151for j ulong 8
152b = ulong get mm_val_buf j
153v = or v shl b mul j 8
154end
155ret v
156end
157
158note Heavy deterministic pure computation for real monons & cross-process persistence
159fn gm_heavy_compute x ulong iters ulong to ulong
160acc = xor x 11400714819323198485
161for i ulong iters bound 200000
162acc = xor mul acc 6364136223846793005 add i 1442695040888963407
163acc = xor acc shr acc 29
164end
165ret acc
166end
167
168note Trivial pure computation (sub-threshold for profitability filter)
169fn gm_trivial_compute x ulong to ulong
170ret add x 1
171end
172
173note ---- Path, Decimal, and Subprocess Argv/Env Packet Builders ----
174
175fn gm_parse_u64 n ulong to ulong
176v = ulong 0
177for i ulong n bound 32
178ch = get gm_arg i
179if or lt ch 48 gt ch 57
180break
181end
182v = add mul v 10 ulong sub ch 48
183end
184ret v
185end
186
187fn gm_arg_eq idx ulong want view uchar * to bool
188rn = argread idx gm_arg 0 1023
189if ne rn long len want
190ret false
191end
192for i ulong len want bound 64
193if ne get gm_arg i get want i
194ret false
195end
196end
197ret true
198end
199
200fn gm_spath suf view uchar * to void
201n = get gm_scl 0
202for i ulong n bound 1024
203put gm_pa i get gm_scp i
204end
205m = len suf
206for j ulong m bound 128
207put gm_pa add n j get suf j
208end
209put gm_pl 0 add n m
210end
211
212fn gm_set_root_pa to ulong
213n = get gm_pl 0
214for i ulong n bound 2048
215put mm_env_buf i get gm_pa i
216end
217ret mm_set_root_from_envbuf n
218end
219
220fn gm_av_reset to void
221put gm_av 0 0
222put gm_av 1 0
223put gm_an 0 2
224end
225
226fn gm_av_inc_cnt to void
227c = or ulong get gm_av 0 shl ulong get gm_av 1 8
228c = add c 1
229put gm_av 0 uchar and c 255
230put gm_av 1 uchar and shr c 8 255
231end
232
233fn gm_av_push_txt s view uchar * to void
234p = get gm_an 0
235m = len s
236put gm_av p uchar and m 255
237put gm_av add p 1 uchar and shr m 8 255
238p = add p 2
239for i ulong m bound 256
240put gm_av add p i get s i
241end
242put gm_an 0 add p m
243gm_av_inc_cnt
244end
245
246fn gm_av_push_pa to void
247p = get gm_an 0
248m = get gm_pl 0
249put gm_av p uchar and m 255
250put gm_av add p 1 uchar and shr m 8 255
251p = add p 2
252for i ulong m bound 2048
253put gm_av add p i get gm_pa i
254end
255put gm_an 0 add p m
256gm_av_inc_cnt
257end
258
259fn gm_av_push_u64 v ulong to void
260x = v
261q = ulong 24
262for i ulong 21
263q = sub q 1
264put gm_dig q uchar add 48 rem x 10
265x = div x 10
266if eq x 0
267break
268end
269end
270m = sub 24 q
271p = get gm_an 0
272put gm_av p uchar and m 255
273put gm_av add p 1 uchar and shr m 8 255
274p = add p 2
275for j ulong m bound 24
276put gm_av add p j get gm_dig add q j
277end
278put gm_an 0 add p m
279gm_av_inc_cnt
280end
281
282fn gm_ev_reset to void
283put gm_ev 0 0
284put gm_ev 1 0
285put gm_en 0 2
286end
287
288fn gm_ev_inc_cnt to void
289c = or ulong get gm_ev 0 shl ulong get gm_ev 1 8
290c = add c 1
291put gm_ev 0 uchar and c 255
292put gm_ev 1 uchar and shr c 8 255
293end
294
295fn gm_ev_push_kv pfx view uchar * use_pa bool to void
296p = get gm_en 0
297m1 = len pfx
298m2 = ulong 0
299if use_pa
300m2 = get gm_pl 0
301end
302m = add m1 m2
303put gm_ev p uchar and m 255
304put gm_ev add p 1 uchar and shr m 8 255
305p = add p 2
306for i ulong m1 bound 128
307put gm_ev add p i get pfx i
308end
309p = add p m1
310if use_pa
311for j ulong m2 bound 2048
312put gm_ev add p j get gm_pa j
313end
314p = add p m2
315end
316put gm_en 0 p
317gm_ev_inc_cnt
318end
319
320fn gm_spawn_self use_env bool to ulong
321pid = fork
322if lt pid 0
323ret 254
324end
325if eq pid 0
326if use_env
327r = execenv gm_av 0 get gm_an 0 gm_ev 0 get gm_en 0
328else
329r = execprog gm_av 0 get gm_an 0
330end
331exit 127
332end
333w = waitpid int pid gm_st 0 4 0
334if ne w pid
335ret 253
336end
337if ne get gm_st 0 0
338ret add 256 ulong get gm_st 0
339end
340ret ulong get gm_st 1
341end
342
343note ---- Real ΩIR v2 Module Builder for Canonical Per-Function SEMANTIC_IDs ----
344
345fn gm_hash_fn_to_fid f uint which ulong to void
346m = ir_wfn_sem f
347for i ulong m bound 65536
348put sh_dat i ar_bg 1 i
349end
350sh_all m
351for i ulong 32
352b = get sh_dig i
353if eq which 0
354put gm_fid_ex i b
355else
356if eq which 1
357put gm_fid_t1 i b
358else
359if eq which 2
360put gm_fid_trv i b
361else
362put gm_fid_hvy i b
363end
364end
365end
366end
367end
368
369fn gm_build_f1 to uint
370f = ir_fnew 0 0 5
371ir_fset_mode f 1 1 0
372ir_fset_phys f 0 1000 10000
373p = ir_fpar f 5
374b = ir_blk f
375c = ir_res ir_op b 1 5 17 0
376om = ir_op b 12 5 0 2
377ir_set om 0 p
378ir_set om 1 p
379oa = ir_op b 10 5 0 2
380ir_set oa 0 ir_res om
381ir_set oa 1 c
382r = ir_op b 63 0 0 2
383ir_set r 0 ir_ftok f
384ir_set r 1 ir_res oa
385ret f
386end
387
388fn gm_build_f2 to uint
389f = ir_fnew 0 0 5
390ir_fset_mode f 1 2 0
391ir_fset_phys f 100 500 5000
392p = ir_fpar f 5
393b = ir_blk f
394c3 = ir_res ir_op b 1 5 3 0
395c5 = ir_res ir_op b 1 5 5 0
396om = ir_op b 12 5 0 2
397ir_set om 0 p
398ir_set om 1 c3
399oa = ir_op b 10 5 0 2
400ir_set oa 0 ir_res om
401ir_set oa 1 c5
402r = ir_op b 63 0 0 2
403ir_set r 0 ir_ftok f
404ir_set r 1 ir_res oa
405ret f
406end
407
408fn gm_build_f3 to uint
409f = ir_fnew 0 0 5
410ir_fset_mode f 1 1 0
411ir_fset_phys f 0 0 0
412p = ir_fpar f 5
413b = ir_blk f
414c1 = ir_res ir_op b 1 5 1 0
415oa = ir_op b 10 5 0 2
416ir_set oa 0 p
417ir_set oa 1 c1
418r = ir_op b 63 0 0 2
419ir_set r 0 ir_ftok f
420ir_set r 1 ir_res oa
421ret f
422end
423
424fn gm_build_f4 to uint
425f = ir_fnew 0 0 5
426ir_fset_mode f 1 1 0
427ir_fset_phys f 0 0 0
428p = ir_fpar f 5
429b = ir_blk f
430c99 = ir_res ir_op b 1 5 99 0
431ox = ir_op b 18 5 0 2
432ir_set ox 0 p
433ir_set ox 1 c99
434r = ir_op b 63 0 0 2
435ir_set r 0 ir_ftok f
436ir_set r 1 ir_res ox
437ret f
438end
439
440fn gm_build_fm to void
441fm = ir_fnew 2 0 8
442bm = ir_blk fm
443cz = ir_res ir_op bm 1 8 0 0
444rm = ir_op bm 63 0 0 2
445ir_set rm 0 ir_ftok fm
446ir_set rm 1 cz
447end
448
449fn gm_build_ir_fids to bool
450ir_init
451f1 = gm_build_f1
452f2 = gm_build_f2
453f3 = gm_build_f3
454f4 = gm_build_f4
455gm_build_fm
456gm_hash_fn_to_fid f1 0
457gm_hash_fn_to_fid f2 1
458gm_hash_fn_to_fid f3 2
459gm_hash_fn_to_fid f4 3
460ret true
461end
462
463fn gm_load_fid which ulong to void
464for i ulong 32
465if eq which 0
466put mm_req_fid i get gm_fid_ex i
467else
468if eq which 1
469put mm_req_fid i get gm_fid_t1 i
470else
471if eq which 2
472put mm_req_fid i get gm_fid_trv i
473else
474if eq which 3
475put mm_req_fid i get gm_fid_hvy i
476else
477put mm_req_fid i get gm_fid_cli i
478end
479end
480end
481end
482end
483end
484
485note ---- Cross-Process Child Worker Entrypoints ----
486
487fn gm_worker_store to int
488if lt argcnt 6
489ret 2
490end
491rl = argread 2 mm_env_buf 0 1900
492if lt rl 1
493ret 3
494end
495if ne mm_set_root_from_envbuf ulong rl MM_OK
496ret 4
497end
498an = argread 3 gm_arg 0 64
499inp = gm_parse_u64 ulong an
500an = argread 4 gm_arg 0 64
501tier = gm_parse_u64 ulong an
502an = argread 5 gm_arg 0 64
503err_b = gm_parse_u64 ulong an
504ok = gm_build_ir_fids
505mp_init
506mp_set_override MP_OVR_FORCE
507mm_reset_req
508gm_load_fid 0
509mm_hash_input_u64 inp
510mm_set_contract 1 1 tier err_b
511mm_set_provenance 1 0
512t0 = monons
513val = gm_heavy_compute inp 50000
514t1 = monons
515cns = ulong 10000
516if gt t1 t0
517cns = ulong sub t1 t0
518end
519gm_put_u64_val val
520src = mm_store cns 500
521if ne src MM_OK
522ret int add 30 src
523end
524ret 0
525end
526
527fn gm_worker_lookup to int
528if lt argcnt 7
529ret 2
530end
531rl = argread 2 mm_env_buf 0 1900
532if lt rl 1
533ret 3
534end
535if ne mm_set_root_from_envbuf ulong rl MM_OK
536ret 4
537end
538an = argread 3 gm_arg 0 64
539inp = gm_parse_u64 ulong an
540an = argread 4 gm_arg 0 64
541tier = gm_parse_u64 ulong an
542an = argread 5 gm_arg 0 64
543err_b = gm_parse_u64 ulong an
544an = argread 6 gm_arg 0 64
545want_val = gm_parse_u64 ulong an
546ok = gm_build_ir_fids
547mp_init
548mp_set_override MP_OVR_FORCE
549mm_reset_req
550gm_load_fid 0
551mm_hash_input_u64 inp
552mm_set_contract 1 1 tier err_b
553note Zero recompute: MUST hit directly from disk written by Process A!
554lrc = mm_lookup
555if ne lrc MM_OK
556ret int add 40 lrc
557end
558if ne get mm_val_len 0 8
559ret 41
560end
561got = gm_get_u64_val
562if ne got want_val
563ret 42
564end
565note Verify cross-process provenance: creator_pid != my own pid!
566mypid = ulong getpid
567if eq get mm_hit_meta 6 mypid
568ret 43
569end
570ret 0
571end
572
573fn gm_worker_refuse to int
574if lt argcnt 7
575ret 2
576end
577rl = argread 2 mm_env_buf 0 1900
578if lt rl 1
579ret 3
580end
581if ne mm_set_root_from_envbuf ulong rl MM_OK
582ret 4
583end
584an = argread 3 gm_arg 0 64
585inp = gm_parse_u64 ulong an
586an = argread 4 gm_arg 0 64
587tier = gm_parse_u64 ulong an
588an = argread 5 gm_arg 0 64
589err_b = gm_parse_u64 ulong an
590an = argread 6 gm_arg 0 64
591want_rc = gm_parse_u64 ulong an
592ok = gm_build_ir_fids
593mp_init
594mp_set_override MP_OVR_FORCE
595mm_reset_req
596gm_load_fid 0
597mm_hash_input_u64 inp
598mm_set_contract 1 1 tier err_b
599lrc = mm_lookup
600if ne lrc want_rc
601ret int add 50 lrc
602end
603if ne get mm_val_len 0 0
604ret 59
605end
606ret 0
607end
608
609fn gm_worker_env to int
610if lt argcnt 3
611ret 2
612end
613want_n = argread 2 gm_arg 0 1023
614if lt want_n 0
615ret 3
616end
617rc = mm_init_default_root
618if eq want_n 0
619if ne rc MM_ERR_NO_ROOT
620ret 60
621end
622ret 0
623end
624if ne rc MM_OK
625ret int add 61 rc
626end
627if ne get mm_root_len 0 ulong want_n
628ret 68
629end
630for i ulong ulong want_n bound 1024
631if ne get mm_root i get gm_arg i
632ret 69
633end
634end
635ret 0
636end
637
638note ---- Stage 1: Default Root Resolution (OMEGA_MEMO_DIR, $HOME, Empty-Env Refusal) ----
639
640fn gm_stage1_env_root to int
641note 1a. OMEGA_MEMO_DIR env variable takes precedence
642gm_spath gm_s_env1
643gm_av_reset
644gm_av_push_txt gm_self
645gm_av_push_txt gm_w_env
646gm_av_push_pa
647gm_ev_reset
648gm_ev_push_kv gm_e_dir true
649if ne gm_spawn_self true 0
650ret gm_fail 10
651end
652
653note 1b. HOME fallback resolves to <HOME>/.local/share/omega-memo
654gm_spath gm_s_env2
655gm_ev_reset
656gm_ev_push_kv gm_e_home true
657n0 = get gm_pl 0
658sl = len gm_s_home_suf
659for j ulong sl bound 64
660put gm_pa add n0 j get gm_s_home_suf j
661end
662put gm_pl 0 add n0 sl
663gm_av_reset
664gm_av_push_txt gm_self
665gm_av_push_txt gm_w_env
666gm_av_push_pa
667if ne gm_spawn_self true 0
668ret gm_fail 11
669end
670
671note 1c. Neither OMEGA_MEMO_DIR nor HOME set -> fail-closed MM_ERR_NO_ROOT
672put gm_pl 0 0
673gm_av_reset
674gm_av_push_txt gm_self
675gm_av_push_txt gm_w_env
676gm_av_push_pa
677gm_ev_reset
678gm_ev_push_kv gm_e_other false
679if ne gm_spawn_self true 0
680ret gm_fail 12
681end
682gm_add_neg
683gm_add_chk 3
684ret 0
685end
686
687note ---- Stage 2: ΩIR v2 SEMANTIC_ID & OMPK v2 Purity Admission + Negative Controls ----
688
689fn gm_stage2_purity to int
690gm_spath gm_s_main
691if ne gm_set_root_pa MM_OK
692ret gm_fail 20
693end
694mp_init
695mp_set_override MP_OVR_FORCE
696mm_reset_stats
697
698note 2a. Build OMPK v2 package for @pure @exact function -> mm_admit_ompk_v2 MUST succeed
699tdInitAll
700tdCacheInit
701pk_reset_all
702n2 = ir_wr
703if lt n2 64
704ret gm_fail 21
705end
706pk_reset_builder
707pk_set_req EXEC_SCALAR_SIMD 0 8 0 0 0
708for i ulong 32
709put pk_fid_in i get gm_fid_ex i
710end
711pk_set_contract 1 1 0 0 1000 10000
712for i ulong n2 bound 4096
713rc = pk_ir_push ar_bg 1 i
714if ne rc PK_OK
715ret gm_fail 22
716end
717end
718if ne pk_pack PK_OK
719ret gm_fail 23
720end
721if ne pk_verify PK_OK
722ret gm_fail 24
723end
724plen = get pk_len 0
725for i ulong plen bound 8192
726put mm_val_buf i get pk_buf i
727end
728if ne mm_admit_ompk_v2 plen 1 MM_OK
729ret gm_fail 25
730end
731for i ulong 32
732if ne get mm_req_fid i get gm_fid_ex i
733ret gm_fail 26
734end
735end
736
737note 2b. NEG: OMPK v2 with purity_flag == 0 -> mm_admit_ompk_v2 REFUSED (MM_ERR_IMPURE)
738pk_reset_builder
739pk_set_req EXEC_SCALAR_SIMD 0 8 0 0 0
740pk_set_contract 0 1 0 0 1000 10000
741rc = pk_ir_push 97
742if ne pk_pack PK_OK
743ret gm_fail 27
744end
745plen2 = get pk_len 0
746for i ulong plen2 bound 8192
747put mm_val_buf i get pk_buf i
748end
749if ne mm_admit_ompk_v2 plen2 1 MM_ERR_IMPURE
750ret gm_fail 28
751end
752gm_add_neg
753
754note 2c. NEG: OMPK v1 (no v2 purity contract) -> mm_admit_ompk_v2 REFUSED (MM_ERR_IMPURE)
755pk_reset_builder
756pk_set_req EXEC_SCALAR_SIMD 0 8 0 0 0
757rc = pk_ir_push 98
758if ne pk_pack PK_OK
759ret gm_fail 29
760end
761plen1 = get pk_len 0
762for i ulong plen1 bound 8192
763put mm_val_buf i get pk_buf i
764end
765if ne mm_admit_ompk_v2 plen1 1 MM_ERR_IMPURE
766ret gm_fail 30
767end
768gm_add_neg
769
770note 2d. NEG: Direct mm_store & mm_lookup with purity_flag == 0 -> MM_ERR_IMPURE
771mm_reset_req
772gm_load_fid 0
773mm_hash_input_u64 777
774gm_put_u64_val 9999
775mm_set_contract 0 1 1 0
776if ne mm_store 50000 500 MM_ERR_IMPURE
777ret gm_fail 31
778end
779if ne mm_lookup MM_ERR_IMPURE
780ret gm_fail 32
781end
782gm_add_neg
783
784note 2e. NEG: purity_flag == 1 WITHOUT ck_pure structural verification (ck_pure_ok == 0) -> MM_ERR_IMPURE
785mm_set_contract 1 0 1 0
786if ne mm_store 50000 500 MM_ERR_IMPURE
787ret gm_fail 33
788end
789if ne mm_lookup MM_ERR_IMPURE
790ret gm_fail 34
791end
792gm_add_neg
793
794note 2f. NEG: Malformed precision contracts (tier=0, tier=3, @exact with err=5) -> MM_ERR_CONTRACT
795mm_set_contract 1 1 0 0
796if ne mm_store 50000 500 MM_ERR_CONTRACT
797ret gm_fail 35
798end
799mm_set_contract 1 1 3 0
800if ne mm_store 50000 500 MM_ERR_CONTRACT
801ret gm_fail 36
802end
803mm_set_contract 1 1 1 5
804if ne mm_store 50000 500 MM_ERR_CONTRACT
805ret gm_fail 37
806end
807gm_add_neg
808
809gm_add_chk 10
810ret 0
811end
812
813note ---- Stage 3: True Cross-Process Persistence (Process A -> Disk -> Fresh Process B) ----
814
815fn gm_stage3_xproc to int
816gm_spath gm_s_main
817if ne gm_set_root_pa MM_OK
818ret gm_fail 40
819end
820inp = ulong 123456789
821expect_val = gm_heavy_compute inp 50000
822
823note Spawn fresh Process A to compute & store @exact entry to disk
824gm_av_reset
825gm_av_push_txt gm_self
826gm_av_push_txt gm_w_store
827gm_av_push_pa
828gm_av_push_u64 inp
829gm_av_push_u64 1
830gm_av_push_u64 0
831if ne gm_spawn_self false 0
832ret gm_fail 41
833end
834
835note Confirm exact sharded path <root>/<2hex>/<64hex>.ome exists with size 232 (224 hdr + 8 val)
836mm_reset_req
837gm_load_fid 0
838mm_hash_input_u64 inp
839if ne mm_derive_paths MM_OK
840ret gm_fail 42
841end
842sr = stat mm_file_path 0 get mm_file_plen 0 mp_stbuf 0 128
843if ne sr 0
844ret gm_fail 43
845end
846if ne mp_st_u64 48 232
847ret gm_fail 44
848end
849
850note Spawn fresh Process B to hit @exact with 0 recompute & verify exact bytes + cross-PID provenance
851gm_av_reset
852gm_av_push_txt gm_self
853gm_av_push_txt gm_w_look
854gm_av_push_pa
855gm_av_push_u64 inp
856gm_av_push_u64 1
857gm_av_push_u64 0
858gm_av_push_u64 expect_val
859if ne gm_spawn_self false 0
860ret gm_fail 45
861end
862put gm_xproc 0 add get gm_xproc 0 1
863
864note Spawn fresh Process C to hit @tol(1000) from Process A's @exact entry (cross-process dominance!)
865gm_av_reset
866gm_av_push_txt gm_self
867gm_av_push_txt gm_w_look
868gm_av_push_pa
869gm_av_push_u64 inp
870gm_av_push_u64 2
871gm_av_push_u64 1000
872gm_av_push_u64 expect_val
873if ne gm_spawn_self false 0
874ret gm_fail 46
875end
876put gm_xproc 0 add get gm_xproc 0 1
877
878gm_add_chk 6
879ret 0
880end
881
882note ---- Stage 4: One-Way Precision Dominance, Refusals, Upgrades, No-Downgrade, Exact Nondet ----
883
884fn gm_stage4_dominance to int
885gm_spath gm_s_main
886if ne gm_set_root_pa MM_OK
887ret gm_fail 50
888end
889mp_init
890mp_set_override MP_OVR_FORCE
891mm_reset_stats
892
893note 4a. @exact -> @tol(1000) and @exact -> @tol(0) PASS (using Stage 3 entry inp=123456789)
894inp_ex = ulong 123456789
895want_ex = gm_heavy_compute inp_ex 50000
896mm_reset_req
897gm_load_fid 0
898mm_hash_input_u64 inp_ex
899mm_set_contract 1 1 2 1000
900if ne mm_lookup MM_OK
901ret gm_fail 51
902end
903if ne gm_get_u64_val want_ex
904ret gm_fail 52
905end
906if ne get mm_stats 2 1
907ret gm_fail 53
908end
909mm_set_contract 1 1 2 0
910if ne mm_lookup MM_OK
911ret gm_fail 54
912end
913if ne gm_get_u64_val want_ex
914ret gm_fail 55
915end
916
917note 4b. Store @tol(100) via fresh Process A on inp=2002; test @tol(100)->@tol(1000) PASS
918inp_t1 = ulong 2002
919want_t1 = gm_heavy_compute inp_t1 50000
920gm_av_reset
921gm_av_push_txt gm_self
922gm_av_push_txt gm_w_store
923gm_av_push_pa
924gm_av_push_u64 inp_t1
925gm_av_push_u64 2
926gm_av_push_u64 100
927if ne gm_spawn_self false 0
928ret gm_fail 56
929end
930
931mm_reset_req
932gm_load_fid 0
933mm_hash_input_u64 inp_t1
934mm_set_contract 1 1 2 1000
935if ne mm_lookup MM_OK
936ret gm_fail 57
937end
938if ne gm_get_u64_val want_t1
939ret gm_fail 58
940end
941mm_set_contract 1 1 2 100
942if ne mm_lookup MM_OK
943ret gm_fail 59
944end
945
946note 4c. NEG: @tol(100) -> @exact poisoning REFUSED (in-process AND fresh cross-process!)
947mm_set_contract 1 1 1 0
948if ne mm_lookup MM_ERR_PRECISION_MISS
949ret gm_fail 60
950end
951if ne get mm_val_len 0 0
952ret gm_fail 61
953end
954gm_av_reset
955gm_av_push_txt gm_self
956gm_av_push_txt gm_w_ref
957gm_av_push_pa
958gm_av_push_u64 inp_t1
959gm_av_push_u64 1
960gm_av_push_u64 0
961gm_av_push_u64 MM_ERR_PRECISION_MISS
962if ne gm_spawn_self false 0
963ret gm_fail 62
964end
965put gm_xproc 0 add get gm_xproc 0 1
966gm_add_neg
967
968note 4d. NEG: @tol(0) -> @exact poisoning STILL REFUSED (tier 2 NEVER satisfies tier 1!)
969mm_reset_req
970gm_load_fid 0
971mm_hash_input_u64 2003
972gm_put_u64_val 55555
973mm_set_contract 1 1 2 0
974if ne mm_store 50000 500 MM_OK
975ret gm_fail 63
976end
977mm_set_contract 1 1 1 0
978if ne mm_lookup MM_ERR_PRECISION_MISS
979ret gm_fail 64
980end
981gm_add_neg
982
983note 4e. NEG: looser @tol(1000) -> tighter @tol(100) REFUSED!
984mm_reset_req
985gm_load_fid 0
986mm_hash_input_u64 2004
987gm_put_u64_val 10040
988mm_set_contract 1 1 2 1000
989if ne mm_store 50000 500 MM_OK
990ret gm_fail 65
991end
992mm_set_contract 1 1 2 100
993if ne mm_lookup MM_ERR_PRECISION_MISS
994ret gm_fail 66
995end
996gm_add_neg
997
998note 4f. Monotonic upgrade: @tol(1000) -> @tol(100) -> @exact; then downgrade attempt REFUSED!
999gm_put_u64_val 10010
1000mm_set_contract 1 1 2 100
1001if ne mm_store 50000 500 MM_OK
1002ret gm_fail 67
1003end
1004if ne get mm_stats 8 1
1005ret gm_fail 68
1006end
1007if ne mm_lookup MM_OK
1008ret gm_fail 69
1009end
1010if ne gm_get_u64_val 10010
1011ret gm_fail 70
1012end
1013gm_put_u64_val 10000
1014mm_set_contract 1 1 1 0
1015if ne mm_store 50000 500 MM_OK
1016ret gm_fail 71
1017end
1018if ne get mm_stats 8 2
1019ret gm_fail 72
1020end
1021note Attempt downgrade @exact -> @tol(500) MUST return MM_OK_DOMINATED and keep @exact=10000!
1022gm_put_u64_val 99999
1023mm_set_contract 1 1 2 500
1024if ne mm_store 50000 500 MM_OK_DOMINATED
1025ret gm_fail 73
1026end
1027mm_set_contract 1 1 1 0
1028if ne mm_lookup MM_OK
1029ret gm_fail 74
1030end
1031if ne gm_get_u64_val 10000
1032ret gm_fail 75
1033end
1034
1035note 4g. NEG: Conflicting @exact output for identical (func_sem_id, inputs_sha256) -> MM_ERR_NONDET!
1036gm_put_u64_val 10001
1037mm_set_contract 1 1 1 0
1038if ne mm_store 50000 500 MM_ERR_NONDET
1039ret gm_fail 76
1040end
1041gm_add_neg
1042
1043gm_add_chk 16
1044ret 0
1045end
1046
1047note ---- Stage 5: Fail-Closed Tamper & Corruption Negative Controls ----
1048
1049fn gm_backup_cur_ome to bool
1050fd = fopen mm_file_path 0 get mm_file_plen 0 0 0
1051if lt fd 0
1052ret false
1053end
1054n = read int fd gm_bkp 0 4096
1055cf = fclose int fd
1056if lt n 224
1057ret false
1058end
1059put gm_bkpl 0 ulong n
1060ret true
1061end
1062
1063fn gm_write_raw_ome n ulong to bool
1064fd = fopen mm_file_path 0 get mm_file_plen 0 577 420
1065if lt fd 0
1066ret false
1067end
1068w = write int fd gm_io 0 n
1069cf = fclose int fd
1070ret eq w long n
1071end
1072
1073fn gm_copy_bkp_to_io to ulong
1074n = get gm_bkpl 0
1075for i ulong n bound 4096
1076put gm_io i get gm_bkp i
1077end
1078ret n
1079end
1080
1081fn gm_stage5_tamper to int
1082gm_spath gm_s_main
1083if ne gm_set_root_pa MM_OK
1084ret gm_fail 80
1085end
1086mp_init
1087mp_set_override MP_OVR_FORCE
1088mm_reset_req
1089gm_load_fid 0
1090mm_hash_input_u64 3001
1091gm_put_u64_val 31415926535
1092mm_set_contract 1 1 2 100
1093if ne mm_store 50000 500 MM_OK
1094ret gm_fail 81
1095end
1096if ne mm_derive_paths MM_OK
1097ret gm_fail 82
1098end
1099if not gm_backup_cur_ome
1100ret gm_fail 83
1101end
1102
1103note 5a. NEG: 1-bit tamper in output_bytes[0] (offset 224) -> MM_ERR_TAMPER (14)
1104n = gm_copy_bkp_to_io
1105put gm_io 224 xor get gm_io 224 uchar 1
1106if not gm_write_raw_ome n
1107ret gm_fail 84
1108end
1109if ne mm_lookup MM_ERR_TAMPER
1110ret gm_fail 85
1111end
1112if ne get mm_val_len 0 0
1113ret gm_fail 86
1114end
1115gm_add_neg
1116
1117note 5b. NEG: 1-bit header tamper trying to forge @tol(100) into @exact (tier 2->1, err->0) -> MM_ERR_TAMPER (14)
1118n = gm_copy_bkp_to_io
1119put gm_io 9 uchar 1
1120for j ulong 8
1121put gm_io add 16 j uchar 0
1122end
1123if not gm_write_raw_ome n
1124ret gm_fail 87
1125end
1126mm_set_contract 1 1 1 0
1127if ne mm_lookup MM_ERR_TAMPER
1128ret gm_fail 88
1129end
1130gm_add_neg
1131mm_set_contract 1 1 2 100
1132
1133note 5c. NEG: Truncated .ome output (minus 1 byte) AND truncated header (100 bytes) -> MM_ERR_TRUNCATED (12)
1134n = gm_copy_bkp_to_io
1135if not gm_write_raw_ome sub n 1
1136ret gm_fail 89
1137end
1138if ne mm_lookup MM_ERR_TRUNCATED
1139ret gm_fail 90
1140end
1141if not gm_write_raw_ome 100
1142ret gm_fail 91
1143end
1144if ne mm_lookup MM_ERR_TRUNCATED
1145ret gm_fail 92
1146end
1147gm_add_neg
1148
1149note 5d. NEG: Trailing garbage byte appended -> MM_ERR_TRAILING (13)
1150n = gm_copy_bkp_to_io
1151put gm_io n uchar 65
1152if not gm_write_raw_ome add n 1
1153ret gm_fail 93
1154end
1155if ne mm_lookup MM_ERR_TRAILING
1156ret gm_fail 94
1157end
1158gm_add_neg
1159
1160note 5e. NEG: Corrupt magic byte -> MM_ERR_MAGIC (10); corrupt version -> MM_ERR_VERSION (11)
1161n = gm_copy_bkp_to_io
1162put gm_io 0 uchar 88
1163if not gm_write_raw_ome n
1164ret gm_fail 95
1165end
1166if ne mm_lookup MM_ERR_MAGIC
1167ret gm_fail 96
1168end
1169n = gm_copy_bkp_to_io
1170put gm_io 4 uchar 9
1171if not gm_write_raw_ome n
1172ret gm_fail 97
1173end
1174if ne mm_lookup MM_ERR_VERSION
1175ret gm_fail 98
1176end
1177gm_add_neg
1178
1179note 5f. Restore valid file for inp=3001, then copy it onto path of inp=3002 (key-substitution attack -> MM_ERR_KEY_MISMATCH)
1180n = gm_copy_bkp_to_io
1181if not gm_write_raw_ome n
1182ret gm_fail 99
1183end
1184mm_hash_input_u64 3002
1185if ne mm_derive_paths MM_OK
1186ret gm_fail 100
1187end
1188sr = makedir mm_shard_path 0 get mm_shard_plen 0 493
1189if not gm_write_raw_ome n
1190ret gm_fail 101
1191end
1192if ne mm_lookup MM_ERR_KEY_MISMATCH
1193ret gm_fail 102
1194end
1195u = unlink mm_file_path 0 get mm_file_plen 0
1196gm_add_neg
1197
1198gm_add_chk 12
1199ret 0
1200end
1201
1202note ---- Stage 6: Profitability Policy (Trivial Skip, Precheck Skip, Heavy Admit, Test Override) ----
1203
1204fn gm_stage6_profitability to int
1205gm_spath gm_s_main
1206if ne gm_set_root_pa MM_OK
1207ret gm_fail 110
1208end
1209mp_init
1210mp_set_override MP_OVR_AUTO
1211mp_set_thresholds 5000 2 1
1212mm_reset_stats
1213
1214note 6a. Trivial sub-threshold op (real monons timing + explicit 150ns vs 4000ns) -> SKIPPED, no file created!
1215mm_reset_req
1216gm_load_fid 2
1217mm_hash_input_u64 4001
1218mm_set_contract 1 1 1 0
1219t0 = monons
1220tv = gm_trivial_compute 4001
1221t1 = monons
1222cns_trv = ulong 150
1223if and gt t1 t0 lt sub t1 t0 4000
1224cns_trv = ulong sub t1 t0
1225end
1226gm_put_u64_val tv
1227if ne mm_store cns_trv 4000 MM_SKIP_UNPROFITABLE
1228ret gm_fail 111
1229end
1230if ne mm_derive_paths MM_OK
1231ret gm_fail 112
1232end
1233if eq stat mm_file_path 0 get mm_file_plen 0 mp_stbuf 0 128 0
1234ret gm_fail 113
1235end
1236gm_add_neg
1237
1238note 6b. Subsequent mm_lookup on already-profiled trivial SEMANTIC_ID fast-skips via mp_precheck!
1239if ne mm_lookup MM_SKIP_UNPROFITABLE
1240ret gm_fail 114
1241end
1242
1243note 6c. Heavy pure op (real monons timing >= 5000ns and >= 2x overhead) -> ADMITTED & HIT!
1244mm_reset_req
1245gm_load_fid 3
1246mm_hash_input_u64 4002
1247mm_set_contract 1 1 1 0
1248h0 = monons
1249hv = gm_heavy_compute 4002 60000
1250h1 = monons
1251cns_hvy = ulong 100000
1252if gt h1 h0
1253cns_hvy = ulong sub h1 h0
1254end
1255if lt cns_hvy 10000
1256cns_hvy = ulong 100000
1257end
1258gm_put_u64_val hv
1259if ne mm_store cns_hvy 2000 MM_OK
1260ret gm_fail 115
1261end
1262if ne mm_lookup MM_OK
1263ret gm_fail 116
1264end
1265if ne gm_get_u64_val hv
1266ret gm_fail 117
1267end
1268
1269note 6d. Explicit override MP_OVR_FORCE admits even trivial sub-threshold op for tests!
1270mp_set_override MP_OVR_FORCE
1271mm_reset_req
1272gm_load_fid 2
1273mm_hash_input_u64 4001
1274mm_set_contract 1 1 1 0
1275gm_put_u64_val tv
1276if ne mm_store 100 4000 MM_OK
1277ret gm_fail 118
1278end
1279if ne mm_lookup MM_OK
1280ret gm_fail 119
1281end
1282if ne gm_get_u64_val tv
1283ret gm_fail 120
1284end
1285
1286gm_add_chk 9
1287ret 0
1288end
1289
1290note ---- Stage 7: Uncapped 128+ Entry Scaling (No 32-Slot Cap) + Disk-Floor Alarm + memo_prune ----
1291
1292fn gm_stage7_scale_alarm_prune to int
1293gm_spath gm_s_scale
1294if ne gm_set_root_pa MM_OK
1295ret gm_fail 130
1296end
1297mp_init
1298mp_set_override MP_OVR_FORCE
1299mp_set_disk_limits 1048576 4096 2097152
1300mm_reset_stats
1301
1302note 7a. Store 128 distinct content-addressed entries (4x past 32-slot cap!)
1303for i ulong 128
1304mm_reset_req
1305gm_load_fid 0
1306mm_hash_input_u64 add 50000 i
1307mm_set_contract 1 1 1 0
1308gm_put_u64_val add mul i 1000 7
1309rc = mm_store 50000 500
1310if ne rc MM_OK
1311ret gm_fail 131
1312end
1313end
1314if ne mp_scan_store MP_DISK_OK
1315ret gm_fail 132
1316end
1317if ne get mp_stat 0 128
1318ret gm_fail 133
1319end
1320if ne get mp_stat 1 29696
1321ret gm_fail 134
1322end
1323put gm_ent 0 get mp_stat 0
1324
1325note Verify sample entries spanning well beyond slot 32 (0, 31, 32, 63, 64, 99, 127)
1326for k ulong 7
1327idx = ulong 0
1328if eq k 1
1329idx = ulong 31
1330end
1331if eq k 2
1332idx = ulong 32
1333end
1334if eq k 3
1335idx = ulong 63
1336end
1337if eq k 4
1338idx = ulong 64
1339end
1340if eq k 5
1341idx = ulong 99
1342end
1343if eq k 6
1344idx = ulong 127
1345end
1346mm_reset_req
1347gm_load_fid 0
1348mm_hash_input_u64 add 50000 idx
1349mm_set_contract 1 1 1 0
1350if ne mm_lookup MM_OK
1351ret gm_fail 135
1352end
1353if ne gm_get_u64_val add mul idx 1000 7
1354ret gm_fail 136
1355end
1356end
1357
1358note 7b. Disk-floor alarm: set max_store_bytes = 20000 (< 29696), store entry #128 -> MUST fire loud ALARM without truncating ANY existing entry!
1359mp_set_disk_limits 20000 4096 2097152
1360mm_reset_req
1361gm_load_fid 0
1362mm_hash_input_u64 50128
1363mm_set_contract 1 1 1 0
1364gm_put_u64_val 128007
1365if ne mm_store 50000 500 MM_OK_ALARM
1366ret gm_fail 137
1367end
1368if or ne get mp_stat 3 1 lt get mp_stat 4 1
1369ret gm_fail 138
1370end
1371note Confirm structured alarm message begins with "OMEGA-MEMO ALARM disk_floor "
1372if lt get mp_alarm_len 0 len gm_al_want
1373ret gm_fail 139
1374end
1375for i ulong len gm_al_want bound 64
1376if ne get mp_alarm_buf i get gm_al_want i
1377ret gm_fail 140
1378end
1379end
1380note Confirm ZERO silent truncation: ALL 129 entries remain intact and verifiable on disk!
1381if ne mp_scan_store MP_DISK_OK
1382ret gm_fail 141
1383end
1384if ne get mp_stat 0 129
1385ret gm_fail 142
1386end
1387put gm_ent 0 get mp_stat 0
1388mm_reset_req
1389gm_load_fid 0
1390mm_hash_input_u64 50000
1391mm_set_contract 1 1 1 0
1392if ne mm_lookup MM_OK
1393ret gm_fail 143
1394end
1395if ne gm_get_u64_val 7
1396ret gm_fail 144
1397end
1398mm_hash_input_u64 50128
1399if ne mm_lookup MM_OK
1400ret gm_fail 145
1401end
1402if ne gm_get_u64_val 128007
1403ret gm_fail 146
1404end
1405gm_add_neg
1406
1407note 7c. Also verify free_bytes < free_floor_bytes alarm triggers loudly
1408mp_set_disk_limits 1048576 8192 2097152
1409mp_set_ext_free 1 1024
1410if ne mp_check_disk_floor MP_ALARM_DISK_FLOOR
1411ret gm_fail 147
1412end
1413mp_set_ext_free 0 0
1414gm_add_neg
1415
1416note 7d. Explicit oldest-first mm_prune down to 11600 bytes (50 entries x 232B) clears alarm cleanly!
1417mp_set_disk_limits 12000 4096 2097152
1418if ne mm_prune 11600 MM_OK
1419ret gm_fail 148
1420end
1421if ne get mp_stat 5 79
1422ret gm_fail 149
1423end
1424if ne get mp_stat 6 18328
1425ret gm_fail 150
1426end
1427if ne get mp_stat 0 50
1428ret gm_fail 151
1429end
1430if gt get mp_stat 1 11600
1431ret gm_fail 152
1432end
1433if ne get mp_stat 3 0
1434ret gm_fail 153
1435end
1436
1437gm_add_chk 16
1438ret 0
1439end
1440
1441note ---- Stage 8 (Optional): Live ng + ngcheck ck_pure End-to-End Integration ----
1442
1443fn gm_cli_write_txt hdr view uchar * body view uchar * to bool
1444gm_spath gm_f_in
1445fd = fopen gm_pa 0 get gm_pl 0 577 420
1446if lt fd 0
1447ret false
1448end
1449if gt len hdr 0
1450w1 = write int fd hdr 0 len hdr
1451if ne w1 long len hdr
1452cf = fclose int fd
1453ret false
1454end
1455end
1456w2 = write int fd body 0 len body
1457cf2 = fclose int fd
1458ret eq w2 long len body
1459end
1460
1461fn gm_cli_write_bin hdr view uchar * n ulong to bool
1462gm_spath gm_f_in
1463fd = fopen gm_pa 0 get gm_pl 0 577 420
1464if lt fd 0
1465ret false
1466end
1467if gt len hdr 0
1468w1 = write int fd hdr 0 len hdr
1469if ne w1 long len hdr
1470cf = fclose int fd
1471ret false
1472end
1473end
1474w2 = write int fd gm_io2 0 n
1475cf2 = fclose int fd
1476ret eq w2 long n
1477end
1478
1479fn gm_cli_read_out to long
1480gm_spath gm_f_out
1481fd = fopen gm_pa 0 get gm_pl 0 0 0
1482if lt fd 0
1483ret neg long 1
1484end
1485n = ulong 0
1486while lt n 65536 bound 1024
1487k = read int fd gm_io n sub 65536 n
1488if lt k 0
1489cf = fclose int fd
1490ret neg long 1
1491end
1492if eq k 0
1493cf = fclose int fd
1494ret long n
1495end
1496n = add n ulong k
1497end
1498cf = fclose int fd
1499ret long n
1500end
1501
1502fn gm_cli_exec use_ng bool to ulong
1503put gm_av 0 1
1504put gm_av 1 0
1505n = ulong 0
1506if use_ng
1507n = get gm_ngl 0
1508put gm_av 2 uchar and n 255
1509put gm_av 3 uchar and shr n 8 255
1510for i ulong n bound 1024
1511put gm_av add 4 i get gm_ngp i
1512end
1513else
1514n = get gm_ckl 0
1515put gm_av 2 uchar and n 255
1516put gm_av 3 uchar and shr n 8 255
1517for i ulong n bound 1024
1518put gm_av add 4 i get gm_ckp i
1519end
1520end
1521put gm_an 0 add 4 n
1522gm_spath gm_f_in
1523fi = fopen gm_pa 0 get gm_pl 0 0 0
1524gm_spath gm_f_out
1525fo = fopen gm_pa 0 get gm_pl 0 577 420
1526gm_spath gm_f_err
1527fe = fopen gm_pa 0 get gm_pl 0 577 420
1528if or lt fi 0 or lt fo 0 lt fe 0
1529ret 255
1530end
1531pid = fork
1532if lt pid 0
1533ret 254
1534end
1535if eq pid 0
1536r = dupfd int fi 0 0
1537r = dupfd int fo 1 0
1538r = dupfd int fe 2 0
1539r = execprog gm_av 0 get gm_an 0
1540exit 127
1541end
1542r = fclose int fi
1543r = fclose int fo
1544r = fclose int fe
1545w = waitpid int pid gm_st 0 4 0
1546if ne w pid
1547ret 253
1548end
1549if ne get gm_st 0 0
1550ret add 256 ulong get gm_st 0
1551end
1552ret ulong get gm_st 1
1553end
1554
1555fn gm_hex_nib ch uchar to ulong
1556if and ge ch 48 le ch 57
1557ret ulong sub ch 48
1558end
1559if and ge ch 97 le ch 102
1560ret add 10 ulong sub ch 97
1561end
1562ret 255
1563end
1564
1565fn gm_parse_semid_hex to bool
1566for i ulong 32
1567hi = gm_hex_nib get gm_io add 5 mul i 2
1568lo = gm_hex_nib get gm_io add 6 mul i 2
1569if or gt hi 15 gt lo 15
1570ret false
1571end
1572put gm_fid_cli i uchar or shl hi 4 lo
1573end
1574ret true
1575end
1576
1577fn gm_stage8_pure to int
1578if not gm_cli_write_txt gm_h_fi gm_src_pure
1579ret gm_fail 161
1580end
1581if ne gm_cli_exec true 0
1582ret gm_fail 162
1583end
1584irn = gm_cli_read_out
1585if lt irn 64
1586ret gm_fail 163
1587end
1588for i ulong ulong irn bound 65536
1589put gm_io2 i get gm_io i
1590end
1591gm_spath gm_f_in
1592fd = fopen gm_pa 0 get gm_pl 0 577 420
1593if lt fd 0
1594ret gm_fail 164
1595end
1596kw = write int fd gm_io2 0 ulong irn
1597cf = fclose int fd
1598if ne kw irn
1599ret gm_fail 164
1600end
1601if ne gm_cli_exec false 0
1602ret gm_fail 165
1603end
1604acn = gm_cli_read_out
1605if lt acn long len gm_pfx_acc
1606ret gm_fail 166
1607end
1608for i ulong len gm_pfx_acc bound 16
1609if ne get gm_io i get gm_pfx_acc i
1610ret gm_fail 167
1611end
1612end
1613if not gm_cli_write_bin gm_h_sd ulong irn
1614ret gm_fail 168
1615end
1616if ne gm_cli_exec true 0
1617ret gm_fail 169
1618end
1619sdn = gm_cli_read_out
1620if lt sdn 70
1621ret gm_fail 170
1622end
1623for i ulong len gm_pfx_fn0 bound 16
1624if ne get gm_io i get gm_pfx_fn0 i
1625ret gm_fail 171
1626end
1627end
1628if not gm_parse_semid_hex
1629ret gm_fail 172
1630end
1631gm_spath gm_s_cli
1632if ne gm_set_root_pa MM_OK
1633ret gm_fail 173
1634end
1635mp_init
1636mp_set_override MP_OVR_FORCE
1637mm_reset_req
1638gm_load_fid 4
1639mm_hash_input_u64 5
1640mm_set_contract 1 1 1 0
1641gm_put_u64_val 48
1642if ne mm_store 50000 500 MM_OK
1643ret gm_fail 174
1644end
1645if ne mm_lookup MM_OK
1646ret gm_fail 175
1647end
1648if ne gm_get_u64_val 48
1649ret gm_fail 176
1650end
1651put gm_cli_chk 0 add get gm_cli_chk 0 5
1652ret 0
1653end
1654
1655fn gm_stage8_impure to int
1656if not gm_cli_write_txt gm_h_fi gm_src_imp
1657ret gm_fail 177
1658end
1659if ne gm_cli_exec true 0
1660ret gm_fail 178
1661end
1662irn = gm_cli_read_out
1663if lt irn 64
1664ret gm_fail 179
1665end
1666for i ulong ulong irn bound 65536
1667put gm_io2 i get gm_io i
1668end
1669gm_spath gm_f_in
1670fd = fopen gm_pa 0 get gm_pl 0 577 420
1671if lt fd 0
1672ret gm_fail 180
1673end
1674kw = write int fd gm_io2 0 ulong irn
1675cf = fclose int fd
1676ckrc = gm_cli_exec false
1677if ne ckrc 22
1678ret gm_fail 181
1679end
1680ckp = ulong 0
1681if eq ckrc 0
1682ckp = ulong 1
1683end
1684mm_reset_req
1685gm_load_fid 4
1686mm_hash_input_u64 99
1687mm_set_contract 1 ckp 1 0
1688gm_put_u64_val 99
1689if ne mm_store 50000 500 MM_ERR_IMPURE
1690ret gm_fail 182
1691end
1692put gm_cli_neg 0 add get gm_cli_neg 0 1
1693ret 0
1694end
1695
1696fn gm_stage8_live_ng to int
1697nl = argread 2 gm_ngp 0 1023
1698cl = argread 3 gm_ckp 0 1023
1699if or lt nl 1 lt cl 1
1700ret gm_fail 160
1701end
1702put gm_ngl 0 ulong nl
1703put gm_ckl 0 ulong cl
1704rc = gm_stage8_pure
1705if ne rc 0
1706ret rc
1707end
1708ret gm_stage8_impure
1709end
1710
1711note ---- Setup Scratch Root & Main ----
1712
1713fn gm_init_scratch to bool
1714if ge argcnt 2
1715sl = argread 1 gm_scp 0 1023
1716if lt sl 1
1717ret false
1718end
1719put gm_scl 0 ulong sl
1720else
1721pl = len gm_def_pfx
1722for i ulong pl bound 64
1723put gm_scp i get gm_def_pfx i
1724end
1725pid = ulong getpid
1726x = pid
1727q = ulong 24
1728for i ulong 21
1729q = sub q 1
1730put gm_dig q uchar add 48 rem x 10
1731x = div x 10
1732if eq x 0
1733break
1734end
1735end
1736dc = sub 24 q
1737for j ulong dc bound 24
1738put gm_scp add pl j get gm_dig add q j
1739end
1740put gm_scl 0 add pl dc
1741end
1742r = makedir gm_scp 0 get gm_scl 0 493
1743if and ne r 0 ne r neg long 17
1744ret false
1745end
1746ret true
1747end
1748
1749fn main to int
1750z = ar_need 0 65536
1751ar_init
1752note Check internal cross-process worker subcommands first
1753if ge argcnt 2
1754if gm_arg_eq 1 gm_w_store
1755ret gm_worker_store
1756end
1757if gm_arg_eq 1 gm_w_look
1758ret gm_worker_lookup
1759end
1760if gm_arg_eq 1 gm_w_ref
1761ret gm_worker_refuse
1762end
1763if gm_arg_eq 1 gm_w_env
1764ret gm_worker_env
1765end
1766end
1767
1768put gm_chk 0 0
1769put gm_neg 0 0
1770put gm_xproc 0 0
1771put gm_ent 0 0
1772put gm_cli_chk 0 0
1773put gm_cli_neg 0 0
1774
1775gm_wrt gm_m_hdr
1776if not gm_init_scratch
1777ret gm_fail 1
1778end
1779ok = gm_build_ir_fids
1780
1781rc = gm_stage1_env_root
1782if ne rc 0
1783ret rc
1784end
1785rc = gm_stage2_purity
1786if ne rc 0
1787ret rc
1788end
1789rc = gm_stage3_xproc
1790if ne rc 0
1791ret rc
1792end
1793rc = gm_stage4_dominance
1794if ne rc 0
1795ret rc
1796end
1797rc = gm_stage5_tamper
1798if ne rc 0
1799ret rc
1800end
1801rc = gm_stage6_profitability
1802if ne rc 0
1803ret rc
1804end
1805rc = gm_stage7_scale_alarm_prune
1806if ne rc 0
1807ret rc
1808end
1809
1810if ge argcnt 4
1811rc = gm_stage8_live_ng
1812if ne rc 0
1813ret rc
1814end
1815end
1816
1817gm_wrt gm_m_pass
1818gm_num get gm_chk 0
1819gm_wrt gm_m_neg
1820gm_num get gm_neg 0
1821gm_wrt gm_m_xp
1822gm_num get gm_xproc 0
1823gm_wrt gm_m_ent
1824gm_num get gm_ent 0
1825gm_wrt gm_m_cli
1826gm_num get gm_cli_chk 0
1827gm_wrt gm_m_cneg
1828gm_num get gm_cli_neg 0
1829gm_wrt gm_m_nl
1830ret 0
1831end