code wiki / _hdl_build / _trunc_probe.nx
_trunc_probe.nx source
↩ module page · 46 lines · 2473 B
1// _trunc_probe.nx -- ROOT-CAUSE probe for LM-022 (operator: audit workarounds, fix roots
2// not bolt patches). The constant O_WRONLY_CT=0x241 INCLUDES O_TRUNC in both syscalls
3// files -- so does sys_openat_wr ACTUALLY truncate? If yes, LM-022 is STALE and the
4// pad-newlines + sibling-tmp+renameat workarounds sprinkled everywhere are UNNECESSARY
5// (the single biggest de-bolting). If no, it is a real bug to fix. Empirical, in nx:
6// write a LONG file -> reopen w/ sys_openat_wr, write SHORT -> read back.
7// TRUNCATES if the readback is EXACTLY the short content (no stale tail).
8// license_tier: ORIGINAL
9import "nx_syscalls.nx"
10func tp_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
11func tp_n(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; var t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
12func tp_write(path: *u8, s: *u8, n: i64) -> i64 {
13 let fd: i64 = sys_openat_wr(path, 0x1a4)
14 if fd < 0 { return 0 - 1 }
15 sys_write(fd, s, n)
16 sys_close(fd)
17 return n
18}
19func main() -> i64 {
20 tp_p("=== TRUNC PROBE (LM-022 root-cause): does sys_openat_wr truncate? ===\n" as *u8)
21 let path: *u8 = "/tmp/_nx_trunc_probe.txt" as *u8
22 // 1) write a LONG file
23 let long: *u8 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA-LONG-CONTENT-60-BYTES-XXXX\n" as *u8
24 var ln: i64 = 0
25 while long[ln] != (0 as u8) { ln = ln + 1 }
26 tp_write(path, long, ln)
27 // 2) reopen w/ sys_openat_wr, write SHORT
28 let short: *u8 = "SHORT\n" as *u8
29 tp_write(path, short, 6)
30 // 3) read back
31 let buf: *u8 = sys_mmap(4096)
32 let fd: i64 = sys_openat_rd(path)
33 let got: i64 = sys_read(fd, buf, 4095)
34 sys_close(fd)
35 tp_p(" wrote long=" as *u8); tp_n(ln); tp_p(" then short=6; read back=" as *u8); tp_n(got); tp_p(" bytes\n" as *u8)
36 if got == 6 {
37 tp_p(" readback EXACTLY 6 bytes -> sys_openat_wr TRUNCATES -> LM-022 is STALE\n" as *u8)
38 tp_p(" TRUNC PROBE: TRUNCATES (the pad-newlines + renameat workarounds are UNNECESSARY -- retire them)\n" as *u8)
39 sys_exit(0)
40 return 0
41 }
42 tp_p(" readback > 6 bytes -> stale tail -> sys_openat_wr does NOT truncate -> LM-022 REAL, fix the open\n" as *u8)
43 tp_p(" TRUNC PROBE: NO-TRUNC (real bug, root fix needed)\n" as *u8)
44 sys_exit(1)
45 return 1
46}