Skip to content
systemcoding.dev
Debugging

How AddressSanitizer Works: Shadow Memory and Redzones

See how AddressSanitizer in Clang 19 maps every 8 bytes of memory to one shadow byte, pads allocations with redzones and checks each access at run time.

6 min read
Summarize in AI
Flat illustration of a rising bar chart and a gauge, with the LLVM, Linux and Intel logos.

AddressSanitizer reports a heap overflow at the exact instruction that caused it, with the allocation stack of the buffer it overran. That precision comes from three cooperating pieces: a compiler pass that inserts a check before every memory access, a runtime that replaces malloc and free, and a shadow memory region that records which bytes the program may touch. This post goes below the usage level covered in the earlier posts on finding heap bugs with AddressSanitizer and explains the mechanism, using Clang 19 and the compiler-rt runtime on x86-64 Linux. It assumes you can read C and a hex dump, and that you know what a heap overflow is.

The shadow mapping

AddressSanitizer keeps one shadow byte for every 8 bytes of application memory. The AddressSanitizer algorithm page gives the translation as a shift and an add:

Text
shadow_address = (application_address >> 3) + offset

On x86-64 Linux the offset is 0x7fff8000, so the shadow region is one eighth of the address space and sits in its own reserved range. Because 8 application bytes share one shadow byte, memory is always tracked in aligned 8-byte groups. The value of the shadow byte says how many bytes of that group are usable:

Shadow byteMeaning
00all 8 bytes are addressable
01 to 07only the first 1 to 7 bytes are addressable
faheap redzone
fdfreed heap memory
f1, f2, f3stack left, middle and right redzones
f9global redzone

The first two rows are the whole trick. A group with no valid bytes is a negative value; a group with all of them is zero; and a group at the edge of an allocation, such as the last 3 bytes of a 19-byte object, holds the count of valid bytes. The full legend is printed under every report, and the values come from the compiler-rt source in asan_internal.h.

What the compiler inserts

With -fsanitize=address, Clang instruments each load and store. For an access of 8 bytes or more, the check is one test of the shadow byte:

c
// Pseudocode for what the instrumented code does for a 4-byte store to addr.
signed char shadow = *(signed char *)((addr >> 3) + 0x7fff8000);
if (shadow != 0 && (signed char)((addr & 7) + 4 - 1) >= shadow) {
    __asan_report_store4(addr);
}
*(int *)addr = value;

The fast path is the shadow != 0 test: most accesses hit fully addressable groups and fall straight through. Only when the shadow byte is non-zero does the second comparison run, which asks whether the last byte of the access lies beyond the number of valid bytes in the group. A negative shadow value fails that comparison for any offset, which is why a redzone byte always reports. Accesses larger than 8 bytes check more than one shadow byte, or use the slow path.

You can see the inserted calls by looking at the IR. This small program has one store to heap memory:

overflow.cc
#include <stdlib.h>

int main(void)
{
    int *a = malloc(4 * sizeof(int));
    a[4] = 1;
    free(a);
    return 0;
}

Compiling it with symbols and the sanitizer enabled:

Shell
clang-19 -g -O0 -fsanitize=address overflow.c -o overflow
./overflow
Text
=================================================================
==4242==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000040 at pc 0x5581a2c3b1a2 bp 0x7ffd1e2a6f60 sp 0x7ffd1e2a6f58
WRITE of size 4 at 0x603000000040 thread T0
    #0 0x5581a2c3b1a1 in main overflow.c:6:10
    #1 0x7f6d3a22a1c9 in __libc_start_call_main
    #2 0x7f6d3a22a28a in __libc_start_main_impl
    #3 0x5581a2b6a0a4 in _start

0x603000000040 is located 0 bytes after 16-byte region [0x603000000030,0x603000000040)
allocated by thread T0 here:
    #0 0x5581a2c10c1e in malloc
    #1 0x5581a2c3b15e in main overflow.c:5:14
    #2 0x7f6d3a22a1c9 in __libc_start_call_main

SUMMARY: AddressSanitizer: heap-buffer-overflow overflow.c:6:10 in main
Shadow bytes around the buggy address:
  0x0c067fff7ff0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x0c067fff8000: fa fa fa fa fa fa 00 00[fa]fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
==4242==ABORTING

Addresses, pids and library frames will differ on your machine, and the report is trimmed here to the lines that matter. Do the arithmetic on the faulting address to see the mapping at work. 0x603000000040 >> 3 is 0x0c0600000008, and adding 0x7fff8000 gives 0x0c067fff8008, which is exactly the shadow byte in square brackets: the ninth in the row that starts at 0x0c067fff8000. The two 00 bytes before it are the 16 usable bytes of the allocation, and the fa after it is the right redzone.

Redzones around heap allocations

The runtime replaces malloc, free and the related allocation functions. For each request it reserves the user region plus a redzone on each side, then poisons the redzone shadow bytes with fa. The default minimum redzone is 16 bytes and the runtime increases it for larger allocations, up to a limit set by the max_redzone flag. In the output above, a[4] writes at byte 16 of a 16-byte region, the first byte of the right redzone, so the check finds fa and aborts.

A redzone only catches an access that lands inside it. An overflow that jumps far past the redzone into another live allocation looks valid to the shadow and goes unreported. This is the main limit of the technique, and it is why a memcpy with a wildly wrong length is sometimes caught and sometimes not.

Freed memory and the quarantine

free does not return memory to the allocator at once. The runtime poisons the whole region with fd and puts the chunk in a quarantine, a FIFO of recently freed chunks whose total size is bounded by the quarantine_size_mb flag. A use-after-free inside that window hits poisoned shadow and is reported as heap-use-after-free, with three stacks: the access, the free and the original allocation. Once a chunk leaves the quarantine it can be reused, and a dangling pointer to it then looks valid again. The quarantine widens the detection window without closing it.

Stack and globals

For locals, the compiler lays out each stack frame with redzones between variables, writes the poison values into the shadow on function entry and clears them on exit. The f1, f2 and f3 values mark the left, middle and right redzones of a frame. For globals, the compiler pads each one and registers it so the runtime poisons the padding at start-up with f9. The same shift-and-compare check covers all three kinds of memory, which is why one sanitizer reports heap, stack and global overflows with a single report format.

What it costs and where it stops

Shadow memory is address space reserved up front, one eighth of the application range, and the redzones and quarantine add real memory use on top. The inserted checks add work to every access. This post gives no figures because they depend entirely on the program; measure your own with the build you ship. The project's own wiki describes the typical overhead in general terms, and ASAN_OPTIONS=quarantine_size_mb=... and max_redzone=... are the knobs that trade detection window for memory.

Three limits follow from the design. Uninitialised reads are not detected, because a byte can be addressable and still hold garbage; that is the job of MemorySanitizer. Accesses that bypass instrumentation, such as inline assembly or code in an uninstrumented library, are not checked, although the replaced malloc still poisons freed memory. And a bug that stays inside a valid allocation, such as writing the wrong field of a struct, passes every check.

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.