code wiki / _hdl_build / nx_arg7_repro.nx

nx_arg7_repro.nx source

↩ module page · 36 lines · 2476 B

1// nx_arg7_repro.nx -- minimal repro of the >6-param segfault. p7 takes 7 pointer params and WRITES THROUGH the 2// 7th (g) LATE, after using the first six -- mirroring expand_one's selbox[0]=sel. If the toolchain mishandles 3// param idx 6 (the stack arg), g[0]=99 dereferences garbage -> SIGSEGV. expect: g[0]==99, sum prints, exit 0. 4// license_tier: ORIGINAL 5import "nx_syscalls.nx" 6 7func pw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 8func pn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var w: i64=0; var q: i64=k-1; while q>=0 { o[w]=t[q]; w=w+1; q=q-1 } sys_write(1,o,w); return 0 } 9 10func clobber(x: i64) -> i64 { var s: i64=0; var i: i64=0; while i<x { s=s+i; i=i+1 } return s } 11// an 8-param callee (like sf_keep) -- itself uses 2 stack args; called from inside the 7-param p7. 12func p8(a: i64, b: i64, c: i64, d: i64, e: i64, f: i64, g: i64, h: i64) -> i64 { return a+b+c+d+e+f+g+h } 13 14func p7(a: *i64, b: *i64, c: *i64, d: *i64, e: *i64, f: *i64, g: *i64) -> i64 { 15 let s: i64 = a[0]+b[0]+c[0]+d[0]+e[0]+f[0] // use the 6 register params first 16 // INTERVENING 8-PARAM CALLS (like expand_one calling sf_keep) -- stack-arg calls from a 7-param fn 17 let junk: i64 = p8(1,2,3,4,5,6,7,8) + p8(8,7,6,5,4,3,2,1) + clobber(300) 18 g[0] = 99 // WRITE THROUGH the 7th param AFTER the calls -- the real test 19 return s + g[0] + (junk * 0) 20} 21 22func main() -> i64 { 23 pw("=== nx_arg7_repro: 7-param function writing through the 7th param ===\n" as *u8) 24 let a: *i64=sys_mmap(8) as *i64; a[0]=1 25 let b: *i64=sys_mmap(8) as *i64; b[0]=2 26 let c: *i64=sys_mmap(8) as *i64; c[0]=3 27 let d: *i64=sys_mmap(8) as *i64; d[0]=4 28 let e: *i64=sys_mmap(8) as *i64; e[0]=5 29 let f: *i64=sys_mmap(8) as *i64; f[0]=6 30 let g: *i64=sys_mmap(8) as *i64; g[0]=0 31 pw("calling p7 (if the next line prints, the 7th param survived)...\n" as *u8) 32 let r: i64=p7(a,b,c,d,e,f,g) 33 pw("p7 returned=" as *u8); pn(r); pw(" (expect 120: 1+2+3+4+5+6=21, +99) g[0]=" as *u8); pn(g[0]); pw(" (expect 99)\n" as *u8) 34 if r==120 { if g[0]==99 { pw("RESULT: PASS -- 7th param works\n" as *u8); sys_exit(0); return 0 } } 35 pw("RESULT: FAIL -- 7th param mishandled\n" as *u8); sys_exit(1); return 1 36}