Skip to content
systemcoding.dev
Debugging

Track Uninitialised Values to Their Source With --track-origins

Use valgrind --track-origins=yes to find where an uninitialised value was created, so the Memcheck report points at the real bug and not just the branch.

6 min read
Summarize in AI
Flat illustration of heap memory as allocated and free blocks, with the Zig, C++ and Rust logos.

The Memcheck message Conditional jump or move depends on uninitialised value(s) is the most puzzling one it prints. It names the line where a branch used the bad value, but the bug is somewhere else, where the value should have been set and was not. That line might be in another function, another file or another thread of the program's history, and by default Memcheck does not say where. The flag --track-origins=yes adds the missing half: it tells you which allocation, stack frame or variable the undefined bits came from. This post, written for Valgrind 3.24 on Linux 6.12 with GCC 14, runs one program with and without the flag, shows what the origin line means for heap and stack values, and explains the cost. The first post of the series covers running Memcheck, and the cornerstone Valgrind guide covers the rest of the tool.

What "uninitialised" means to Memcheck

Memcheck keeps a second bit for each bit of memory and each register, called the definedness bit. It starts as "undefined" for fresh malloc memory and for stack space, and becomes "defined" when a value is written. The value of a copy or an arithmetic result is defined only if its inputs were. Memcheck does not complain when you copy an undefined value, or add one, since that is legitimate: a struct with padding is copied as a whole all the time. It complains when the undefined value affects something observable: a branch condition, an address used for a load or store, or an argument to a system call that reads it. The Memcheck manual describes this rule, and it explains why the report lands far from the bug: the program is allowed to carry bad data for a long time before it acts on it.

A program with an uninitialised read

uninit.cc
#include <stdlib.h>

struct config {
    int verbose;
    int retries;
    int timeout;
};

static struct config *make_config(void)
{
    struct config *c = malloc(sizeof *c);
    c->verbose = 1;
    c->retries = 3;
    return c;
}

static int is_slow(const struct config *c)
{
    return c->timeout > 30;
}

int main(void)
{
    struct config *c = make_config();
    int result = is_slow(c) ? 1 : 0;
    free(c);
    return result;
}

make_config sets two of the three fields. is_slow reads timeout, which was never written, in a comparison that decides a branch. Compile with gcc -g -O0 -o uninit uninit.c and run Memcheck without the flag:

Shell
valgrind ./uninit
Text
==6012== Conditional jump or move depends on uninitialised value(s)
==6012==    at 0x1091A4: is_slow (uninit.c:17)
==6012==    by 0x1091D2: main (uninit.c:25)
==6012==
==6012== HEAP SUMMARY:
==6012==     in use at exit: 0 bytes in 0 blocks
==6012==   total heap usage: 1 allocs, 1 frees, 12 bytes allocated

The report says a comparison in is_slow, line 17, depends on a bad value, called from main at line 25. You know which expression is wrong, but not where the value came from: c->timeout could have been left unset by any code that ever touched that struct.

Add --track-origins=yes

Shell
valgrind --track-origins=yes ./uninit
Text
==6040== Conditional jump or move depends on uninitialised value(s)
==6040==    at 0x1091A4: is_slow (uninit.c:17)
==6040==    by 0x1091D2: main (uninit.c:25)
==6040==  Uninitialised value was created by a heap allocation
==6040==    at 0x4846828: malloc (vg_replace_malloc.c:381)
==6040==    by 0x10916E: make_config (uninit.c:10)
==6040==    by 0x1091B3: main (uninit.c:24)

The new lines say the undefined value was created by a heap allocation, and give the stack: malloc called from make_config at line 10, called from main at line 24. The origin is the block, not the field, so the report points you to the function that allocated the struct, and from there you check every field it should have set. A look at make_config shows timeout is missing. Add the assignment, or better, allocate with calloc so every field starts at zero:

c
struct config *c = calloc(1, sizeof *c);

Run again and the error disappears, since calloc marks the memory as defined.

The origin kinds

Memcheck reports one of three origins, and each suggests a different fix.

Origin textMeaningTypical fix
created by a heap allocationThe value came from a malloc block, or realloc growth, never fully writtenUse calloc, set every field, or memset
created by a stack allocationThe value came from a local variable or array on the stackInitialize the variable at its declaration
created by a client requestThe program marked memory undefined itself, through Valgrind's APILook for VALGRIND_MAKE_MEM_UNDEFINED in the code

For a stack value, the stack trace points at the function where the variable lives, and the line is that of the function's entry, not the declaration. With debug information Memcheck also names the variable when it can:

Text
==6101==  Uninitialised value was created by a stack allocation
==6101==    at 0x109189: compute (stack.c:4)

You then look at compute and find a local that is read before it is written, often on one branch of an if that does not assign it.

What the flag costs

Origin tracking stores an extra 32-bit tag per undefined memory word and records stack traces when memory is allocated or an undefined value is created. The result is slower runs and noticeably more memory, so it is off by default. Expect it to add noticeable time on top of Memcheck's own slowdown, so use it when an uninitialised-value report is unclear, not on every run. A sensible workflow is to run plain Memcheck in routine tests, and rerun the failing test with --track-origins=yes when a report appears.

There are two limits to know. Origin tracking follows a value through copies and arithmetic, but when two undefined values are combined, for example a + b, only one origin is kept, so the origin you see may be the less interesting of the two. Second, origins are tracked at the granularity of 4-byte units, so a report about a single byte inside a larger word may point at the whole word's origin.

Reading the chain when the use is far from the origin

Often the undefined value travels. A field is copied into a local, passed to a function, stored in an array, and only then used in a branch. Memcheck reports the use, and --track-origins=yes reports the origin, and the distance between them is your search area. Work from the origin: open the allocating function and list every field or element the struct or array has. Then compare with the code that writes them. A typical pattern is a struct with a new field added months after make_config was written, so the allocator never learned about it.

Another frequent source is a partially filled array from a function that returns early on error. The caller checks the return code correctly but a second caller does not, so it reads the elements the error path never wrote. The origin here is again the allocation, and the fix is to zero the array at allocation so a missed check reads zeros instead of garbage. Zeroing is not a license to ignore error codes. It turns an unpredictable crash into a deterministic wrong answer, which is easier to test for.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.