code wiki / _hdl_build / nx_runpath_gate.nx

nx_runpath_gate.nx source

↩ module page · 65 lines · 3701 B

1import "nx_gate_base.nx" 2// nx_runpath_gate.nx -- proves the isolation primitive (nx_runpath): two parallel workstreams (wsid gw1, gw2) 3// get SEPARATE, non-interfering namespaces under the stable RUNDIR -- so they CANNOT collide (the fix for the 4// 21-workstream /tmp free-for-all). Asserts: wsid2's write does not clobber wsid1's file; the two paths differ; 5// the path is under <RUNDIR> and NOT under /tmp (systemd-tmpfiles-safe); per-process default wsid works. 6// GREEN iff all pass. license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_runpath.nx" 9 10func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 11" as *u8); return ok } 12func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func g_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]!=(0 as u8){return 0} return 1 } 14func g_starts(a: *u8, b: *u8) -> i64 { var i: i64=0; while b[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } return 1 } 15func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" "); g_w(id); g_w(": "); if ok==1 { g_w("OK\n"); pass[0]=pass[0]+1 } else { g_w("FAIL\n") } return 0 } 16 17func rg_write(path: *u8, content: *u8) -> i64 { 18 let fd: i64 = sys_openat_wr(path, 0x1a4) 19 if fd < 0 { return 0 - 1 } 20 sys_write(fd, content, g_slen(content)); sys_close(fd) 21 return 0 22} 23func rg_read(path: *u8, buf: *u8, cap: i64) -> i64 { 24 let fd: i64 = sys_openat_rd(path) 25 if fd < 0 { return 0 - 1 } 26 let n: i64 = sys_read(fd, buf, cap - 1) 27 sys_close(fd) 28 var m: i64 = n; if m < 0 { m = 0 } 29 buf[m] = 0 as u8 30 return n 31} 32 33func main() -> i64 { 34 let pass: *i64 = sys_mmap(8) as *i64; pass[0] = 0 35 g_w("=== NX-RUNPATH GATE (per-workstream isolation, off /tmp) ===\n") 36 37 let p1: *u8 = sys_mmap(512); let p2: *u8 = sys_mmap(512) 38 let r1: *u8 = sys_mmap(256); let r2: *u8 = sys_mmap(256) 39 40 // two parallel workstreams write the SAME logical file name under DIFFERENT wsids 41 rp_ensure("gw1" as *u8); rp_path("gw1" as *u8, "probe" as *u8, p1) 42 rg_write(p1, "AAA-ws1" as *u8) 43 rp_ensure("gw2" as *u8); rp_path("gw2" as *u8, "probe" as *u8, p2) 44 rg_write(p2, "BBB-ws2" as *u8) 45 46 // ISOLATION: ws2's write did NOT touch ws1's file (they are separate by construction) 47 rg_read(p1, r1, 256); rg_read(p2, r2, 256) 48 g_row("ws1 file intact after ws2 wrote the same name" as *u8, g_streq(r1, "AAA-ws1" as *u8), pass) 49 g_row("ws2 file is its own content" as *u8, g_streq(r2, "BBB-ws2" as *u8), pass) 50 g_row("the two paths differ (no shared file)" as *u8, (g_streq(p1, p2) == 0) as i64, pass) 51 52 // off /tmp: under the stable RUNDIR, not systemd-tmpfiles-managed /tmp 53 g_row("path is under /home/.../.nishi/run (stable)" as *u8, g_starts(p1, "/home/elderwesto/.nishi/run/gw1/" as *u8), pass) 54 g_row("LIAR-KILL: path is NOT under /tmp" as *u8, (g_starts(p1, "/tmp" as *u8) == 0) as i64, pass) 55 56 // the per-workstream id resolves from NISHI_WSID env (fallback "shared") -> a coherent id all the 57 // session's processes share across the build -> push -> fork chain 58 let w: *u8 = sys_mmap(32); rp_wsid(w) 59 g_row("NISHI_WSID env wsid resolves (non-empty; fallback shared)" as *u8, (w[0] != (0 as u8)) as i64, pass) 60 61 g_w("RUNPATH-GATE rows=6 pass=") 62 var m: i64=pass[0]; let t: *u8=sys_mmap(8); 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; let ob: *u8=sys_mmap(8); while i<k{ob[i]=t[k-1-i];i=i+1}; sys_write(1,ob,k) 63 if pass[0] == 6 { g_w(" verdict=GREEN\n"); sys_exit(0); return 0 } 64 g_w(" verdict=RED\n"); sys_exit(1); return 1 65}