views:

107

answers:

3

I wrote a C program in which I did some pretty heavy stack allocation, around 2 MiB. Since I use the poor man's IDE* I was automatically running the program via make in order to test it, each time I compiled.

I had pretty much wrapped everything up, but for some reason, during some of the final optimization, I ran it directly from the shell. Instant segfault! Running it with make still worked, and running it by hand always produced the same segfault.

I eventually reduced the amount of stack allocation I was doing to 256 KiB, which solved the problem. My rational was that make was probably exec-ing the process, and thus it was inheriting some weird parameters that allowed it to use more stack space.

Although everything is fine now, I have no way of testing my theory. Can anyone confirm or deny, or suggest some way of testing?

* zsh, vim, gcc, gdb, and some nutty makefiles

+5  A: 

You can try setting the maximum stack size with ulimit(1) and see if it works:

# Limit stack to 1024 KiB
ulimit -s 1024; ./myprogram
# Now no limit
ulimit -s unlimited; ./myprogram
Adam Rosenfield
Yup, that did it! It worked directly from the shell after 'ulimit -s unlimited'.Man, seems like a kinda severe bug for a mature make program. (I'm using gmake 3.81) I guess I'll write them a friendly email :-)
rodarmor
I don't think it's a bug with make since it only failed outside of make: make was probably setting ulimit to a high (or unlimited) value for its own nefarious purposes and this was inherited by its subprocesses.
paxdiablo
@pax, agreed. But, it seems to me that changing the environment that child processes inherit is still a bug. People do things like 'make test' all the time, and expect the fact that they're running through make not to affect anything.
rodarmor
A: 

More information is needed to diagnose this. Namely, it'd be nice to see the makefile and the shell scripts which you were using.

Nik Reiman
+1  A: 

Personally, my first step would be to try to locate where in the code the segfault occurred, either with gdb or debugging printfs or whatever. (This is why you always check return values from malloc, it cuts down on the possible sources of segfaults ;-p) For one thing, finding the exact source of the problem could give you evidence for or against your theory about stack allocation; also, it'll let you insert error-checking code so the program can exit gracefully with an informative error message rather than segfaulting.

David Zaslavsky