[clang] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 15 21:50:37 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Jesse Huang (jaidTw)
<details>
<summary>Changes</summary>
This patches add a string attribute "hw-shadow-stack" to every function if `-fcf-protection=return` is set on RISC-V
---
Full diff: https://github.com/llvm/llvm-project/pull/112477.diff
2 Files Affected:
- (modified) clang/lib/Basic/Targets/RISCV.h (+6)
- (modified) clang/lib/CodeGen/CodeGenFunction.cpp (+4)
``````````diff
diff --git a/clang/lib/Basic/Targets/RISCV.h b/clang/lib/Basic/Targets/RISCV.h
index bf40edb8683b3e..3165623593fdf7 100644
--- a/clang/lib/Basic/Targets/RISCV.h
+++ b/clang/lib/Basic/Targets/RISCV.h
@@ -141,6 +141,12 @@ class RISCVTargetInfo : public TargetInfo {
return true;
}
+ bool checkCFProtectionReturnSupported(DiagnosticsEngine &Diags) const override {
+ if (ISAInfo->hasExtension("zimop"))
+ return true;
+ return TargetInfo::checkCFProtectionReturnSupported(Diags);
+ }
+
CFBranchLabelSchemeKind getDefaultCFBranchLabelScheme() const override {
return CFBranchLabelSchemeKind::FuncSig;
}
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 2306043c90f406..d8f0f7c14f6b40 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -899,6 +899,10 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
if (CodeGenOpts.PointerAuth.IndirectGotos)
Fn->addFnAttr("ptrauth-indirect-gotos");
+ // Add return control flow integrity attributes.
+ if (CodeGenOpts.CFProtectionReturn)
+ Fn->addFnAttr("hw-shadow-stack");
+
// Apply xray attributes to the function (as a string, for now)
bool AlwaysXRayAttr = false;
if (const auto *XRayAttr = D ? D->getAttr<XRayInstrumentAttr>() : nullptr) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/112477
More information about the cfe-commits
mailing list