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
3
votes
1 answer

"There is no need to deallocate the stack at the end of the function when the inner-stack frame isn't modified", but it is being modified in this case

Here is a simple function #include int foo() { int a = 3; int b = 4; int c = 5; return a * b * c; } int main() { int a = foo(); } And the assembly for foo() looks like foo: push rbp mov rbp,…
Happy Jerry
  • 164
  • 1
  • 8
3
votes
0 answers

What happens to the value stored in [rbp-1] after pop rbp

I am total newbie to assembly, I learning assembly by compiling c code to assembly. For this c++ code int foo() { bool x = true; return 1; } This is the generated assembly code (intel syntax) foo(): push rbp mov …
Nivekithan
  • 953
  • 1
  • 6
  • 10
3
votes
0 answers

Why there is no `leave` instruction at function epilog on x64?

I'm on the way to get idea how the stack works on x86 and x64 machines. What I observed however is that when I manually write a code and disassembly it, it differs from what I see in the code people provide (eg. in their questions and tutorials).…
Timur Fayzrakhmanov
  • 17,967
  • 20
  • 64
  • 95
2
votes
1 answer

Why is it that the rsp register is not decremented in a leaf function as it is at the beginning of any other functions?

Here's some information about my system: Ubuntu 22.04.3 running on Intel x86_64; ggc version 11.4.0; I've noticed that the rsp register is not decremented in leaf functions as it is in non leaf functions. For example consider this C program in a…
alessio solari
  • 313
  • 1
  • 6
2
votes
0 answers

Interpreting asan stack-buffer-underflow report

Today we've encountered an stack buffer underflow reported by gcc 10.2 && gcc 11 asan. Looking into asan's output, we found that it points to following shadow byte pattern: someaddr => [f1]f1 f3 f3 f3 where f1 is stack left redzone f3 is stack…
tdiff
  • 113
  • 7
2
votes
0 answers

Why does gcc use stack area without decrementing stack pointer?

For this simple C program, int test(int a,int b) { int k=89; return a+b; } int main() { test(5,3); } gcc 10 produces the following assembly code (using https://godbolt.org/ and verified on my machine): test: push rbp …
Mah35h
  • 1,127
  • 1
  • 7
  • 18
2
votes
1 answer

disassembly of C array gives weird results

int square() { char test[50]; } The above code produces square(): push rbp mov rbp, rsp When i change the code a little to int square() { char test[150]; } The assembly generated is square(): push rbp …
user282909
  • 115
  • 1
  • 7
2
votes
1 answer

How system V ABI's red zone is implemented

how compiler makes sure that the red zone is not clobbered? Is there any overallocation of space? And what factors lead to choosing 128 byte as the size of red zone?
Mah35h
  • 1,127
  • 1
  • 7
  • 18
2
votes
1 answer

use of -mcmodel=kernel flag in x86 platform

I am trying to cross compile a device driver built for x86 architecture to arm platform. It got compiled without any errors, but I dont think whole features are available. So I checked the makefile and found this particular part. ifeq…
Xter
  • 63
  • 1
  • 10
2
votes
2 answers

why preserve stack space for local variables?

I'm new to assembly language and I was wondering about local variables, why do we (or the compilers) preserve a space for them on the stack usually by decrement the "ESP" register at the prologue of the procedure and as the procedure ends we assign…
2
votes
2 answers

Incorrect stack red-zoning on x86-64 code generation

This is compiler output from a Linux kernel function (compiled with -mno-red-zone): load_balance: .LFB2408: .loc 2 6487 0 .cfi_startproc .LVL1355: pushq %rbp # .cfi_def_cfa_offset 16 .cfi_offset 6, -16 …
BufBills
  • 8,005
  • 12
  • 48
  • 90
1
vote
1 answer

Invalid access of stack red zone when merging a complex object

I'm using play! framework 1.2.4 on a project who passes objects from an external context (Flex actually) to a service. The gateway uses the cinnamon framework (http://www.spicefactory.org/pimento/) to handle AMF requests to be routed to services. I…
Hervé Labas
  • 161
  • 1
  • 7
1
vote
0 answers

assembly epilogue composition

I'm learning assembly, and I tried compiling the following C code into assembly with GCC with optimization disabled (https://godbolt.org/z/4cz3ocfa5) void f() { int x = 1; int y = 2; int z = 3; } int main() { f(); return…
Guanwei HU
  • 55
  • 4
1
vote
1 answer

the stack pointer doesn't point to the top of the stack

This is a basic C code for a basic function call: int multiply(int num, int k) { return num * k; } int main(int argc, char** argv) { int k = multiply(5,2); } When I tried diassembling this code using the tool available at godbolt.org…
1
vote
0 answers

mov rbp-XX vs push/pop semantics - is it "proper" stack use?

I wrote a program and compiled it with GCC 12.2.1 on Fedora x64 Linux using the flags -Wall and -g3, and I disassembled it in gdb-gef. Source code: #include int addNumbers(int a,int b,int c,int d, int e, int f, int g, int h); int…
the_endian
  • 2,259
  • 1
  • 24
  • 49