Absence of stack allocation on 64-bit system while

2019-07-26 22:30发布

问题:

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 code:

void function() {
  char buffer1[5];
  char buffer2[10];
  int *ret;

  ret = buffer1 + 32;
  (*ret) +=8;   
}

And this is the compiled version

function:
.LFB0:
.cfi_startproc
pushq   %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq    %rsp, %rbp
.cfi_def_cfa_register 6
movl    $1868654947, -16(%rbp)
movb    $0, -12(%rbp)
leaq    -16(%rbp), %rax
addq    $32, %rax
movq    %rax, -8(%rbp)
movq    -8(%rbp), %rax
movl    (%rax), %eax
leal    8(%rax), %edx
movq    -8(%rbp), %rax
movl    %edx, (%rax)
nop
popq    %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc

Why is there no standard stack allocation while if I use the -m32 option with gcc it appears?

回答1:

The amd64 SysV ABI contains a concept called the red zone. The red zone is the area of 128 bytes just below the stack pointer. It's purpose is to allow functions to allocate small amounts of stack with having to decrement the stack pointer. That's why you don't see the stack pointer decremented.

Compile with -mno-red-zone to turn this feature off.