code wiki / _hdl_build / nx_snapfeed_gate.nx

nx_snapfeed_gate.nx source

↩ module page · 85 lines · 4914 B

1// nx_snapfeed_gate.nx -- PURE isolation gate for the /proc + supervisor.log parse tier (nx_snapfeed). 2// Proves the two hard-won correctness lessons with fixtures shaped like the real nishihost ground truth: 3// - robust /proc/<pid>/stat parse survives a comm with SPACES AND PARENS (T2) 4// - a service is matched by its FULL cmdline, and a 15-char-truncated comm SILENTLY MISSES it (T4 vs T5 NEG) 5// - crash-loop restart counting from the supervisor.log window (T6/T7) + a NEG (T8) 6// - session-leader vs child distinction at the source data (T1 leader, T3 keeper child) = the lineage fix's input 7// No IO/socket/exec. GREEN iff every assertion holds. Sovereign: nx_snapfeed + nx_syscalls. license_tier: ORIGINAL 8import "nx_snapfeed.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_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 13func g_row(name: *u8, ok: i64) -> i64 { 14 if ok == 1 { g_w(" PASS " as *u8) } else { g_w(" FAIL " as *u8) } 15 g_w(name); g_w("\n" as *u8) 16 return ok 17} 18 19func main() -> i64 { 20 g_w("snapfeed PARSE gate -- /proc stat+cmdline + supervisor.log; full-cmdline match (anti-truncation) + robust stat\n" as *u8) 21 var pass: i64 = 0 22 let total: i64 = 8 23 let box: *i64 = sys_mmap(32) as *i64 24 25 // T1: normal stat -> pid/ppid/sid + session LEADER (pid==sid) 26 let s1: *u8 = "1234 (sites.elf) S 1 1000 1234 0" as *u8 27 sf_parse_stat(s1, g_len(s1), box) 28 var ok1: i64 = 1 29 if box[0] != 1234 { ok1 = 0 } 30 if box[1] != 1 { ok1 = 0 } 31 if box[2] != 1234 { ok1 = 0 } 32 if sf_is_leader(box[0], box[2]) != 1 { ok1 = 0 } 33 pass = pass + g_row("T1 '1234 (sites.elf) S 1 1000 1234' -> pid1234 ppid1 sid1234 LEADER\x00" as *u8, ok1) 34 35 // T2: comm WITH spaces AND nested parens -> robust last-')' anchor keeps ppid/sid correct 36 let s2: *u8 = "555 (nx hostctl (s p)) S 1 1 555 0" as *u8 37 sf_parse_stat(s2, g_len(s2), box) 38 var ok2: i64 = 1 39 if box[0] != 555 { ok2 = 0 } 40 if box[1] != 1 { ok2 = 0 } 41 if box[2] != 555 { ok2 = 0 } 42 pass = pass + g_row("T2 comm with spaces+parens -> pid555 ppid1 sid555 (naive split would corrupt this)\x00" as *u8, ok2) 43 44 // T3: the reader-keeper CHILD -> shares sid, pid != sid -> NOT a leader (the false-positive's source signal) 45 let s3: *u8 = "556 (nx_hostctl) S 555 555 555 0" as *u8 46 sf_parse_stat(s3, g_len(s3), box) 47 var ok3: i64 = 1 48 if box[0] != 556 { ok3 = 0 } 49 if box[2] != 555 { ok3 = 0 } 50 if sf_is_leader(box[0], box[2]) != 0 { ok3 = 0 } 51 pass = pass + g_row("T3 keeper child '556 ... sid 555' -> pid556 sid555 NOT-leader (same lineage as T2)\x00" as *u8, ok3) 52 53 // T4: FULL cmdline contains the 22-char service name -> matched 54 let cmd: *u8 = "/volume1/homes/elderwesto/nishihost/nx_gallery_gateway.elf 18190 keys store 1000000000" as *u8 55 pass = pass + g_row("T4 full cmdline contains 'nx_gallery_gateway' -> matched\x00" as *u8, sf_contains(cmd, g_len(cmd), "nx_gallery_gateway" as *u8)) 56 57 // T5 NEG: the 15-char-truncated comm does NOT contain the full name -> a comm-match SILENTLY MISSES it 58 let trunc: *u8 = "nx_gallery_gate" as *u8 59 var ok5: i64 = 0 60 if sf_contains(trunc, g_len(trunc), "nx_gallery_gateway" as *u8) == 0 { ok5 = 1 } 61 pass = pass + g_row("T5 NEG truncated comm 'nx_gallery_gate' MISSES 'nx_gallery_gateway' (= why we use cmdline)\x00" as *u8, ok5) 62 63 // T6/T7/T8: restart counting from the supervisor.log window 64 let logf: *u8 = " [guard] restarted nx_clock_tickless.elf (was dead)\n [reap] pid=1 sig=0 code=0\n [guard] restarted nx_clock_tickless.elf (was dead)\n [guard] restarted nx_gallery_gateway.elf :18190 (was dead)\n [guard] restarted nx_clock_tickless.elf (was dead)\n" as *u8 65 let ln: i64 = g_len(logf) 66 var ok6: i64 = 0 67 if sf_count_restarts(logf, ln, "nx_clock_tickless" as *u8) == 3 { ok6 = 1 } 68 pass = pass + g_row("T6 supervisor.log -> nx_clock_tickless restarted 3 (crash-loop window)\x00" as *u8, ok6) 69 var ok7: i64 = 0 70 if sf_count_restarts(logf, ln, "nx_gallery_gateway" as *u8) == 1 { ok7 = 1 } 71 pass = pass + g_row("T7 supervisor.log -> nx_gallery_gateway restarted 1\x00" as *u8, ok7) 72 var ok8: i64 = 0 73 if sf_count_restarts(logf, ln, "nx_vroom_daemon" as *u8) == 0 { ok8 = 1 } 74 pass = pass + g_row("T8 NEG nx_vroom_daemon not in window -> 0 (no fabricated restarts)\x00" as *u8, ok8) 75 76 if pass == total { 77 let lg: i64 = sys_openat_append("knowledge/status/snapfeed_gate.log" as *u8, 0x1a4) 78 if lg >= 0 { sys_write(lg, "SNAPFEED-GATE pass=8/8 verdict=GREEN\n" as *u8, 36); sys_close(lg) } 79 g_w("SNAPFEED GATE GREEN 8/8 (full-cmdline match defeats 15-char comm truncation; robust stat parse)\n" as *u8) 80 sys_exit(0) 81 } 82 g_w("SNAPFEED GATE RED\n" as *u8) 83 sys_exit(1) 84 return 1 85}