Skip to content

Commit 7ee57c4

Browse files
author
Yeting Kuo
committed
[RISCV] Add option to enable hardware shadow stack.
1 parent fd1bb8c commit 7ee57c4

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

clang/docs/ShadowCallStack.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ the operating system should be preferred since otherwise all thread creation
5858
and destruction would need to be intercepted by the application.
5959

6060
The instrumentation makes use of the platform register ``x18`` on AArch64,
61-
``x3`` (``gp``) on RISC-V without `Zicfiss`_ and ``ssp`` on RISCV with `Zicfiss`_.
61+
``x3`` (``gp``) on RISC-V with software shadow stack and ``ssp`` on RISC-V with
62+
hardware shadow stack, which needs `Zicfiss`_ and ``-mllvm -riscv-hardware-shadow-stack``.
6263
For simplicity we will refer to this as the ``SCSReg``. On some platforms,
6364
``SCSReg`` is reserved, and on others, it is designated as a scratch register.
6465
This generally means that any code that may run on the same thread as code compiled with ShadowCallStack must either target
@@ -152,8 +153,8 @@ Usage
152153

153154
To enable ShadowCallStack, just pass the ``-fsanitize=shadow-call-stack`` flag
154155
to both compile and link command lines. On aarch64, you also need to pass
155-
``-ffixed-x18`` unless your target already reserves ``x18``. On RISC-V without
156-
`Zicfiss`_, ``x3`` (``gp``) is always reserved. It is, however, important to
156+
``-ffixed-x18`` unless your target already reserves ``x18``. On RISC-V with software
157+
shadow stack, ``x3`` (``gp``) is always reserved. It is, however, important to
157158
disable GP relaxation in the linker. This can be done with the ``--no-relax-gp``
158159
flag in GNU ld.
159160

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727

2828
using namespace llvm;
2929

30+
static cl::opt<bool>
31+
HardwareShadowStack("riscv-hardware-shadow-stack", cl::init(false),
32+
cl::Hidden,
33+
cl::desc("Enable hardware shadow stack with Zicfiss."));
34+
3035
static const Register AllPopRegs[] = {
3136
RISCV::X1, RISCV::X8, RISCV::X9, RISCV::X18, RISCV::X19,
3237
RISCV::X20, RISCV::X21, RISCV::X22, RISCV::X23, RISCV::X24,
@@ -52,7 +57,9 @@ static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB,
5257
return;
5358

5459
const RISCVInstrInfo *TII = STI.getInstrInfo();
55-
if (STI.hasFeature(RISCV::FeatureStdExtZicfiss)) {
60+
if (HardwareShadowStack) {
61+
if (!STI.hasFeature(RISCV::FeatureStdExtZicfiss))
62+
report_fatal_error("Hardware shadow stack needs Zicfiss to be enabled");
5663
BuildMI(MBB, MI, DL, TII->get(RISCV::SSPUSH)).addReg(RAReg);
5764
return;
5865
}
@@ -112,7 +119,9 @@ static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB,
112119
return;
113120

114121
const RISCVInstrInfo *TII = STI.getInstrInfo();
115-
if (STI.hasFeature(RISCV::FeatureStdExtZicfiss)) {
122+
if (HardwareShadowStack) {
123+
if (!STI.hasFeature(RISCV::FeatureStdExtZicfiss))
124+
report_fatal_error("Hardware shadow stack needs Zicfiss to be enabled");
116125
BuildMI(MBB, MI, DL, TII->get(RISCV::SSPOPCHK)).addReg(RAReg);
117126
return;
118127
}

llvm/test/CodeGen/RISCV/shadowcallstack.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
; RUN: | FileCheck %s --check-prefix=RV32
44
; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
55
; RUN: | FileCheck %s --check-prefix=RV64
6-
; RUN: llc -mtriple=riscv32 -mattr=+experimental-zicfiss -verify-machineinstrs < %s \
6+
; RUN: llc -mtriple=riscv32 -mattr=+experimental-zicfiss -riscv-hardware-shadow-stack -verify-machineinstrs < %s \
77
; RUN: | FileCheck %s --check-prefix=RV32-ZICFISS
8-
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zicfiss -verify-machineinstrs < %s \
8+
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zicfiss -riscv-hardware-shadow-stack -verify-machineinstrs < %s \
99
; RUN: | FileCheck %s --check-prefix=RV64-ZICFISS
1010

1111
define void @f1() shadowcallstack {

0 commit comments

Comments
 (0)