I'm trying to understand how exactly memory pages for stack is allocated/assigned.
I wrote the following proof-of-concept C-code which obviously causes segmentation fault (on x86_64 Linux):
#include <string.h>
int main()
{
char a;
memset( (&a - 4444444), 0, 3333333 );
return 0;
}
The following fragment of assembly code (AT&T syntax) is generated by gcc from above C-program:
subq $16, %rsp
leaq -1(%rbp), %rax
subq $4444444, %rax
movl $3333333, %edx
movl $0, %esi
movq %rax, %rdi
call memset
If I add subq $5555555, %rsp manually before calling memset:
subq $16, %rsp
leaq -1(%rbp), %rax
subq $4444444, %rax
movl $3333333, %edx
movl $0, %esi
movq %rax, %rdi
subq $5555555, %rsp /* added manually */
call memset
Then segmentation fault disappears because virtual memory pages for stack was assigned after subtracting rsp register caused some hardware exception and assigned exception handler was called (of course, in kernel space).
I know that calling memset here will cause "minor page fault" exceptions. But it's a different story (i.e. allocating physical memory pages).
My question is: Which exception was generated when subq $5555555, %rsp is invoked? I suggest it would be "stack fault" exception but I did not find exact proof for it.