code wiki / (root) / nx_madvise_probe.nx

nx_madvise_probe.nx source

↩ module page · 65 lines · 2847 B

1// nx_madvise_probe.nx -- SYSCALL-IDENTITY PROBE: does raw __syscall(28) reach x86_64 madvise(2)? 2// WHY RAW 28: the portable rv64/asm-generic number is 233 and x86ctx_rv64_to_x86_64_syscall carries 3// no 233 row yet, so a portable const would fall through `return num` to x86_64 233 = epoll_ctl -- 4// the wrong-syscall-not-an-error class (setpgid/flock precedent). Raw 28 is the sys_exit_group 5// pattern (raw x86 231) and is conflict-checked: nothing in the tree calls __syscall(28,...) today. 6// PROOF SHAPE (print the numbers, never just a verdict): 7// T1 positive: MADV_WILLNEED(3) on a live 1MB anonymous mapping -> expect 0 8// T2 negative-control: MADV_WILLNEED on the unmapped page at 4096 -> expect -12 (ENOMEM) 9// T3 identity-control: advice=9999 on the live mapping -> expect -22 (EINVAL) 10// The TRIPLE (0,-12,-22) is a madvise signature; any other syscall answers differently (epoll_ctl 11// would say EBADF/EINVAL on T1). exit 0 = MADVISE-LIVE, 1 = WRONG-SYSCALL with the numbers printed. 12// license_tier: ORIGINAL No hw writes (Rule 26). 13import "nx_syscalls.nx" 14const K_MAGIC_1048576: i64 = 1048576 15const K_MAGIC_8192: i64 = 8192 16const K_MAGIC_4096: i64 = 4096 17const K_MAGIC_9999: i64 = 9999 18 19func mp_wnum(buf: *u8, off: i64, v: i64) -> i64 { 20 var o: i64 = off 21 var x: i64 = v 22 if x < 0 { buf[o] = 45 as u8; o = o + 1; x = 0 - x } 23 let tmp: *u8 = sys_mmap(24) 24 var n: i64 = 0 25 if x == 0 { tmp[0] = 48 as u8; n = 1 } 26 while x > 0 { tmp[n] = (48 + x % 10) as u8; x = x / 10; n = n + 1 } 27 while n > 0 { n = n - 1; buf[o] = tmp[n]; o = o + 1 } 28 return o 29} 30 31func mp_cat(buf: *u8, off: i64, s: *u8) -> i64 { 32 var o: i64 = off 33 var i: i64 = 0 34 while s[i] != (0 as u8) { buf[o] = s[i]; o = o + 1; i = i + 1 } 35 return o 36} 37 38func main() -> i64 { 39 let m: *u8 = sys_mmap(K_MAGIC_1048576) 40 let r1: i64 = __syscall(28, m, K_MAGIC_8192, 3, 0, 0, 0) 41 let r2: i64 = __syscall(28, K_MAGIC_4096, K_MAGIC_4096, 3, 0, 0, 0) 42 let r3: i64 = __syscall(28, m, K_MAGIC_8192, K_MAGIC_9999, 0, 0, 0) 43 let out: *u8 = sys_mmap(256) 44 var o: i64 = 0 45 let t1: *u8 = "madvise-probe raw28: willneed=" as *u8 46 o = mp_cat(out, o, t1) 47 o = mp_wnum(out, o, r1) 48 let t2: *u8 = " unmapped=" as *u8 49 o = mp_cat(out, o, t2) 50 o = mp_wnum(out, o, r2) 51 let t3: *u8 = " badadvice=" as *u8 52 o = mp_cat(out, o, t3) 53 o = mp_wnum(out, o, r3) 54 var vok: i64 = 0 55 if r1 == 0 { if r2 == (0 - 12) { if r3 == (0 - 22) { vok = 1 } } } 56 let t4: *u8 = " verdict=" as *u8 57 o = mp_cat(out, o, t4) 58 if vok == 1 { let sg: *u8 = "MADVISE-LIVE" as *u8; o = mp_cat(out, o, sg) } 59 if vok == 0 { let sr: *u8 = "WRONG-SYSCALL" as *u8; o = mp_cat(out, o, sr) } 60 out[o] = 10 as u8 61 o = o + 1 62 sys_write(1, out, o) 63 if vok == 1 { return 0 } 64 return 1 65}