code wiki / _hdl_build / _eg_gate.nx
_eg_gate.nx source
↩ module page · 129 lines · 7198 B
1// _eg_gate.nx -- gate for the GENERALIZED emitter-of-emitters (X-AUT-006e-GEN, the daemon-class rung).
2// nx_eoe.nx proved the keystone on a BOUNDED shape (depth-counting loops). THIS gate proves the
3// generalization removed the ceiling: the ORGAN nx_eg pretty-prints an emitter whose CONTROL FLOW
4// (socket/bind/listen/`while accept`/`if cfd<0 continue`/fork/flat-if /health dispatch/send) is ABSENT
5// from every prior emitter, FROM A STRUCTURAL-DATA spec, byte-identical to a hand reference, hands-off.
6//
7// NO mocks. The gate runs the REAL nx_eg ELF to (1) author the daemon from spec 1 and assert its
8// spec-synthesized BODY is byte-identical to the hand reference _eg_httpd_ref.nx (PROOF-2 + PROOF-1
9// regression guard in one: the generalized walker re-authors the reference byte-identical), and (2)
10// re-author with tamper=1 (one F_ARG0 route literal flipped) and assert the authored BODY now DIFFERS
11// (NO-FAKE-GREEN: a green here cannot be reached by constant-lifting -- the control flow + route are
12// synthesized from the spec table, so flipping a spec field changes the emitted source). Compile + bind
13// + GET /health 200 are proven by the driver harness; this gate proves the AUTHORSHIP invariants that
14// constant-lifting provably cannot satisfy. Evidence -> knowledge/status/eg.log. license_tier: ORIGINAL
15import "nx_syscalls.nx"
16
17func g_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
18func g_fp(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
19func g_fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;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(fd,bb,k); return 0 }
20
21// exec prog with up to 3 string args; stdout -> /dev/null. returns child WEXITSTATUS.
22func g_run(prog: *u8, a1: *u8, a2: *u8, a3: *u8) -> i64 {
23 let pid: i64 = sys_fork()
24 if pid == 0 {
25 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4)
26 if dn >= 0 { sys_dup3(dn, 1, 0) }
27 let argv: *i64 = sys_mmap(64) as *i64
28 argv[0] = prog as i64
29 var k: i64 = 1
30 if a1 != (0 as *u8) { argv[k] = a1 as i64; k = k + 1 }
31 if a2 != (0 as *u8) { argv[k] = a2 as i64; k = k + 1 }
32 if a3 != (0 as *u8) { argv[k] = a3 as i64; k = k + 1 }
33 argv[k] = 0
34 let envp: *i64 = sys_mmap(16) as *i64
35 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
36 sys_execve(prog, argv, envp)
37 sys_exit(127)
38 }
39 let st: *i64 = sys_mmap(16) as *i64
40 sys_wait4(pid, st, 0)
41 return (st[0] >> 8) & 0xff
42}
43
44// read a whole file into buf (cap), return byte count.
45func g_read(path: *u8, buf: *u8, cap: i64) -> i64 {
46 let fd: i64 = sys_openat_rd(path)
47 if fd < 0 { return 0 }
48 var n: i64 = 0
49 var go: i64 = 1
50 while go == 1 { let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - 1 - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap - 1 { go = 0 } }
51 sys_close(fd)
52 return n
53}
54
55// emit the spec-synthesized BODY of <path> (everything from the first "func eh_find" line) into out;
56// returns body length. This isolates the spec-driven region from the module-frame header comment.
57func g_body(path: *u8, out: *u8, cap: i64) -> i64 {
58 let full: *u8 = sys_mmap(65536)
59 let fn: i64 = g_read(path, full, 65536)
60 // find "func eh_find" start
61 var i: i64 = 0
62 var start: i64 = 0 - 1
63 while i + 12 <= fn {
64 if full[i] == (102 as u8) { // 'f'
65 var ok: i64 = 1
66 let pat: *u8 = "func eh_find" as *u8
67 var j: i64 = 0
68 while j < 12 { if full[i+j] != pat[j] { ok = 0 } j = j + 1 }
69 if ok == 1 { if start < 0 { start = i } }
70 }
71 i = i + 1
72 }
73 if start < 0 { return 0 }
74 var k: i64 = 0
75 while start + k < fn { out[k] = full[start + k]; k = k + 1 }
76 return k
77}
78
79// fnv1a-64 over n bytes -- a deterministic content fingerprint (we only need equality, not crypto).
80func g_hash(buf: *u8, n: i64) -> i64 {
81 var h: i64 = 0x100000001b3
82 var i: i64 = 0
83 while i < n { h = (h ^ (buf[i] as i64)) * 0x100000001b3; h = h & 0x7fffffffffffffff; i = i + 1 }
84 return h
85}
86
87func main() -> i64 {
88 g_p("=== generalized emitter-of-emitters gate (structural spec -> daemon source, byte-identical, tamper->RED) ===\n" as *u8)
89 let lfd: i64 = sys_openat_append("knowledge/status/eg.log" as *u8, 0x1a4)
90
91 // (1) author the daemon from spec 1 (clean), hands-off.
92 let g1: i64 = g_run("/tmp/nx_eg.sov.elf" as *u8, "1" as *u8, "_eg_g_clean" as *u8, 0 as *u8)
93 // (2) author with tamper=1 (flip one F_ARG0 route literal).
94 let g2: i64 = g_run("/tmp/nx_eg.sov.elf" as *u8, "1" as *u8, "_eg_g_tamper" as *u8, "1" as *u8)
95
96 let refb: *u8 = sys_mmap(65536)
97 let clnb: *u8 = sys_mmap(65536)
98 let tmpb: *u8 = sys_mmap(65536)
99 let rn: i64 = g_body("runtime/_hdl_build/_eg_httpd_ref.nx" as *u8, refb, 65536)
100 let cn: i64 = g_body("runtime/_hdl_build/_eg_g_clean.nx" as *u8, clnb, 65536)
101 let tn: i64 = g_body("runtime/_hdl_build/_eg_g_tamper.nx" as *u8, tmpb, 65536)
102
103 let rh: i64 = g_hash(refb, rn)
104 let ch: i64 = g_hash(clnb, cn)
105 let th: i64 = g_hash(tmpb, tn)
106
107 // PROOF-2 + PROOF-1 regression: organ-authored clean BODY == hand reference BODY (byte-identical).
108 var byte_identical: i64 = 0
109 if rn == cn { if rh == ch { byte_identical = 1 } }
110 // NO-FAKE-GREEN: tampered BODY differs from the clean authored BODY (and from the ref).
111 var tamper_red: i64 = 0
112 if th != ch { tamper_red = 1 }
113
114 g_p(" authored(clean) body_bytes=" as *u8); g_fn(1, cn); g_p(" ref body_bytes=" as *u8); g_fn(1, rn)
115 g_p(" byte_identical=" as *u8); g_fn(1, byte_identical); g_p("\n" as *u8)
116 g_p(" tampered body_bytes=" as *u8); g_fn(1, tn); g_p(" differs_from_clean(tamper_red)=" as *u8); g_fn(1, tamper_red); g_p("\n" as *u8)
117
118 var pass: i64 = 0
119 if g1 == 0 { if g2 == 0 { if byte_identical == 1 { if tamper_red == 1 { pass = 1 } } } }
120 if pass == 1 {
121 g_p("EGGATE verdict=GREEN (structural-DATA spec -> ORGAN-authored daemon source; accept/fork/flat-if control flow ABSENT from prior emitters, byte-identical to hand ref, tamper-of-one-spec-field -> RED = control flow synthesized, NOT constant-lifted)\n" as *u8)
122 if lfd >= 0 { g_fp(lfd, "EGGATE verdict=GREEN keystone=emitter-of-emitters-GENERALIZED mechanism=structural-node-table->nishilang-source->nx_cc target=http-accept-dispatch-daemon byte_identical=1 tamper_red=1 new-control-flow=accept/fork/flat-if-dispatch epoch=" as *u8); g_fn(lfd, sys_now_realtime_sec()); g_fp(lfd, "\n" as *u8); sys_close(lfd) }
123 sys_exit(0); return 0
124 }
125 g_p("EGGATE verdict=RED (g1/g2/byte_identical/tamper_red not all green)\n" as *u8)
126 if lfd >= 0 { g_fp(lfd, "EGGATE verdict=RED g1=" as *u8); g_fn(lfd, g1); g_fp(lfd, " g2=" as *u8); g_fn(lfd, g2); g_fp(lfd, " byte_identical=" as *u8); g_fn(lfd, byte_identical); g_fp(lfd, " tamper_red=" as *u8); g_fn(lfd, tamper_red); g_fp(lfd, "\n" as *u8); sys_close(lfd) }
127 sys_exit(1)
128 return 1
129}