Red zone (computing)
{{Short description|Computing term}}
In computing, the red zone is a fixed-size area in a function's stack frame below (for a push-down stack) the current stack pointer that is reserved and safe to use. It is most commonly used in leaf functions (functions that don't call other functions) for allocating additional stack memory, without moving the stack pointer, which saves an instruction.
Whether a red zone is present depends on the calling convention. x86-64 systems that use the System V AMD64 ABI (including Linux and macOS) use a 128-byte red zone that begins directly under the current value of the stack pointer.{{cite web |url=https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-1.0.pdf |title=System V Application Binary Interface: AMD64 Architecture Processor Supplement (With LP64 and ILP32 Programming Models) Version 1.0 |website=GitHub |journal=|date=2018-01-28 |editor1=Michael Matz |editor2=Jan Hubička |editor3=Andreas Jaeger |editor4=Mark Mitchell |editor5=Milind Girkar |editor6=Hongjiu Lu |editor7=David Kreitzer |editor8=Vyacheslav Zakharin |display-editors=3 |version=1.0}}{{cite web | title = i386 and x86-64 Options - Using the GNU Compiler Collection (GCC) | url = https://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/i386-and-x86_002d64-Options.html#index-no_002dred_002dzone-998 | accessdate = 2011-04-10 }} The OpenRISC toolchain assumes a 128-byte red zone.{{cite web | title = OpenCores Wiki - OpenRISC GNU toolchain | url = http://opencores.org/wiki1/index.php?title=OpenRISC_GNU_tool_chain&oldid=1198 | accessdate = 2014-05-28 }} Microsoft Windows does not have the concept of a red zone on x86.{{cite book |last1=Fog |first1=Agner |title=Calling conventions for different C++ compilers and operating systems |date=2021-01-31 |location=Denmark |page=20 |url=https://www.agner.org/optimize/calling_conventions.pdf |access-date=2021-07-11}} In fact, the ABI explicitly states that the memory beyond the stack pointer is volatile and may be overwritten by debuggers or interrupt handlers.{{cite web |last1=Bendersky |first1=Eli |title=Stack frame layout on x86-64 |url=https://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64 |website=Eli Bendersky's website |access-date=2021-07-11 |date=2011-09-06}}{{cite web |last1=Chen |first1=Raymond | title=Why do we even need to define a red zone? Can't I just use my stack for anything? | website=The Old New Thing | date=2019-01-11 | url=https://devblogs.microsoft.com/oldnewthing/20190111-00/?p=100685 | access-date=2021-07-11}} However, Microsoft Windows has a red zone of 16 bytes on IA-64, 8 bytes on AArch32, and 16 bytes on AArch64.
The red zone is safe from modification by interrupt/exception/signal handlers.