Skip to content

Commit

Permalink
[RISCV] Add feature forced-sw-shadow-stack to decide use hw/sw implem…
Browse files Browse the repository at this point in the history
…enet for shadow stack.
  • Loading branch information
Yeting Kuo committed Jan 8, 2024
1 parent 7ee57c4 commit f99f73f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/Target/RISCV/RISCVFeatures.td
Original file line number Diff line number Diff line change
Expand Up @@ -1044,3 +1044,8 @@ def FeatureTaggedGlobals : SubtargetFeature<"tagged-globals",
"AllowTaggedGlobals",
"true", "Use an instruction sequence for taking the address of a global "
"that allows a memory tag in the upper address bits">;

def FeatureForcedSWShadowStack : SubtargetFeature<
"forced-sw-shadow-stack", "HasForcedSWShadowStack", "true",
"Implement shadow stack with software.">;
def HasForcedSWShadowStack : Predicate<"Subtarget->hasForcedSWShadowStack()">;
15 changes: 4 additions & 11 deletions llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@

using namespace llvm;

static cl::opt<bool>
HardwareShadowStack("riscv-hardware-shadow-stack", cl::init(false),
cl::Hidden,
cl::desc("Enable hardware shadow stack with Zicfiss."));

static const Register AllPopRegs[] = {
RISCV::X1, RISCV::X8, RISCV::X9, RISCV::X18, RISCV::X19,
RISCV::X20, RISCV::X21, RISCV::X22, RISCV::X23, RISCV::X24,
Expand All @@ -57,9 +52,8 @@ static void emitSCSPrologue(MachineFunction &MF, MachineBasicBlock &MBB,
return;

const RISCVInstrInfo *TII = STI.getInstrInfo();
if (HardwareShadowStack) {
if (!STI.hasFeature(RISCV::FeatureStdExtZicfiss))
report_fatal_error("Hardware shadow stack needs Zicfiss to be enabled");
if (!STI.hasForcedSWShadowStack() &&
STI.hasFeature(RISCV::FeatureStdExtZicfiss)) {
BuildMI(MBB, MI, DL, TII->get(RISCV::SSPUSH)).addReg(RAReg);
return;
}
Expand Down Expand Up @@ -119,9 +113,8 @@ static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB,
return;

const RISCVInstrInfo *TII = STI.getInstrInfo();
if (HardwareShadowStack) {
if (!STI.hasFeature(RISCV::FeatureStdExtZicfiss))
report_fatal_error("Hardware shadow stack needs Zicfiss to be enabled");
if (!STI.hasForcedSWShadowStack() &&
STI.hasFeature(RISCV::FeatureStdExtZicfiss)) {
BuildMI(MBB, MI, DL, TII->get(RISCV::SSPOPCHK)).addReg(RAReg);
return;
}
Expand Down
12 changes: 8 additions & 4 deletions llvm/test/CodeGen/RISCV/shadowcallstack.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
; RUN: | FileCheck %s --check-prefix=RV32
; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
; RUN: | FileCheck %s --check-prefix=RV64
; RUN: llc -mtriple=riscv32 -mattr=+experimental-zicfiss -riscv-hardware-shadow-stack -verify-machineinstrs < %s \
; RUN: | FileCheck %s --check-prefix=RV32-ZICFISS
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zicfiss -riscv-hardware-shadow-stack -verify-machineinstrs < %s \
; RUN: | FileCheck %s --check-prefix=RV64-ZICFISS
; RUN: llc -mtriple=riscv32 -mattr=+experimental-zicfiss < %s \
; RUN: -verify-machineinstrs | FileCheck %s --check-prefix=RV32-ZICFISS
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zicfiss < %s \
; RUN: -verify-machineinstrs | FileCheck %s --check-prefix=RV64-ZICFISS
; RUN: llc -mtriple=riscv32 -mattr=+experimental-zicfiss,forced-sw-shadow-stack \
; RUN: -verify-machineinstrs < %s | FileCheck %s --check-prefix=RV32
; RUN: llc -mtriple=riscv64 -mattr=+experimental-zicfiss,forced-sw-shadow-stack \
; RUN: -verify-machineinstrs < %s | FileCheck %s --check-prefix=RV64

define void @f1() shadowcallstack {
; RV32-LABEL: f1:
Expand Down

0 comments on commit f99f73f

Please sign in to comment.