code wiki / (root) / nx_map_file_gate.nx

nx_map_file_gate.nx source

↩ module page · 96 lines · 4604 B

1// nx_map_file_gate.nx -- gates the DEBT-EATEN file I/O floor (2026-07-15): (a) sys_read_file now sizes its 2// buffer from the file itself (the old fixed 4 GiB cap SILENTLY TRUNCATED bigger files into plausible-garbage 3// tensor reads); (b) NEW sys_map_file = read-only file-backed mmap (any size, zero-copy, only touched pages 4// resident -- the lazy-MoE serving shape). Teeth: 5// T1 sys_map_file: exact length + BYTE-IDENTICAL to sys_read_file on a patterned temp file 6// T2 honesty: absent path -> 0 pointer + 0 length (both functions) 7// T3 read-only by construction: the mapping's first byte reads correctly twice (stability), and the 8// pattern check covers head/middle/tail (page boundaries crossed) 9// expect_exit: 0 license_tier: ORIGINAL No hw writes (Rule 26). 10import "nx_syscalls.nx" 11import "nx_gate_verdict.nx" 12 13func mf_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 14func mf_n(v: i64) -> i64 { 15 var m: i64 = v 16 if m < 0 { mf_w("-" as *u8); m = 0 - m } 17 let t: *u8 = sys_mmap(24) 18 var k: i64 = 0 19 if m == 0 { t[0] = 48 as u8; k = 1 } 20 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 21 let o: *u8 = sys_mmap(24) 22 var i: i64 = 0 23 while i < k { o[i] = t[k - 1 - i]; i = i + 1 } 24 sys_write(1, o, k) 25 return 0 26} 27 28func main() -> i64 { 29 mf_w("=== NX-MAP-FILE -- exact-size read + zero-copy read-only file map ===\n" as *u8) 30 let path: *u8 = "/tmp/nx_map_file_gate.bin" as *u8 31 let N: i64 = 20000 // > 4 pages: head/middle/tail cross page boundaries 32 let wfd: i64 = sys_openat_wr(path, 0x1a4) 33 if wfd < 0 { mf_w("cannot create temp file\n" as *u8); return 1 } 34 let wb: *u8 = sys_mmap(N) 35 var i: i64 = 0 36 while i < N { wb[i] = ((i * 131) % 251) as u8; i = i + 1 } 37 var off: i64 = 0 38 while off < N { let w: i64 = sys_write(wfd, ((wb as i64) + off) as *u8, N - off); if w <= 0 { off = N } else { off = off + w } } 39 sys_close(wfd) 40 41 var pass: i64 = 0 42 var ttl: i64 = 0 43 // T1: map == read, byte-identical, exact length 44 let ml: *i64 = sys_mmap(8) as *i64 45 let rl: *i64 = sys_mmap(8) as *i64 46 let mp: *u8 = sys_map_file(path, ml) 47 let rp: *u8 = sys_read_file(path, rl) 48 var mism: i64 = 0 49 if (mp as i64) == 0 { mism = 1000000 } 50 if (rp as i64) == 0 { mism = mism + 1000000 } 51 if ml[0] != N { mism = mism + 1000 } 52 if rl[0] != N { mism = mism + 1000 } 53 if mism == 0 { 54 i = 0 55 while i < N { if mp[i] != rp[i] { mism = mism + 1 } i = i + 1 } 56 if mp[0] != (0 as u8) { } // touch head twice (stability) 57 if mp[0] != rp[0] { mism = mism + 1 } 58 } 59 ttl = ttl + 1 60 mf_w(" T1 map == read, len " as *u8); mf_n(ml[0]); mf_w("/" as *u8); mf_n(N); mf_w(", mism " as *u8); mf_n(mism); mf_w(": " as *u8) 61 if mism == 0 { pass = pass + 1; mf_w("PASS\n" as *u8) } else { mf_w("FAIL\n" as *u8) } 62 // T2: absent path honest 63 let al: *i64 = sys_mmap(8) as *i64 64 let ap: *u8 = sys_map_file("/tmp/nx_no_such_file_xyz.bin" as *u8, al) 65 let al2: *i64 = sys_mmap(8) as *i64 66 let ap2: *u8 = sys_read_file("/tmp/nx_no_such_file_xyz.bin" as *u8, al2) 67 ttl = ttl + 1 68 var ok2: i64 = 1 69 if (ap as i64) != 0 { ok2 = 0 } 70 if al[0] != 0 { ok2 = 0 } 71 if (ap2 as i64) != 0 { ok2 = 0 } 72 if al2[0] != 0 { ok2 = 0 } 73 mf_w(" T2 absent path -> 0/0 from both: " as *u8) 74 if ok2 == 1 { pass = pass + 1; mf_w("PASS\n" as *u8) } else { mf_w("FAIL\n" as *u8) } 75 // T3 pattern spot-checks across pages via the MAP 76 ttl = ttl + 1 77 var ok3: i64 = 1 78 if (mp as i64) != 0 { 79 if (mp[0] as i64) != 0 { ok3 = 0 } 80 if (mp[4097] as i64) != ((4097 * 131) % 251) { ok3 = 0 } 81 if (mp[N-1] as i64) != (((N-1) * 131) % 251) { ok3 = 0 } 82 } else { ok3 = 0 } 83 mf_w(" T3 mapped pattern head/middle/tail exact: " as *u8) 84 if ok3 == 1 { pass = pass + 1; mf_w("PASS\n" as *u8) } else { mf_w("FAIL\n" as *u8) } 85 86 mf_w("NX-MAP-FILE-GATE passed " as *u8); mf_n(pass); mf_w("/" as *u8); mf_n(ttl) 87 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 88 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 89 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 90 let ctr__dry: *i64 = gv_ctr() 91 ctr__dry[0] = pass 92 ctr__dry[1] = ttl 93 let rc__dry: i64 = gv_verdict("MAP-FILE-GATE" as *u8, ctr__dry, "exact-size reads + zero-copy read-only maps)" as *u8) 94 sys_exit(rc__dry) 95 return rc__dry 96}