code wiki / (root) / nx_fd_hygiene_gate.nx

nx_fd_hygiene_gate.nx source

↩ module page · 48 lines · 2356 B

1// nx_fd_hygiene_gate.nx -- prove nx_fd_scrub_inherited() closes inherited fds >= 3 while PRESERVING stdio. 2// This is the regression tooth for the mgmt :18098 fd-leak cure (2026-07-17): if the scrub ever stops closing 3// inherited fds, a supervisor would leak listen sockets into its children again. license_tier: ORIGINAL 4// expect_exit: 0 5import "nx_fd_hygiene.nx" 6import "nx_syscalls.nx" 7 8func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func pn(v: i64) -> i64 { 10 let b: *u8 = sys_mmap(32) as *u8 11 var x: i64 = v; if x < 0 { sys_write(1,"-" as *u8,1); x = 0 - x } 12 var i: i64 = 31 13 if x == 0 { b[i] = 48 as u8; i = i - 1 } 14 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 } 15 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i) 16 return 0 17} 18 19func main() -> i64 { 20 w("=== nx_fd_hygiene_gate -- inherited-fd scrub (mgmt :18098 leak cure) ===\n" as *u8) 21 var fails: i64 = 0 22 23 // open 3 marker fds (>= 3); /dev/null is always openable. 24 let a: i64 = sys_openat_rd("/dev/null" as *u8) 25 let b: i64 = sys_openat_rd("/dev/null" as *u8) 26 let c: i64 = sys_openat_rd("/dev/null" as *u8) 27 w("T1 opened markers a=" as *u8); pn(a); w(" b=" as *u8); pn(b); w(" c=" as *u8); pn(c); w("\n" as *u8) 28 if a < 3 { fails = fails + 1 } 29 if b < 3 { fails = fails + 1 } 30 if c < 3 { fails = fails + 1 } 31 32 // scrub -- must close a,b,c (and any other inherited fd >= 3). 33 let closed: i64 = nx_fd_scrub_inherited() 34 w("T2 scrub closed " as *u8); pn(closed); w(" fds\n" as *u8) 35 if closed < 3 { fails = fails + 1 } 36 37 // each marker is now CLOSED: re-closing returns EBADF (nonzero), never 0. 38 if sys_close(a) == 0 { fails = fails + 1; w(" BAD a still open\n" as *u8) } 39 if sys_close(b) == 0 { fails = fails + 1; w(" BAD b still open\n" as *u8) } 40 if sys_close(c) == 0 { fails = fails + 1; w(" BAD c still open\n" as *u8) } 41 42 // NEG-CONTROL: stdio survived -- this very write proves fd 1 is still open after the scrub. 43 w("T3 stdio preserved (you are reading this on fd 1 AFTER the scrub)\n" as *u8) 44 45 if fails == 0 { w("FD-HYGIENE-GATE 3/3 verdict=GREEN -- scrub closes inherited fds>=3, preserves stdio\n" as *u8); sys_exit(0); return 0 } 46 w("FD-HYGIENE-GATE RED fails=" as *u8); pn(fails); w("\n" as *u8) 47 sys_exit(1); return 1 48}