code wiki / _hdl_build / nx_vmleak_gate.nx

nx_vmleak_gate.nx source

↩ module page · 36 lines · 2082 B

1// nx_vmleak_gate.nx -- FAST (<10s, no codec) VM per-operation-allocation leak regression test. Runs 2// probe(100000) in the sovereign wasm VM = 100000 calls + 100000 if-opens = ~300000 internal allocations. 3// Under the OLD per-op sys_mmap-without-free leak this exhausts memory and faults; with the munmap fix it 4// returns 100000. Guards the fix against regression, cheaply. expect_exit: 0 license_tier: ORIGINAL 5import "nx_syscalls.nx" 6import "nx_wasm_vm.nx" 7 8const PROBE_WASM: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/web_assets/_video_build/nx_vmleak_probe.wasm" 9 10func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func g_pn(v: i64) -> i64 { 12 if v==0 { sys_write(1,"0" as *u8,1); return 0 } 13 let b: *u8 = sys_mmap(28); var x: i64 = v; if x<0 { sys_write(1,"-" as *u8,1); x=0-x } 14 var d: i64=0; var y: i64=x 15 while y>0 { d=d+1; y=y/10 } 16 var i: i64=d-1; y=x 17 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 } 18 sys_write(1,b,d); return 0 19} 20 21func main() -> i64 { 22 g_puts("nx_vmleak_gate -- VM per-op allocation leak regression (probe 100000 calls+ifs)\n" as *u8) 23 let box: *i64 = sys_mmap(16) as *i64 24 let wasm: *u8 = sys_read_file(PROBE_WASM, box) 25 if (wasm as i64) == 0 { g_puts(" FAIL cannot read probe wasm\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 } 26 let mod: *WasmMod = wm_new(wasm, box[0]) 27 if wm_parse(mod) != 0 { g_puts(" FAIL parse\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 } 28 var mb: i64 = mod.mem_bytes 29 if mb < 65536 { mb = 65536 } 30 mod.mem = sys_mmap(mb) as *u8 31 let N: i64 = 100000 32 let r: i64 = wm_run(mod, "probe" as *u8, N, 0, 0, 0, 0, 1) 33 g_puts(" probe(100000) returned " as *u8); g_pn(r); g_puts(" (expect 100000)\n" as *u8) 34 if r == N { g_puts("---- vmleak gate: passed 1 / 1 ----\nverdict=GREEN -- VM survives 300k allocations: the per-op mmap leak is fixed\n" as *u8); sys_exit(0); return 0 } 35 g_puts("---- vmleak gate: passed 0 / 1 ----\nverdict=RED\n" as *u8); sys_exit(1); return 1 36}