Eli5-What does a “stack overflow” mean?

1.26K views

Eli5-What does a “stack overflow” mean?

In: 847

41 Answers

Anonymous 0 Comments

Many other commenters are describing what I would call a “buffer overflow”, and not a proper “stack overflow”.

ELI5: A stack overflow is when the computer thinks the computer programmer is a dumbass and says “fuck this shit, I give up” because the programmer told it to perform a task which contains itself and the computer has been doing the same thing over and over again for too long.

ELI25: A computer has something called a “runtime stack”. The runtime stack is responsible for keeping track of program state when a “function” is called. This is necessary because computers are stupid and have a VERY short attention span. While a computer can have many gigabytes of RAM memory, the amount of memory that is actually directly accessible to the computer at any given moment in time is about 100 bytes worth of CPU registers or so. So every time a function is called, the computer has to write down the contents of the CPU registers (along with any input parameters) into a “stack frame” which is placed on top of the runtime stack. This is necessary so that when the function has completed, the computer program can take the stack frame off of the runtime stack, restore the CPU registers to their original state and pick up where it left off (usually with some additional data that the function has written to the stack frame for the program to use).

Important to our discussion is the fact that functions can call other functions. Each function that is called requires its own stack frame to be placed on top of the runtime stack, causing it to grow. Of particular note is the fact that functions *can call themselves*. This is called “recursion”, which is commonly used in a programming strategy called “divide and conquer”. Recursion can be a dangerous technique, because if you don’t write your function carefully, it will keep calling itself over and over again. This is called “infinite recursion”, and is the most common causes of stack overflows.

Modern operating systems specify a maximum size for a runtime stack. If the runtime stack tries to access unallocated memory, the computer hardware (on a modern computer) detects this automatically and causes a “page fault” to occur. A “page fault” will automatically halt the program’s execution and hand control back to the operating system, which has the power to decide the program’s fate. If the runtime stack has not exceeded the maximum size, the operating system will allocate more memory for it to use and allow it to occur. If the maximum size HAS been reached, then the operating system kills the process immediately, reports a “stack overflow error” and begins cleaning up all of the program’s resources.

On very old or simple computers, the above mentioned process for detecting a stack overflow does not happen, and the program will keep running while growing the runtime stack indefinitely. Eventually the runtime stack will grow so large, that it will start overwriting other data which will inevitably cause Bad Things(tm) to happen. If the system vectors are overwritten, the computer can forget how to perform basic tasks like multiplication or how to turn itself off. Worst case scenario, malicious code can take advantage of a stack overflow in order to corrupt other programs and gain total control over the system.

You are viewing 1 out of 41 answers, click here to view all answers.