code wiki / (root) / nx_buf_owned_gate_t218.nx

nx_buf_owned_gate_t218.nx source

↩ module page · 48 lines · 4089 B

1import "nx_buf_dyn_owned_candidate_t218.nx" 2import "nx_fs_create_lib.nx" 3func bo_check(ok:i64,name:*u8,counts:*i64)->i64{counts[1]=counts[1]+1;fsx_puts(name);if ok!=0{counts[0]=counts[0]+1;fsx_puts(" PASS\n")}else{fsx_puts(" FAIL\n")};return 0} 4func main()->i64{ 5 let b:*NxBufOwned=sys_mmap_try(NX_BO_BYTES) as *NxBufOwned;let src:*u8=sys_mmap_try(257);let c:*i64=sys_mmap_try(16) as *i64 6 if (b as i64)<=0||(src as i64)<=0||(c as i64)<=0{return 3};b.buf=0 as *u8;b.len=0;b.cap=0;c[0]=0;c[1]=0 7 var i:i64=0;while i<257{src[i]=(i%251) as u8;i=i+1} 8 bo_check(nx_bo_append(b,0 as *u8,0,0)==0&&b.len==0&&b.cap==0,"zero empty append",c) 9 bo_check(nx_bo_append(0 as *NxBufOwned,src,1,0)==NX_BO_INVALID,"null descriptor",c) 10 bo_check(nx_bo_append(b,src,-1,0)==NX_BO_INVALID,"negative length",c) 11 bo_check(nx_bo_append(b,src,1,-1)==NX_BO_INVALID,"negative budget",c) 12 bo_check(nx_bo_append(b,0 as *u8,1,0)==NX_BO_INVALID,"null source",c) 13 bo_check(nx_bo_append(b,src,32,31)==NX_BO_CAPACITY&&b.cap==0,"caller budget before allocation",c) 14 bo_check(nx_bo_append(b,src,32,0)==0&&b.len==32&&b.cap==32,"first actual need",c) 15 let old:*u8=b.buf;let oldcap:i64=b.cap 16 bo_check(nx_bo_append(b,src,1,0)==0&&b.len==33&&b.cap==64,"amortized growth",c) 17 var equal:i64=1;i=0;while i<32{if b.buf[i]!=src[i]{equal=0};i=i+1};if b.buf[32]!=src[0]{equal=0} 18 bo_check(equal,"old and appended bytes preserved",c) 19 bo_check(sys_madvise(old,oldcap,0)==-12,"prior mapping released kernel ENOMEM",c) 20 let kept:*u8=b.buf;let keptcap:i64=b.cap 21 bo_check(nx_bo_append(b,src,2,0)==0&&b.len==35&&b.buf==kept&&b.cap==keptcap,"append within mapping",c) 22 bo_check(nx_bo_append(b,b.buf,1,0)==NX_BO_ALIAS&&b.len==35,"own source alias refused",c) 23 bo_check(nx_bo_append(b,((b.buf as i64)+1) as *u8,1,0)==NX_BO_ALIAS,"interior source alias refused",c) 24 bo_check(nx_bo_append(b,b as *u8,1,0)==NX_BO_ALIAS,"descriptor source alias refused",c) 25 bo_check(nx_bo_append(b,src,NX_BO_I64_MAX,0)==NX_BO_RANGE&&b.len==35,"length overflow refusal",c) 26 bo_check(nx_bo_append(b,NX_BO_I64_MAX as *u8,1,0)==NX_BO_RANGE,"source extent overflow refusal",c) 27 bo_check(nx_bo_append(b,src,1,35)==NX_BO_CAPACITY&&b.len==35&&b.buf==kept,"budget failure retains bytes ownership",c) 28 b.len=b.cap+1;bo_check(nx_bo_append(b,src,1,0)==NX_BO_INVALID,"invalid initialized state",c);b.len=35 29 bo_check(nx_bo_release(b)==0&&(b.buf as i64)==0&&b.len==0&&b.cap==0,"release resets descriptor",c) 30 bo_check(sys_madvise(kept,keptcap,0)==-12,"final mapping released kernel ENOMEM",c) 31 bo_check(nx_bo_release(b)==0,"idempotent release",c) 32 bo_check(nx_bo_append(b,src,3,0)==0&&nx_bo_append(b,src,2,5)==0&&b.len==5&&b.cap==5,"growth respects caller budget",c) 33 bo_check(nx_bo_release(b)==0,"second release",c) 34 // Gate process only: preserve hard AS limit, lower soft limit temporarily, 35 // use a valid one-byte source, restore before diagnostics allocate anything. 36 let previous:*i64=sys_mmap_try(16) as *i64;let limited:*i64=sys_mmap_try(16) as *i64 37 if (previous as i64)<=0||(limited as i64)<=0{return 3} 38 let queried:i64=nx_prlimit(0,RLIMIT_AS,0 as *u8,previous as *u8) 39 var changed:i64=-1;var alloc_rc:i64=0;var restored:i64=-1 40 if queried==0{limited[0]=0;limited[1]=previous[1];changed=nx_prlimit(0,RLIMIT_AS,limited as *u8,0 as *u8);if changed==0{alloc_rc=nx_bo_append(b,src,1,0);restored=nx_prlimit(0,RLIMIT_AS,previous as *u8,0 as *u8)}} 41 bo_check(queried==0&&changed==0&&restored==0,"gate-local soft AS limit restored",c) 42 bo_check(changed==0&&alloc_rc==NX_BO_ALLOCATION&&(b.buf as i64)==0&&b.len==0&&b.cap==0,"actual kernel allocation refusal preserves state",c) 43 sys_munmap_direct(previous as *u8,16);sys_munmap_direct(limited as *u8,16) 44 b.cap=1;bo_check(nx_bo_append(b,src,1,0)==NX_BO_RANGE,"null buffer nonempty capacity refused",c);b.cap=0 45 bo_check(nx_bo_release(0 as *NxBufOwned)==NX_BO_INVALID,"null release",c) 46 fsx_puts("OWNED-BUFFER pass=");fsx_putn(c[0]);fsx_puts(" total=");fsx_putn(c[1]);fsx_puts("\n") 47 var rc:i64=0;if c[0]!=c[1]{rc=1};sys_munmap_direct(src,257);sys_munmap_direct(b as *u8,NX_BO_BYTES);sys_munmap_direct(c as *u8,16);return rc 48}