[RISCV] Make SCS prologue interrupt safe on RISC-V
authorPaul Kirth <paulkirth@google.com>
Mon, 24 Apr 2023 21:03:21 +0000 (21:03 +0000)
committerPaul Kirth <paulkirth@google.com>
Wed, 26 Apr 2023 15:58:09 +0000 (15:58 +0000)
commitbface3947ea15b45c78aff4792355651f72626a1
treec2cbb85e476dfe178958785159703f8abe86287a
parentd311cb64a7d339c8d2be5351edaebd9bc21ebfe6
[RISCV] Make SCS prologue interrupt safe on RISC-V

Prior to this patch the SCS prologue used the following instruction
sequence.

```
s[w|d]  ra, 0(gp)
addi    gp, gp, [4|8]
```

The problem with this sequence is that an interrupt occurring between the
store and the increment could clobber the value just written to the SCS.

https://reviews.llvm.org/D84414#inline-813203 pointed out a similar
issues that could have affected the epilogue.

This patch changes the instruction sequence in the prologue to:

```
addi    gp, gp, [4|8]
s[w|d]  ra, -[4|8](gp)
```

The downside to this is that there is now a data dependency between the
add and the store.

Reviewed By: asb

Differential Revision: https://reviews.llvm.org/D149099
llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
llvm/test/CodeGen/RISCV/shadowcallstack.ll