code wiki / _hdl_build / nx_mgmt_snapshot_run_gate.nx

nx_mgmt_snapshot_run_gate.nx source

↩ module page · 61 lines · 3889 B

1// nx_mgmt_snapshot_run_gate.nx -- PURE isolation gate for the producer's INSTANCE-vs-fork-child rule 2// (msr_is_instance). This is the refinement that makes the per-service `procs` count trustworthy on the NAS: a 3// fork-per-connection server (sites.elf / the gateways) has ONE listener + N connection-handler children that all 4// match the same cmdline; counting them raw would false-flag `dup`. The rule (setsid-independent): a matched proc 5// is a CHILD iff its parent is another matched proc of the SAME service. Negative controls T2/T3 are the children 6// (must be filtered); T4/T5 prove a GENUINE duplicate (both parented by the supervisor) is still counted twice. 7// No IO. GREEN iff every rule holds. Sovereign: nx_mgmt_snapshot_run + nx_syscalls. license_tier: ORIGINAL 8import "nx_mgmt_snapshot_run.nx" 9import "nx_syscalls.nx" 10 11func 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 } 12func g_row(name: *u8, ok: i64) -> i64 { 13 if ok == 1 { g_w(" PASS " as *u8) } else { g_w(" FAIL " as *u8) } 14 g_w(name); g_w("\n" as *u8) 15 return ok 16} 17func g_eq(name: *u8, got: i64, want: i64) -> i64 { var ok: i64 = 0; if got == want { ok = 1 } return g_row(name, ok) } 18 19func main() -> i64 { 20 g_w("mgmt-snapshot-run gate -- instance-vs-fork-child rule (setsid-independent; kills false-dup on forking servers)\n" as *u8) 21 var pass: i64 = 0 22 let total: i64 = 6 23 24 // ---- fork-per-conn: 1 listener (pid100, parent=supervisor 50) + 2 conn-children (parent=listener 100) ---- 25 let a_pid: *i64 = sys_mmap(64) as *i64 26 let a_ppid: *i64 = sys_mmap(64) as *i64 27 let a_canon: *i64 = sys_mmap(64) as *i64 28 a_pid[0] = 100; a_ppid[0] = 50; a_canon[0] = 0 29 a_pid[1] = 101; a_ppid[1] = 100; a_canon[1] = 0 30 a_pid[2] = 102; a_ppid[2] = 100; a_canon[2] = 0 31 pass = pass + g_eq("T1 listener (parent=supervisor) -> INSTANCE\x00" as *u8, msr_is_instance(0, a_pid, a_ppid, a_canon, 3), 1) 32 pass = pass + g_eq("T2 NEG conn-child (parent=listener) -> NOT instance (filtered)\x00" as *u8, msr_is_instance(1, a_pid, a_ppid, a_canon, 3), 0) 33 pass = pass + g_eq("T3 NEG conn-child 2 -> NOT instance (=> forking server counts as procs=1, no false dup)\x00" as *u8, msr_is_instance(2, a_pid, a_ppid, a_canon, 3), 0) 34 35 // ---- genuine duplicate: TWO listeners, both parented by the SUPERVISOR (50), not by each other ---- 36 let b_pid: *i64 = sys_mmap(64) as *i64 37 let b_ppid: *i64 = sys_mmap(64) as *i64 38 let b_canon: *i64 = sys_mmap(64) as *i64 39 b_pid[0] = 100; b_ppid[0] = 50; b_canon[0] = 0 40 b_pid[1] = 200; b_ppid[1] = 50; b_canon[1] = 0 41 pass = pass + g_eq("T4 dup listener A -> INSTANCE\x00" as *u8, msr_is_instance(0, b_pid, b_ppid, b_canon, 2), 1) 42 pass = pass + g_eq("T5 dup listener B -> INSTANCE (real dup STILL flagged: 2 instances)\x00" as *u8, msr_is_instance(1, b_pid, b_ppid, b_canon, 2), 1) 43 44 // ---- cross-service isolation: a canon-1 proc whose parent (100) is a canon-0 proc -> still an instance ---- 45 let c_pid: *i64 = sys_mmap(64) as *i64 46 let c_ppid: *i64 = sys_mmap(64) as *i64 47 let c_canon: *i64 = sys_mmap(64) as *i64 48 c_pid[0] = 100; c_ppid[0] = 50; c_canon[0] = 0 49 c_pid[1] = 101; c_ppid[1] = 100; c_canon[1] = 1 50 pass = pass + g_eq("T6 cross-service: parent is a DIFFERENT service -> still an INSTANCE (not falsely excluded)\x00" as *u8, msr_is_instance(1, c_pid, c_ppid, c_canon, 2), 1) 51 52 if pass == total { 53 let lg: i64 = sys_openat_append("knowledge/status/mgmt_snapshot_run_gate.log" as *u8, 0x1a4) 54 if lg >= 0 { sys_write(lg, "MGMT-SNAPSHOT-RUN-GATE pass=6/6 verdict=GREEN\n" as *u8, 45); sys_close(lg) } 55 g_w("MGMT-SNAPSHOT-RUN GATE GREEN 6/6 (fork-per-conn children filtered; genuine dup still flagged)\n" as *u8) 56 sys_exit(0) 57 } 58 g_w("MGMT-SNAPSHOT-RUN GATE RED\n" as *u8) 59 sys_exit(1) 60 return 1 61}