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.

red-zone
Filter by
Sorted by
Tagged with
1 vote
1 answer
92 views

Aarch64 is there a Red Zone on Linux, If so 16 or 128 bytes?

There doesn't seem to be any mention of a "Red Zone" for Aarch64 in the ABI, but Microsoft makes reference to a 16-byte red zone for Aarch64, Apple claims a 128-byte red zone in Writing ...
David C. Rankin's user avatar
3 votes
1 answer
258 views

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-...
alessio solari's user avatar
1 vote
1 answer
91 views

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

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 ...
alessio solari's user avatar
1 vote
0 answers
20 views

assembly epilogue composition [duplicate]

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 ...
Guanwei HU's user avatar
1 vote
1 answer
294 views

the stack pointer doesn't point to the top of the stack [duplicate]

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 ...
Mehdi Charife's user avatar
1 vote
0 answers
99 views

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

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 <stdio.h> int addNumbers(int a,int b,...
the_endian's user avatar
  • 2,379
0 votes
1 answer
280 views

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 ...
igorpoolday's user avatar
0 votes
0 answers
121 views

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 ...
yokiyaa J's user avatar
1 vote
1 answer
104 views

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 <sys/syscall.h> .global _start _start: movq $1, -...
Joseph Sible-Reinstate Monica's user avatar
3 votes
1 answer
122 views

"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 <stdio.h> 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() ...
Happy Jerry's user avatar
0 votes
0 answers
316 views

Why we need stack's Red Zone? [duplicate]

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 ...
algo's user avatar
  • 131
2 votes
0 answers
434 views

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 => [...
tdiff's user avatar
  • 113
3 votes
0 answers
324 views

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 ...
Nivekithan's user avatar
0 votes
0 answers
108 views

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

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. <...
Changjin Koo's user avatar
1 vote
0 answers
277 views

Why doesn't gcc assembly substract rsp? [duplicate]

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 ...
gavrilikhin.d's user avatar
0 votes
0 answers
90 views

Understanding function prologue with multiple function calls [duplicate]

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 ...
David542's user avatar
  • 108k
-1 votes
1 answer
657 views

Value of rbp changing after jumping into a new function

I have the following assembly program: .globl main main: push %rbp mov %rsp, %rbp movb $8, -1(%rbp) movw $30, -4(%rbp) mov -1(%rbp), %rax add -4(%rbp), %rax call func ...
samuelbrody1249's user avatar
0 votes
0 answers
198 views

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

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 ...
Pavel Gurkov's user avatar
2 votes
0 answers
48 views

Why does gcc use stack area without decrementing stack pointer? [duplicate]

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 ...
Mah35h's user avatar
  • 1,201
1 vote
0 answers
34 views

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

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 ....
Rost's user avatar
  • 65
0 votes
0 answers
337 views

Different ways how gcc allocate memory for stack [duplicate]

How does gcc decides how much memory allocate for stack and why does it not decrement %rsp anymore when I remove printf() (or any function call) from my main? 1. I noticed when I played around with a ...
miran80's user avatar
  • 1,045
2 votes
1 answer
149 views

disassembly of C array gives weird results [duplicate]

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 ...
user282909's user avatar
2 votes
1 answer
490 views

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's user avatar
  • 1,201
0 votes
0 answers
291 views

Using the Frame pointer instead of the Stack pointer in x86_64 [duplicate]

Compiling a simple C code into assembly using GCC will have the following output: ... 13 xorl %eax, %eax 14 movl $0, -4(%rbp) 15 movl $5, -8(%rbp) 16 movl ...
Josh's user avatar
  • 53
0 votes
2 answers
412 views

What is the actual size of stack red zone? [duplicate]

In the x86-64 System V ABI it is specified that the space behind the $rsp - 128 is the so-called red zone which is not touched by any signal handlers. On my machine $ ulimit -s 8192 I expected ...
St.Antario's user avatar
  • 26.8k
1 vote
0 answers
184 views

Understanding stack and red zone of a C program

Here is a simple C program: #include <stdio.h> int main(int argc, char* argv[] ){ int x = 10; int* ptr = &x; ptr++; printf("%x %d \n", ptr, *ptr); } Using a GNU ...
A6SE's user avatar
  • 270
1 vote
0 answers
37 views

Assembly weird stack usage [duplicate]

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 ...
Maras's user avatar
  • 982
3 votes
0 answers
668 views

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

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 ...
Timur Fayzrakhmanov's user avatar
1 vote
0 answers
38 views

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

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| ...
a kind person's user avatar
1 vote
2 answers
905 views

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 ...
Riolku's user avatar
  • 652
2 votes
1 answer
3k views

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 ...
Xter's user avatar
  • 73
4 votes
1 answer
498 views

rsp doesn't move when entering new function [duplicate]

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 ...
badabum's user avatar
  • 73
1 vote
0 answers
82 views

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

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 ...
Dr. Zoidberg's user avatar
12 votes
1 answer
958 views

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 ...
Paul Ogilvie's user avatar
  • 25.1k
6 votes
2 answers
3k views

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 ...
clockw0rk's user avatar
  • 569
1 vote
1 answer
64 views

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 $...
last's user avatar
  • 11
7 votes
2 answers
544 views

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 ...
fuz's user avatar
  • 90.8k
34 votes
2 answers
9k views

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 ...
Sep Roland's user avatar
  • 36.7k
7 votes
2 answers
2k views

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 ...
Gilgamesz's user avatar
  • 4,883
17 votes
2 answers
9k views

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' ...
jaw's user avatar
  • 510
2 votes
2 answers
491 views

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 accountant's user avatar
14 votes
2 answers
4k views

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, ...
Mark's user avatar
  • 395
0 votes
1 answer
185 views

Setting up local stack according to x86-64 calling convention on linux

I am doing some extended assembly optimization on gnu C code running on 64 bit linux. I wanted to print debugging messages from within the assembly code and that's how I came accross the following. I ...
Ivan's user avatar
  • 409
28 votes
4 answers
7k views

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 -...
mmk's user avatar
  • 595
2 votes
2 answers
257 views

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 # ....
BufBills's user avatar
  • 7,935
5 votes
1 answer
766 views

GCC access memory above stack top [duplicate]

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 ...
Alexander Dzyoba's user avatar
17 votes
1 answer
4k views

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;...
csstudent2233's user avatar
0 votes
2 answers
305 views

Grails: invalid access of stack red zone

I am running a Grails 2.0.4 app on OS X (10.7.4). The app starts without any trouble, but when I try to access the home page in a browser, I get stack red zone errors: Invalid access of stack red ...
Ben Klein's user avatar
  • 1,819
1 vote
1 answer
304 views

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/) ...
Hervé Labas's user avatar
13 votes
2 answers
2k views

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: ...
user avatar