nx_tool_run_gate.nx source
↩ module page · 63 lines · 3873 B
1// nx_tool_run_gate.nx -- GATE for R0 of the executable-API rung (nx_tool_run). Proves the sovereign
2// exec+capture primitive forks a child, runs a REAL ELF, captures its true stdout, and returns its true
3// exit code -- with a never-brick negative control (a bad path can only yield exit 127 + empty capture,
4// never a shell injection or a persistent-state write). Target = the canonical no-op nx_tool_ping (a
5// SEPARATE tiny ELF, so there is zero recursion risk). Mirrors the nx_*_gate idiom (pass/tot -> GREEN/RED).
6// PREREQ: build nx_tool_ping FIRST (produces _offc/nx_tool_ping.elf).
7// license_tier: ORIGINAL expect_exit: 0
8import "nx_tool_run.nx"
9import "nx_gate_verdict.nx"
10
11const TR_PING: *u8 = "_offc/nx_tool_ping.elf" as *u8 // relative to CWD (nxc2 root at gate time)
12const TR_BADPATH: *u8 = "/nonexistent/nope.elf" as *u8
13
14func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
15func gn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(28); 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; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 }
16
17func main() -> i64 {
18 gw("=== nx_tool_run_gate (R0: sovereign exec+capture primitive -- the executable-API rung) ===\n" as *u8)
19 var pass: i64 = 0
20 var tot: i64 = 0
21 let cap: i64 = 65536
22 let out: *u8 = sys_mmap(cap)
23 let lenp: *i64 = sys_mmap(16) as *i64
24
25 // T1: run a real ELF (nx_tool_ping) and capture its stdout + exit code
26 tot = tot + 1
27 let ec1: i64 = tr_run1(TR_PING, 0 as *u8, out, cap, lenp)
28 let len1: i64 = lenp[0]
29 let has_sent: i64 = tr_contains(out, len1, "NX_TOOL_PING_OK" as *u8)
30 var ok1: i64 = 1
31 if ec1 != 0 { ok1 = 0 }
32 if has_sent != 1 { ok1 = 0 }
33 if len1 <= 0 { ok1 = 0 }
34 if ok1 == 1 { pass = pass + 1; gw("PASS T1 exec+capture: ping exit=" as *u8); gn(ec1); gw(" captured " as *u8); gn(len1); gw(" bytes incl sentinel\n" as *u8) }
35 else { gw("FAIL T1 exit=" as *u8); gn(ec1); gw(" len=" as *u8); gn(len1); gw(" has_sentinel=" as *u8); gn(has_sent); gw("\n" as *u8) }
36
37 // T2: capture CORRECTNESS -- the exact sentinel string round-trips through the pipe verbatim
38 tot = tot + 1
39 let ok2: i64 = tr_contains(out, len1, "NX_TOOL_PING_OK" as *u8)
40 if ok2 == 1 { pass = pass + 1; gw("PASS T2 capture exact: 'NX_TOOL_PING_OK' round-tripped through the pipe\n" as *u8) }
41 else { gw("FAIL T2 sentinel not found verbatim\n" as *u8) }
42
43 // T3 NEG-CONTROL (never-brick): a bad ELF path can ONLY produce exit 127 + empty capture -- no shell, no crash
44 tot = tot + 1
45 lenp[0] = 0
46 let ec3: i64 = tr_run1(TR_BADPATH, 0 as *u8, out, cap, lenp)
47 var ok3: i64 = 1
48 if ec3 != 127 { ok3 = 0 } // execve failed in child -> sys_exit(127), captured by parent
49 if lenp[0] != 0 { ok3 = 0 } // nothing on stdout
50 if ok3 == 1 { pass = pass + 1; gw("PASS T3 never-brick neg-control: bad path -> exit 127, empty capture (no shell, no injection, no state write)\n" as *u8) }
51 else { gw("FAIL T3 badpath exit=" as *u8); gn(ec3); gw(" len=" as *u8); gn(lenp[0]); gw("\n" as *u8) }
52
53 gw("nx_tool_run_gate pass=" as *u8); gn(pass); gw("/" as *u8); gn(tot)
54 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
55 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
56 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
57 let ctr__dry: *i64 = gv_ctr()
58 ctr__dry[0] = pass
59 ctr__dry[1] = tot
60 let rc__dry: i64 = gv_verdict("TOOL-RUN-GATE" as *u8, ctr__dry, "the ecosystem can now RUN organs and capture real output -- API tools/call can stop stubbing)" as *u8)
61 sys_exit(rc__dry)
62 return rc__dry
63}