|Submitted by mikeperry on Tue, 08/05/2008 - 06:04|
NJAMD (Not Just Another Malloc Debugger) was a project I started after I realized memory issues in C were my biggest source of program bugs and the current solution at the time (Electric Fence) didn't have all the features I wanted. It eventually made it into RedHat, Debian, and a few other distributions, but fell by the wayside once Valgrind came on the scene. My last effort with the project was to rewrite it in C++, just to prove you could write something that low level successfully in C++ and have it still be just as fast, portable, and flexible as plain C. Unfortunately, I never polished or released the C++ code, but you can check it out of sourceforge.
A malloc debugger is usually a drop-in replacement for standard libc malloc and friends. Normally, when a program does something illegal with its dynamic memory (such as writing past the end of a buffer returned by malloc(3), ie: an overflow), its execution may not immediately terminate. Instead, bizarre and unexpected results can occur later on during program execution. This is due to the fact that malloc implementations store book keeping information before and after allocated segments. So overwriting these regions won't cause your program to crash right away, but will cause chaos during subsequent malloc requests, or even during usage of memory returned from otherwise seemingly valid malloc()
Malloc debuggers change all this by mapping protected memory before or after your buffer, so your program crashes immediately after an overflow.
Currently, there are about a dozen freely available malloc debuggers, with new ones popping up every day. However, frustrated with other malloc debuggers, I went above and beyond to create NJAMD: Not Just Another Malloc Debugger.
NJAMD sets out to pick up where other malloc debuggers left off. That is, it protects against all common dynamic memory bugs, including overflow, underflow, writes to freed memory, and memory leaks, all without recompiling or even relinking your executable. In addition, it is able to trace memory leaks even through arbitrary library functions that wrap malloc(3), such as strdup(3), GUI widget allocators, and even C++ new and delete.
Current available malloc debuggers are bloated and slow, and/or require a lengthy recompile of your entire project. NJAMD is fast enough for everyday use, and light enough to debug most apps. (Large apps, such as the gimp, will require a ram-heavy development machine).
- Completely threadsafe
- Interfaces with any debugger easily.
- Postmortem debugging & pointer lookup
- Memory statistics and usage information (NEW in 0.8)
- Offers buffer overflow and underflow detection in a single library.
- Offers 3 different kinds of protection for freed memory.
- Allows you to control the permissions of protected segments, to trap writes or reads specifically.
- Allows you to optionally only track memory leaks by wrapping libc malloc, to save on the overhead of a full malloc debugger
- Supports multiple architectures, and allows you to tune alignment on these architectures.
- Well documented from both a user's perspective and a programmer's perspective.
- Traces all calls to malloc, even those wrapped in other libraries, back to your code, and can provide a call trace on every malloc
- Symbol translation on these return addresses outside of gdb (NEW in 0.8)
- No need to recompile! It does all this without any need to include a special .h file, or even relink!
- An ncurses front end, written by Steve Engelhardt
* Some features are not present on all platforms
How it Works
Glad you asked! The following documentation is available: