2

I know that in x86_64, there exists a 128 byte red zone above (or below, address-wise) the stack pointer that functions can use without subtracting from rsp.

This sounds to me like the only things that need to be aware of the red-zone are situations in which code will get interrupted by other code running on the same stack.

The only cases where this seems relevant to me are:

  • OS-level interrupt handlers (either switch stacks without touching the red zone, or subtract red zone size before using the stack)
  • user-level signal handlers (since AFAIK POSIX signals deliver on the same stack if sigaltstack() wasn't used)

In those cases, when that code handles the existence of a red zone, linking together code compiled with -mno-red-zone and without it should not be a problem, right?

It'd only be a problem if the code assumes -mno-red-zone and some code uses it anyway.

Is my reasoning about this correct?

If this is correct, why do so many OS-dev resources tell you to enable -mno-red-zone? is it just to reduce complexity?

Ferdi265
  • 129
  • 1
  • [Why can't kernel code use a Red Zone?](https://stackoverflow.com/q/25787408/) – Robert Harvey Jul 14 '19 at 17:42
  • [What is the purpose of red zone?](https://softwareengineering.stackexchange.com/q/230089) – Robert Harvey Jul 14 '19 at 17:45
  • [A tale about data corruption, stack and red zone](https://alex.dzyoba.com/blog/redzone/) – Robert Harvey Jul 14 '19 at 17:56
  • [Comment on Reddit about creating an OS in Rust](https://www.reddit.com/r/programming/comments/3q6y2l/writing_an_os_in_rust/cwcu8g0/) – Robert Harvey Jul 14 '19 at 17:58
  • 1
    [From Linus Torvalds](https://gcc.gnu.org/bugzilla//show_bug.cgi?id=61904): *"The kernel cannot use the x86-64 stack red-zoning, because the hardware only switches stacks on privilege transfers, so interrupts that happen in kernel mode will not honor the normal 128-byte stack red-zone."* – Robert Harvey Jul 14 '19 at 18:01
  • Thanks! The Citation from Torvalds was most helpful :) – Ferdi265 Jul 14 '19 at 21:46

0 Answers0