0

the following code crashes. buf i constant stack_size change to 40 or 56, no crash occurs. why is this?

    ; nasm -f elf64 t.asm && gcc -no-pie t.o
    section .text
        global main
        extern printf, scanf

    stack_size equ 48
    main:
        sub     rsp, stack_size

        lea     rdi, [rel fmt_str]
        mov     rsi, rsp
        xor     eax, eax
        call    scanf

        add     rsp, stack_size
        ret

    fmt_str:  db "%ld",0
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • the program will not crash in either of below 2 circumstances: 1). change the constant variable stack_size to 40 or 56 or 64 ... 2). don't call scanf(), but call some other function, like printf – Steven Yi Jan 13 '20 at 07:11
  • Add those details to the question, not a comment. Use the "edit" link. – Barmar Jan 13 '20 at 07:47
  • What is common about the resultant value of rsp in the cases when this does not crash? – w08r Jan 13 '20 at 07:50
  • Can you provide an example binary that crashes? – Roguebantha Jan 13 '20 at 08:10
  • 1
    given `-felf64` and the symptoms, you're almost certainly on Linux where glibc's scanf compiles with some SSE instructions for 16-byte copies to/from the stack. – Peter Cordes Jan 13 '20 at 08:18

0 Answers0