code wiki / _hdl_build / nx_emu_probe_wasm_gate.nx
nx_emu_probe_wasm_gate.nx source
↩ module page · 83 lines · 4424 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"
12import "nx_gate_verdict.nx"
13
14const CWAT_ELF: *u8 = "/tmp/nx_compile_wat.sov.elf"
15const WATC_ELF: *u8 = "/tmp/nx_wat_compiler.sov.elf"
16const SRC_NX: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/runtime/nx_emu_probe.nx"
17const OUT_WAT: *u8 = "/tmp/emu.wat"
18const OUT_WASM: *u8 = "/tmp/emu.wasm"
19const KAT: i64 = 8081519
20
21func run_elf(elf: *u8, a1: *u8, a2: *u8) -> i64 {
22 let pid: i64 = sys_fork()
23 if pid == 0 {
24 let argv: *i64 = sys_mmap(64) as *i64
25 argv[0] = elf as i64
26 var ai: i64 = 1
27 if (a1 as i64) != 0 { argv[ai] = a1 as i64; ai = ai + 1 }
28 if (a2 as i64) != 0 { argv[ai] = a2 as i64; ai = ai + 1 }
29 argv[ai] = 0
30 let envp: *i64 = sys_mmap(16) as *i64
31 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
32 envp[1] = 0
33 sys_execve(elf, argv, envp)
34 sys_exit(127)
35 }
36 let st: *i64 = sys_mmap(16) as *i64
37 sys_wait4(pid, st, 0)
38 return (st[0] >> 8) & 0xff
39}
40
41func main() -> i64 {
42 g_puts("nx_emu_probe WASM gate (sovereign compile via forked ELFs -> executed in nx_wasm_vm -> vs native KAT)\n" as *u8)
43 var pass: i64 = 0
44 var total: i64 = 0
45
46 // 1) .nx -> .wat (nx_compile_wat writes WAT to OUT_WAT via argv[2] -- no stdout plumbing)
47 let rc1: i64 = run_elf(CWAT_ELF, SRC_NX, OUT_WAT)
48 g_puts(" [build] nx_compile_wat rc=" as *u8); g_pn(rc1); g_puts("\n" as *u8)
49 // 2) .wat -> .wasm (nx_wat_compiler takes in.wat out.wasm as args)
50 let rc2: i64 = run_elf(WATC_ELF, OUT_WAT, OUT_WASM)
51 g_puts(" [build] nx_wat_compiler rc=" as *u8); g_pn(rc2); g_puts("\n" as *u8)
52
53 // 3) read the produced wasm, parse it in the sovereign VM
54 let box: *i64 = sys_mmap(16) as *i64
55 let wasm: *u8 = sys_read_file(OUT_WASM, box)
56 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 }
57 g_puts(" [measure] wasm bytes=" as *u8); g_pn(box[0]); g_puts("\n" as *u8)
58 let mod: *WasmMod = wm_new(wasm, box[0])
59 if wm_parse(mod) != 0 { g_puts(" FAIL wasm parse\nverdict=RED\n" as *u8); sys_exit(1); return 1 }
60 mod.mem = sys_mmap(65536) as *u8
61
62 let fidx: i64 = wm_find_export(mod, "p_probe" as *u8)
63 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)
64 pass = pass + g_check("p_probe is exported (emulator-shaped .nx compiled to structured wasm)" as *u8, fidx >= 0)
65 total = total + 1
66
67 // 4) run p_probe(base=0) in the VM and check its return == the native KAT
68 let r: i64 = wm_run(mod, "p_probe" as *u8, 0, 0, 0, 0, 0, 1)
69 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)
70 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)
71 total = total + 1
72
73 g_puts("---- emu_probe wasm gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
74 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
75 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
76 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
77 let ctr__dry: *i64 = gv_ctr()
78 ctr__dry[0] = pass
79 ctr__dry[1] = total
80 let rc__dry: i64 = gv_verdict("EMU-PROBE-WASM-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
81 sys_exit(rc__dry)
82 return rc__dry
83}