Skip to content

Commit 392807e

Browse files
authored
[RISCV] Separate HW/SW shadow stack on RISC-V (#112478)
This patch follows #112477. Previously `-fsanitize=shadow-call-stack` (which get transform to `Attribute::ShadowCallStack`) is used for enable both hardware and software shadow stack, and another option `-force-sw-shadow-stack` is needed if the user wants to use the software shadow stack where hardware software shadow stack could be supported. It decouples both by using the string attribute `hw-shadow-stack` to distinguish from the software shadow stack attribute.
1 parent 522880c commit 392807e

File tree

2 files changed

+665
-19
lines changed

2 files changed

+665
-19
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@ static const std::pair<MCPhysReg, int8_t> FixedCSRFIMap[] = {
6767
static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB,
6868
MachineBasicBlock::iterator MI,
6969
const DebugLoc &DL) {
70-
if (!MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
70+
const auto &STI = MF.getSubtarget<RISCVSubtarget>();
71+
bool HasHWShadowStack = MF.getFunction().hasFnAttribute("hw-shadow-stack") &&
72+
STI.hasStdExtZicfiss();
73+
bool HasSWShadowStack =
74+
MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
75+
if (!HasHWShadowStack && !HasSWShadowStack)
7176
return;
7277

73-
const auto &STI = MF.getSubtarget<RISCVSubtarget>();
7478
const llvm::RISCVRegisterInfo *TRI = STI.getRegisterInfo();
7579
Register RAReg = TRI->getRARegister();
7680

@@ -82,7 +86,7 @@ static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB,
8286
return;
8387

8488
const RISCVInstrInfo *TII = STI.getInstrInfo();
85-
if (!STI.hasForcedSWShadowStack() && STI.hasStdExtZicfiss()) {
89+
if (HasHWShadowStack) {
8690
BuildMI(MBB, MI, DL, TII->get(RISCV::SSPUSH)).addReg(RAReg);
8791
return;
8892
}
@@ -129,10 +133,14 @@ static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB,
129133
static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB,
130134
MachineBasicBlock::iterator MI,
131135
const DebugLoc &DL) {
132-
if (!MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack))
136+
const auto &STI = MF.getSubtarget<RISCVSubtarget>();
137+
bool HasHWShadowStack = MF.getFunction().hasFnAttribute("hw-shadow-stack") &&
138+
STI.hasStdExtZicfiss();
139+
bool HasSWShadowStack =
140+
MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
141+
if (!HasHWShadowStack && !HasSWShadowStack)
133142
return;
134143

135-
const auto &STI = MF.getSubtarget<RISCVSubtarget>();
136144
Register RAReg = STI.getRegisterInfo()->getRARegister();
137145

138146
// See emitSCSPrologue() above.
@@ -142,7 +150,7 @@ static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB,
142150
return;
143151

144152
const RISCVInstrInfo *TII = STI.getInstrInfo();
145-
if (!STI.hasForcedSWShadowStack() && STI.hasStdExtZicfiss()) {
153+
if (HasHWShadowStack) {
146154
BuildMI(MBB, MI, DL, TII->get(RISCV::SSPOPCHK)).addReg(RAReg);
147155
return;
148156
}

0 commit comments

Comments
 (0)