code wiki / (root) / nx_gate_run.nx

nx_gate_run.nx source

↩ module page · 101 lines · 4584 B

1// nx_gate_run.nx -- SOVEREIGN gate runner: build an organ through the sovereign lane, run it, and check its 2// output/exit -- with NO shell and NO .sh wrapper. Retires the simple bench/gate_*.sh pattern (which does 3// compile-via-nx_compile_x86_native + as + ld + run + grep, all under a /bin/bash script) by composing a single 4// fork+exec of _offc/nx_sov_build_run.elf <organ> (itself the sovereign compile+assemble+run) plus a substring 5// check on the captured output. This is the enabling primitive for migrating nx_engineer's gates off /bin/bash 6// (the agent-sovereignty debt: nx_engineer.nx runs its 7 gate scripts via a shell; nx_conductor_live already 7// went sovereign). Run from the nxc2 root so the relative organ paths + the installed runner resolve. 8// 9// Usage: nx_gate_run <organ> [expect-substring] 10// PASS (exit 0) iff the build+run did not crash AND (an expect string was given AND the captured output 11// contains it) OR (no expect string AND the run exited 0). FAIL (exit 1) otherwise. Sovereign (syscalls 12// only). license_tier: ORIGINAL expect_exit: 0 13import "nx_syscalls.nx" 14const K_MAGIC_1048576: i64 = 1048576 15 16func gr_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 17func gr_putn(v: i64) -> i64 { 18 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 19 var m: i64 = v 20 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 21 let d: *u8 = sys_mmap(24); var k: i64 = 0 22 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 23 let o: *u8 = sys_mmap(24); var i: i64 = 0 24 while i < k { o[i] = d[k - 1 - i]; i = i + 1 } 25 sys_write(1, o, k); return 0 26} 27 28func gr_read_file(path: *u8, buf: *u8, cap: i64) -> i64 { 29 let fd: i64 = sys_openat_rd(path) 30 if fd < 0 { return 0 } 31 var total: i64 = 0 32 var go: i64 = 1 33 while go == 1 { 34 let r: i64 = sys_read(fd, ((buf as i64) + total) as *u8, cap - total) 35 if r <= 0 { go = 0 } else { total = total + r; if total >= cap { go = 0 } } 36 } 37 sys_close(fd) 38 return total 39} 40 41// does buf[0..n) contain the NUL-terminated pat as a substring? 42func gr_contains(buf: *u8, n: i64, pat: *u8) -> i64 { 43 var pl: i64 = 0; while pat[pl] != (0 as u8) { pl = pl + 1 } 44 if pl == 0 { return 1 } 45 var i: i64 = 0 46 while i + pl <= n { 47 var m: i64 = 0; var hit: i64 = 1 48 while m < pl { if buf[i + m] != pat[m] { hit = 0; m = pl } else { m = m + 1 } } 49 if hit == 1 { return 1 } 50 i = i + 1 51 } 52 return 0 53} 54 55func main(argc: i64, argv: *i64) -> i64 { 56 if argc < 2 { gr_puts("usage: nx_gate_run <organ> [expect-substring] (sovereign build+run+check, no shell)\n" as *u8); sys_exit(2); return 2 } 57 let organ: *u8 = argv[1] as *u8 58 // expects = argv[2..argc]; ALL must be present in the captured output. If none given, require exit 0. 59 60 let pid: i64 = sys_fork() 61 if pid == 0 { 62 let ofd: i64 = sys_openat_wr("/tmp/nx_gate_run.out\x00" as *u8, 0x1a4) 63 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) } 64 let elf: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 65 let av: *i64 = sys_mmap(32) as *i64 66 av[0] = elf as i64; av[1] = organ as i64; av[2] = 0 67 let envp: *i64 = sys_mmap(16) as *i64 68 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 69 sys_execve(elf, av, envp) 70 sys_exit(127) 71 } 72 let st: *i64 = sys_mmap(16) as *i64 73 sys_wait4(pid, st, 0) 74 let crashed: i64 = st[0] % 128 75 let ec: i64 = (st[0] >> 8) & 0xff 76 77 let buf: *u8 = sys_mmap(K_MAGIC_1048576) 78 let n: i64 = gr_read_file("/tmp/nx_gate_run.out\x00" as *u8, buf, K_MAGIC_1048576) 79 80 gr_puts("=== nx_gate_run (sovereign, no shell) organ=" as *u8); gr_puts(organ) 81 gr_puts(" ===\n build+run exit=" as *u8); gr_putn(ec); gr_puts(" output_bytes=" as *u8); gr_putn(n); gr_puts("\n" as *u8) 82 83 var pass: i64 = 1 84 if crashed != 0 { pass = 0 } 85 if argc >= 3 { 86 var ai: i64 = 2 87 while ai < argc { 88 let want: *u8 = argv[ai] as *u8 89 if gr_contains(buf, n, want) == 0 { pass = 0; gr_puts(" MISSING expect: " as *u8); gr_puts(want); gr_puts("\n" as *u8) } 90 ai = ai + 1 91 } 92 } else { 93 if ec != 0 { pass = 0 } 94 } 95 if pass == 1 { 96 gr_puts("NX-GATE-RUN PASS: sovereign build+run+check succeeded (retires the shell gate_*.sh wrapper for this shape)\n" as *u8) 97 sys_exit(0); return 0 98 } 99 gr_puts("NX-GATE-RUN FAIL: build+run crashed, nonzero exit, or expected marker absent\n" as *u8) 100 sys_exit(1); return 1 101}