[clang] [llvm] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)
Jesse Huang via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 24 01:22:28 PDT 2024
https://github.com/jaidTw updated https://github.com/llvm/llvm-project/pull/112477
>From fe4a28fb691b69d9af384f1dc2f0667761adef44 Mon Sep 17 00:00:00 2001
From: Jesse Huang <jesse.huang at sifive.com>
Date: Sun, 13 Oct 2024 15:11:06 +0800
Subject: [PATCH 1/3] [Clang][RISCV] Support -fcf-protection=return for RISC-V
---
clang/lib/Basic/Targets/RISCV.h | 7 +++++++
clang/lib/CodeGen/CodeGenFunction.cpp | 4 ++++
2 files changed, 11 insertions(+)
diff --git a/clang/lib/Basic/Targets/RISCV.h b/clang/lib/Basic/Targets/RISCV.h
index bf40edb8683b3e..3f2cee72fc3731 100644
--- a/clang/lib/Basic/Targets/RISCV.h
+++ b/clang/lib/Basic/Targets/RISCV.h
@@ -141,6 +141,13 @@ 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) {
>From 7dc168af5758d130042c71ba5d6249c042bc356c Mon Sep 17 00:00:00 2001
From: Jesse Huang <jesse.huang at sifive.com>
Date: Wed, 23 Oct 2024 14:42:35 +0800
Subject: [PATCH 2/3] [Clang][RISCV] Add RISCV check for hw-shadow-stack
---
clang/lib/CodeGen/CodeGenFunction.cpp | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index d8f0f7c14f6b40..cca52f3769845e 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -899,8 +899,9 @@ 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)
+ // Add return control flow integrity attributes for RISCV.
+ if (CodeGenOpts.CFProtectionReturn &&
+ getContext().getTargetInfo().getTriple().isRISCV())
Fn->addFnAttr("hw-shadow-stack");
// Apply xray attributes to the function (as a string, for now)
>From 3cdb6c584bc1c7714b43fc15fe31a44200710262 Mon Sep 17 00:00:00 2001
From: Jesse Huang <jesse.huang at sifive.com>
Date: Thu, 24 Oct 2024 15:39:07 +0800
Subject: [PATCH 3/3] [Clang][RISCV] Add function attribute in RISC-V specific
code
---
clang/lib/CodeGen/CodeGenFunction.cpp | 5 -----
clang/lib/CodeGen/Targets/RISCV.cpp | 3 +++
test.ll | 30 +++++++++++++++++++++++++++
3 files changed, 33 insertions(+), 5 deletions(-)
create mode 100644 test.ll
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index cca52f3769845e..2306043c90f406 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -899,11 +899,6 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
if (CodeGenOpts.PointerAuth.IndirectGotos)
Fn->addFnAttr("ptrauth-indirect-gotos");
- // Add return control flow integrity attributes for RISCV.
- if (CodeGenOpts.CFProtectionReturn &&
- getContext().getTargetInfo().getTriple().isRISCV())
- 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) {
diff --git a/clang/lib/CodeGen/Targets/RISCV.cpp b/clang/lib/CodeGen/Targets/RISCV.cpp
index fd72fe673b9b14..998a1e8de17dce 100644
--- a/clang/lib/CodeGen/Targets/RISCV.cpp
+++ b/clang/lib/CodeGen/Targets/RISCV.cpp
@@ -607,6 +607,9 @@ class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
auto *Fn = cast<llvm::Function>(GV);
Fn->addFnAttr("interrupt", Kind);
+
+ if (CGM.getCodeGenOpts().CFProtectionReturn)
+ Fn->addFnAttr("hw-shadow-stack");
}
};
} // namespace
diff --git a/test.ll b/test.ll
new file mode 100644
index 00000000000000..3157c4df34da58
--- /dev/null
+++ b/test.ll
@@ -0,0 +1,30 @@
+; ModuleID = '/home/jhuang4/workspace/test.c'
+source_filename = "/home/jhuang4/workspace/test.c"
+target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
+target triple = "riscv64-unknown-unknown"
+
+; Function Attrs: noinline nounwind optnone
+define dso_local void @foo(ptr noundef %str) #0 {
+entry:
+ %str.addr = alloca ptr, align 8
+ store ptr %str, ptr %str.addr, align 8
+ %0 = load ptr, ptr %str.addr, align 8
+ %call = call signext i32 @puts(ptr noundef %0)
+ ret void
+}
+
+declare dso_local signext i32 @puts(ptr noundef) #1
+
+attributes #0 = { noinline nounwind optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic-rv64" "target-features"="+64bit,+a,+c,+d,+experimental,+experimental-zicfilp,+experimental-zicfiss,+f,+m,+relax,+zicsr,+zifencei,+zimop,+zmmul,-b,-e,-experimental-smctr,-experimental-smmpm,-experimental-smnpm,-experimental-ssctr,-experimental-ssnpm,-experimental-sspm,-experimental-supm,-experimental-zalasr,-experimental-zvbc32e,-experimental-zvkgs,-h,-shcounterenw,-shgatpa,-shtvala,-shvsatpa,-shvstvala,-shvstvecd,-smaia,-smcdeleg,-smcsrind,-smepmp,-smstateen,-ssaia,-ssccfg,-ssccptr,-sscofpmf,-sscounterenw,-sscsrind,-ssqosid,-ssstateen,-ssstrict,-sstc,-sstvala,-sstvecd,-ssu64xl,-svade,-svadu,-svbare,-svinval,-svnapot,-svpbmt,-v,-xcvalu,-xcvbi,-xcvbitmanip,-xcvelw,-xcvmac,-xcvmem,-xcvsimd,-xsfcease,-xsfvcp,-xsfvfnrclipxfqf,-xsfvfwmaccqqq,-xsfvqmaccdod,-xsfvqmaccqoq,-xsifivecdiscarddlone,-xsifivecflushdlone,-xtheadba,-xtheadbb,-xtheadbs,-xtheadcmo,-xtheadcondmov,-xtheadfmemidx,-xtheadmac,-xtheadmemidx,-xtheadmempair,-xtheadsync,-xtheadvdot,-xventanacondops,-xwchc,-za128rs,-za64rs,-zaamo,-zabha,-zacas,-zalrsc,-zama16b,-zawrs,-zba,-zbb,-zbc,-zbkb,-zbkc,-zbkx,-zbs,-zca,-zcb,-zcd,-zce,-zcf,-zcmop,-zcmp,-zcmt,-zdinx,-zfa,-zfbfmin,-zfh,-zfhmin,-zfinx,-zhinx,-zhinxmin,-zic64b,-zicbom,-zicbop,-zicboz,-ziccamoa,-ziccif,-zicclsm,-ziccrse,-zicntr,-zicond,-zihintntl,-zihintpause,-zihpm,-zk,-zkn,-zknd,-zkne,-zknh,-zkr,-zks,-zksed,-zksh,-zkt,-ztso,-zvbb,-zvbc,-zve32f,-zve32x,-zve64d,-zve64f,-zve64x,-zvfbfmin,-zvfbfwma,-zvfh,-zvfhmin,-zvkb,-zvkg,-zvkn,-zvknc,-zvkned,-zvkng,-zvknha,-zvknhb,-zvks,-zvksc,-zvksed,-zvksg,-zvksh,-zvkt,-zvl1024b,-zvl128b,-zvl16384b,-zvl2048b,-zvl256b,-zvl32768b,-zvl32b,-zvl4096b,-zvl512b,-zvl64b,-zvl65536b,-zvl8192b" }
+attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic-rv64" "target-features"="+64bit,+a,+c,+d,+experimental,+experimental-zicfilp,+experimental-zicfiss,+f,+m,+relax,+zicsr,+zifencei,+zimop,+zmmul,-b,-e,-experimental-smctr,-experimental-smmpm,-experimental-smnpm,-experimental-ssctr,-experimental-ssnpm,-experimental-sspm,-experimental-supm,-experimental-zalasr,-experimental-zvbc32e,-experimental-zvkgs,-h,-shcounterenw,-shgatpa,-shtvala,-shvsatpa,-shvstvala,-shvstvecd,-smaia,-smcdeleg,-smcsrind,-smepmp,-smstateen,-ssaia,-ssccfg,-ssccptr,-sscofpmf,-sscounterenw,-sscsrind,-ssqosid,-ssstateen,-ssstrict,-sstc,-sstvala,-sstvecd,-ssu64xl,-svade,-svadu,-svbare,-svinval,-svnapot,-svpbmt,-v,-xcvalu,-xcvbi,-xcvbitmanip,-xcvelw,-xcvmac,-xcvmem,-xcvsimd,-xsfcease,-xsfvcp,-xsfvfnrclipxfqf,-xsfvfwmaccqqq,-xsfvqmaccdod,-xsfvqmaccqoq,-xsifivecdiscarddlone,-xsifivecflushdlone,-xtheadba,-xtheadbb,-xtheadbs,-xtheadcmo,-xtheadcondmov,-xtheadfmemidx,-xtheadmac,-xtheadmemidx,-xtheadmempair,-xtheadsync,-xtheadvdot,-xventanacondops,-xwchc,-za128rs,-za64rs,-zaamo,-zabha,-zacas,-zalrsc,-zama16b,-zawrs,-zba,-zbb,-zbc,-zbkb,-zbkc,-zbkx,-zbs,-zca,-zcb,-zcd,-zce,-zcf,-zcmop,-zcmp,-zcmt,-zdinx,-zfa,-zfbfmin,-zfh,-zfhmin,-zfinx,-zhinx,-zhinxmin,-zic64b,-zicbom,-zicbop,-zicboz,-ziccamoa,-ziccif,-zicclsm,-ziccrse,-zicntr,-zicond,-zihintntl,-zihintpause,-zihpm,-zk,-zkn,-zknd,-zkne,-zknh,-zkr,-zks,-zksed,-zksh,-zkt,-ztso,-zvbb,-zvbc,-zve32f,-zve32x,-zve64d,-zve64f,-zve64x,-zvfbfmin,-zvfbfwma,-zvfh,-zvfhmin,-zvkb,-zvkg,-zvkn,-zvknc,-zvkned,-zvkng,-zvknha,-zvknhb,-zvks,-zvksc,-zvksed,-zvksg,-zvksh,-zvkt,-zvl1024b,-zvl128b,-zvl16384b,-zvl2048b,-zvl256b,-zvl32768b,-zvl32b,-zvl4096b,-zvl512b,-zvl64b,-zvl65536b,-zvl8192b" }
+
+!llvm.module.flags = !{!0, !1, !2, !4, !5}
+!llvm.ident = !{!6}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 1, !"target-abi", !"lp64d"}
+!2 = !{i32 6, !"riscv-isa", !3}
+!3 = !{!"rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicfilp1p0_zicfiss1p0_zicsr2p0_zifencei2p0_zimop1p0_zmmul1p0"}
+!4 = !{i32 7, !"frame-pointer", i32 2}
+!5 = !{i32 8, !"SmallDataLimit", i32 0}
+!6 = !{!"clang version 20.0.0git (git at github.com:jaidTw/llvm-project.git 7dc168af5758d130042c71ba5d6249c042bc356c)"}
More information about the cfe-commits
mailing list