<p>Why does the linux kernel generate a segfault on stack overflow? This can make debugging very awkward when alloca in c or fortran creation of temporary arrays overflows. Surely it mjust be possible for the runtime to produce a more helpful error.</p>

[{'answer_id': 81223, 'author': 'paxdiablo', 'author_id': 14860, 'author_profile': 'https://Stackoverflow.com/users/14860', 'pm_score': 3, 'selected': False, 'text': '<p>The "kernel" (it\'s actually not the kernel running your code, it\'s the CPU) doesn\'t know how your code is referencing the memory it\'s not supposed to be touching.  It only knows that you tried to do it.</p>\n\n<p>The code:</p>\n\n<pre><code>char *x = alloca(100);\nchar y = x[150];\n</code></pre>\n\n<p>can\'t really be evaluated by the CPU as you trying to access beyond the bounds of x.</p>\n\n<p>You may hit the exact same address with:</p>\n\n<pre><code>char y = *((char*)(0xdeadbeef));\n</code></pre>\n\n<p>BTW, I would discourage the use of alloca since stack tends to be much more limited than heap (use malloc instead).</p>\n'}, {'answer_id': 81264, 'author': 'Sargun Dhillon', 'author_id': 10432, 'author_profile': 'https://Stackoverflow.com/users/10432', 'pm_score': 3, 'selected': False, 'text': '<p>A stack overflow is a segmentation fault. As in you\'ve broken the given bounds of memory that the you were initially allocated. The stack of of finite size, and you have exceeded it. You can read more about it at <a href="http://en.wikipedia.org/wiki/Segmentation_fault" rel="noreferrer">wikipedia</a></p>\n\n<p>Additionally, one thing I\'ve done for projects in the past is write my own signal handler to segfault (look at man page signal (2)). I usually caught the signal and wrote out "Fatal error has occured" to the console. I did some further stuff with checkpoint flags, and debugging. </p>\n\n<p>In order to debug segfaults you can run a program in GDB. For example, the following C program will segfault:\n    #segfault.c\n    #include \n    #include </p>\n\n<pre><code>int main() \n{\n        printf("Starting\\n");\n        void *foo=malloc(1000);\n        memcpy(foo, 0, 100); //this line will segfault\n        exit(0);\n}\n</code></pre>\n\n<p>If I compile it like so:</p>\n\n<pre><code>gcc -g -o segfault segfault.c \n</code></pre>\n\n<p>and then run it like so:</p>\n\n<pre><code>$ gdb ./segfault\nGNU gdb 6.7.1\nCopyright (C) 2007 Free Software Foundation, Inc.\nLicense GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.  Type "show copying"\nand "show warranty" for details.\nThis GDB was configured as "i686-pc-linux-gnu"...\nUsing host libthread_db library "/lib/libthread_db.so.1".\n(gdb) run\nStarting program: /tmp/segfault \nStarting\n\nProgram received signal SIGSEGV, Segmentation fault.\n0x4ea43cbc in memcpy () from /lib/libc.so.6\n(gdb) bt\n#0  0x4ea43cbc in memcpy () from /lib/libc.so.6\n#1  0x080484cb in main () at segfault.c:8\n(gdb) \n</code></pre>\n\n<p>I find out from GDB that there was a segmentation fault on line 8. Of course there are more complex ways of handling stack overflows and other memory errors, but this will suffice.</p>\n'}, {'answer_id': 81324, 'author': 'squadette', 'author_id': 7754, 'author_profile': 'https://Stackoverflow.com/users/7754', 'pm_score': 1, 'selected': False, 'text': '<p>Simply use <a href="http://valgrind.org" rel="nofollow noreferrer">Valgrind</a>.   It will point out all your memory allocation mistakes with excruciating preciseness.</p>\n'}, {'answer_id': 88437, 'author': 'miguel.de.icaza', 'author_id': 16929, 'author_profile': 'https://Stackoverflow.com/users/16929', 'pm_score': 5, 'selected': False, 'text': '<p>You can actually catch the condition for a stack overflow using signal handlers.</p>\n\n<p>To do this, you must do two things:</p>\n\n<ul>\n<li><p>Setup a signal handler for SIGSEGV (the segfault) using sigaction, to do this set the SO_ONSTACK flag.   This instructs the kernel to use an alternative stack when delivering the signal.</p></li>\n<li><p>Call sigaltstack() to setup the alternate stack that the handler for SIGSEGV will use.</p></li>\n</ul>\n\n<p>Then when you overflow the stack, the kernel will switch to your alternate stack before delivering the signal.   Once in your signal handler, you can examine the address that caused the fault and determine if it was a stack overflow, or a regular fault.</p>\n'}, {'answer_id': 96352, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>A stack overflow does not necessarily yield a crash. It may silently trash data of your program but continue to execute.</p>\n\n<p>I wouldn't use SIGSEGV handler kludges but instead fix the original problem.</p>\n\n<p>If you want automated help, you can use gcc's -Wstack-protector option, which will spot some overflows at runtime and abort the program.</p>\n\n<p>valgrind is good for dynamic memory allocation bugs, but not for stack errors.</p>\n"}, {'answer_id': 276669, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Some of the comments are helpful, but the problem is not of memory allocation errors. That is there is no mistake in the code. It's quite a nuisance in fortran where the runtime allocates temporary values on the stack. Thus a command such as\nwrite(fp)x,y,z \ncan trigger are segfault with no warning. The technical support for the intel Fortran compiler say that there is no way that the runtime library can print a more helpful message. However if Miguel is right than this should be possible as he suggests. So thanks a lot. The remaining question then is how do I firstly find the address of the seg fault and the figure out if it came from a stack overflow or some other problem.</p>\n\n<p>For others who find this problem there is a compiler flag which puts temporary varibles above a certain size on the heap.</p>\n"}]
['https://Stackoverflow.com/questions/81202', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']