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
1
vote
1 answer

Can you use the red zone with/across syscalls?

Consider this GNU Assembler program, that copies one byte at a time from stdin to stdout, with a delay of one second between each: #include .global _start _start: movq $1, -16(%rsp) movq $0, -8(%rsp) movl $1,…
1
vote
0 answers

Why doesn't gcc assembly substract rsp?

While exploring godbolt, I noticed that gcc almost never substracts from rsp. For example: square(int): push rbp mov rbp, rsp mov DWORD PTR [rbp-4], edi ; [rbp-4] is not in square(int)'s stack frame mov …
gavrilikhin.d
  • 554
  • 1
  • 7
  • 20
1
vote
0 answers

gcc doesn't move esp register to allocate memory for local variables (x86_64)

I have the following function: void myFunc() { int a, b, c; a = 1; b = 2; c = 3; } gcc -S file.c gives such assembly code: myFunc: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 …
Rost
  • 65
  • 1
  • 7
1
vote
0 answers

Understanding stack and red zone of a C program

Here is a simple C program: #include int main(int argc, char* argv[] ){ int x = 10; int* ptr = &x; ptr++; printf("%x %d \n", ptr, *ptr); } Using a GNU debugger on Ubuntu 64-bit, I debugged the program step by step from…
A6SE
  • 260
  • 1
  • 3
  • 13
1
vote
0 answers

Assembly weird stack usage

I have few questions about how stack is used in Assembly. As far as I know, the %rsp register is used as stack pointer. To allocate new memory on stack in Assembly code, you just subtract needed amount from %rsp, moving it backwards. Then you can…
Maras
  • 982
  • 9
  • 15
1
vote
0 answers

assembly: I don't understand why the stackpointer seems(!) to reserve insufficient memory here

I'm a beginner in assembly, so the answer to my question probably is totaly obvious for most of you, but not for me. Please don't blame. On a 64-bit-system this C-code: 1| int main () 2| { 3| char ary[230]; 4| ary[0] = 2; 5| return 0; 6|…
a kind person
  • 329
  • 1
  • 6
  • 17
1
vote
2 answers

Why does the compiler reserve a little stack space but not the whole array size?

The following code int main() { int arr[120]; return arr[0]; } Compiles into this: sub rsp, 360 mov eax, DWORD PTR [rsp-480] add rsp, 360 ret Knowing the ints are 4 bytes and the array is size 120, the array should take…
Riolku
  • 572
  • 1
  • 4
  • 10
1
vote
0 answers

Suspicious usage of below-stack-pointer memory area for local variables

I have recently discovered unclear fact about memory reservation for local variables in generated by g++ code: the variables are placed at the addresses below the stack pointer. For example, when this program (main.cpp) is compiled: void…
Dr. Zoidberg
  • 155
  • 5
1
vote
1 answer

Absence of stack allocation on 64-bit system while working on Aleph One article

I've been messing up with "Smash the Stack for Fun and Profit" from Aleph One and found that, while compiling the code for my 64-bit processor, stack memory doesn't get allocated using the usual "sub $VALUE, %REG." This is the function source…
last
  • 11
  • 3
0
votes
1 answer

Is the red zone a problem for inline assembly for 32-bit x86 with MSVC?

Does the red zone exist in x86 ? And even if not, can you explain to me by AMD64 ? Where is the red zone ? WIKI: "the red zone is a fixed-size area in a function's stack frame below the current stack pointer" "current stack pointer": the meaning…
0
votes
0 answers

Why won't GCC use the red zone to store my local variables?

The callee function may use the red zone for storing local variables without the extra overhead of modifying the stack pointer. The x86-64 ABI used by System V mandates a 128-byte red zone, which begins directly after the return address and…
0
votes
0 answers

Why we need stack's Red Zone?

According to wikipedia: https://en.wikipedia.org/wiki/Red_zone_(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…
algo
  • 101
  • 6
0
votes
0 answers

x86-64 gcc doesn't assign a stack frame for local variable

I had some problem when I learned about assembly code. I use "compiler explorer" that is a website that supporting a lot of compiler. I made a simple code and compiled it as x86-64 gcc. : int sum(int a, int b) { return a + b; } int…
0
votes
0 answers

Understanding function prologue with multiple function calls

Let's take the following example I have from a single function: first_function: pushq %rbp movq %rsp, %rbp movq $2, -8(%rbp) movq $4, -16(%rbp) ... pop %rbp ret If we look at the stack before the ..., it gives…
David542
  • 104,438
  • 178
  • 489
  • 842
0
votes
0 answers

Why stack pointer %rsp is not (always) grown before using it?

I wrote a small example program to play around with: int f(int x){ x = x + 1; return x; } int main(){ int x = 10; x = f(x); return 0; } Compiled on x86-64: gcc -o simple simple.c And looked at disassembly for f(): Dump of assembler code…
Pavel Gurkov
  • 737
  • 5
  • 14