Does the OS reserve the fixed amount of valid virtual space for stack or something else? Am I able to produce a stack overflow just by using big local variables?
I've wrote a small C
program to test my assumption. It's running on X86-64 CentOS 6.5.
#include <string.h>
#include <stdio.h>
int main()
{
int n = 10240 * 1024;
char a[n];
memset(a, 'x', n);
printf("%x\n%x\n", &a[0], &a[n-1]);
getchar();
return 0;
}
Running the program gives &a[0] = f0ceabe0
and &a[n-1] = f16eabdf
The proc maps shows the stack: 7ffff0cea000-7ffff16ec000. (10248 * 1024B)
Then I tried to increase n = 11240 * 1024
Running the program gives &a[0] = b6b36690
and &a[n-1] = b763068f
The proc maps shows the stack: 7fffb6b35000-7fffb7633000. (11256 * 1024B)
ulimit -s
prints 10240
in my PC.
As you can see, in both case the stack size is bigger than which ulimit -s
gives. And the stack grows with bigger local variable. The top of stack is somehow 3-5kB more off &a[0]
(AFAIK the red zone is 128B).
So how does this stack map get allocated?