C stackoverflow runtime

#include <stdio.h>

int doHello(){
    doHello();
}

int main(){
    doHello();
    printf("\nLeaving Main");
    return 0;
}

When you run this, the program terminates without printing the message "Exit the main" on the screen. This is the case and because of which the program ends, but I do not see error messages in the command window. (Ran on Windows / Cygwin /)

Q1. I did not declare any local variables in the doHello function, but still the stack is becoming familiar. This is because

  • return values
  • information stored about function calls?

Explanation

Q2. How to debug such cases in your program? I do not ask you to debug the endless loop that I mentioned above.

eg:

#define SIZE 512*1024
void doOVerflow(){
   char str[SIZE];
   doHello();
}

void doHello(){
   char strHello[256];  // stack gets filled up at this point
   doNothing();         // program terminates and function doNothing does not get called
}

EDIT:

Q3. What information is stored in the execution stack?

+3
source share
6 answers

, . . " " wikipedia. :

$ gcc -S test.c  # <--- assembles, but does not compile, test.c; output in test.s
$ cat test.s
// [some contents snipped]
_doHello:
        pushl   %ebp        // <--- pushes address of stack frame onto stack
        movl    %esp, %ebp  // <--- sets up new stack frame
        call    _doHello    // <--- pushes return value onto stack, makes call
        popl    %ebp        // <--- pops address of stack frame off stack
        ret                 // <--- pops return value off stack, returns to it

"-fomit-frame-":

$ gcc -fomit-frame-pointers -S test.c
$ cat test.s
// [some contents snipped]
_doHello:
        call    _doHello   // <--- pushes return value onto stack, makes call
        ret                // <--- pops return value off stack, returns to it

, , , :

$ gcc -fomit-frame-pointers -O4 -S test.c # <--- heavy optimization
$ cat test.s
// [some contents snipped]
_doHello:
L2:
        jmp     L2         // <--- no more stack operations!

, , , (cygwin, ).

, (, Microsoft Visual ++ gdb) stackdumps, ( .core .stackdump), .

, : , . , .

, , valgrind Application Verifier .

+10

, , .

+6

- , .

. , .

+3

- - a. . , ?

, .

?

. .

Q3. ?

.

, omto , . , , . , - , , , , , .. ad infimnitum, .

+2

, .

. . . , .

+1

.

( so.c):

int doHello(){
    doHello();
}

int main(){
    doHello();
    printf("\nLeaving Main");
    return 0;
}

doHello():

$ gcc -Wall -ggdb3 -O0 so.c -o so
$ gdb --annotate=3 so
(gdb) disassemble doHello
Dump of assembler code for function doHello:
0x080483e4 <doHello+0>: push   %ebp
0x080483e5 <doHello+1>: mov    %esp,%ebp
0x080483e7 <doHello+3>: sub    $0x8,%esp
0x080483ea <doHello+6>: call   0x80483e4 <doHello>
0x080483ef <doHello+11>:        leave
0x080483f0 <doHello+12>:        ret
End of assembler dump.

, , . , : dword ( ).

, .

Greetings.

+1
source

Source: https://habr.com/ru/post/1715248/


All Articles