code wiki / (root) / nx_srcdiverge_gate.nx

nx_srcdiverge_gate.nx source

↩ module page · 129 lines · 5967 B

1// nx_srcdiverge_gate.nx -- BINARY-AHEAD-OF-SOURCE detector (seq1392 rung 2). license_tier: ORIGINAL 2// THE CLASS IT KILLS: a deployed elf can SERVE a route whose handler no longer exists in source -- because a 3// concurrent whole-file write or an /api/unpack tar reverted the source while the binary kept running. Nothing 4// warns. The next /api/build by ANY session or cron then silently DELETES a live capability that other lanes 5// may already depend on. This happened FOUR times in one session (seq1392/1445); the perf lane had already 6// adopted /api/gate_run by the third. I only ever caught it by luck, when an unrelated anchored edit returned 7// NOMATCH. Luck is not a control. 8// HOW: extract every /api/<name> token from the DEPLOYED elf's string data, then assert each one also appears 9// in the SOURCE. A route the binary advertises but the source cannot produce is DIVERGENCE = RED. 10// ★NON-VACUITY IS THE WHOLE POINT (T0): a scanner that extracts ZERO routes would report GREEN forever while 11// the tree burned. So the gate REFUSES unless it finds at least SD_MIN_ROUTES -- an instrument that cannot fail 12// to find anything is an instrument that proves nothing. 13import "nx_syscalls.nx" 14import "nx_gate.nx" 15 16const SD_ELF: *u8 = "/volume1/homes/elderwesto/nishihost/nx_mgmt_api.elf" as *u8 17const SD_SRC: *u8 = "/volume1/homes/elderwesto/nishihost/buildroot/runtime/_hdl_build/nx_mgmt_api.nx" as *u8 18const SD_MIN_ROUTES: i64 = 8 19const SD_MAXR: i64 = 128 20const SD_NAMEMAX: i64 = 48 21 22func sd_slen(s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { i = i + 1 } return i } 23// is byte c a route-name char? [a-zA-Z0-9_/] 24func sd_tok(c: i64) -> i64 { 25 if c >= 97 { if c <= 122 { return 1 } } 26 if c >= 65 { if c <= 90 { return 1 } } 27 if c >= 48 { if c <= 57 { return 1 } } 28 if c == 95 { return 1 } 29 if c == 47 { return 1 } 30 return 0 31} 32func sd_find(hay: *u8, n: i64, pat: *u8, pl: i64) -> i64 { 33 if pl <= 0 { return 0 - 1 } 34 var i: i64 = 0 35 while i + pl <= n { 36 var k: i64 = 0 37 var ok: i64 = 1 38 while k < pl { if hay[i+k] != pat[k] { ok = 0; k = pl } else { k = k + 1 } } 39 if ok == 1 { return i } 40 i = i + 1 41 } 42 return 0 - 1 43} 44 45func main() -> i64 { 46 gw("=== nx_srcdiverge_gate: a deployed route that SOURCE cannot rebuild is DIVERGENCE ===\n" as *u8) 47 let szp: *i64 = sys_mmap(16) as *i64 48 let elf: *u8 = sys_read_file(SD_ELF, szp) 49 if (elf as i64) == 0 { gw(" [FAIL] cannot read elf\n" as *u8); gw("SRCDIVERGE UNREADABLE\n" as *u8); sys_exit(2); return 2 } 50 let en: i64 = szp[0] 51 let szp2: *i64 = sys_mmap(16) as *i64 52 let src: *u8 = sys_read_file(SD_SRC, szp2) 53 if (src as i64) == 0 { gw(" [FAIL] cannot read source\n" as *u8); gw("SRCDIVERGE UNREADABLE\n" as *u8); sys_exit(2); return 2 } 54 let sn: i64 = szp2[0] 55 56 // collect unique /api/<name> tokens out of the elf's string data 57 let names: *u8 = sys_mmap(SD_MAXR * SD_NAMEMAX) 58 let nlen: *i64 = sys_mmap(SD_MAXR * 8) as *i64 59 var nroutes: i64 = 0 60 let pat: *u8 = "/api/" as *u8 61 var i: i64 = 0 62 while i + 5 <= en { 63 var hit: i64 = 1 64 var k: i64 = 0 65 while k < 5 { if elf[i+k] != pat[k] { hit = 0; k = 5 } else { k = k + 1 } } 66 if hit == 1 { 67 var e: i64 = i + 5 68 var stop: i64 = 0 69 while stop == 0 { if e >= en { stop = 1 } else { if sd_tok(elf[e] as i64) == 1 { e = e + 1 } else { stop = 1 } } } 70 if e > en { e = en } 71 var L: i64 = e - i 72 if L > 5 { 73 if L >= SD_NAMEMAX { L = SD_NAMEMAX - 1 } 74 // dedup 75 var dup: i64 = 0 76 var q: i64 = 0 77 while q < nroutes { 78 if nlen[q] == L { 79 var m: i64 = 1 80 var z: i64 = 0 81 while z < L { if names[q*SD_NAMEMAX + z] != elf[i+z] { m = 0; z = L } else { z = z + 1 } } 82 if m == 1 { dup = 1; q = nroutes } 83 } 84 q = q + 1 85 } 86 if dup == 0 { if nroutes < SD_MAXR { 87 var z2: i64 = 0 88 while z2 < L { names[nroutes*SD_NAMEMAX + z2] = elf[i+z2]; z2 = z2 + 1 } 89 nlen[nroutes] = L 90 nroutes = nroutes + 1 91 } } 92 } 93 i = i + 5 94 } else { i = i + 1 } 95 } 96 97 var pass: i64 = 0 98 var tot: i64 = 0 99 100 // T0 NON-VACUITY: if the extractor finds nothing, every later check passes trivially. REFUSE. 101 tot = tot + 1 102 var t0: i64 = 0 103 if nroutes >= SD_MIN_ROUTES { t0 = 1 } 104 if t0 == 1 { pass = pass + 1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 105 gw("T0 NON-VACUITY: extracted " as *u8); gn(nroutes); gw(" routes from the elf (min " as *u8); gn(SD_MIN_ROUTES); gw(")\n" as *u8) 106 107 // T1: every route the BINARY advertises must be present in SOURCE 108 var missing: i64 = 0 109 var r: i64 = 0 110 while r < nroutes { 111 let np: *u8 = ((names as i64) + r*SD_NAMEMAX) as *u8 112 if sd_find(src, sn, np, nlen[r]) < 0 { 113 missing = missing + 1 114 gw(" DIVERGENT (in binary, ABSENT from source): " as *u8) 115 sys_write(1, np, nlen[r]) 116 gw("\n" as *u8) 117 } 118 r = r + 1 119 } 120 tot = tot + 1 121 var t1: i64 = 0 122 if missing == 0 { t1 = 1 } 123 if t1 == 1 { pass = pass + 1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 124 gw("T1 every deployed route exists in source (divergent=" as *u8); gn(missing); gw(")\n" as *u8) 125 126 gw("\n=== nx_srcdiverge_gate " as *u8); gn(pass); gw("/" as *u8); gn(tot); gw(" ===\n" as *u8) 127 if pass == tot { gw("SRCDIVERGE GREEN -- the source can rebuild every route the binary serves\n" as *u8); sys_exit(0); return 0 } 128 gw("SRCDIVERGE RED -- a rebuild would DELETE live capability; restore source before ANY /api/build\n" as *u8); sys_exit(1); return 1 129}