code wiki / _hdl_build / nx_clobber_guard_gate.nx
nx_clobber_guard_gate.nx source
↩ module page · 73 lines · 3635 B
1// nx_clobber_guard_gate.nx -- gate for the acl F744 guard. Fixture-based + DETERMINISTIC, with a
2// negative control (the scale-law/liar-kill discipline): captures the guard's stdout via tr_run_capture
3// (same proven idiom as nx_drift_watch) and substring-matches the verdict -- avoids exit-status decode.
4// T1 identical surfaces -> verdict=CONVERGED (positive)
5// T2 different surfaces -> verdict=DIVERGED (NEGATIVE CONTROL: a real clobber must be caught)
6// T3 absent surface -> verdict=ABSENT (fail-closed on a missing copy)
7// Runs _offc/nx_clobber_guard.elf (staged before this gate). license_tier: ORIGINAL expect_exit: 0
8import "nx_tool_run.nx"
9
10func g_w(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 }
11func g_wn(fd: i64, v: i64) -> i64 { var m: i64 = v; if m < 0 { g_w(fd, "-" as *u8); 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 } let o: *u8 = sys_mmap(28); var i: i64 = 0; while i < k { o[i] = t[k - 1 - i]; i = i + 1 } sys_write(fd, o, k); return 0 }
12func g_wf(path: *u8, s: *u8) -> i64 {
13 let fd: i64 = sys_openat_wr(path, 0x1a4)
14 if fd < 0 { return 0 - 1 }
15 var n: i64 = 0
16 while s[n] != (0 as u8) { n = n + 1 }
17 sys_write(fd, s, n)
18 sys_close(fd)
19 return 0
20}
21// substring search: 1 if pat occurs in buf[0..n), else 0.
22func g_has(buf: *u8, n: i64, pat: *u8) -> i64 {
23 var pl: i64 = 0
24 while pat[pl] != (0 as u8) { pl = pl + 1 }
25 if pl == 0 { return 0 }
26 var i: i64 = 0
27 while i + pl <= n {
28 var k: i64 = 0
29 var m: i64 = 1
30 while k < pl { if buf[i + k] != pat[k] { m = 0; k = pl } else { k = k + 1 } }
31 if m == 1 { return 1 }
32 i = i + 1
33 }
34 return 0
35}
36// run the guard cmp A B, capture stdout into out[0..ol[0]).
37func g_run(pa: *u8, pb: *u8, out: *u8, ocap: i64, ol: *i64) -> i64 {
38 let av: *i64 = sys_mmap(64) as *i64
39 av[0] = "_offc/nx_clobber_guard.elf" as i64
40 av[1] = "cmp" as i64
41 av[2] = pa as i64
42 av[3] = pb as i64
43 av[4] = 0
44 return tr_run_capture("_offc/nx_clobber_guard.elf" as *u8, av, out, ocap, ol)
45}
46func main() -> i64 {
47 g_wf("/tmp/cg_a" as *u8, "hello world alpha line one\nline two\n" as *u8)
48 g_wf("/tmp/cg_b" as *u8, "hello world alpha line one\nline two\n" as *u8)
49 g_wf("/tmp/cg_c" as *u8, "hello world BRAVO totally different content\n" as *u8)
50 let ocap: i64 = 4096
51 let out: *u8 = sys_mmap(ocap)
52 let ol: *i64 = sys_mmap(16) as *i64
53 var pass: i64 = 0
54 var tot: i64 = 0
55
56 tot = tot + 1
57 g_run("/tmp/cg_a" as *u8, "/tmp/cg_b" as *u8, out, ocap, ol)
58 if g_has(out, ol[0], "verdict=CONVERGED" as *u8) == 1 { pass = pass + 1; g_w(1, "T1 identical->CONVERGED PASS\n" as *u8) } else { g_w(1, "T1 FAIL\n" as *u8) }
59
60 tot = tot + 1
61 g_run("/tmp/cg_a" as *u8, "/tmp/cg_c" as *u8, out, ocap, ol)
62 if g_has(out, ol[0], "verdict=DIVERGED" as *u8) == 1 { pass = pass + 1; g_w(1, "T2 different->DIVERGED PASS (neg-control)\n" as *u8) } else { g_w(1, "T2 FAIL\n" as *u8) }
63
64 tot = tot + 1
65 g_run("/tmp/cg_a" as *u8, "/tmp/cg_MISSING_XYZ" as *u8, out, ocap, ol)
66 if g_has(out, ol[0], "verdict=ABSENT" as *u8) == 1 { pass = pass + 1; g_w(1, "T3 absent->ABSENT PASS\n" as *u8) } else { g_w(1, "T3 FAIL\n" as *u8) }
67
68 g_w(1, "CLOBBER-GUARD-GATE pass=" as *u8); g_wn(1, pass); g_w(1, "/" as *u8); g_wn(1, tot)
69 if pass == tot { g_w(1, " verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
70 g_w(1, " verdict=RED\n" as *u8)
71 sys_exit(1)
72 return 1
73}