0

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 used in leaf functions (functions that don't call other functions) for allocating additional stack memory, without moving the stack pointer, which saves an instruction.

Focusing on the last sentence I don't agree at all. The fact that rsp is the end of the frame is just to make things easier on us (if we want to write assembly manually or as a type of convention). So we can do something like: - at&t syntax-

moveq $5, -200(%rsp)

without using the red zone at all, so what red zone is helpful for (It doesn't save a call as claimed)?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
algo
  • 101
  • 6
  • Please Note, writing outside rsp isn't a problem for leaf functions, and I am referring to this case. – algo Oct 05 '21 at 17:30
  • 2
    It's not that writing below RSP is unsafe (unless you go too far, into an unmapped page), you just aren't guaranteed to read the same value you wrote. e.g. a signal handler could clobber it. Like in [Is it valid to write below ESP?](https://stackoverflow.com/q/52258402) in 32-bit code. Also, nobody said it saves a `call` instruction; it saves a `sub $24, %rsp` / `add $24, %rsp`. – Peter Cordes Oct 05 '21 at 17:34
  • As well as the linked duplicate which explains what a red-zone gives you that you wouldn't get otherwise, also see [Why can't kernel code use a Red Zone](https://stackoverflow.com/q/25787408) for another example that illustrates when there'd be a problem. – Peter Cordes Oct 05 '21 at 17:41
  • `moveq $5, -200(%rsp)` **is** using the red zone. Why do you say it isn't? – prl Oct 05 '21 at 18:05
  • @PeterCordes didn't understand this at all: "Also, nobody said it saves a call instruction; it saves a sub $24, %rsp / add $24, %rsp" – algo Oct 05 '21 at 21:12
  • 1
    @prl in linux red zone is 128 bytes... – algo Oct 05 '21 at 21:13
  • You wrote "(It doesn't save a call as claimed)". I assumed you meant a `call` instruction, i.e. saving a function call. Other instructions that execute aren't calls. – Peter Cordes Oct 05 '21 at 21:47

0 Answers0