Note: This question is about x86_64 architecture and Linux ABI.
When the program is launched, some space is allocated for stack. Later on, during program execution the stack area can get resized (when more space is required) up to some maximum specified by OS.
Let's take for instance simple program:
int main() {
char bytes[7 * 1024 * 1024];
}
Let's run it under gdb and set breakpoints: before main and after declaring an array.
gdb> b *main
gdb> b main
gdb> r
gdb> info proc mapping // breakpoint before pushing stack
Start Addr End Addr Size Offset objfile
0x7ffffffde000 0x7ffffffff000 0x21000 0x0 [stack]
gdb> c
gdb> info proc mapping // breakpoint after pushing stack
Start Addr End Addr Size Offset objfile
0x7fffff8fe000 0x7ffffffff000 0x701000 0x0 [stack]
So we can see the stack actually got resized.
The question is how does the OS know when the stack has to be resized?. Some internet resources say OS handles page fault exception
and if the accessed address is within possible stack address range, it gets resized.
But when I was debugging the program step by step, it turned out, the stack got resized just after following instruction:
0x40129d <main+4> sub rsp, 0x700010
So (as far as I know), there is no page fault
yet since we are not accessing the addresses actually. We only change rsp
register. So how is it possible OS handles it? Or maybe there is a page fault exception
after changing rsp
?
rsp
around does nothing until you actually perform a suitable memory access. You did not show full disassembly. I have checked and stack doesn't grow here. – Unconcerninfo proc mappings
output or some other way? – Goldensealinfo proc mappings
. Note,gdb
may cause the stack to grow as well, e.g. if you dox/a $rsp
or have something set up that does so. – Unconcern