code wiki / _hdl_build / nx_magicname.nx
nx_magicname.nx source
↩ module page · 258 lines · 11008 B
1// nx_magicname.nx -- THE MEANINGLESS-NAME DETECTOR, and the ratchet that keeps the number falling.
2//
3// THE DEFECT IT MEASURES, stated plainly: the estate's own rule-11 remedy manufactures the disease.
4// nx_law_warden counts BARE LITERALS and nx_magic apply hoists them to a named const -- but it names them
5// X_MAGIC_<value>, which restates the number instead of saying what it MEANS. The ratchet then reads GREEN
6// while the code is no more legible than before: a magic number wearing a costume, and harder to grep for
7// because it no longer looks like a literal. Measured cases from one afternoon: BG_MAGIC_40500 was hiding
8// BHASKARA I'S SINE APPROXIMATION; AS_MAGIC_73856093/19349663/83492791 were hiding the TESCHNER 2003
9// spatial-hash primes; AS_MAGIC_8388607 was an IEEE-754 mantissa mask sharing its value with an unrelated
10// triangle cap. In every case the value was right and the NAME was the defect.
11//
12// THE RULE, mechanical and unarguable: a const whose identifier contains _MAGIC_ followed only by digits is
13// named for its value. That is decidable from the identifier alone -- no heuristic, no threshold, no sample.
14//
15// RATCHET: knowledge/status/magicname.conf holds the agreed ceiling. The count may FALL freely; a RISE is
16// refused. First run with no conf SEEDS it and says so, rather than inventing a floor and calling it a pass.
17// It NAMES every offender it counts -- a count without a worklist is not actionable (banked law).
18// nx_magicname [dir1] [dir2] default: runtime/_hdl_build and runtime
19// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
20import "nx_syscalls.nx"
21
22const MN_CONF: *u8 = "knowledge/status/magicname.conf"
23const MN_DIRBUF: i64 = 131072
24const MN_FILEBUF: i64 = 1048576 // per-source read window; a source over this is REPORTED, never silently cut
25const MN_NAMEBUF: i64 = 4096
26const MN_MAXSHOW: i64 = 40 // the printed worklist head; the COUNT is always the full population
27const MN_CH_COLON: i64 = 58
28const MN_CH_NL: i64 = 10
29const MN_CH_SP: i64 = 32
30const MN_CH_0: i64 = 48
31const MN_CH_9: i64 = 57
32const MN_DEC: i64 = 10
33// ★ANTI-VACUITY FLOOR, earned on this organ's OWN first run. The default dirs were CWD-relative, so from
34// the daemon's working directory they resolved to a near-empty tree: it scanned 24 files, found 0, and
35// reported GREEN. A census that cannot see the corpus must REFUSE, never pass -- so a scan that finds
36// fewer sources than this says UNPROVEN and touches nothing. (Same class as the banked seq789 CWD trap.)
37const MN_MIN_FILES: i64 = 1000
38
39func mn_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
40func mn_num(v: i64) -> i64 {
41 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
42 var m: i64 = v
43 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
44 let t: *u8 = sys_mmap(32)
45 let o: *u8 = sys_mmap(32)
46 var k: i64 = 0
47 while m > 0 { t[k] = (MN_CH_0 + (m % MN_DEC)) as u8; m = m / MN_DEC; k = k + 1 }
48 var i: i64 = 0
49 while i < k { o[i] = t[k-1-i]; i = i + 1 }
50 sys_write(1, o, k)
51 sys_munmap(t, 32)
52 sys_munmap(o, 32)
53 return 0
54}
55func mn_is_nx(nm: *u8) -> i64 {
56 var n: i64 = 0
57 while nm[n] != (0 as u8) { n = n + 1 }
58 if n <= 3 { return 0 }
59 if nm[n-3] != (46 as u8) { return 0 }
60 if nm[n-2] != (110 as u8) { return 0 }
61 if nm[n-1] != (120 as u8) { return 0 }
62 return 1
63}
64// does buf[s..e) contain _MAGIC_ followed ONLY by digits, to the end? that is a value-name.
65func mn_is_valuename(b: *u8, s: i64, e: i64) -> i64 {
66 var i: i64 = s
67 var hit: i64 = 0 - 1
68 while i + 7 <= e {
69 if b[i] == (95 as u8) {
70 if b[i+1] == (77 as u8) { if b[i+2] == (65 as u8) { if b[i+3] == (71 as u8) {
71 if b[i+4] == (73 as u8) { if b[i+5] == (67 as u8) { if b[i+6] == (95 as u8) { hit = i + 7 } } }
72 } } }
73 }
74 i = i + 1
75 }
76 if hit < 0 { return 0 }
77 if hit >= e { return 0 }
78 var j: i64 = hit
79 while j < e {
80 let c: i64 = b[j] as i64
81 if c < MN_CH_0 { return 0 }
82 if c > MN_CH_9 { return 0 }
83 j = j + 1
84 }
85 return 1
86}
87// count value-named consts in one source; prints each offender while the show budget lasts
88func mn_scan_file(path: *u8, buf: *u8, shown: *i64) -> i64 {
89 let lp: *i64 = sys_mmap(16) as *i64
90 let fd: i64 = sys_openat_rd(path)
91 if fd < 0 { return 0 }
92 var got: i64 = 0
93 var go: i64 = 1
94 while go == 1 {
95 let k: i64 = sys_read(fd, ((buf as i64) + got) as *u8, MN_FILEBUF - got)
96 if k <= 0 { go = 0 } else { got = got + k }
97 if got >= MN_FILEBUF { go = 0 }
98 }
99 sys_close(fd)
100 if got >= MN_FILEBUF {
101 mn_puts(" OVERSIZE-DECLARED " as *u8); mn_puts(path); mn_puts(" read window full, count is a FLOOR for this file\n" as *u8)
102 }
103 var n: i64 = 0
104 var p: i64 = 0
105 while p < got {
106 // a const declaration starts at column 0: "const "
107 var atline: i64 = 0
108 if p == 0 { atline = 1 } else { if b_at(buf, p-1) == MN_CH_NL { atline = 1 } }
109 if atline == 1 {
110 if p + 6 < got {
111 if buf[p] == (99 as u8) { if buf[p+1] == (111 as u8) { if buf[p+2] == (110 as u8) {
112 if buf[p+3] == (115 as u8) { if buf[p+4] == (116 as u8) { if buf[p+5] == (MN_CH_SP as u8) {
113 let s: i64 = p + 6
114 var e: i64 = s
115 while e < got { let c: i64 = buf[e] as i64; if c == MN_CH_COLON { e = got + 1 } else { if c == MN_CH_NL { e = got + 1 } else { e = e + 1 } } }
116 // e overshot by the sentinel; recompute the true end
117 var q: i64 = s
118 var fin: i64 = s
119 var stop: i64 = 0
120 while stop == 0 {
121 if q >= got { fin = q; stop = 1 } else {
122 let c2: i64 = buf[q] as i64
123 if c2 == MN_CH_COLON { fin = q; stop = 1 } else { if c2 == MN_CH_NL { fin = q; stop = 1 } else { q = q + 1 } }
124 }
125 }
126 if mn_is_valuename(buf, s, fin) == 1 {
127 n = n + 1
128 if shown[0] < MN_MAXSHOW {
129 mn_puts(" VALUE-NAMED " as *u8); mn_puts(path); mn_puts(" : " as *u8)
130 sys_write(1, ((buf as i64) + s) as *u8, fin - s)
131 mn_puts("\n" as *u8)
132 shown[0] = shown[0] + 1
133 }
134 }
135 } } } } } }
136 }
137 }
138 p = p + 1
139 }
140 return n
141}
142func b_at(b: *u8, i: i64) -> i64 { return b[i] as i64 }
143
144func mn_scan_dir(dir: *u8, buf: *u8, nbuf: *u8, shown: *i64, files: *i64) -> i64 {
145 let fd: i64 = sys_openat_rd(dir)
146 if fd < 0 { return 0 }
147 let dbuf: *u8 = sys_mmap(MN_DIRBUF)
148 var total: i64 = 0
149 var go: i64 = 1
150 while go == 1 {
151 let nb: i64 = sys_getdents64(fd, dbuf, MN_DIRBUF)
152 if nb <= 0 { go = 0 } else {
153 var off: i64 = 0
154 while off < nb {
155 let rec: *u8 = ((dbuf as i64) + off) as *u8
156 let rl: i64 = dirent_reclen(rec)
157 if rl <= 0 { off = nb } else {
158 let nm: *u8 = dirent_name(rec)
159 if mn_is_nx(nm) == 1 {
160 var c: i64 = 0
161 while dir[c] != (0 as u8) { nbuf[c] = dir[c]; c = c + 1 }
162 nbuf[c] = 47 as u8
163 c = c + 1
164 var d: i64 = 0
165 while nm[d] != (0 as u8) { nbuf[c+d] = nm[d]; d = d + 1 }
166 nbuf[c+d] = 0 as u8
167 total = total + mn_scan_file(nbuf, buf, shown)
168 files[0] = files[0] + 1
169 }
170 off = off + rl
171 }
172 }
173 }
174 }
175 sys_close(fd)
176 sys_munmap(dbuf, MN_DIRBUF)
177 return total
178}
179
180func mn_read_floor() -> i64 {
181 let lp: *i64 = sys_mmap(16) as *i64
182 let b: *u8 = sys_read_file(MN_CONF, lp)
183 if (b as i64) == 0 { return 0 - 1 }
184 let n: i64 = lp[0]
185 var v: i64 = 0
186 var i: i64 = 0
187 var seen: i64 = 0
188 while i < n {
189 let c: i64 = b[i] as i64
190 if c >= MN_CH_0 { if c <= MN_CH_9 { v = v*MN_DEC + (c - MN_CH_0); seen = 1 } }
191 if c == MN_CH_NL { if seen == 1 { i = n } }
192 i = i + 1
193 }
194 if seen == 0 { return 0 - 1 }
195 return v
196}
197func mn_write_floor(v: i64) -> i64 {
198 let fd: i64 = sys_openat_wr(MN_CONF, 0x1a4)
199 if fd < 0 { return 0 - 1 }
200 let t: *u8 = sys_mmap(32)
201 let o: *u8 = sys_mmap(32)
202 var m: i64 = v
203 var k: i64 = 0
204 if m == 0 { t[0] = MN_CH_0 as u8; k = 1 }
205 while m > 0 { t[k] = (MN_CH_0 + (m % MN_DEC)) as u8; m = m / MN_DEC; k = k + 1 }
206 var i: i64 = 0
207 while i < k { o[i] = t[k-1-i]; i = i + 1 }
208 o[k] = MN_CH_NL as u8
209 sys_write(fd, o, k+1)
210 sys_close(fd)
211 return 0
212}
213
214func main(argc: i64, argv: *i64) -> i64 {
215 let buf: *u8 = sys_mmap(MN_FILEBUF)
216 let nbuf: *u8 = sys_mmap(MN_NAMEBUF)
217 let shown: *i64 = sys_mmap(8) as *i64
218 let files: *i64 = sys_mmap(8) as *i64
219 shown[0] = 0
220 files[0] = 0
221 var d1: *u8 = "buildroot/runtime/_hdl_build" as *u8
222 var d2: *u8 = "buildroot/runtime" as *u8
223 if argc >= 2 { d1 = argv[1] as *u8 }
224 if argc >= 3 { d2 = argv[2] as *u8 }
225 mn_puts("nx_magicname -- consts named for their VALUE instead of their MEANING\n" as *u8)
226 var total: i64 = mn_scan_dir(d1, buf, nbuf, shown, files)
227 total = total + mn_scan_dir(d2, buf, nbuf, shown, files)
228 if shown[0] >= MN_MAXSHOW { mn_puts(" (worklist head shown; the COUNT below is the full population)\n" as *u8) }
229 mn_puts("MAGICNAME files=" as *u8); mn_num(files[0])
230 mn_puts(" value_named=" as *u8); mn_num(total)
231 if files[0] < MN_MIN_FILES {
232 mn_puts(" verdict=UNPROVEN\n" as *u8)
233 mn_puts("NX-MAGICNAME UNPROVEN: scanned fewer sources than the corpus floor, so this is a report on the\n" as *u8)
234 mn_puts(" working directory and not on the estate. The ratchet was NOT touched. Pass the source roots.\n" as *u8)
235 sys_exit(3)
236 return 3
237 }
238 let floor: i64 = mn_read_floor()
239 if floor < 0 {
240 mn_write_floor(total)
241 mn_puts(" floor=SEEDED verdict=GREEN\n" as *u8)
242 mn_puts("NX-MAGICNAME seeded the ratchet at this count; it was NOT a pass against an agreed bar\n" as *u8)
243 sys_exit(0)
244 return 0
245 }
246 mn_puts(" floor=" as *u8); mn_num(floor)
247 if total > floor {
248 mn_puts(" verdict=RED\n" as *u8)
249 mn_puts("NX-MAGICNAME RED: value-named consts ROSE above the agreed ceiling -- name the new ones for what they MEAN\n" as *u8)
250 sys_exit(1)
251 return 1
252 }
253 if total < floor { mn_write_floor(total) }
254 mn_puts(" verdict=GREEN\n" as *u8)
255 mn_puts("NX-MAGICNAME GREEN\n" as *u8)
256 sys_exit(0)
257 return 0
258}