code wiki / _hdl_build / nx_gated_edit_gate.nx

nx_gated_edit_gate.nx source

↩ module page · 122 lines · 6404 B

1// nx_gated_edit_gate.nx -- GATE for nx_gated_edit (gated source-edit applier). 5 teeth incl 1 neg-control 2// (never-live-edit) + restore-verify. PREREQ (staged by build): ./nx_gated_edit.elf + ./nx_ge_mockcat.elf. 3// mock verify = cat(argv1): CONSOLIDATED when verify targets a file the edit doesn't touch; REFUSED when it 4// targets the edited file itself (output changes -> restore). Run from nxc2. Verdict exit 0 iff 5/5. 5// license_tier: ORIGINAL No hw writes (Rule 26). 6import "nx_seat_drive_lib.nx" 7import "nx_seg_store.nx" 8import "nx_deploy_lib.nx" 9import "nx_syscalls.nx" 10import "nx_ag_lib.nx" 11import "nx_gate_verdict.nx" 12 13// ag_atoi/ag_itoa/ag_len/ag_read now shared via nx_ag_lib.nx (rule-15 extraction, behaviour-preserving) 14 15func gg_run(src: *u8, oldf: *u8, newf: *u8, velf: *u8, varg: *u8, outp: *u8) -> i64 { 16 let av: *i64 = sys_mmap(64) as *i64 17 av[0] = src as i64 18 av[1] = oldf as i64 19 av[2] = newf as i64 20 av[3] = velf as i64 21 av[4] = varg as i64 22 return dep_run_capture("./nx_gated_edit.elf" as *u8, av, 5, outp) 23} 24 25func main(argc: i64, argv: *i64) -> i64 { 26 var stage: i64 = 1 27 var pass: i64 = 0 28 if argc >= 2 { let ss1: *u8 = argv[1] as *u8; stage = ag_atoi(ss1) } 29 if argc >= 3 { let ps: *u8 = argv[2] as *u8; pass = ag_atoi(ps) } 30 if stage < 1 { stage = 1 } 31 32 ss_writefile("/tmp/ge_old" as *u8, "AAA" as *u8, ag_len("AAA" as *u8)) 33 ss_writefile("/tmp/ge_new" as *u8, "BBB" as *u8, ag_len("BBB" as *u8)) 34 ss_writefile("/tmp/ge_fixed" as *u8, "constant\n" as *u8, ag_len("constant\n" as *u8)) 35 36 if stage == 1 { 37 ss_writefile("/tmp/ge_src1" as *u8, "hello AAA world\n" as *u8, ag_len("hello AAA world\n" as *u8)) 38 let rc: i64 = gg_run("/tmp/ge_src1" as *u8, "/tmp/ge_old" as *u8, "/tmp/ge_new" as *u8, "./nx_ge_mockcat.elf" as *u8, "/tmp/ge_fixed" as *u8, "/tmp/gg_t1.out" as *u8) 39 let sb: *u8 = sys_mmap(4096) 40 let m: i64 = ag_read("/tmp/ge_src1" as *u8, sb, 4096) 41 var ok: i64 = 0 42 if rc == 0 { if sd_count(sb, m, "BBB" as *u8) == 1 { if sd_count(sb, m, "AAA" as *u8) == 0 { ok = 1 } } } 43 if ok == 1 { sd_w("T1 consolidated-applied PASS\n" as *u8); pass = pass + 1 } else { sd_w("T1 consolidated-applied FAIL\n" as *u8) } 44 } 45 if stage == 2 { 46 ss_writefile("/tmp/ge_src2" as *u8, "hello AAA world\n" as *u8, ag_len("hello AAA world\n" as *u8)) 47 let rc: i64 = gg_run("/tmp/ge_src2" as *u8, "/tmp/ge_old" as *u8, "/tmp/ge_new" as *u8, "./nx_ge_mockcat.elf" as *u8, "/tmp/ge_src2" as *u8, "/tmp/gg_t2.out" as *u8) 48 let ob: *u8 = sys_mmap(4096) 49 let n: i64 = dp_read("/tmp/gg_t2.out" as *u8, ob, 4096) 50 let sb: *u8 = sys_mmap(4096) 51 let m: i64 = ag_read("/tmp/ge_src2" as *u8, sb, 4096) 52 var ok: i64 = 0 53 if rc == 1 { if sd_count(ob, n, "verdict=REFUSED" as *u8) == 1 { if sd_count(sb, m, "AAA" as *u8) == 1 { if sd_count(sb, m, "BBB" as *u8) == 0 { ok = 1 } } } } 54 if ok == 1 { sd_w("T2 neg-live-edit-refused PASS\n" as *u8); pass = pass + 1 } else { sd_w("T2 neg-live-edit-refused FAIL\n" as *u8) } 55 } 56 if stage == 3 { 57 let sb: *u8 = sys_mmap(4096) 58 let m: i64 = ag_read("/tmp/ge_src2" as *u8, sb, 4096) 59 let orig: *u8 = "hello AAA world\n" as *u8 60 var ok: i64 = 0 61 if m == ag_len(orig) { 62 var same: i64 = 1 63 var i: i64 = 0 64 while i < m { if sb[i] != orig[i] { same = 0; i = m } else { i = i + 1 } } 65 if same == 1 { ok = 1 } 66 } 67 if ok == 1 { sd_w("T3 restore-byte-exact PASS\n" as *u8); pass = pass + 1 } else { sd_w("T3 restore-byte-exact FAIL\n" as *u8) } 68 } 69 if stage == 4 { 70 ss_writefile("/tmp/ge_src4" as *u8, "hello world\n" as *u8, ag_len("hello world\n" as *u8)) 71 ss_writefile("/tmp/ge_old4" as *u8, "ZZZ" as *u8, ag_len("ZZZ" as *u8)) 72 let rc: i64 = gg_run("/tmp/ge_src4" as *u8, "/tmp/ge_old4" as *u8, "/tmp/ge_new" as *u8, "./nx_ge_mockcat.elf" as *u8, "/tmp/ge_fixed" as *u8, "/tmp/gg_t4.out" as *u8) 73 let sb: *u8 = sys_mmap(4096) 74 let m: i64 = ag_read("/tmp/ge_src4" as *u8, sb, 4096) 75 var ok: i64 = 0 76 if rc == 2 { if sd_count(sb, m, "hello world" as *u8) == 1 { ok = 1 } } 77 if ok == 1 { sd_w("T4 nomatch-untouched PASS\n" as *u8); pass = pass + 1 } else { sd_w("T4 nomatch-untouched FAIL\n" as *u8) } 78 } 79 if stage == 5 { 80 ss_writefile("/tmp/ge_src5" as *u8, "AAA and AAA\n" as *u8, ag_len("AAA and AAA\n" as *u8)) 81 let rc: i64 = gg_run("/tmp/ge_src5" as *u8, "/tmp/ge_old" as *u8, "/tmp/ge_new" as *u8, "./nx_ge_mockcat.elf" as *u8, "/tmp/ge_fixed" as *u8, "/tmp/gg_t5.out" as *u8) 82 let sb: *u8 = sys_mmap(4096) 83 let m: i64 = ag_read("/tmp/ge_src5" as *u8, sb, 4096) 84 var ok: i64 = 0 85 if rc == 2 { if sd_count(sb, m, "BBB" as *u8) == 0 { if sd_count(sb, m, "AAA" as *u8) == 2 { ok = 1 } } } 86 if ok == 1 { sd_w("T5 ambiguous-untouched PASS\n" as *u8); pass = pass + 1 } else { sd_w("T5 ambiguous-untouched FAIL\n" as *u8) } 87 } 88 89 if stage >= 5 { 90 // MIGRATED onto nx_gate_verdict (D001). The five teeth above are untouched, so the PASS/FAIL 91 // vector cannot change; only the hand-rolled exit is replaced, so /api/gate_run and the harness 92 // frame can READ the outcome. ⚠the old receipt built its own digit with `(48 + pass)`, which is 93 // the very idiom nx_itoaclone hunts -- correct here ONLY because pass is 0..5 by construction, 94 // and silently wrong the moment a sixth tooth is added or the count goes negative. 95 sd_w("NX-GATED-EDIT-GATE pass=" as *u8) 96 let pb: *u8 = sys_mmap(16) 97 let pn: i64 = ccz_cat_num(pb, 0, pass) 98 sd_w(pb) 99 sd_w("/5\n" as *u8) 100 let ctr: *i64 = gv_ctr() 101 ctr[0] = pass 102 ctr[1] = 5 103 return gv_verdict("GATED-EDIT-GATE" as *u8, ctr, "teeth unchanged; ag_itoa now composes the shared ccz_cat_num emitter" as *u8) 104 } 105 106 let self: *u8 = argv[0] as *u8 107 let sb2: *u8 = sys_mmap(24) 108 ag_itoa(sb2, stage + 1) 109 let pb2: *u8 = sys_mmap(24) 110 ag_itoa(pb2, pass) 111 let nav: *i64 = sys_mmap(40) as *i64 112 nav[0] = self as i64 113 nav[1] = sb2 as i64 114 nav[2] = pb2 as i64 115 nav[3] = 0 116 let envp: *i64 = sys_mmap(16) as *i64 117 envp[0] = 0 118 sys_execve(self, nav, envp) 119 sd_w("GG-EXEC-FAIL\n" as *u8) 120 sys_exit(1) 121 return 1 122}