nx_gate_total_scan.nx source
↩ module page · 184 lines · 8114 B
1// nx_gate_total_scan.nx -- scans *_gate.nx sources for the DUPLICATED tooth-total magic number.
2// The corpus-dominant gate epilogue writes the total TWICE:
3// g_puts("/15 verdict=" ...) <- the printed denominator
4// if pass[0] == 15 { GREEN } <- the verdict comparison
5// Both must also agree with a THIRD thing (the real number of assertions), so it is a three-way
6// drift. This organ proves the mechanically-checkable half exactly: printed N vs compared N.
7// A gate printing /15 while comparing ==14 is LYING ABOUT ITS OWN COVERAGE.
8//
9// Verdicts per file: OK (both found, equal) · MISMATCH (both found, differ) ·
10// UNPAIRED (printed N with no comparison to check it against) · SKIP (neither)
11// usage: nx_gate_total_scan <dir> [dir2] [dir3] exit 0 clean, 1 if any MISMATCH
12// license_tier: ORIGINAL expect_exit: 0
13import "nx_syscalls.nx"
14
15const S_NAMECAP: i64 = 4096
16const S_DENTCAP: i64 = 65536
17
18func s_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
19func s_putn(v: i64) -> i64 {
20 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
21 var m: i64 = v
22 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
23 let d: *u8 = sys_mmap(32); var k: i64 = 0
24 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
25 let o: *u8 = sys_mmap(32); var w: i64 = 0
26 while w < k { o[w] = d[k-1-w]; w = w + 1 }
27 sys_write(1, o, k)
28 sys_munmap(d, 32); sys_munmap(o, 32)
29 return 0
30}
31func s_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
32// find `needle` in hay[0..hn); return index or -1
33func s_find(hay: *u8, hn: i64, needle: *u8, from: i64) -> i64 {
34 let nl: i64 = s_slen(needle)
35 if nl == 0 { return 0 - 1 }
36 var i: i64 = from
37 while i + nl <= hn {
38 var k: i64 = 0
39 var ok: i64 = 1
40 while k < nl { if hay[i+k] != needle[k] { ok = 0; k = nl } k = k + 1 }
41 if ok == 1 { return i }
42 i = i + 1
43 }
44 return 0 - 1
45}
46// read a non-negative int at hay[p]; -1 if no digits
47func s_int(hay: *u8, hn: i64, p: i64) -> i64 {
48 var i: i64 = p
49 var v: i64 = 0
50 var got: i64 = 0
51 var d: i64 = 0
52 while d == 0 {
53 if i >= hn { d = 1 } else {
54 if hay[i] < (48 as u8) { d = 1 } else {
55 if hay[i] > (57 as u8) { d = 1 } else { v = v * 10 + ((hay[i] as i64) - 48); i = i + 1; got = 1 }
56 }
57 }
58 }
59 if got == 0 { return 0 - 1 }
60 return v
61}
62// the printed denominator: the int inside a `"/<N> verdict` literal
63// MUST scan EVERY `"/` candidate: a gate with an earlier path literal like "/tmp/x" would abandon
64// the search on the first miss. That narrowness skipped 656 of 668 files on the first run -- the
65// skip counter in main() is what exposed it, so keep that counter loud.
66func s_printed(buf: *u8, n: i64) -> i64 {
67 var from: i64 = 0
68 var res: i64 = 0 - 1
69 var done: i64 = 0
70 while done == 0 {
71 let q: i64 = s_find(buf, n, "\"/" as *u8, from)
72 if q < 0 { done = 1 } else {
73 let v: i64 = s_int(buf, n, q + 2)
74 if v >= 0 {
75 let vd: i64 = s_find(buf, n, " verdict" as *u8, q)
76 if vd >= 0 { if vd - q <= 12 { res = v; done = 1 } }
77 }
78 if done == 0 { from = q + 2 }
79 }
80 }
81 return res
82}
83// Does the printed denominator appear as a comparison ANYWHERE in the file?
84//
85// DELIBERATELY WEAKER THAN A POSITIONAL MATCH, and that is the point. Two earlier positional
86// heuristics both produced FALSE POSITIVES: anchoring on `] == ` matched test assertions like
87// `snw[0] == 1` instead of the verdict, because some gates count with a SCALAR `pass` and compare
88// `pass == 6` while others use `pass[0] == 6`, and the comparison sits BEFORE the print in one
89// idiom and AFTER it in the other. Rather than cascade a third positional patch, this makes an
90// EXACT claim that holds across every idiom: printed N is CONSISTENT iff `== N` occurs somewhere.
91// A gate that prints /15 while only ever comparing ==14 cannot hide from this; a gate whose
92// counter style we do not recognise is never falsely accused.
93func s_has_cmp(buf: *u8, n: i64, pv: i64) -> i64 {
94// Accepts BOTH exact idioms: a literal comparison `== N`, and a named const `const G_TOTAL: i64 = N`
95// compared symbolically. The const form is the BETTER pattern (single source of truth) -- an earlier
96// pass flagged the three best-built gates in the corpus as SUSPECT precisely because it only looked
97// for `== N`. Checking `= N` as well covers the const definition without any positional guessing.
98 var from: i64 = 0
99 var found: i64 = 0
100 var done: i64 = 0
101 while done == 0 {
102 let c: i64 = s_find(buf, n, "= " as *u8, from)
103 if c < 0 { done = 1 } else {
104 let v: i64 = s_int(buf, n, c + 2)
105 if v == pv { found = 1; done = 1 }
106 from = c + 2
107 }
108 }
109 return found
110}
111// does this name end in "_gate.nx"?
112func s_is_gate(name: *u8) -> i64 {
113 let l: i64 = s_slen(name)
114 if l < 8 { return 0 }
115 let suf: *u8 = "_gate.nx" as *u8
116 var k: i64 = 0
117 while k < 8 { if name[l-8+k] != suf[k] { return 0 } k = k + 1 }
118 return 1
119}
120
121func main(argc: i64, argv: *i64) -> i64 {
122 if argc < 2 { s_puts("REFUSED reason=no_dir\n" as *u8); return 2 }
123 var nok: i64 = 0
124 var nmis: i64 = 0
125 var nunp: i64 = 0
126 var nskip: i64 = 0
127 var ai: i64 = 1
128 while ai < argc {
129 let dir: *u8 = argv[ai] as *u8
130 let fd: i64 = sys_openat_rd(dir)
131 if fd < 0 { s_puts("WARN unreadable dir " as *u8); s_puts(dir); s_puts("\n" as *u8) } else {
132 let db: *u8 = sys_mmap(S_DENTCAP)
133 var nr: i64 = sys_getdents64(fd, db, S_DENTCAP)
134 while nr > 0 {
135 var off: i64 = 0
136 while off < nr {
137 let rec: *u8 = (db as i64 + off) as *u8
138 let rl: i64 = dirent_reclen(rec)
139 let nm: *u8 = dirent_name(rec)
140 if s_is_gate(nm) == 1 {
141 let path: *u8 = sys_mmap(S_NAMECAP)
142 var pi: i64 = 0
143 var di: i64 = 0
144 while dir[di] != (0 as u8) { path[pi] = dir[di]; pi = pi + 1; di = di + 1 }
145 path[pi] = 47 as u8
146 pi = pi + 1
147 var ni: i64 = 0
148 while nm[ni] != (0 as u8) { path[pi] = nm[ni]; pi = pi + 1; ni = ni + 1 }
149 path[pi] = 0 as u8
150 let cl: *i64 = sys_mmap(16) as *i64
151 cl[0] = 0
152 let buf: *u8 = sys_read_file(path, cl)
153 let bn: i64 = cl[0]
154 if (buf as i64) != 0 {
155 let pv: i64 = s_printed(buf, bn)
156 if pv < 0 { nskip = nskip + 1 } else {
157 if s_has_cmp(buf, bn, pv) == 1 { nok = nok + 1 } else {
158 nmis = nmis + 1
159 s_puts("SUSPECT " as *u8); s_puts(path)
160 s_puts(" printed=" as *u8); s_putn(pv)
161 s_puts(" but no == " as *u8); s_putn(pv)
162 s_puts(" anywhere\n" as *u8)
163 }
164 }
165 }
166 sys_munmap(path, S_NAMECAP)
167 }
168 if rl <= 0 { off = nr } else { off = off + rl }
169 }
170 nr = sys_getdents64(fd, db, S_DENTCAP)
171 }
172 sys_close(fd)
173 sys_munmap(db, S_DENTCAP)
174 }
175 ai = ai + 1
176 }
177 s_puts("GATE-TOTAL-SCAN ok=" as *u8); s_putn(nok)
178 s_puts(" mismatch=" as *u8); s_putn(nmis)
179 s_puts(" unpaired=" as *u8); s_putn(nunp)
180 s_puts(" skipped=" as *u8); s_putn(nskip)
181 if nmis > 0 { s_puts(" verdict=RED\n" as *u8); return 1 }
182 s_puts(" verdict=GREEN\n" as *u8)
183 return 0
184}