code wiki / _hdl_build / test_munmap.nx

test_munmap.nx source

↩ module page · 22 lines · 1084 B

1// test_munmap.nx -- verify sys_munmap actually frees. 200k cycles of mmap(1MB)+touch+munmap = 200GB churned; 2// if munmap frees, peak stays ~1MB and it completes; if munmap is a no-op, address space exhausts -> mmap 3// returns <0 and we report failure. expect_exit: 0 4import "nx_syscalls.nx" 5func p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 6func pn(v: i64) -> i64 { 7 let b: *u8=sys_mmap(28); var m: i64=v; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} 8 while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k) 9 sys_munmap(b,28); sys_munmap(t,28); return 0 10} 11func main() -> i64 { 12 var i: i64=0 13 while i<200000 { 14 let b: *u8 = sys_mmap(1048576) 15 if (b as i64) < 0 { p("MMAP-FAILED at iter=" as *u8); pn(i); p(" (munmap NOT freeing)\n" as *u8); sys_exit(1); return 1 } 16 b[0]=42 as u8 17 sys_munmap(b, 1048576) 18 i=i+1 19 } 20 p("MUNMAP-OK: 200k mmap+munmap cycles, no exhaustion\n" as *u8) 21 sys_exit(0); return 0 22}