I’ve been tracking down a particularly insidious bug at work lately. Last night I had a breakthrough and discovered what I thought was a buffer overrun. This morning I built a test application that would trigger the overrun and ran it using Electric Fence. Let me say, thank you Bruce Perens.
Electric Fence takes over the memory allocation functions in C/C++. By default, for every memory allocation the application requests, Electric Fence allocates 2 memory sections: the memory segment that will be given to the application and another memory segment immediately after the first that is locked from being used by the application. Any attempt by the application to write into the second segment will result in (on FreeBSD, anyway) a SIGBUS. This is perfect for detecting buffer overruns because they are situations where the code writes past the area of memory allocated to them.
Needless to say, Electric Fence beared out exactly what I thought was happening. What a great tool.