code wiki / _hdl_build / nx_doc_constscan_gate.nx

nx_doc_constscan_gate.nx source

↩ module page · 47 lines · 3168 B

1import "nx_gate_gn.nx" 2// nx_doc_constscan_gate.nx -- liar-kill gate for the LM-030 const-pointer-direct-index detector. MUST-FLAG: a 3// `const NAME: *T` indexed directly as NAME[i]. MUST-NOT-FLAG: the SAFE bind-then-index, a const passed to a func, 4// a NON-const pointer indexed, and the declaration itself. expect_exit: 0 5import "nx_syscalls.nx" 6import "nx_doc_constscan.nx" 7 8func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 10func seq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]!=(0 as u8){return 0} return 1 } 11func have_file(p: *u8) -> i64 { let fd: i64=sys_openat_rd(p); if fd<0 { return 0 } sys_close(fd); return 1 } 12 13func main() -> i64 { 14 gp("=== nx_doc_constscan_gate: detect LM-030 const-pointer direct index CONST[i] (recycler hardening) ===\n" as *u8) 15 let name: *u8 = sys_mmap(64) 16 let pos: *i64 = sys_mmap(16) as *i64 17 var pass: i64=0; var fail: i64=0 18 19 // KAT1 MUST-FLAG: a const pointer indexed directly 20 let s1: *u8 = "const SM_LIB: *u8 = \"abc\"\nfunc f() -> i64 { return SM_LIB[2] as i64 }" as *u8 21 let r1: i64 = cs_scan(s1, slen(s1), name, pos) 22 if r1 >= 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kat1-missed\n" as *u8) } 23 if seq(name, "SM_LIB" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kat1-wrong-name=" as *u8); gp(name); gp("\n" as *u8) } 24 25 // KAT2 MUST-NOT-FLAG: bind-then-index (the LM-030 fix) 26 let s2: *u8 = "const SM_LIB: *u8 = \"abc\"\nfunc g() -> i64 { let p: *u8 = SM_LIB; return p[2] as i64 }" as *u8 27 if cs_scan(s2, slen(s2), name, pos) < 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kat2-flagged-safe-bind\n" as *u8) } 28 29 // KAT3 MUST-NOT-FLAG: const passed to a func (indexes its param) 30 let s3: *u8 = "const SM_LIB: *u8 = \"abc\"\nfunc h() -> i64 { return foo(SM_LIB) }" as *u8 31 if cs_scan(s3, slen(s3), name, pos) < 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kat3-flagged-passed-const\n" as *u8) } 32 33 // KAT4 MUST-NOT-FLAG: a NON-const pointer indexed 34 let s4: *u8 = "func k() -> i64 { let arr: *u8 = z(); return arr[0] as i64 }" as *u8 35 if cs_scan(s4, slen(s4), name, pos) < 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kat4-flagged-nonconst\n" as *u8) } 36 37 // KAT5 MUST-NOT-FLAG: just the declaration, never indexed 38 let s5: *u8 = "const SM_LIB: *u8 = \"abc\"\nfunc m() -> i64 { return 0 }" as *u8 39 if cs_scan(s5, slen(s5), name, pos) < 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kat5-flagged-decl-only\n" as *u8) } 40 41 // KAT6 grounding: the motivating critique artifact exists 42 if have_file("knowledge/fetched/recyc_crit_memsafety.raw" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL kat6-ungrounded\n" as *u8) } 43 44 gp("DOC-CONSTSCAN-GATE pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail) 45 if fail==0 { gp(" verdict=GREEN (const-pointer direct-index flagged with its name; safe bind/pass/non-const/decl-only NOT flagged)\n" as *u8); sys_exit(0); return 0 } 46 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 47}