2026-05-12
The red zone is a region of 128 bytes below the stack pointer (%rsp) that the System V AMD64 ABI guarantees won't be clobbered by signal handlers or interrupts. Leaf functions — functions that call nothing else — can use this scratch space without bothering to decrement %rsp at all. It's a small but pervasive optimization that shows up in nearly every optimized x86-64 binary.
Normally, allocating stack space means sub $N, %rsp on entry and add $N, %rsp on exit. For a tiny leaf function that needs 16 bytes for a temporary, those two instructions are pure overhead. With the red zone, the compiler just writes to -8(%rsp) and -16(%rsp) directly. No prologue, no epilogue stack adjustment.
Why it's safe: on x86-64 Linux, when a signal arrives, the kernel pushes the signal frame below the 128-byte red zone before invoking the handler. Hardware interrupts in user mode behave the same way. Your data in the red zone survives untouched.
Concrete example. Compile double square(double x) { double t = x*x; return t; } with gcc -O2 and you'll see:
movsd %xmm0, -8(%rsp) — store below the stack pointermulsd -8(%rsp), %xmm0ret — no sub/add on %rsp anywhereWhere it bites you:
-mno-red-zone because interrupt handlers in kernel mode push directly onto the current stack — there's no skip-128 guarantee. If kernel code used the red zone, an interrupt would corrupt it.-8(%rsp).Rule of thumb: if your leaf function needs ≤128 bytes of locals and makes zero calls, the compiler will skip the stack adjustment entirely. Above 128, or any CALL in the body, and you pay the normal prologue cost. You can disable it globally with -mno-red-zone — useful when writing freestanding code, bootloaders, or anything that runs with interrupts on its own stack.
Check it yourself: objdump -d /usr/bin/* | grep -B1 'sub.*%rsp' | wc -l versus total functions — you'll find a surprising fraction of functions never touch %rsp at all.
