Identify Stack Overflow
Learn how to identify stack overflow, stack boundaries, and reconstruct stack trace.
What is stack overflow?
When an application runs out of memory in the call stack, a stack overflow occurs. It can happen due to an overuse of stack, like in the case of infinite or deep recursion, or when a local variable—like an array—that uses more space than the size of the call stack is declared.
Application source code
We have created a multi-threaded application that encounters a stack overflow and times out during its execution:
Press + to interact
// Build:// gcc main.c -pthread -static -o App6#include <stdio.h>#include <pthread.h>#include <unistd.h>#include <string.h>#include <stdlib.h>void procF(int i){int buffer[128] = {-1, 0, i + 1, 0, -1};procF(buffer[2]);}void procE(){procF(1);}#define THREAD_DECLARE(num, func) void bar_##num() \{ \sleep(300); \func; \} \\void foo_##num() \{ \bar_##num(); \} \\void * thread_##num (void *arg) \{ \foo_##num(); \\return 0; \}THREAD_DECLARE(one, procE())THREAD_DECLARE(two, sleep(-1))THREAD_DECLARE(three, sleep(-1))THREAD_DECLARE(four, sleep(-1))THREAD_DECLARE(five, sleep(-1))#define THREAD_CREATE(num) {pthread_t threadID_##num; pthread_create(&threadID_##num, NULL, thread_##num, NULL);}int main(int argc, const char * argv[]){THREAD_CREATE(one)THREAD_CREATE(two)THREAD_CREATE(three)THREAD_CREATE(four)THREAD_CREATE(five)sleep(-1);return 0;}
As you can see, calling procF
will result in infinite recursion, filling up the stack quickly.
Loading the core dump
We will load the core dump file with the following command:
gdb -c core.App6 -se App6
The above command will output the following to the terminal:
Logging the GDB output
...Access this course and 1400+ top-rated courses and projects.