nx_catnlaw_gate.nx source
↩ module page · 193 lines · 8920 B
1// nx_catnlaw_gate.nx -- HOW DOES EVERY NUMBER FORMATTER IN THE ESTATE RENDER A NEGATIVE?
2//
3// WHY. nx_tools_api.ta_catn had `while m > 0` with NO negative guard, so a negative value emitted
4// ZERO CHARACTERS and produced malformed JSON -- literally `"exit_code":,"bytes":0`. A timed-out exec
5// returns a negative rc, so the one value the formatter could not print was the one that appears only
6// when something has gone wrong, and every caller had to GUESS whether its write had landed.
7//
8// Three behaviours exist in this estate and they are NOT equally bad:
9// SIGNED `if m < 0 { d[o] = 45; m = 0 - m }` -- correct
10// CLAMPED `if m < 0 { m = 0 }` -- prints 0 for -1. VALID output, WRONG number, and
11// the direction is the dangerous one: a -1 meaning
12// UNKNOWN/FAILED renders as 0, which reads as SUCCESS.
13// SILENT no guard -- emits nothing; breaks the surrounding format.
14// ★ A FORMATTER THAT CANNOT PRINT A SENTINEL TURNS AN ERROR PATH INTO EITHER A PROTOCOL VIOLATION OR
15// A FALSE SUCCESS, AND BOTH ARE INVISIBLE UNTIL SOMETHING IS ALREADY WRONG.
16//
17// Census only -- names offenders, blocks nothing. Comments STRIPPED so it measures code, not prose.
18// license_tier: ORIGINAL expect_exit: 0
19import "syscalls.nx"
20import "nx_gate_verdict.nx"
21
22const CL_CAP: i64 = 1048576
23const CL_MAXF: i64 = 24000
24const CL_NAMEBUF: i64 = 1400000
25
26func cl_read(path: *u8, buf: *u8, cap: i64) -> i64 {
27 let fd: i64 = sys_openat_rd(path)
28 if fd < 0 { return 0 - 1 }
29 var tot: i64 = 0
30 while tot < cap {
31 let n: i64 = sys_read(fd, (buf as i64 + tot) as *u8, cap - tot)
32 if n <= 0 { break }
33 tot = tot + n
34 }
35 sys_close(fd)
36 return tot
37}
38func cl_strip(src: *u8, n: i64, dst: *u8) -> i64 {
39 var i: i64 = 0
40 var o: i64 = 0
41 while i < n {
42 if src[i] == (47 as u8) {
43 if i + 1 < n { if src[i+1] == (47 as u8) { while i < n { if src[i] == (10 as u8) { break } i = i + 1 } } }
44 }
45 if i < n { dst[o] = src[i]; o = o + 1; i = i + 1 }
46 }
47 return o
48}
49func cl_find(b: *u8, n: i64, pat: *u8, from: i64) -> i64 {
50 var m: i64 = 0
51 while pat[m] != (0 as u8) { m = m + 1 }
52 var i: i64 = from
53 while i + m <= n {
54 var j: i64 = 0
55 var ok: i64 = 1
56 while j < m { if b[i+j] != pat[j] { ok = 0; j = m } else { j = j + 1 } }
57 if ok == 1 { return i }
58 i = i + 1
59 }
60 return 0 - 1
61}
62func cl_ends_nx(nm: *u8, nl: i64) -> i64 {
63 if nl < 4 { return 0 }
64 if nm[nl-3] != (46 as u8) { return 0 }
65 if nm[nl-2] != (110 as u8) { return 0 }
66 if nm[nl-1] != (120 as u8) { return 0 }
67 return 1
68}
69
70func main() -> i64 {
71 let ctr: *i64 = gv_ctr()
72 gv_head("=== NX-CATNLAW -- how every number formatter renders a negative ===" as *u8)
73 sys_chdir("buildroot" as *u8)
74
75 let names: *u8 = sys_mmap(CL_NAMEBUF)
76 let noff: *i64 = sys_mmap(8 * (CL_MAXF + 8)) as *i64
77 var nfiles: i64 = 0
78 var npos: i64 = 0
79 var dirn: i64 = 0
80 while dirn < 2 {
81 var dpath: *u8 = "runtime" as *u8
82 var pfx: *u8 = "runtime/" as *u8
83 var pfxlen: i64 = 8
84 if dirn == 1 { dpath = "runtime/_hdl_build" as *u8; pfx = "runtime/_hdl_build/" as *u8; pfxlen = 19 }
85 let dfd: i64 = __syscall(257, 0-100, dpath, 0x10000, 0, 0, 0)
86 if dfd >= 0 {
87 let dbuf: *u8 = sys_mmap(65536)
88 var go: i64 = 1
89 while go == 1 {
90 let nread: i64 = __syscall(217, dfd, dbuf, 65536, 0, 0, 0)
91 if nread <= 0 { go = 0 } else {
92 var pos: i64 = 0
93 while pos < nread {
94 let reclen: i64 = (dbuf[pos+16] as i64) | ((dbuf[pos+17] as i64) << 8)
95 var nl: i64 = 0
96 while dbuf[pos+19+nl] != (0 as u8) { nl = nl + 1 }
97 if cl_ends_nx(((dbuf as i64) + pos + 19) as *u8, nl) == 1 {
98 if nfiles < CL_MAXF {
99 if npos + nl + pfxlen + 4 < CL_NAMEBUF {
100 noff[nfiles] = npos
101 var c: i64 = 0
102 while c < pfxlen { names[npos] = pfx[c]; npos = npos + 1; c = c + 1 }
103 c = 0
104 while c < nl { names[npos] = dbuf[pos+19+c]; npos = npos + 1; c = c + 1 }
105 names[npos] = 0 as u8; npos = npos + 1
106 nfiles = nfiles + 1
107 }
108 }
109 }
110 if reclen <= 0 { pos = nread } else { pos = pos + reclen }
111 }
112 }
113 }
114 sys_close(dfd)
115 }
116 dirn = dirn + 1
117 }
118
119 let raw: *u8 = sys_mmap(CL_CAP)
120 let code: *u8 = sys_mmap(CL_CAP)
121 var total: i64 = 0
122 var signed_c: i64 = 0
123 var clamped: i64 = 0
124 var silent: i64 = 0
125 var delegated: i64 = 0
126
127 var fi: i64 = 0
128 while fi < nfiles {
129 let path: *u8 = ((names as i64) + noff[fi]) as *u8
130 // SKIP SELF. This organ carries the search literal in its own code, so it matched itself and
131 // reported the census tool as an offender. A CENSUS THAT COUNTS ITS OWN INSTRUMENT IS
132 // MEASURING ITS REFLECTION.
133 var n: i64 = 0
134 if cl_find(path, 64, "nx_catnlaw_gate" as *u8, 0) < 0 { n = cl_read(path, raw, CL_CAP) }
135 if n > 0 {
136 let cn: i64 = cl_strip(raw, n, code)
137 var at: i64 = cl_find(code, cn, "_catn(d: *u8, o: i64, v: i64)" as *u8, 0)
138 while at >= 0 {
139 total = total + 1
140 // window = the 700 bytes after the signature: enough for a one-liner or a short body
141 var wend: i64 = at + 700
142 if wend > cn { wend = cn }
143 let wlen: i64 = wend - at
144 let w: *u8 = ((code as i64) + at) as *u8
145 // DELEGATION IS NOT ABSENCE. Several formatters are `return sj_catn(d, o, v)` and
146 // sj_catn renders the sign correctly -- scoring them SILENT because their own body has
147 // no `m < 0` would condemn code that is already right.
148 // ★ A SCANNER THAT CANNOT FOLLOW A ONE-HOP DELEGATION MEASURES SYNTAX, NOT BEHAVIOUR.
149 let dele: i64 = cl_find(w, wlen, "_catn(d, o, v)" as *u8, 0)
150 // Match BOTH spacings: the compact one-liner writes `=45 as u8` and a detector that
151 // only knows the spaced form reports a CORRECT formatter as broken. A WHITESPACE-
152 // SENSITIVE DETECTOR MEASURES FORMATTING, NOT BEHAVIOUR.
153 var sgn: i64 = cl_find(w, wlen, "= 45 as u8" as *u8, 0)
154 if sgn < 0 { sgn = cl_find(w, wlen, "=45 as u8" as *u8, 0) }
155 let clp: i64 = cl_find(w, wlen, "m = 0 }" as *u8, 0)
156 let neg: i64 = cl_find(w, wlen, "m < 0" as *u8, 0)
157 if dele >= 0 {
158 delegated = delegated + 1
159 } else {
160 if sgn >= 0 {
161 signed_c = signed_c + 1
162 } else {
163 if clp >= 0 {
164 clamped = clamped + 1
165 gv_puts(" CLAMPED(-1 prints as 0) " as *u8); gv_puts(path); gv_puts("\n" as *u8)
166 } else {
167 if neg < 0 {
168 silent = silent + 1
169 gv_puts(" SILENT(emits nothing) " as *u8); gv_puts(path); gv_puts("\n" as *u8)
170 } else {
171 signed_c = signed_c + 1
172 }
173 }
174 }
175 }
176 at = cl_find(code, cn, "_catn(d: *u8, o: i64, v: i64)" as *u8, at + 8)
177 }
178 }
179 fi = fi + 1
180 }
181
182 gv_puts("\n --- CATN CENSUS (both trees, whole corpus) ---\n" as *u8)
183 gv_puts(" files_scanned=" as *u8); gv_num(nfiles); gv_puts("\n" as *u8)
184 gv_puts(" formatters=" as *u8); gv_num(total)
185 gv_puts(" SIGNED=" as *u8); gv_num(signed_c)
186 gv_puts(" CLAMPED=" as *u8); gv_num(clamped)
187 gv_puts(" SILENT=" as *u8); gv_num(silent)
188 gv_puts(" DELEGATED(to a signed impl)=" as *u8); gv_num(delegated); gv_puts("\n" as *u8)
189
190 gv_check("census-non-vacuous" as *u8, (total > 20) as i64, ctr)
191 gv_check("neg-control-partition-sums" as *u8, ((total > 0) & (signed_c + clamped + silent + delegated == total)) as i64, ctr)
192 return gv_verdict("nx_catnlaw_gate" as *u8, ctr,
193 "census only; a partition that does not sum is an assertion, not a measurement" as *u8)
194}