Questions tagged [red-zone]

The red zone is stack space that's safe from asynchronous modification even though it's not reserved the normal way. Some ABIs (notably the x86-64 SysV ABI) provide one.

The red zone is a fixed-length area of stack space that's safe from asynchronous modification (by signals or interrupts) even though it's outside the reserved part of the stack. (e.g. the first 128 bytes below rsp in the SysV ABI for x86-64, where the stack grows down. See the tag wiki.)

It can be used as a temporary scratch area for the function, in order to avoid having to spend 2 instructions to decrement and increment the stack pointer. The red zone is not preserved across function calls, so it's best used for leaf functions, or in a function tail.

Compiler options can disable use of the red zone for compiler-generated code. For example, Linux kernel code is compiled with -mno-red-zone because it's very difficult if not impossible for x86 interrupt handlers to respect the standard 128B red-zone, unlike signal handlers respecting the user-space stack's red-zone.

The location and implementation of the red zone differs by platform (operating system) .

Resources

51 questions
34
votes
2 answers

Where exactly is the red zone on x86-64?

From Wikipedia: In computing, a red zone is a fixed-size area in a function's stack frame beyond the return address which is not preserved by that function. The callee function may use the red zone for storing local variables without the extra…
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
27
votes
4 answers

Why can't kernel code use a Red Zone

It is highly recommended when creating a 64-bit kernel (for x86_64 platform), to instruct the compiler not to use the 128-byte Red Zone that the user-space ABI does. (For GCC the compiler flag is -mno-red-zone). The kernel would not be…
mmk
  • 585
  • 5
  • 13
25
votes
5 answers

Inline assembly that clobbers the red zone

I'm writing a cryptography program, and the core (a wide multiply routine) is written in x86-64 assembly, both for speed and because it extensively uses instructions like adc that are not easily accessible from C. I don't want to inline this…
Mike Hamburg
  • 359
  • 2
  • 5
17
votes
2 answers

Using base pointer register in C++ inline asm

I want to be able to use the base pointer register (%rbp) within inline asm. A toy example of this is like so: void Foo(int &x) { asm volatile ("pushq %%rbp;" // 'prologue' "movq %%rsp, %%rbp;" // 'prologue' …
jaw
  • 510
  • 6
  • 15
17
votes
1 answer

Why does the x86-64 GCC function prologue allocate less stack than the local variables?

Consider the following simple program: int main(int argc, char **argv) { char buffer[256]; buffer[0] = 0x41; buffer[128] = 0x41; buffer[255] = 0x41; return 0; } Compiled with GCC 4.7.0 on a x86-64 machine.…
csstudent2233
  • 659
  • 10
  • 17
15
votes
4 answers

Invalid access of stack red zone from Java VM

I'm trying to figure out what can cause this error in Java: Invalid access of stack red zone 0x115ee0ed0 rip=0x114973900 Has anyone ever encountered this error message? It's literally killing the JVM and everything stops there. I'm currently using…
Malaxeur
  • 5,973
  • 1
  • 36
  • 34
14
votes
2 answers

Why is there no "sub rsp" instruction in this function prologue and why are function parameters stored at negative rbp offsets?

That's what I understood by reading some memory segmentation documents: when a function is called, there are a few instructions (called function prologue) that save the frame pointer on the stack, copy the value of the stack pointer into the base…
Mark
  • 405
  • 4
  • 10
13
votes
2 answers

Why does GCC subtract the wrong value to the stack pointer when allocating a big array with no subsequent function calls?

Really bizarre gcc quirk. Check this out: main() { int a[100]; a[0]=1; } produces this assembly: 0: 55 push %rbp 1: 48 89 e5 mov %rsp,%rbp 4: 48 81 ec 18 01 00 00 sub $0x118,%rsp b: …
user1074897
12
votes
1 answer

Compiler using local variables without adjusting RSP

In question Compilers: Understanding assembly code generated from small programs the compiler uses two local variables without adjusting the stack pointer. Not adjusting RSP for the use of local variables seems not interrupt safe and so the compiler…
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
7
votes
2 answers

Why do we need stack allocation when we have a red zone?

I have the following doubts: As we know System V x86-64 ABI gives us about a fixed-size area (128 bytes) in the stack frame, so called redzone. So, as a result we don't need to use, for example, sub rsp, 12. Just make mov [rsp-12], X and that's…
Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
6
votes
2 answers

Can I pop from the middle of a stack?

In x86 Assembly language: I assume that I have a normal function prologue, read push ebp mov ebp,esp I know I can read or write registers by accessing a memory destination operand, let's say I wanted the first argument. I would do mov eax,[ebp…
clockw0rk
  • 576
  • 5
  • 26
6
votes
1 answer

How do I tell gcc that my inline assembly clobbers part of the stack?

Consider inline assembly like this: uint64_t flags; asm ("pushf\n\tpop %0" : "=rm"(flags) : : /* ??? */); Nonwithstanding the fact that there is probably some kind of intrinsic to get the contents of RFLAGS, how do I indicate to the compiler that…
fuz
  • 88,405
  • 25
  • 200
  • 352
5
votes
1 answer

GCC access memory above stack top

I have C function that does some SSE calculations. When I compile it with GCC I get next code /* Start of function */ mov (%rdi),%rax movslq %ecx,%rcx ... mov 0x8(%rdi),%rax pxor %xmm12,%xmm3 movaps %xmm0,-0x28(%rsp) movaps…
Alexander Dzyoba
  • 4,009
  • 1
  • 24
  • 29
4
votes
1 answer

rsp doesn't move when entering new function

When entering in a C function I expected to see in the disassembly how the stack pointer gets subtracted enough to make space for variables, but no; I only see how the address of variables is directly access through ebp, when esp still points to…
badabum
  • 73
  • 5
3
votes
1 answer

Does the red zone still exist even if we use the -mno-red-zone flag in gcc?

My system: Ubuntu 22.04.3 running on x86_64. GCC version 11.4.0 I've read that the System V ABI mandates the red zone. From the GCC manual: The red zone is mandated by the x86-64 ABI, it is a 128-byte area beyond the location of the stack pointer…
alessio solari
  • 313
  • 1
  • 6
1
2 3 4