Home > Enterprise >  How much memory is allocated to call stack?
How much memory is allocated to call stack?

Time:03-03

Previously I had seen assembly of many functions in C . In gcc, all of them start with these instructions:

push    rbp
mov     rbp, rsp
sub     rsp, <X>  ; <X> is size of frame

I know that these instructions store the frame pointer of previous function and then sets up a frame for current function. But here, assembly is neither asking for mapping memory (like malloc) and nor it is checking weather the memory pointed by rbp is allocated to the process.

So it assumes that startup code has mapped enough memory for the entire depth of call stack. So exactly how much memory is allocated for call stack? How does startup code can know the maximum depth of call stack?

It also means that, I can access array out of bound for a long distance since although it is not in current frame, it mapped to the process. So I wrote this code:

int main() {
    int arr[3] = {};
    printf("%d", arr[900]);
}

This is exiting with SIGSEGV when index is 900. But surprisingly not when index is 901. Similarly, it is exiting with SIGSEGV for some random indices and not for some. This behavior was observed when compiled with gcc-x86-64-11.2 in compiler explorer.

CodePudding user response:

How does startup code can know the maximum depth of call stack?

It doesn't.

In most common implementation, the size of the stack is constant.

If the program exceeds the constant sized stack, that is called a stack overflow. This is why you must avoid creating large objects (which are typically, but not necessarily, arrays) in automatic storage, and why you must avoid recursion with linear depth (such as recursive linked list algorithms).

So exactly how much memory is allocated for call stack?

On most desktop/server systems it's configurable, and defaults to one to few megabytes. It can be much less on embedded systems.

This is exiting with SIGSEGV when index is 900. But surprisingly not when index is 901.

In both cases, the behaviour of the program is undefined.

Is it possible to know the allocated stack size?

Yes. You can read the documentation of the target system. If you intend to write a portable program, then you must assume the minimum of all target systems. For desktop/server, 1 megabyte that I mentioned is reasonable.

There is no standard way to acquire the size within C .

  • Related