Definition
AddressSanitizer (ASan) is a sanitizer §10.21, a bug detector built into a compiler, for memory errors in C and C++ programs. It reports reads and writes outside heap, stack and global objects, uses of heap memory after it was freed, and frees of memory already freed, at the moment the faulty access happens. The compiler inserts a check before each memory access, and the check looks up shadow memory, a table that records which bytes the program may access. Clang §12.17 and other major C compilers provide it through the flag -fsanitize=address.
The idea
A program asks malloc for 13 bytes and later writes a 14th byte. The extra byte lies in memory the program has mapped. The memory management unit §6.9 (MMU), the hardware that enforces memory protection, works in pages of typically 4 KiB, so a one-byte overrun almost never reaches an unmapped page, and nothing faults. The byte overwrites the allocator’s bookkeeping or a neighboring object, and the damage shows up much later, or never during testing.
ASan makes the question may this byte be accessed now? answerable for every access. It keeps one shadow byte for every 8 bytes of memory, places guard areas called redzones around objects, marks the redzones and all freed memory as forbidden, and checks each access against these marks. The 14th byte lands in a redzone and is reported at once. A car park with painted bays is a fair picture: the paint around each bay is red, and a car that touches red is caught. The picture also shows the main gap: a car that jumps clean over the red stripe into another occupied bay touches no red and is not caught.
How it works
Shadow memory. ASan divides the program’s memory into granules of 8 bytes, each starting at an address divisible by 8. Each granule has one shadow byte, at the address , where is any address inside the granule, means rounded down to a whole number, and is a constant fixed for each platform; for 64-bit Linux on x86 processors it is
0x7fff8000. A shadow value of 0 means all 8 bytes may be accessed. A value from 1 to 7 means the first bytes may be accessed and the rest may not. A negative value, a byte with its top bit set, means none of the 8 bytes may be accessed, and the value records why: for example,0xfamarks a heap redzone and0xfdmarks freed heap memory. Memory that its shadow forbids is called poisoned.Checks. Before every load and store in instrumented code, the compiler inserts a few instructions that read the shadow byte and report an error if the access touches a poisoned byte.
Heap. The runtime library replaces
mallocandfree.mallocsurrounds each block with redzones and poisons them.freepoisons the whole block and puts it into a quarantine, a queue of limited size, so that the memory is not handed out again for a while. An access through a stale pointer §4.1, one that still refers to the freed block, touches poisoned memory as long as the block is in quarantine.Stack and globals. The compiler places redzones between the local arrays §14.5 of each instrumented function, poisons them when the function is entered and clears them when it returns. It also places a redzone after each global variable.
Library calls. The runtime wraps standard functions such as
memcpyandstrcpyand checks their whole source and destination ranges, because the C library itself is usually not instrumented.Report. When a check fails, ASan prints the kind of error, such as
heap-buffer-overfloworheap-use-after-free, the address, the chain of calls that made the access, the chains of calls that allocated and freed the block, and the shadow bytes around the address. Then it stops the program.
Worked example
The addresses are small invented values; the arithmetic is the same for real ones. A call p = malloc(13) returns the address 0x10000, so the block holds the bytes 0x10000 to 0x1000c.
Granule
0x10000to0x10007: all 8 bytes belong to the block, so its shadow value is 0.Granule
0x10008to0x1000f: bytes belong to the block, so its shadow value is 5.The granules after it belong to the redzone, with the shadow value
0xfa.
The shadow byte of the second granule is at .
For an access of bytes at address , ASan reads the shadow value of the granule and reports when and , where is the remainder of divided by 8, the position of inside its granule.
Read 1 byte at
p[12], address0x1000c: , , and , which is less than 5. No report; this is the last byte of the block.Write 1 byte at
p[13], address0x1000d: , and . ASan reports aheap-buffer-overflow.Read 4 bytes at
p[12], the bytes0x1000cto0x1000f: . Reported.Write 1 byte at
p[40], address . If the allocator placed another live block there, the shadow value is 0 and nothing is reported: the write jumped over the redzone into a valid object.
A second gap. An 8-byte block starts at 0x10040, so its single granule has the shadow value 0. A 4-byte read at 0x10046 covers the bytes 0x10046 to 0x10049, and the last two lie outside the block. The check looks only at the granule of the first byte, 0x10040 to 0x10047, whose shadow value is 0, so nothing is reported. ASan’s developers document such unaligned, partly out-of-bounds accesses as a known gap.
Stated precisely
Let be the address of an access of bytes, with , that stays inside one granule, meaning . Let be the shadow byte at , read as a signed 8-bit number from to . ASan reports the access exactly when A negative always satisfies the second condition, so every access to a fully poisoned granule is reported. For the access fills a whole granule, and the rule reduces to .
In terms of the program, a faulty access is reported when all of these hold:
the access executes;
it lies in instrumented code or in a library function that the runtime wraps;
the object it overruns was allocated by ASan’s allocator or laid out by instrumented code, so redzones surround it, or, for a use after free §4.6, the freed block is still in quarantine;
the bytes it touches outside the object lie in a redzone or in quarantined memory, not inside another live object;
if it crosses from one granule into the next, at least one poisoned byte lies in the first of the two granules.
What the shadow forbids is narrower than what the C language forbids. C forbids any access outside the object that a pointer was derived from; ASan detects only accesses that touch poisoned bytes.
Limits and common mistakes
A clean run shows only that no checked access in that run touched a poisoned byte. Bugs slip through when the faulty code does not run or is not instrumented; when an access jumps over a redzone into another live object, or overflows from one field of a structure into the next, where there are no redzones; when a freed block has left the quarantine and been reused; or when an access is unaligned and only partly out of bounds. A program’s own memory pool, which carves many objects out of one large block, looks to ASan like one valid object unless the pool marks its free parts through ASan’s interface for manual poisoning.
ASan does not look for reads of uninitialized memory (MemorySanitizer §10.24 does), data races §9.3 (ThreadSanitizer), signed integer overflow §4.15 (UndefinedBehaviorSanitizer), or logic errors such as a missing authorization §1.7 check.
The sanitized program is a different program. It runs about 2 times slower, uses extra memory for redzones, quarantine and shadow, and has a different memory layout, so its timing and memory figures do not describe the release build, and a bug that depends on layout can vanish under ASan.
ASan needs its runtime library and reserves a large range of virtual address space for shadow memory, which mainstream 64-bit desktop and server operating systems provide. Firmware for a small microcontroller §6.1 is commonly checked with ASan by building its hardware-independent code for a desktop computer or for a simulator of the device.
ASan is a testing tool, not a defense in production: an attacker who controls an offset can choose one that jumps over the redzones.
Questions answered
Why can a memory bug remain undetected despite an instrumented build?
A memory bug stays undetected when any one of the conditions that AddressSanitizer (ASan) needs for a report fails. ASan reports an access only when the access executes, passes through an inserted check or a library function its runtime wraps, and touches a byte that its shadow memory marks as forbidden at that moment: a redzone, the guard area around an object, or freed memory still held in quarantine. A bug therefore goes unreported when:
the tests never execute the faulty code;
the faulty access is in code not compiled with ASan, such as a precompiled library or assembly code;
the access jumps past the redzone into another live object;
the overflow stays inside one object, from one field of a structure into the next;
the freed memory has already been reused, so the stale pointer now points into a live object;
the memory comes from a custom pool that ASan sees as one large valid block;
the access is unaligned and only partly out of bounds;
the bug is not an addressing error, such as a read of uninitialized memory, a data race or a signed integer overflow, which other sanitizers check.
A clean ASan run therefore means only that no checked access in that run touched forbidden memory.
Related entries: Sanitizer (§10.21), Buffer overflow (§4.4), Use-after-free (§4.6), Object lifetime (§4.1), Fuzz harness (§10.13), Out-of-bounds read (§4.5), Memory management unit (MMU) (§6.9). Further reading: [25].