Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum stack size, ulimit -s, segfault 11 -- how does this work?

Tags:

c

So, I do:

$ ulimit -s
8192

Great. As I understand this, the stack segment of any process cannot exceed 8192 kilobytes.

Now, to challenge that..

#include <stdio.h>

void over_8k(void) {
    char buf[1024*1024*20];
}

int main(int argc, char** argv) {
    printf("Starting .. ");
    over_8k();
    printf(" finishing.\nHow did this work?\n");
    return 0;
}

Compiled. Ran. No problems. Well this isn't right? over_8k alone should have a stack frame of, well, over 20 megabytes. Well, let's try accessing those 20 million bytes:

#include <stdio.h>
#include <string.h>

void over_8k(void) {
    char buf[1024*1024*20];
    memset(buf, 'A', sizeof(buf));
}

int main(int argc, char** argv) {
    printf("Starting .. ");
    over_8k();
    printf(" finishing.\nHow did this work?\n");
    return 0;
}

.. drum roll ..

Segmentation fault: 11

Great. But that's not the error I'd expect? Invalid memory access?

Why does it raise a segfault, and doesn't error out earlier? On call to over_8k perhaps? How does this work? I want to know everything.

like image 572
ntl0ve Avatar asked Mar 02 '12 09:03

ntl0ve


People also ask

How do you calculate segfault?

Use debuggers to diagnose segfaults Start your debugger with the command gdb core , and then use the backtrace command to see where the program was when it crashed. This simple trick will allow you to focus on that part of the code.

What happens segfault?

A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).

Do Memory leaks cause segfault?

No, memory leaks by themselves would not cause a segmentation fault. However, memory leaks usually indicate sloppy code, and in sloppy code other issues, which would cause a segmentation fault, are likely to be present.

What is segfault in Linux?

What Is Segmentation Fault? In a nutshell, segmentation fault refers to errors due to a process's attempts to access memory regions that it shouldn't. When the kernel detects odd memory access behaviors, it terminates the process issuing a segmentation violation signal (SIGSEGV).


2 Answers

Expanding on my comment...

There's two possibilities I can think of:

The compiler is optimizing out the entire buf array:

In MSVC, with optimizations enabled, the entire array is being completely optimized out and is not allocated at all. So it's not using any stack.

Stack allocation is just an increment/decrement to the stack pointer:

sub rsp, 20971520

won't segfault. It's just a pointer. It will only segfault when you try to access it into unmapped memory.

like image 82
Mysticial Avatar answered Oct 06 '22 23:10

Mysticial


Declaring your buf array involves nothing more than incrementing the stack pointer.

Incrementing the stack pointer itself beyond the limit of the stack area is not going to make the program crash - it's just a register that happens to have a larger value. Referring to memory beyond that area, however, will most certainly make the program crash.

like image 30
Blagovest Buyukliev Avatar answered Oct 07 '22 01:10

Blagovest Buyukliev