Read the direct leak first#
LeakSanitizer (LSan) runs once, when the process exits. Its design document describes the check: it collects a root set that includes "global variables" and "stacks of running threads", follows everything that looks like a pointer, and reports every heap block it could not reach. A block nothing points at is a direct leak. A block that only other leaked blocks point at is an indirect leak.
This constructed request handler parses a request into a struct that owns an array of headers, and each header owns two strings. The handler never frees the request. A configuration buffer is loaded once into a global:
static char *config; /* loaded once, kept until exit */
static struct request *parse(const char *name, const char *value) {
struct request *r = malloc(sizeof *r); /* line 16 */
r->count = 1;
r->headers = malloc(sizeof(struct header)); /* line 18 */
r->headers[0].name = strdup(name); /* line 19 */
r->headers[0].value = strdup(value); /* line 20 */
return r;
}
static int handle(int i) {
struct request *r = parse("host", "example.test");
int len = (int)strlen(r->headers[0].value) + i;
/* bug: r is never freed */
return len;
}
Built with Ubuntu clang 18.1.3 as clang -O0 -g -fsanitize=address -fno-omit-frame-pointer server.c and run for one request, it prints four leaks and exits 1 (paths, libc frames and the indirect leaks' repeated handle and main frames trimmed):
==12229==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 16 byte(s) in 1 object(s) allocated from:
#0 0x... in malloc
#1 0x... in parse server.c:16:23
#2 0x... in handle server.c:25:23
#3 0x... in main server.c:35:40
Indirect leak of 16 byte(s) in 1 object(s) allocated from:
#0 0x... in malloc
#1 0x... in parse server.c:18:16
Indirect leak of 13 byte(s) in 1 object(s) allocated from:
#0 0x... in strdup
#1 0x... in parse server.c:20:25
Indirect leak of 5 byte(s) in 1 object(s) allocated from:
#0 0x... in strdup
#1 0x... in parse server.c:19:24
SUMMARY: AddressSanitizer: 50 byte(s) leaked in 4 allocation(s).
The direct leak is the struct request from line 16. The three indirect leaks are the header array and its two strings: they are reachable, but only through the lost request. One free path for the request, written to release what it owns, clears all four. Start with the direct leak's allocation stack, then look for the function that took ownership of that pointer and returned without releasing it. Here that is handle, not parse.
The 4,096-byte configuration buffer is missing from the report, although the program never frees it. The global config still points at it when the leak check runs, so it is in the root set.
Compare the same run under Valgrind#
Valgrind's Memcheck splits the same heap into four kinds. Built without ASan and run as valgrind --leak-check=full ./server_plain 10 (Valgrind 3.22.0), the ten-request run reports:
==12545== 500 (160 direct, 340 indirect) bytes in 10 blocks are definitely lost in loss record 4 of 5
==12545== at 0x...: malloc (in .../vgpreload_memcheck-amd64-linux.so)
==12545== by 0x...: parse (server.c:16)
==12545== by 0x...: handle (server.c:25)
==12545== by 0x...: main (server.c:35)
==12545== LEAK SUMMARY:
==12545== definitely lost: 160 bytes in 10 blocks
==12545== indirectly lost: 340 bytes in 30 blocks
==12545== possibly lost: 0 bytes in 0 blocks
==12545== still reachable: 4,096 bytes in 1 blocks
The categories map onto LSan's report. Memcheck's manual (source of the 3.22.0 manual, read from a GitHub mirror of the Valgrind repository) says of indirect blocks: "Because the problem will disappear if the definitely lost block that caused the indirect leak is fixed, Memcheck won't report such blocks individually by default." "Definitely lost" corresponds to LSan's direct leak, and "indirectly lost" to its indirect leak.
Two Memcheck kinds have no LSan counterpart. "Still reachable" is the global buffer: the manual says "the programmer could, at least in principle, have freed it before program exit" and that such blocks "are very common and arguably not a problem." LSan never prints them. "Possibly lost" covers blocks reached only through a pointer into their middle, which "could just be a random value in memory that happens to point into a block". LSan does not make that distinction: its design document says "interior pointers are treated the same as pointers to the beginning of a block." A block Memcheck calls possibly lost can therefore pass LSan silently, and a codebase that relies on Memcheck's "possibly lost" list will not see those blocks in LSan output.
Scale the run to see whether the leak grows#
A leak at exit is only a symptom. What decides its priority is whether the program loses more memory as it does more work. The handler takes a request count, so the same binary can run 1, 10 and 1,000 requests:
$ ./server_asan 1 2>&1 | grep -E 'SUMMARY|^Direct'
Direct leak of 16 byte(s) in 1 object(s) allocated from:
SUMMARY: AddressSanitizer: 50 byte(s) leaked in 4 allocation(s).
$ ./server_asan 10 2>&1 | grep -E 'SUMMARY|^Direct'
Direct leak of 160 byte(s) in 10 object(s) allocated from:
SUMMARY: AddressSanitizer: 500 byte(s) leaked in 40 allocation(s).
$ ./server_asan 1000 2>&1 | grep -E 'SUMMARY|^Direct'
Direct leak of 16000 byte(s) in 1000 object(s) allocated from:
SUMMARY: AddressSanitizer: 50000 byte(s) leaked in 4000 allocation(s).
The object count equals the request count. This is a per-request leak, and in a long-running server it is unbounded growth. Fix it. After adding the frees to handle, the 1,000-request run prints workers=4 handled=1000 total=511500 and exits 0, and Memcheck reports only still reachable: 4,096 bytes in 1 blocks.
The disconfirming run: a leak that does not grow#
The same check can push the decision the other way. In this variant, the fixed handler is kept and a startup function prints a banner from a copy it never frees:
static void print_banner(void) {
char *b = strdup("server v1");
puts(b); /* b is never freed */
}
LSan reports it as a direct leak, because no pointer to the block survives. Running 1 and 1,000 requests gives the same report:
$ ./once_asan 1 2>&1 | grep -E 'SUMMARY|^Direct|#1 '
Direct leak of 10 byte(s) in 1 object(s) allocated from:
#1 0x... in print_banner server_once.c:16:13
SUMMARY: AddressSanitizer: 10 byte(s) leaked in 1 allocation(s).
$ ./once_asan 1000 2>&1 | grep -E 'SUMMARY|^Direct|#1 '
Direct leak of 10 byte(s) in 1 object(s) allocated from:
#1 0x... in print_banner server_once.c:16:13
SUMMARY: AddressSanitizer: 10 byte(s) leaked in 1 allocation(s).
The count stays at one, so it costs ten bytes for the life of the process. It is still a lost pointer and the fix is one free, but it does not justify the urgency of the request leak. If the allocation is meant to live until exit, store it where the root set can see it, as the configuration buffer is, or mark it with __lsan_ignore_object, which the LeakSanitizer wiki says "marks the heap object pointed to by p (and anything reachable from it) as a non-leak." Prefer either to a leak: suppression on a function name, which would also hide a later per-call leak from the same function.
Run the scaling check with an input the test actually repeats. A loop that never reaches the leaking branch shows a constant count for the wrong reason, so confirm the allocation stack in the report is inside the repeated path.
What the report cannot show#
LSan checks reachability once, at exit, and the clang LeakSanitizer documentation describes it as adding "almost no performance overhead until the very end of the process, at which point there is an extra leak detection phase." Memory a program keeps reachable but never uses again, such as a cache with no eviction, is still reachable at exit and produces no report. For growth of that kind, measure the heap while the program runs.
Decide from the scaling run#
| What the report says | Evidence to collect | Decision |
|---|---|---|
| Direct leak with indirect leaks under it | The direct leak's allocation stack and the function that took ownership | Fix the owner's release path; the indirect leaks go with it |
| Direct leak whose object count grows with iterations | Runs at two or more iteration counts through the leaking path | Fix; treat as unbounded growth in a long-running process |
| Direct leak that stays at one object as iterations grow | The same scaling runs | Fix the single free, or hold the block in a global or with __lsan_ignore_object if it is meant to live until exit |
| Memcheck "still reachable", absent from LSan | Whether a global or static still points at it | Dismiss as intended lifetime, unless it is a cache that grows |
| Memcheck "possibly lost", absent from LSan | Whether the code keeps interior pointers on purpose | Collect the pointer's origin before deciding |
When an automated reviewer rather than a sanitizer raised the leak, the guide to memory growth comments covers the different evidence it needs. The guide to ASan reports covers the heap errors ASan reports alongside LSan.