flat assembler
Message board for the users of flat assembler.
Index
> Linux > return stack access segfaults |
Author |
|
redsock 08 Dec 2018, 20:17
your code snippets aren't very good at debugging segfaults. It is much better if you can recreate the condition in a working program.
I took a few minutes to write a stackframe min/max/size program that does compile, maybe you can modify mine to see where your error is? Code: format ELF64 section '.text' executable align 16 macro check_stackuse { mov rax, [stack_min] cmp rsp, rax cmovb rax, rsp mov [stack_min], rax } public example_func example_func: sub rsp, 4096 check_stackuse add rsp, 4096 ret align 16 public _start _start: mov [stack_top], rsp mov [stack_min], rsp mov eax, 97 ; syscall_getrlimit mov edi, 3 ; RLIMIT_STACK sub rsp, 16 mov rsi, rsp syscall mov rdx, [rsp] ; rlimit.rlim_cur mov rcx, [rsp+8] ; rlimit.rlim_max mov [stack_size], rdx add rsp, 16 ; breakpoint so we can inspect our values int3 nop ; use some stack call example_func mov rcx, [stack_top] sub rcx, [stack_min] int3 nop ; inspect our stack usage in rcx add rsp, 16 ; [rsp] back to commandline args mov eax, 60 ; syscall_exit xor edi, edi syscall section '.data' writable align 16 stack_top dq 0 stack_size dq 0 stack_min dq 0 When run from inside gdb, we get the following values: Code: (gdb) run Starting program: /tmp/test Program received signal SIGTRAP, Trace/breakpoint trap. 0x0000000000400116 in _start () (gdb) i r rdx rcx rdx 0x800000 8388608 rcx 0xffffffffffffffff -1 (gdb) cont Continuing. Program received signal SIGTRAP, Trace/breakpoint trap. 0x000000000040012b in _start () (gdb) i r rcx rcx 0x1008 4104 (gdb) cont Continuing. [Inferior 1 (process 55241) exited normally] |
|||
08 Dec 2018, 20:17 |
|
sts-q 08 Dec 2018, 20:17
The following code is apparently working.
In stead of mov a, [k] add k, 8 it does pop a starting at rs_minval. Best Regards and a Happy Second Advent sts-q Code: Stacks.max.rs: mov k, rsp mov rsp, [rs_minval] mov v, 1048000 clr d doloop pop a dec v notzero? a, .done inc d until_zero v error errmess_ssw .done: mov rsp, k mov a, [rs_base] sub a, [rs_minval] shr a,3 sub a, d int a ; a := (rs_base - rs_minval ) / 8 - d shiftup a exec |
|||
08 Dec 2018, 20:17 |
|
sts-q 08 Dec 2018, 20:46
Thank you for your answer, redsock!
You solution is simpler and more straight forward than mine: update stack usage together with check for overflow. Ok, the marco is 4 instructions more. I will try it. That was close: 20:17 both! Code: result return-stack-usage ackermann( 3, 13 ) 65533 196608 ackermann( 3, 14 ) 131069 396216 ackermann( 3, 15 ) 262141 786432 |
|||
08 Dec 2018, 20:46 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.