Wednesday, September 26, 2012

Segmentation fault in the header of a main function...

Well, I just compiled one program I got from the Internet and to my surprise it segfaulted immediately after the start. To protect innocent, I'll call the binary of this program gensigseg. This is what happened:
$ ./gensigseg
Segmentation fault (core dumped)
First, I thought that it accesses command line arguments without checking they are present. This application proved already to be very lousy written, and it wouldn't surprise me that this is the problem. So, I tried with a dummy parameters, all in hope that I'll get some kind of a help message. But, the same thing happened again and there was no help message. It was time to see where it exactly segfaulted:
$ ulimit -c unlimited
$ ./gensigseg
Segmentation fault (core dumped)

$ gdb ./gensigseg core
...
Core was generated by `./gensigseg'.
Program terminated with signal 11, Segmentation fault.
#0  0x0000000000407100 in main (argc=Cannot access memory at address 0x7fff40d80f8c
) at gensigsegmain.c:137
137    int main (int argc, const char *argv []) {
You can imagine my surprise when I saw that! Segmentation fault in the header of the function main itself! Well, that was weird. Googling for this didn't revealed anything useful. After all, how many times did you see that?!

So, after poking with assembly, different optimization options, etc. I finally realized that the problem was in stack. Namely,  when disassembling main function in gdb, it pointed exact assembly instruction that caused the segmentation fault, and it turned out to be the instruction that accessed stack after the stack's value was subtracted with some large value (this was allocation of space for local variables). Anyway, after increasing stack size from default 8M to 32M (using ulimit command) the command finally worked.

Instead of conclusion, let me say that this application is unbelievably lousy written. So much about those who wrote it. More interesting is that this application was written for ITU, and if ITU accepts such shit (I have to use that word!) that what it says about ITU itslef? I'll leave this question unansvered...

No comments:

About Me

scientist, consultant, security specialist, networking guy, system administrator, philosopher ;)

Blog Archive