Valgrind


Valgrind is a programming tool for memory debugging, memory leak detection, and profiling.
Valgrind was originally designed to be a free memory debugging tool for Linux on x86, but has since evolved to become a generic framework for creating dynamic analysis tools such as checkers and profilers.
The name Valgrind is a reference to the main entrance of Valhalla from Norse Mythology. During development the project was named Heimdall; however, the name would have conflicted with a security package.

Overview

Valgrind is in essence a virtual machine using just-in-time compilation techniques, including dynamic recompilation. Nothing from the original program ever gets run directly on the host processor. Instead, Valgrind first translates the program into a temporary, simpler form called Intermediate Representation, which is a processor-neutral, SSA-based form. After the conversion, a tool is free to do whatever transformations it would like on the IR, before Valgrind translates the IR back into machine code and lets the host processor run it. Valgrind recompiles binary code to run on host and target CPUs of the same architecture. It also includes a GDB stub to allow debugging of the target program as it runs in Valgrind, with "monitor commands" that allow you to query the Valgrind tool for various sorts of information.
A considerable amount of performance is lost in these transformations ; usually, code run with Valgrind and the "none" tool runs at 20% to 25% of the speed of the normal program.

Tools

Memcheck

There are multiple tools included with Valgrind. The default tool is Memcheck. Memcheck inserts extra instrumentation code around almost all instructions, which keeps track of the validity and addressability, stored in the so-called V bits and A bits respectively. As data is moved around or manipulated, the instrumentation code keeps track of the A and V bits, so they are always correct on a single-bit level.
In addition, Memcheck replaces the standard C memory allocator with its own implementation, which also includes memory guards around all allocated blocks. This feature enables Memcheck to detect off-by-one errors where a program reads or writes outside an allocated block by a small amount. The problems Memcheck can detect and warn about include the following:
The price of this is lost performance. Programs running under Memcheck usually run 20–30 times slower than running outside Valgrind and use more memory. Thus, few developers run their code under Memcheck all the time. They most commonly use such tools either to trace down some specific bug, or to verify that there are no latent bugs in the code.

Other tools

In addition to Memcheck, Valgrind has several other tools:
There are also several externally developed tools available. One such tool is ThreadSanitizer, another detector of race conditions.

Platforms supported

As of version 3.4.0, Valgrind supports Linux on x86, x86-64 and PowerPC. Support for was added in version 3.5.0. Support for Linux on ARMv7 was added in version 3.6.0. Support for Solaris was added in version 3.11.0. There are unofficial ports to other UNIX-like platforms. From version 3.7.0 the ARM/Android platform support was added.
Since version 3.9.0 there is support for Linux on MIPS64 little and big endian, for MIPS DSP ASE on MIPS32, for s390x Decimal Floating Point instructions, for POWER8 instructions, for Intel AVX2 instructions, for Intel Transactional Synchronization Extensions, both RTM and HLE and initial support for Hardware Transactional Memory on POWER.

History and development

It is named after the main entrance to Valhalla in Norse mythology.
The original author of Valgrind is Julian Seward, who in 2006 won a Google-O'Reilly Open Source Award for his work on Valgrind.
Several others have also made significant contributions, including Cerion Armour-Brown, Jeremy Fitzhardinge, Tom Hughes, Nicholas Nethercote, Paul Mackerras, Dirk Mueller, Bart Van Assche, Josef Weidendorfer, and Robert Walsh.
It is used by a number of Linux-based projects.

Limitations of Memcheck

In addition to the performance penalty, an important limitation of Memcheck is its inability to detect all cases of bounds errors in the use of static or stack-allocated data. The following code will pass the Memcheck tool in Valgrind without incident, despite containing the errors described in the comments:

int Static;

int func


The experimental valgrind tool exp-sgcheck has been written to address this limitation in Memcheck. It will detect array overrun errors, provided the first access to an array is within the array bounds. Note that exp-sgcheck will not detect the array overrun in the code above, since the first access to an array is out of bounds, but it will detect the array overrun error in the following code.

void func


The inability to detect all errors involving the access of stack allocated data is especially noteworthy since
certain types of stack errors make software vulnerable to the classic
stack smashing exploit.