Skip to content

Commit 335e68d

Browse files
authored
[Clang][RISCV] Support -fcf-protection=return for RISC-V (#112477)
Enables the support of `-fcf-protection=return` on RISC-V, which requires Zicfiss. It also adds a string attribute "hw-shadow-stack" to every function if the option is set on RISC-V
1 parent 66fc81c commit 335e68d

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

clang/lib/Basic/Targets/RISCV.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ class RISCVTargetInfo : public TargetInfo {
141141
return true;
142142
}
143143

144+
bool
145+
checkCFProtectionReturnSupported(DiagnosticsEngine &Diags) const override {
146+
if (ISAInfo->hasExtension("zicfiss"))
147+
return true;
148+
return TargetInfo::checkCFProtectionReturnSupported(Diags);
149+
}
150+
144151
CFBranchLabelSchemeKind getDefaultCFBranchLabelScheme() const override {
145152
return CFBranchLabelSchemeKind::FuncSig;
146153
}

clang/lib/CodeGen/Targets/RISCV.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
594594
const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
595595
if (!FD) return;
596596

597+
auto *Fn = cast<llvm::Function>(GV);
598+
599+
if (CGM.getCodeGenOpts().CFProtectionReturn)
600+
Fn->addFnAttr("hw-shadow-stack");
601+
597602
const auto *Attr = FD->getAttr<RISCVInterruptAttr>();
598603
if (!Attr)
599604
return;
@@ -604,8 +609,6 @@ class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
604609
case RISCVInterruptAttr::machine: Kind = "machine"; break;
605610
}
606611

607-
auto *Fn = cast<llvm::Function>(GV);
608-
609612
Fn->addFnAttr("interrupt", Kind);
610613
}
611614
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang_cc1 -triple riscv64 -target-feature +experimental-zicfiss -emit-llvm -o - %s -fcf-protection=return | FileCheck %s
2+
// RUN: %clang_cc1 -triple riscv64 -target-feature +experimental-zicfiss -emit-llvm -o - %s | FileCheck -check-prefix=NOSHADOWSTACK %s
3+
// RUN: %clang_cc1 -triple riscv32 -target-feature +experimental-zicfiss -emit-llvm -o - %s -fcf-protection=return | FileCheck %s
4+
// RUN: %clang_cc1 -triple riscv32 -target-feature +experimental-zicfiss -emit-llvm -o - %s | FileCheck -check-prefix=NOSHADOWSTACK %s
5+
6+
int foo(int *a) { return *a; }
7+
8+
// CHECK: attributes {{.*}}"hw-shadow-stack"{{.*}}
9+
// NOSHADOWSTACK-NOT: attributes {{.*}}"hw-shadow-stack"{{.*}}

0 commit comments

Comments
 (0)