Valgrind Explained

Valgrind
Logo Caption:Valgrind logo
Author:Julian Seward, Nicholas Nethercote[1]
Developer:Valgrind Development Team[2]
Released:Jul 27, 2002[3]
Programming Language:C
Operating System:Linux


Android[4]
Genre:Profiler, Memory debugger
License:GNU General Public License

Valgrind [5] is a programming tool for memory debugging, memory leak detection, and profiling.

Valgrind was originally designed to be a freely licensed 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.

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 (IR), which is a processor-neutral, static single assignment form-based form. After the conversion, a tool (see below) 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 (or simulated) 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 querying the Valgrind tool for various information.

A considerable amount of performance is lost in these transformations (and usually, the code the tool inserts); usually, code run with Valgrind and the "none" tool (which does nothing to the IR) runs at 20% to 25% of the speed of the normal program.[6] [7]

Tools

See also: Memory debugger and Profiling (computer programming).

Memcheck

There are multiple tools included with Valgrind (and several external ones). The default (and most used) tool is Memcheck. Memcheck inserts extra instrumentation code around almost all instructions, which keeps track of the validity (all unallocated memory starts as invalid or "undefined", until it is initialized into a deterministic state, possibly from other memory) and addressability (whether the memory address in question points to an allocated, non-freed memory block), 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++ allocators and C memory allocator with its own implementation, which also includes memory guards around all allocated blocks (with the A bits set to "invalid"). 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[8] than running outside Valgrind and use more memory (there is a memory penalty per allocation). Thus, few developers run their code under Memcheck (or any other Valgrind tool) 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 (of the kind Memcheck can detect) in the code.

Other tools

In addition to Memcheck, Valgrind has several other tools:[9]

exp-sgcheck (named exp-ptrcheck prior to version 3.7), was removed in version 3.16.0. It was an experimental tool to find stack and global array overrun errors, which Memcheck cannot find.

There are also several externally developed tools available. One such tool is ThreadSanitizer, another detector of race conditions.[11] [12]

Platforms supported

As of version 3.4.0, Valgrind supports Linux on x86, x86-64 and PowerPC. Support for Linux on ARMv7 (used for example in certain smartphones) was added in version 3.6.0.[13] Support for Solaris was added in version 3.11.0. Support for was added in version 3.5.0.[14] Support for FreeBSD x86 and amd64 was added in version 3.18.0. Support for FreeBSD aarch64 was added in version 3.23.0. There are unofficial ports to other Unix-like platforms (like OpenBSD,[15] NetBSD[16] and QNX[17]). 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 (Power ISA 2.07) 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

The name Valgrind is a reference to the main entrance of Valhalla from Norse mythology.[18] [19] During development (before release) the project was named Heimdall; however, the name would have conflicted with a security package.

The original author of Valgrind is Julian Seward, who in 2006 won a Google-O'Reilly Open Source Award for his work on Valgrind.[20] [21]

Several others have also made significant contributions, including Nicholas Nethercote, Bart Van Assche, Florian Krohm, Tom Hughes, Philippe Waroquiers, Mark Wielaard, Paul Floyd, Petar Jovanovic, Carl Love, Cerion Armour-Brown and Ivo Raisr.[22]

It is used by a number of Linux-based projects.[23]

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.[24] The following code will pass the Memcheck tool in Valgrind without incident, despite containing the errors described in the comments:

int Static[5]; int func(void)

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

See also

References

Notes and References

  1. Web site: AUTHORS . 2022-09-19 . valgrind.org.
  2. Web site: Valgrind: The Developers.
  3. Web site: Twenty years of Valgrind . 2023-08-04 .
  4. https://valgrind.org/docs/manual/dist.news.html Valgrind release notes
  5. Web site: Valgrind . 2023-05-04 . valgrind.org.
  6. https://valgrind.org/info/about.html Valgrind homepage
  7. http://www.network-theory.co.uk/docs/valgrind/valgrind_10.html Valgrind Manual
  8. Web site: Valgrind.
  9. https://valgrind.org/info/tools.html Valgrind main tool list
  10. Web site: Valgrind.
  11. Web site: Valgrind: Variants / Patches.
  12. K Serebryany, T Iskhodzhanov, ThreadSanitizer–data race detection in practice, Proceedings of the Workshop on Binary Instrumentation and Applications WBIA'09
  13. http://talk.maemo.org/showthread.php?t=65335 ARM/Linux port
  14. http://blog.mozilla.com/nnethercote/2009/05/28/mac-os-x-now-supported-on-the-valgrind-trunk/ OS X port
  15. http://ports.su/devel/valgrind Valgrind OpenBSD port
  16. Web site: Valgrind NetBSD port . 2006-01-28 . https://web.archive.org/web/20060209063503/http://vg4nbsd.berlios.de/ . 2006-02-09 . dead .
  17. Web site: foundry27 : View Release . 2024-05-24 . community.qnx.com.
  18. http://www.valgrind.org/docs/manual/faq.html#faq.whence Valgrind FAQ
  19. Web site: Grímnismál. Völuspá.org.
  20. https://valgrind.org/gallery/awards.html valgrind.org's list of awards
  21. http://code.google.com/events/osa-hall-of-fame.html Google-O'Reilly Open Source Awards – Hall of Fame
  22. https://valgrind.org/info/developers.html The Valgrind Developers
  23. https://valgrind.org/gallery/users.html valgrind.org's list of users
  24. http://www.valgrind.org/docs/manual/faq.html#faq.overruns Valgrind FAQ