The x86-64 Red Zone

The x86-64 ABI makes an interesting guarantee. It guarantees that at any time you can access up to 128 bytes past %rsp. This is called the red zone.

This is really useful for GDB scripting because it gives you 128 bytes that you can just use without calling malloc() or related routines. Besides the fact that this is more efficient, it might be necessary because calls to userspace methods like malloc() don’t always work right in GDB (e.g. if you attach to a process that is itself calling malloc(), your call to malloc() from GDB could deadlock).

This is really useful for functions that need a pointer to memory that normally you’d pass on the stack. For instance, typically you’d allocate the struct rlimit to pass to getrlimit(2) on the stack. You can modify the stack in GDB by modifying %rsp but the red zone feature means in most cases there’s no neeed.

I wrote a GDB script for “fixing” the rlimit of remote processes that uses this feature. You can find the code here on GitHub.