I have executed this dumb code for some testing:
int main(){
char a[4];
a[3000] = '2'; //no problem
sleep(30);
printf("wake up\n");
a[20000] = '2'; //segmentation fault
printf("No segmentation fault!\n");
while(1);
}
I know that this is so dumb (creating a 4 bytes array and wanting to store at the position 3000 and at the position 20000) but, as I said, it's only for testing. What I wanted to test is if accessing to a page reserved for stack space will result in a Segmentation Fault. It shouldn't, but it did!
How I know that the page that belongs to the a[20000] is a reserved page for this process? Well, during the sleep(30) time I watched the content of /proc/pid/maps and I got this:
7ffdce868000-7ffdce889000 rw-p 00000000 00:00 0 [stack]
If you look at starting and finishing directions of the stack, you will see that an access to a[20000] is covered by this directions, since OS has assigned for that moment 33 pages of stack (which is 135168 Bytes). On those pages the process has reading and writing permision.
So, why did I got segmentation fault??