Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overflow the stack without pushing new stack frames?

One obvious way to cause a stack overflow and get Segmentation fault would be to recursively push stack frames on top of each other until it booms. I'm wondering if stack overflow could happen without even pushing new stack frames.

Creating a large enough array could do it too from experience, but any other possible scenarios?

like image 383
Ann Brown Avatar asked Apr 11 '12 09:04

Ann Brown


People also ask

Can a stack contain multiple stack frames?

A stack consists of an ordered set of stack elements, called stack frames, which are managed in a last-in first-out manner. In this information, unqualified references to stack mean invocation stack. The invocation stack can contain multiple invocation stack frames, which represent invocation instances of routines.

How can we Minimise the stack overflow?

One method to prevent stack overflow is to track the stack pointer with test and measurement methods. Use timer interrupts that periodically check the location of the stack pointer, record the largest value, and watch that it does not grow beyond that value.

What is use of next instruction in stack frame?

It will immediately return the current frame from the stack following which the next frame is now in the activation record.

How many stack frames are created?

When your program is started, the stack has only one frame, that of the function main(). This is called the initial frame or the outermost frame. Each time a function is called, a new frame is made. Each time a function returns, the frame for that function invocation is eliminated.


1 Answers

C99 uses a resizable array, which you could use and keep resizing it to a larger one. However this resizable array is implemented using alloca. Here's a sample code in UNIX env:

#include <stdio.h>
#include <alloca.h>
#include <stdlib.h>
#include <stdbool.h>

int
main()
{
    while (true)
    {
        void *p = alloca(32UL);
        printf("new memory allocated at %p \n", p);
    }
    exit(EXIT_SUCCESS);
}

And your output will look like this

new memory allocated at 0xbf800a60 
new memory allocated at 0xbf800a30 
new memory allocated at 0xbf800a00 
new memory allocated at 0xbf8009d0 
new memory allocated at 0xbf8009a0 
[1]    3977 segmentation fault  ./a.out

alloca is in the malloc family of functions, except that it allocated memory on the stack by adjusting the stack pointer.

like image 65
Reza Toghraee Avatar answered Oct 29 '22 04:10

Reza Toghraee