nx_emitmap_gate.nx source
↩ module page · 227 lines · 12258 B
1// nx_emitmap_gate.nx -- referee for nx_emitmap (emission provenance: which function emits this text, which
2// constant bounds this buffer). END-TO-END: forks the DEPLOYED ./nx_emitmap.elf into a /tmp/<gate>/ fixture
3// artifact (never the production one), then asks it the questions it exists to answer.
4// T1 census exits 0 and its stamp (LAST line, by position) carries asof, corpus_complete=1, files > 0
5// T2 partition: E rows counted == blocks declared, C rows == consts, R rows == reads (the stamp is a claim)
6// T3 positive control: find of the catalogue's refusal text names nx_catalog.nx / main (a stable emitter this
7// lane owns; the transport files that motivated the organ were mid-rewrite by a sibling seat when v1 of
8// this gate ran, and a control naming a file another seat is editing measures the edit, not the organ)
9// T4 positive control 2: find "capability denied for tool" names nx_tools_api.nx (the reply every seat reads)
10// T5 anti-vacuity: find of a fabricated literal prints 0 sites and exits 1; the needle is ASSEMBLED AT RUNTIME
11// (v1 carried it in this source and the census indexed the gate's own fixture -- a known class)
12// T6 bound CLB_TOK_CAP names its DEFINITION in nx_catalog_lib.nx (C row, top level) and a READ inside clb_classify
13// T7 neg-control-bound: a runtime-assembled fabricated *_MAX name prints 0 rows and exits 1
14// T8 invariant over every row: file:line parses with line > 0 and the function field is non-empty
15// T9 blind spot DECLARED: the stamp names it, so no reader mistakes a literal index for a byte-code index
16// license_tier: ORIGINAL expect_exit: 0 GREEN | 1 RED | 3 SKIP
17import "nx_syscalls.nx"
18import "nx_tool_run.nx"
19import "nx_gate_verdict.nx"
20
21const EG_CAP: i64 = 1048576 // capture of one query: the emitter rows for one needle; announces if it fills
22const EG_FIX_DIR: *u8 = "/tmp/nx_emitmap_gate"
23const EG_FIX_OUT: *u8 = "/tmp/nx_emitmap_gate/emitmap.tsv"
24const EG_ELF: *u8 = "./nx_emitmap.elf"
25
26func eg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
27func eg_starts(s: *u8, p: *u8) -> i64 { var i: i64 = 0; while p[i] != (0 as u8) { if s[i] != p[i] { return 0 } i = i + 1 } return 1 }
28func eg_digits(b: *u8, i: i64) -> i64 {
29 var v: i64 = 0
30 var any: i64 = 0
31 var j: i64 = i
32 var go: i64 = 1
33 while go == 1 {
34 let c: i64 = b[j] as i64
35 if c >= 48 { if c <= 57 { v = v * 10 + (c - 48); any = 1; j = j + 1 } else { go = 0 } } else { go = 0 }
36 }
37 if any == 0 { return 0 - 1 }
38 return v
39}
40func eg_kv(line: *u8, key: *u8) -> i64 {
41 let kl: i64 = eg_slen(key)
42 var i: i64 = 0
43 while line[i] != (0 as u8) {
44 if eg_starts((line as i64 + i) as *u8, key) == 1 { return eg_digits(line, i + kl) }
45 i = i + 1
46 }
47 return 0 - 1
48}
49// run the organ with up to 3 args; capture into cap (NUL-terminated); returns rc
50func eg_run(a1: *u8, a2: *u8, a3: *u8, cap: *u8, clen: *i64) -> i64 {
51 let av: *i64 = sys_mmap(6 * 8) as *i64
52 av[0] = EG_ELF as i64
53 av[1] = a1 as i64
54 av[2] = a2 as i64
55 av[3] = a3 as i64
56 av[4] = 0
57 if (a3 as i64) == 0 { av[3] = 0 }
58 if (a2 as i64) == 0 { av[2] = 0 }
59 let rc: i64 = tr_run_capture(EG_ELF, av, cap, EG_CAP - 1, clen)
60 cap[clen[0]] = 0 as u8
61 if clen[0] >= EG_CAP - 1 { gv_puts(" capture FULL at EG_CAP (announced; the rows below are a prefix)\n" as *u8) }
62 return rc
63}
64// does the capture hold a row whose file:line field starts with `file` and whose function field equals `fn`?
65func eg_has_site(cap: *u8, n: i64, kind: i64, file: *u8, fn: *u8) -> i64 {
66 var p: i64 = 0
67 while p < n {
68 var e: i64 = p
69 while e < n { if cap[e] == (10 as u8) { break } e = e + 1 }
70 if e - p > 4 { if (cap[p] as i64) == kind { if cap[p + 1] == (9 as u8) {
71 let f0: i64 = p + 2
72 if eg_starts((cap as i64 + f0) as *u8, file) == 1 {
73 // function field = after the 2nd tab
74 var tabs: i64 = 0
75 var q: i64 = p
76 var fs: i64 = 0 - 1
77 while q < e { if cap[q] == (9 as u8) { tabs = tabs + 1; if tabs == 2 { fs = q + 1; q = e } } q = q + 1 }
78 if fs >= 0 {
79 let fl: i64 = eg_slen(fn)
80 var same: i64 = 1
81 var k: i64 = 0
82 while k < fl { if cap[fs + k] != fn[k] { same = 0; k = fl } else { k = k + 1 } }
83 if same == 1 { if cap[fs + fl] == (9 as u8) { return 1 } }
84 }
85 }
86 } } }
87 p = e + 1
88 }
89 return 0
90}
91
92func main(argc: i64, argv: *i64) -> i64 {
93 gv_head("NX-EMITMAP-GATE: emission provenance end to end -- census, find, bound, both controls" as *u8)
94 let ctr: *i64 = gv_ctr()
95 sys_mkdir(EG_FIX_DIR, MODE_0755)
96 let cap: *u8 = sys_mmap(EG_CAP)
97 let clen: *i64 = sys_mmap(16) as *i64
98 // ---- census into the fixture path ----
99 let rc: i64 = eg_run("census" as *u8, EG_FIX_OUT, 0 as *u8, cap, clen)
100 gv_puts(" census rc=" as *u8); gv_num(rc); gv_puts(" receipt: " as *u8); gv_puts(cap)
101 let lp: *i64 = sys_mmap(16) as *i64
102 let b: *u8 = sys_read_file(EG_FIX_OUT, lp)
103 var n: i64 = lp[0]
104 if (b as i64) == 0 { n = 0 }
105 var ok1: i64 = 0
106 var asof: i64 = 0 - 1
107 var files: i64 = 0 - 1
108 var blocks: i64 = 0 - 1
109 var consts: i64 = 0 - 1
110 var reads: i64 = 0 - 1
111 var blind: i64 = 0
112 var datalen: i64 = 0
113 if n > 2 {
114 var ls: i64 = n - 1
115 if b[ls] == (10 as u8) { ls = ls - 1 }
116 while ls > 0 { if b[ls - 1] == (10 as u8) { break } ls = ls - 1 }
117 datalen = ls
118 let last: *u8 = sys_mmap(n - ls + 2)
119 var q: i64 = 0
120 while ls + q < n { if b[ls + q] == (10 as u8) { break } last[q] = b[ls + q]; q = q + 1 }
121 last[q] = 0 as u8
122 if eg_starts(last, "# asof=" as *u8) == 1 { ok1 = 1 }
123 asof = eg_kv(last, "asof=" as *u8)
124 files = eg_kv(last, "files=" as *u8)
125 blocks = eg_kv(last, "blocks=" as *u8)
126 consts = eg_kv(last, "consts=" as *u8)
127 reads = eg_kv(last, "reads=" as *u8)
128 if tr_contains(last, q, "corpus_complete=1" as *u8) == 0 { ok1 = 0 }
129 if tr_contains(last, q, "blind_spot=byte-code-emissions-not-indexed" as *u8) == 1 { blind = 1 }
130 gv_puts(" stamp: " as *u8); gv_puts(last); gv_puts("\n" as *u8)
131 }
132 var t1: i64 = 0
133 if rc == 0 { if ok1 == 1 { if asof > 0 { if files > 0 { t1 = 1 } } } }
134 gv_check("T1 census exits 0; stamp carries asof, corpus_complete=1 and files > 0" as *u8, t1, ctr)
135 // ---- count rows by kind and check the per-row invariant ----
136 var ce: i64 = 0
137 var cc: i64 = 0
138 var cr: i64 = 0
139 var bad: i64 = 0
140 var p: i64 = 0
141 while p < datalen {
142 var e: i64 = p
143 while e < datalen { if b[e] == (10 as u8) { break } e = e + 1 }
144 if e > p {
145 let k: i64 = b[p] as i64
146 if k == 69 { ce = ce + 1 }
147 if k == 67 { cc = cc + 1 }
148 if k == 82 { cr = cr + 1 }
149 // file:line then a non-empty function: find ':' before the 2nd tab and digits after it
150 var tabs: i64 = 0
151 var q2: i64 = p
152 var colon: i64 = 0 - 1
153 var t2: i64 = 0 - 1
154 var t3: i64 = 0 - 1
155 while q2 < e {
156 if b[q2] == (9 as u8) { tabs = tabs + 1; if tabs == 2 { t2 = q2 } if tabs == 3 { t3 = q2; q2 = e } }
157 if tabs == 1 { if b[q2] == (58 as u8) { colon = q2 } }
158 q2 = q2 + 1
159 }
160 var rowok: i64 = 0
161 if colon > 0 { if t2 > colon { if t3 > t2 + 1 { if eg_digits(b, colon + 1) > 0 { rowok = 1 } } } }
162 if rowok == 0 { bad = bad + 1 }
163 }
164 p = e + 1
165 }
166 gv_subjects("rows in the fixture artifact" as *u8, ce + cc + cr, ctr)
167 gv_puts(" E=" as *u8); gv_num(ce); gv_puts(" C=" as *u8); gv_num(cc); gv_puts(" R=" as *u8); gv_num(cr)
168 gv_puts(" declared blocks=" as *u8); gv_num(blocks); gv_puts(" consts=" as *u8); gv_num(consts); gv_puts(" reads=" as *u8); gv_num(reads); gv_puts(" bad_rows=" as *u8); gv_num(bad); gv_puts("\n" as *u8)
169 var t2ok: i64 = 0
170 if ce == blocks { if cc == consts { if cr == reads { if ce > 0 { t2ok = 1 } } } }
171 gv_check("T2 partition: counted E/C/R rows equal the stamp's blocks/consts/reads" as *u8, t2ok, ctr)
172 // ---- T3: the transport's own over-cap literal ----
173 let r3: i64 = eg_run("find" as *u8, "NX-CATALOG REFUSED: tool_allowlist.conf unreadable" as *u8, EG_FIX_OUT, cap, clen)
174 let h3: i64 = eg_has_site(cap, clen[0], 69, "buildroot/runtime/nx_catalog.nx:" as *u8, "main" as *u8)
175 gv_puts(" find(catalog refusal) rc=" as *u8); gv_num(r3); gv_puts(" names nx_catalog.nx main=" as *u8); gv_num(h3); gv_puts("\n" as *u8)
176 var t3v: i64 = 0
177 if r3 == 0 { if h3 == 1 { t3v = 1 } }
178 gv_check("T3 positive control: the catalogue's refusal literal is attributed to nx_catalog.nx main" as *u8, t3v, ctr)
179 let r4: i64 = eg_run("find" as *u8, "capability denied for tool" as *u8, EG_FIX_OUT, cap, clen)
180 var h4: i64 = 0
181 if tr_contains(cap, clen[0], "buildroot/runtime/nx_tools_api.nx:" as *u8) == 1 { h4 = 1 }
182 var t4v: i64 = 0
183 if r4 == 0 { if h4 == 1 { t4v = 1 } }
184 gv_check("T4 positive control: the cap-denied reply is attributed to nx_tools_api.nx" as *u8, t4v, ctr)
185 // the fabricated needle is ASSEMBLED AT RUNTIME from two halves: a detector that scans source finds its
186 // own test fixture (measured on v1 of this gate -- the census indexed the gate's own literal and T5 failed)
187 let fab: *u8 = sys_mmap(64)
188 var fo: i64 = 0
189 let fa: *u8 = "zzq-no-such-emit" as *u8
190 let fb: *u8 = "ted-lit-9f1-qqz" as *u8
191 var fi: i64 = 0
192 while fa[fi] != (0 as u8) { fab[fo] = fa[fi]; fo = fo + 1; fi = fi + 1 }
193 fi = 0
194 while fb[fi] != (0 as u8) { fab[fo] = fb[fi]; fo = fo + 1; fi = fi + 1 }
195 fab[fo] = 0 as u8
196 let r5: i64 = eg_run("find" as *u8, fab, EG_FIX_OUT, cap, clen)
197 var t5v: i64 = 0
198 if r5 == 1 { if tr_contains(cap, clen[0], "sites=0" as *u8) == 1 { t5v = 1 } }
199 gv_check("T5 neg-control-find: a fabricated literal prints sites=0 and exits 1 (anti-vacuity)" as *u8, t5v, ctr)
200 // a STABLE bound this lane owns: the transport's TSV_REQ_CAP was mid-rewrite by a sibling seat when v1 of
201 // this gate ran, and a control that names a file another seat is editing measures the edit, not the organ
202 let r6: i64 = eg_run("bound" as *u8, "CLB_TOK_CAP" as *u8, EG_FIX_OUT, cap, clen)
203 let d6: i64 = eg_has_site(cap, clen[0], 67, "buildroot/runtime/nx_catalog_lib.nx:" as *u8, "-" as *u8)
204 let u6: i64 = eg_has_site(cap, clen[0], 82, "buildroot/runtime/nx_catalog_lib.nx:" as *u8, "clb_classify" as *u8)
205 gv_puts(" bound(CLB_TOK_CAP) rc=" as *u8); gv_num(r6); gv_puts(" def_in_lib=" as *u8); gv_num(d6); gv_puts(" read_in_clb_classify=" as *u8); gv_num(u6); gv_puts("\n" as *u8)
206 var t6v: i64 = 0
207 if r6 == 0 { if d6 == 1 { if u6 == 1 { t6v = 1 } } }
208 gv_check("T6 bound: CLB_TOK_CAP's definition (top level) and its read inside clb_classify are both named" as *u8, t6v, ctr)
209 let fabc: *u8 = sys_mmap(64)
210 var fco: i64 = 0
211 let fca: *u8 = "ZZQ_NOT_A_CO" as *u8
212 let fcb: *u8 = "NST_QQZ_MAX" as *u8
213 var fci: i64 = 0
214 while fca[fci] != (0 as u8) { fabc[fco] = fca[fci]; fco = fco + 1; fci = fci + 1 }
215 fci = 0
216 while fcb[fci] != (0 as u8) { fabc[fco] = fcb[fci]; fco = fco + 1; fci = fci + 1 }
217 fabc[fco] = 0 as u8
218 let r7: i64 = eg_run("bound" as *u8, fabc, EG_FIX_OUT, cap, clen)
219 var t7v: i64 = 0
220 if r7 == 1 { if tr_contains(cap, clen[0], "sites=0" as *u8) == 1 { t7v = 1 } }
221 gv_check("T7 neg-control-bound: a fabricated *_MAX name prints sites=0 and exits 1" as *u8, t7v, ctr)
222 var t8v: i64 = 0
223 if bad == 0 { if ce + cc + cr > 0 { t8v = 1 } }
224 gv_check("T8 invariant on every row: file:line with line > 0 and a non-empty function field" as *u8, t8v, ctr)
225 gv_check("T9 the blind spot (byte-code emissions) is DECLARED in the stamp" as *u8, blind, ctr)
226 return gv_verdict("EMITMAP-GATE" as *u8, ctr, "emission provenance: census, partition, find both ways, bound both ways" as *u8)
227}