Author Topic: Seg Fault when calling scanf  (Read 12818 times)

Offline pedrodemargomes

  • Jr. Member
  • *
  • Posts: 3
Seg Fault when calling scanf
« on: April 24, 2020, 03:16:56 PM »
This is my code:

Code: [Select]
   global main
   extern  printf
   extern  scanf
   section .text
main:
   push rbp
   mov rbp, rsp
   mov rax, 42
   push rax
   
   lea rsi, [rbp-8]
   mov rdi, formatNumScanf
   xor rax, rax
   call scanf wrt ..plt
   
   mov rax, [rbp-8]
   mov rdi, formatNumPrintf
   mov rsi, rax
   xor rax, rax
   call printf wrt ..plt
   
   mov rsp, rbp
   pop rbp
   section .rodata
formatNumPrintf:   db '%ld', 10, 0
formatNumScanf:   db '%ld', 0
It erros with a seg fault when calling scanf with [rbp-8] address, that is the top of the stack.
It is strange that if i push two registers onto the stack at the beggining, it works as expected.
What am i doing wrong?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Seg Fault when calling scanf
« Reply #1 on: April 25, 2020, 12:01:04 AM »
Hi Pedro,
Welcome to the forum.

Curiously, your code works for me, just as posted. You don't seem to "ret" from "main"... but it works anyway. I am confused!

Best,
Frank


Offline pedrodemargomes

  • Jr. Member
  • *
  • Posts: 3
Re: Seg Fault when calling scanf
« Reply #2 on: April 25, 2020, 03:29:16 AM »
That is awkward.
I am using Ubuntu in a x86-64 pc and compiling with this command line:
Code: [Select]
nasm -felf64 teste.s && gcc teste.o
Is there something wrong with it?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Seg Fault when calling scanf
« Reply #3 on: April 25, 2020, 04:21:06 AM »
Hi again...

I am using Debian. Shouldn't make any difference.

As I said, I tried your code exactly as posted. It worked. Then I added a "ret" as the last thing on your ".text" section. I really think that should be there. No difference. Try it and see if it makes any difference for you. This is a real puzzle!

Best,
Frank


Offline pedrodemargomes

  • Jr. Member
  • *
  • Posts: 3
Re: Seg Fault when calling scanf
« Reply #4 on: April 25, 2020, 04:50:13 AM »
I added the ret instruction and it continues to give seg fault.
I runned the program with gdb, and discovered that the seg fault occurs in scanf.
Here is the gdb output:
Code: [Select]
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a4f4d6 in char_buffer_rewind (buffer=0x7fffffffd8c8) at vfscanf.c:224
Strangely, when i push two registers onto the stack before calling scanf it works.
I think that it has something to do with stack aligment, maybe the stack must be align in 16 byte boundaries.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Seg Fault when calling scanf
« Reply #5 on: April 25, 2020, 05:15:04 AM »
I am not good at 64-bit code. It does have some stack alignment requirements. I think you've probably got it.

Best,
Frank