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.hu...@sifive.com> Date: Sun, 13 Oct 2024 15:11:06 +0800 Subject: [PATCH 1/8] [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.hu...@sifive.com> Date: Wed, 23 Oct 2024 14:42:35 +0800 Subject: [PATCH 2/8] [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 42132c2460333358f6c1aaa646266847a3b8c854 Mon Sep 17 00:00:00 2001 From: Jesse Huang <jesse.hu...@sifive.com> Date: Thu, 24 Oct 2024 15:39:07 +0800 Subject: [PATCH 3/8] [Clang][RISCV] Add function attribute in RISC-V specific code --- clang/lib/CodeGen/CodeGenFunction.cpp | 5 ----- clang/lib/CodeGen/Targets/RISCV.cpp | 7 +++++-- 2 files changed, 5 insertions(+), 7 deletions(-) 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..b04e436c665f52 100644 --- a/clang/lib/CodeGen/Targets/RISCV.cpp +++ b/clang/lib/CodeGen/Targets/RISCV.cpp @@ -594,6 +594,11 @@ class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { const auto *FD = dyn_cast_or_null<FunctionDecl>(D); if (!FD) return; + auto *Fn = cast<llvm::Function>(GV); + + if (CGM.getCodeGenOpts().CFProtectionReturn) + Fn->addFnAttr("hw-shadow-stack"); + const auto *Attr = FD->getAttr<RISCVInterruptAttr>(); if (!Attr) return; @@ -604,8 +609,6 @@ class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { case RISCVInterruptAttr::machine: Kind = "machine"; break; } - auto *Fn = cast<llvm::Function>(GV); - Fn->addFnAttr("interrupt", Kind); } }; >From 2b6bdc5e95043a0f481314c0c2e5441804addf06 Mon Sep 17 00:00:00 2001 From: Jesse Huang <jesse.hu...@sifive.com> Date: Fri, 25 Oct 2024 14:13:45 +0800 Subject: [PATCH 4/8] [Clang] Add test for hw-shadodw-stack --- clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c diff --git a/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c b/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c new file mode 100644 index 00000000000000..4353d27d6d7725 --- /dev/null +++ b/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop -emit-llvm -o - %s -fcf-protection=return | FileCheck -check-prefix=NOTIGNORELISTED %s +// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop -emit-llvm -o - %s | FileCheck -check-prefix=IGNORELISTED %s + +int foo(int *a) { return *a; } + +// CHECK: define i32 @foo(ptr %a) + +// IGNORELISTED-NOT: attributes {{.*}}"hw-shadow-stack"{{.*}} +// NOTIGNORELISTED: attributes {{.*}}"hw-shadow-stack"{{.*}} >From e16c341eee3b78eebbcc22553f355403d92b7f10 Mon Sep 17 00:00:00 2001 From: Jesse Huang <jesse.hu...@sifive.com> Date: Sat, 26 Oct 2024 01:33:15 +0800 Subject: [PATCH 5/8] !fixup update test --- clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c b/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c index 4353d27d6d7725..14cf6268c28516 100644 --- a/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c +++ b/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c @@ -1,9 +1,7 @@ -// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop -emit-llvm -o - %s -fcf-protection=return | FileCheck -check-prefix=NOTIGNORELISTED %s -// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop -emit-llvm -o - %s | FileCheck -check-prefix=IGNORELISTED %s +// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop -emit-llvm -o - %s -fcf-protection=return | FileCheck %s +// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop -emit-llvm -o - %s | FileCheck -check-prefix=NOSHADOWSTACK %s int foo(int *a) { return *a; } -// CHECK: define i32 @foo(ptr %a) - -// IGNORELISTED-NOT: attributes {{.*}}"hw-shadow-stack"{{.*}} -// NOTIGNORELISTED: attributes {{.*}}"hw-shadow-stack"{{.*}} +// SHADOWSTACK-NOT: attributes {{.*}}"hw-shadow-stack"{{.*}} +// CHECK: attributes {{.*}}"hw-shadow-stack"{{.*}} >From 6fe0e7bec50291fd565206d061d8917fd8fde55f Mon Sep 17 00:00:00 2001 From: Jesse Huang <jesse.hu...@sifive.com> Date: Sat, 26 Oct 2024 16:09:51 +0800 Subject: [PATCH 6/8] ! fixup test --- clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c b/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c index 14cf6268c28516..75bcbbcd65a2b0 100644 --- a/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c +++ b/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c @@ -3,5 +3,5 @@ int foo(int *a) { return *a; } -// SHADOWSTACK-NOT: attributes {{.*}}"hw-shadow-stack"{{.*}} // CHECK: attributes {{.*}}"hw-shadow-stack"{{.*}} +// NOSHADOWSTACK-NOT: attributes {{.*}}"hw-shadow-stack"{{.*}} >From d317e864f3fa4809806174b535ba1157a6041f05 Mon Sep 17 00:00:00 2001 From: Jesse Huang <jesse.hu...@sifive.com> Date: Mon, 28 Oct 2024 13:38:03 +0800 Subject: [PATCH 7/8] !fixup add tests for rv32 --- clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c b/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c index 75bcbbcd65a2b0..8e65165ab539bf 100644 --- a/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c +++ b/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c @@ -1,5 +1,7 @@ // RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop -emit-llvm -o - %s -fcf-protection=return | FileCheck %s // RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop -emit-llvm -o - %s | FileCheck -check-prefix=NOSHADOWSTACK %s +// RUN: %clang_cc1 -triple riscv32-linux-unknown -target-feature +zimop -emit-llvm -o - %s -fcf-protection=return | FileCheck %s +// RUN: %clang_cc1 -triple riscv32-linux-unknown -target-feature +zimop -emit-llvm -o - %s | FileCheck -check-prefix=NOSHADOWSTACK %s int foo(int *a) { return *a; } >From 5fbb604b3c6f583615cb6be9e100dec72aadd98d Mon Sep 17 00:00:00 2001 From: Jesse Huang <jesse.hu...@sifive.com> Date: Mon, 28 Oct 2024 13:56:38 +0800 Subject: [PATCH 8/8] !fixup target triples --- clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c b/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c index 8e65165ab539bf..80d20b03f9a913 100644 --- a/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c +++ b/clang/test/CodeGen/RISCV/attr-hw-shadow-stack.c @@ -1,7 +1,7 @@ -// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop -emit-llvm -o - %s -fcf-protection=return | FileCheck %s -// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop -emit-llvm -o - %s | FileCheck -check-prefix=NOSHADOWSTACK %s -// RUN: %clang_cc1 -triple riscv32-linux-unknown -target-feature +zimop -emit-llvm -o - %s -fcf-protection=return | FileCheck %s -// RUN: %clang_cc1 -triple riscv32-linux-unknown -target-feature +zimop -emit-llvm -o - %s | FileCheck -check-prefix=NOSHADOWSTACK %s +// RUN: %clang_cc1 -triple riscv64-unknown-linux -target-feature +zimop -emit-llvm -o - %s -fcf-protection=return | FileCheck %s +// RUN: %clang_cc1 -triple riscv64-unknown-linux -target-feature +zimop -emit-llvm -o - %s | FileCheck -check-prefix=NOSHADOWSTACK %s +// RUN: %clang_cc1 -triple riscv32-unknown-linux -target-feature +zimop -emit-llvm -o - %s -fcf-protection=return | FileCheck %s +// RUN: %clang_cc1 -triple riscv32-unknown-linux -target-feature +zimop -emit-llvm -o - %s | FileCheck -check-prefix=NOSHADOWSTACK %s int foo(int *a) { return *a; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits