_canary_stack_overflow.nx source
↩ module page · 28 lines · 1083 B
1// Canary: deliberate infinite recursion to trigger SIGSEGV from
2// stack overflow. With SA_ONSTACK + sigaltstack(2) wired into
3// _narr_install_traps the produced ELF should NOT die silently
4// with "qemu: uncaught target signal 11"; instead the substrate
5// trap handler runs on the alt stack and emits a structured
6// [✗CHK] event naming WHO/WHAT/WHY/HOW before exiting 128+11=139.
7//
8// USAGE:
9// _offc/nxc.elf bench/_canary_stack_overflow.nx 1>elf 3>asm
10// gcc-riscv64-linux-gnu (whatever) elf -> a.out
11// qemu-riscv64-static a.out 2>fd2 4>fd4
12// grep "received fatal signal 11" fd2 # MUST be present
13//
14// PRE-FIX: handler tries to push signal frame onto exhausted stack
15// -> recursive SIGSEGV -> SIGKILL -> empty fd2.
16// POST-FIX: handler runs on alt stack -> emits [✗CHK] -> rc=139.
17
18import "nx_narr.nx"
19
20func recurse(n: i64) -> i64 {
21 let pad: [i64; 64] // 512-byte frame to deplete stack faster
22 pad[0] = n
23 return recurse(n + 1) + pad[0]
24}
25
26func main(argc: i64, argv: *u8) -> i64 {
27 return recurse(0)
28}