-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Clang][RISCV] Support -fcf-protection=return for RISC-V #112477
Conversation
@llvm/pr-subscribers-backend-risc-v @llvm/pr-subscribers-clang Author: Jesse Huang (jaidTw) ChangesThis patches add a string attribute "hw-shadow-stack" to every function if Full diff: https://github.com/llvm/llvm-project/pull/112477.diff 2 Files Affected:
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) {
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
2a7a6ef
to
fe4a28f
Compare
449c0d9
to
7dc168a
Compare
LGTM, but please wait for others. I'm too junior to this community that I'm not sure if my words are enough 😂 |
bb59a70
to
3cdb6c5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test?
3cdb6c5
to
42132c2
Compare
Addressed comments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
After a meeting with Kito, we decided to use Zicfiss as the requirement in this patch. |
421a369
to
f272724
Compare
@mylai-mtk could you take a look at the latest version? |
LGTM. Thanks. |
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
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.
This option was used to override the behavior of `-fsanitize=shadowcallstack` on RISC-V backend, which by default use a hardware implementation if possible, to use the software implementation instead. After #112477 and #112478, now two implementation is represented by independent options and we no longer need it.
This patch follows llvm#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.
This option was used to override the behavior of `-fsanitize=shadowcallstack` on RISC-V backend, which by default use a hardware implementation if possible, to use the software implementation instead. After llvm#112477 and llvm#112478, now two implementation is represented by independent options and we no longer need it.
The `-fcf-protection` flag is now also used to enable CFI features for the RISC-V target, so it's not suitable to define `__CET__` solely based on the flag anymore. This patch moves the definition of the `__CET__` macro into X86 target hook, so only X86 targets with the `-fcf-protection` flag would enable the `__CET__` macro. See #109784 and #112477 for the adoption of `-fcf-protection` flag for RISC-V targets.
…27616) The `-fcf-protection` flag is now also used to enable CFI features for the RISC-V target, so it's not suitable to define `__CET__` solely based on the flag anymore. This patch moves the definition of the `__CET__` macro into X86 target hook, so only X86 targets with the `-fcf-protection` flag would enable the `__CET__` macro. See llvm/llvm-project#109784 and llvm/llvm-project#112477 for the adoption of `-fcf-protection` flag for RISC-V targets.
The `-fcf-protection` flag is now also used to enable CFI features for the RISC-V target, so it's not suitable to define `__CET__` solely based on the flag anymore. This patch moves the definition of the `__CET__` macro into X86 target hook, so only X86 targets with the `-fcf-protection` flag would enable the `__CET__` macro. See llvm#109784 and llvm#112477 for the adoption of `-fcf-protection` flag for RISC-V targets.
This patches enables the support of
-fcf-protection=return
on RISC-V, and add a string attribute "hw-shadow-stack" to every function if the option is set on RISC-V