code wiki / _hdl_build / nx_riscv_gate.nx

nx_riscv_gate.nx source

↩ module page · 61 lines · 3849 B

1// nx_riscv_gate.nx -- SOVEREIGN verifier for the whole RISC-V toolchain. The ECOSYSTEM checks itself: this organ 2// reads the green-only status logs that each riscv organ writes on PASS, and confirms every rung is GREEN. This 3// REPLACES the (banned) shell for-loop/grep -- no bash, no sh, no grep. Pattern reused from the ladder/adversary. 4// T1 all 6 backend rungs have a GREEN status-log: emit, decode, elf, codegen, loop, emu. 5// T2 (teeth) a bogus marker is NOT found (the check is real, not a rubber stamp). 6// expect_exit: 0 Sovereign: nx_riscv_lib (UI) + nx_syscalls. 7import "nx_riscv_lib.nx" 8import "nx_syscalls.nx" 9 10func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 11func read_file(path: *u8, buf: *u8, cap: i64) -> i64 { 12 let fd: i64=sys_openat_rd(path); if fd<0 { return 0 } 13 var total: i64=0; var go: i64=1 14 while go==1 { let k: i64=sys_read(fd, buf+total as i64, cap-total); if k<=0 { go=0 } else { total=total+k; if total>=cap { go=0 } } } 15 sys_close(fd); return total 16} 17func contains(buf: *u8, n: i64, needle: *u8) -> i64 { 18 let m: i64=slen(needle); if m==0 { return 0 } 19 var i: i64=0 20 while i+m<=n { var j: i64=0; var ok: i64=1; while j<m { if buf[i+j]!=needle[j] { ok=0; j=m } else { j=j+1 } } if ok==1 { return 1 } i=i+1 } 21 return 0 22} 23func check_log(path: *u8, marker: *u8, buf: *u8, cap: i64) -> i64 { 24 let n: i64=read_file(path, buf, cap) 25 var ok: i64=0; if n>0 { ok=contains(buf, n, marker) } 26 if ok==1 { g_puts(" [GREEN] ") } else { g_puts(" [RED ] ") } 27 g_puts(path); g_puts("\n" as *u8); return ok 28} 29 30func main() -> i64 { 31 g_puts("nx_riscv_gate (SOVEREIGN toolchain verifier: reads the green-only status logs; no shell/grep)\n" as *u8) 32 var pass: i64=0; var total: i64=0 33 let cap: i64=8192; let buf: *u8=sys_mmap(cap) 34 35 g_puts(" -- RISC-V backend rungs (status-log GREEN markers) --\n" as *u8) 36 var g: i64=0 37 g=g+check_log("knowledge/status/riscv_emit.log" as *u8, "RISCVEMIT" as *u8, buf, cap) 38 g=g+check_log("knowledge/status/riscv_decode.log" as *u8, "RISCVDECODE" as *u8, buf, cap) 39 g=g+check_log("knowledge/status/riscv_elf.log" as *u8, "RISCVELF" as *u8, buf, cap) 40 g=g+check_log("knowledge/status/riscv_codegen.log" as *u8, "RISCVCODEGEN" as *u8, buf, cap) 41 g=g+check_log("knowledge/status/riscv_loop.log" as *u8, "RISCVLOOP" as *u8, buf, cap) 42 g=g+check_log("knowledge/status/riscv_emu.log" as *u8, "RISCVEMU" as *u8, buf, cap) 43 g_puts(" GREEN rungs: "); g_pn(g); g_puts(" / 6\n" as *u8) 44 var t1: i64=0; if g==6 { t1=1 } 45 pass=pass+ck("T1: all 6 RISC-V backend rungs have a GREEN status-log (encoder/decoder/ELF/codegen/loop/emulator)" as *u8, t1); total=total+1 46 47 // teeth: a bogus marker must NOT be found in a real log 48 let n2: i64=read_file("knowledge/status/riscv_emu.log" as *u8, buf, cap) 49 var bogus: i64=contains(buf, n2, "RISCVNOTREAL" as *u8) 50 var t2: i64=0; if bogus==0 { t2=1 } 51 pass=pass+ck("T2 (teeth): a bogus marker is NOT found -- the check is real, not a rubber stamp" as *u8, t2); total=total+1 52 53 var okall: i64=0; if pass==total { okall=1 } 54 g_puts("---- riscv_gate: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8) 55 if okall==1 { 56 let logf: i64=sys_openat_append("knowledge/status/riscv_gate.log" as *u8, 420) 57 if logf>=0 { let z: i64=sys_write(logf,"RISCVGATE sovereign toolchain verifier GREEN: 6/6 backend rungs on nx_riscv_lib (no shell)\n" as *u8,88); sys_close(logf) } 58 g_puts("verdict=GREEN (the ECOSYSTEM verified its own RISC-V toolchain: 6/6 rungs GREEN on the general nx_riscv_lib; no shell/grep)\n" as *u8); sys_exit(0); return 0 59 } 60 g_puts("verdict=RED (a rung is missing its GREEN log -- run that organ via the sovereign runner)\n" as *u8); sys_exit(1); return 1 61}