code wiki / _hdl_build / nx_emu_probe_wasm_gate.nx

nx_emu_probe_wasm_gate.nx source

↩ module page · 76 lines · 3979 B

1// nx_emu_probe_wasm_gate.nx -- R0.0 WASM half, NISHI all the way up. 2// 3// Builds nx_emu_probe.nx -> emu.wasm by FORKING the two sovereign compiler ELFs (nx_compile_wat + 4// nx_wat_compiler) -- the nx_aw_push fork-a-sovereign-sub-ELF pattern, NOT a .sh script -- then EXECUTES 5// p_probe inside the sovereign nx_wasm_vm and checks the result == the native KAT (8081519, from 6// nx_emu_probe_gate). Proves the emulator-shaped kernel compiles to faithful wasm through the fully 7// sovereign pipeline. The build glue is this organ; the verify is nx_wasm_vm. No grep/xxd/.sh anywhere. 8// license_tier: ORIGINAL (build-fork spine from nx_aw_push; VM-verify spine from nx_sha256_wasm_vm_gate) 9import "nx_syscalls.nx" 10import "nx_gate_emit_lib.nx" 11import "nx_wasm_vm.nx" 12 13const CWAT_ELF: *u8 = "/tmp/nx_compile_wat.sov.elf" 14const WATC_ELF: *u8 = "/tmp/nx_wat_compiler.sov.elf" 15const SRC_NX: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_emu_probe.nx" 16const OUT_WAT: *u8 = "/tmp/emu.wat" 17const OUT_WASM: *u8 = "/tmp/emu.wasm" 18const KAT: i64 = 8081519 19 20func run_elf(elf: *u8, a1: *u8, a2: *u8) -> i64 { 21 let pid: i64 = sys_fork() 22 if pid == 0 { 23 let argv: *i64 = sys_mmap(64) as *i64 24 argv[0] = elf as i64 25 var ai: i64 = 1 26 if (a1 as i64) != 0 { argv[ai] = a1 as i64; ai = ai + 1 } 27 if (a2 as i64) != 0 { argv[ai] = a2 as i64; ai = ai + 1 } 28 argv[ai] = 0 29 let envp: *i64 = sys_mmap(16) as *i64 30 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64 31 envp[1] = 0 32 sys_execve(elf, argv, envp) 33 sys_exit(127) 34 } 35 let st: *i64 = sys_mmap(16) as *i64 36 sys_wait4(pid, st, 0) 37 return (st[0] >> 8) & 0xff 38} 39 40func main() -> i64 { 41 g_puts("nx_emu_probe WASM gate (sovereign compile via forked ELFs -> executed in nx_wasm_vm -> vs native KAT)\n" as *u8) 42 var pass: i64 = 0 43 var total: i64 = 0 44 45 // 1) .nx -> .wat (nx_compile_wat writes WAT to OUT_WAT via argv[2] -- no stdout plumbing) 46 let rc1: i64 = run_elf(CWAT_ELF, SRC_NX, OUT_WAT) 47 g_puts(" [build] nx_compile_wat rc=" as *u8); g_pn(rc1); g_puts("\n" as *u8) 48 // 2) .wat -> .wasm (nx_wat_compiler takes in.wat out.wasm as args) 49 let rc2: i64 = run_elf(WATC_ELF, OUT_WAT, OUT_WASM) 50 g_puts(" [build] nx_wat_compiler rc=" as *u8); g_pn(rc2); g_puts("\n" as *u8) 51 52 // 3) read the produced wasm, parse it in the sovereign VM 53 let box: *i64 = sys_mmap(16) as *i64 54 let wasm: *u8 = sys_read_file(OUT_WASM, box) 55 if (wasm as i64) == 0 { g_puts(" FAIL cannot read emu.wasm (are /tmp compilers present?)\nverdict=RED\n" as *u8); sys_exit(1); return 1 } 56 g_puts(" [measure] wasm bytes=" as *u8); g_pn(box[0]); g_puts("\n" as *u8) 57 let mod: *WasmMod = wm_new(wasm, box[0]) 58 if wm_parse(mod) != 0 { g_puts(" FAIL wasm parse\nverdict=RED\n" as *u8); sys_exit(1); return 1 } 59 mod.mem = sys_mmap(65536) as *u8 60 61 let fidx: i64 = wm_find_export(mod, "p_probe" as *u8) 62 g_puts(" [measure] funcs=" as *u8); g_pn(mod.n_funcs); g_puts(" exports=" as *u8); g_pn(mod.n_exports); g_puts(" p_probe fidx=" as *u8); g_pn(fidx); g_puts("\n" as *u8) 63 pass = pass + g_check("p_probe is exported (emulator-shaped .nx compiled to structured wasm)" as *u8, fidx >= 0) 64 total = total + 1 65 66 // 4) run p_probe(base=0) in the VM and check its return == the native KAT 67 let r: i64 = wm_run(mod, "p_probe" as *u8, 0, 0, 0, 0, 0, 1) 68 g_puts(" [measure] wasm p_probe(0) = " as *u8); g_pn(r); g_puts(" (native KAT = " as *u8); g_pn(KAT); g_puts(")\n" as *u8) 69 pass = pass + g_check("wasm p_probe matches native KAT (codegen faithful for load8/store8/gep/i64.store + fetch-decode-execute)" as *u8, r == KAT) 70 total = total + 1 71 72 g_puts("---- emu_probe wasm gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 73 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 74 g_puts("verdict=RED\n" as *u8); sys_exit(1) 75 return 1 76}