Skip to main content
Be Bitwise

Valgrind & Dynamic Analysis

Memory Safety & Debugging · Memory & Systems track

7 min read

In this module

Binary Instrumentation for Memory Analysis

Sanitizers need recompilation. Valgrind works on unmodified binaries. It runs your program on a synthetic CPU, interpreting every instruction and weaving in analysis code on the fly. That makes Valgrind useful on anything you can't rebuild, including commercial software and pre-built libraries.

Key Concept

Valgrind doesn't need source or recompilation — it instruments machine code at runtime by emulating the CPU. The trade-off is speed: programs run 10–50× slower under Valgrind, but every memory access is checked.

Memcheck: The Default Tool

Memcheck is Valgrind's most-used tool. It tracks accessibility and definedness for every byte of memory, catching:

  • Invalid reads/writes: accessing memory that was not allocated or has been freed
  • Use of uninitialised values: using values that have not been written to in conditional branches, system calls, or address calculations
  • Memory leaks: allocated memory that is no longer reachable when the program exits
  • Mismatched allocation/deallocation: using free() on memory from new, or delete on memory from malloc()
  • Invalid free: freeing a pointer that was not returned by an allocator
text
$ valgrind --tool=memcheck --leak-check=full ./program
==12345== Invalid read of size 4
==12345== at 0x401234: process (program.c:42)
==12345== by 0x401567: main (program.c:80)
==12345== Address 0x5204044 is 4 bytes after a block of size 16 alloc'd
==12345== at 0x4C2BF0F: malloc (vg_replace_malloc.c:299)
==12345== by 0x401200: process (program.c:38)

Memcheck's overhead lands around 10-50x slowdown (much higher than ASan), since every block of code gets translated, instrumented and recompiled on the fly rather than run as-is. The trade is that it runs on any binary for a platform Valgrind supports, no rebuild required. Linux on x86-64 and ARM64 is well covered; recent macOS, including every Apple Silicon Mac, isn't, so Mac users should reach for ASan, Apple's leaks tool, or a Linux VM.

Memcheck sorts leaks by how they were lost. A block is definitely lost when no pointer to it exists anywhere. It's indirectly lost when the only pointers to it live inside other leaked blocks, like every node after the head of a leaked linked list. The runner can't host Valgrind, so this program counts its own live allocations instead. Run it, then write free_list so the count reaches zero.

Try itFill in free_list so the count drops to zero. Each node owns its name as well as itself.
C (GCC 9.2.0)
What would valgrind --leak-check=full report for the unfixed version?

Once process returns, nothing points to the "alpha" node, so that one 16-byte block is definitely lost. The other two nodes and all three name strings are still pointed to, but only from inside leaked memory, so they're indirectly lost: 5 blocks, 49 bytes (two 16-byte nodes plus 6 + 5 + 6 bytes of strings). Memcheck prints the definitely lost block with the indirect bytes attached, along these lines:

text
==PID== 65 (16 direct, 49 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==PID== at 0x...: malloc (vg_replace_malloc.c:...)
==PID== by 0x...: xmalloc (main.c:8)
==PID== by 0x...: push (main.c:24)
==PID== by 0x...: process (main.c:40)
==PID== by 0x...: main (main.c:47)
...
==PID== definitely lost: 16 bytes in 1 blocks
==PID== indirectly lost: 49 bytes in 5 blocks

Fix the definitely lost block and the indirect ones usually go with it, which is why you work through Memcheck's leak report from the definite losses first.

Memcheck vs ASan

ASan is faster (roughly 2x versus 10-50x overhead), has better stack buffer overflow detection (red zones around every stack variable), and better reporting (with compile-time information available). Memcheck detects uninitialised value use (which ASan does not), works without recompilation, and can detect some bugs that ASan misses (like mismatched alloc/dealloc). Use ASan during development when you can recompile, and Memcheck when you cannot or need its unique detection capabilities.

Callgrind: Profiling Execution

Callgrind harvests function call relationships and instruction counts. It records how often each function is called, how many instructions it executes, and the full call graph. Results visualise nicely in KCachegrind or QCachegrind.

bash
$ valgrind --tool=callgrind ./program
$ kcachegrind callgrind.out.12345

Callgrind doesn't bother with wall-clock time — Valgrind's overhead makes timing meaningless. It counts instruction references (Ir), data reads (Dr), data writes (Dw), and assorted cache events. The result is a hardware-independent performance profile.

Cachegrind: Cache Simulation

Cachegrind simulates the CPU's cache hierarchy (L1 instruction, L1 data, last-level cache) and tallies cache hits and misses per line of code. The output exposes which data structures and access patterns chew through the cache.

text
$ valgrind --tool=cachegrind ./program
==12345== I refs: 100,000,000
==12345== I1 misses: 10,000
==12345== LLi misses: 5,000
==12345== D refs: 50,000,000 (30,000,000 rd + 20,000,000 wr)
==12345== D1 misses: 500,000 (300,000 rd + 200,000 wr)
==12345== LLd misses: 100,000 (60,000 rd + 40,000 wr)

Cachegrind annotates each source line with miss counts, so cache-hostile code is hard to miss. Use cg_annotate to see the per-line breakdown.

Helgrind and DRD: Thread Error Detection

Helgrind flags synchronisation errors in multi-threaded programs: data races, abuse of POSIX threading primitives (unlocking an unlocked mutex, destroying a locked one), and potential deadlocks from inconsistent lock ordering.

DRD (Data Race Detector) is a second thread-checking tool with a different detection algorithm. It runs a happens-before analysis similar to ThreadSanitizer. DRD usually consumes less memory than Helgrind and is more precise on certain race classes.

Both tools run on unmodified binaries using POSIX threads. They hook pthread calls to track synchronisation and watch memory accesses for races.

Massif: Heap Profiling

Massif snapshots the heap on a regular cadence, recording how much memory each function allocated. The output charts heap usage over time and surfaces memory-use patterns, peak usage, and likely leaks.

text
$ valgrind --tool=massif ./program
$ ms_print massif.out.12345
MB
3.5 ^ #
| @@:#
| @@@@@:#
| @@@@@@@@:#
| @@@@@@@@@@@:#
| @@@@@@@@@@@@@@:#
| @@@@@@@@@@@@@@@@@:#
| @@@@@@@@@@@@@@@@@@@@:#
| @@@@@@@@@@@@@@@@@@@@@@@:#
0 +------------------------------------------------------> time

Massif also breaks the total heap down by responsible function. Handy for finding the data structures eating the most memory and figuring out where optimisation effort actually pays off.

Practical Workflow

A full Valgrind workflow for a C/C++ project: run memcheck on the test suite for memory errors and leaks, run helgrind or DRD on the multi-threaded tests to chase races, run cachegrind on performance-critical paths to spot cache issues, and run massif to profile memory usage and find optimisation opportunities. Each tool exposes a different facet of the program's behaviour. Together they paint a thorough dynamic-analysis picture.

In practice you start from a symptom, not a tool. This is a reasonable first pick for each, with the sanitizers from the previous lesson alongside Valgrind:

The sanitizers are much faster but need a rebuild; Valgrind works on the binary you already have. When you can recompile, start with the sanitizer.