From 6dcda2f080df9592a7beaf7e29aa94c89cbffe7b Mon Sep 17 00:00:00 2001 From: liyunfei Date: Fri, 21 Jun 2024 09:21:49 +0800 Subject: [PATCH] Revert "Revert "Support stack clash protection"" This reverts commit 7be2e4982661c3184196af8c1ca31ad55b8f3496. --- ...h-protection-options-to-LLVM-Backend.patch | 99 +++++++++++++++++++ clang.spec | 6 +- 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 0021-Backport-clang-AArch64-Pass-down-stack-clash-protection-options-to-LLVM-Backend.patch diff --git a/0021-Backport-clang-AArch64-Pass-down-stack-clash-protection-options-to-LLVM-Backend.patch b/0021-Backport-clang-AArch64-Pass-down-stack-clash-protection-options-to-LLVM-Backend.patch new file mode 100644 index 0000000..509486b --- /dev/null +++ b/0021-Backport-clang-AArch64-Pass-down-stack-clash-protection-options-to-LLVM-Backend.patch @@ -0,0 +1,99 @@ +From da192e499e442fc433ef8ab2290cf0021e214c85 Mon Sep 17 00:00:00 2001 +From: Momchil Velikov +Date: Thu, 30 Nov 2023 11:18:02 +0000 +Subject: [PATCH 2/7] [clang][AArch64] Pass down stack clash protection options + to LLVM/Backend (#68993) + +--- + clang/lib/CodeGen/CodeGenModule.cpp | 13 +++++++++++++ + clang/lib/Driver/ToolChains/Clang.cpp | 2 +- + clang/test/CodeGen/stack-clash-protection.c | 18 +++++++++++------- + 3 files changed, 25 insertions(+), 8 deletions(-) + +diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp +index f09d1129b128..eabc4aabea06 100644 +--- a/clang/lib/CodeGen/CodeGenModule.cpp ++++ b/clang/lib/CodeGen/CodeGenModule.cpp +@@ -1067,6 +1067,15 @@ void CodeGenModule::Release() { + "sign-return-address-with-bkey", 1); + } + ++ if (CodeGenOpts.StackClashProtector) ++ getModule().addModuleFlag( ++ llvm::Module::Override, "probe-stack", ++ llvm::MDString::get(TheModule.getContext(), "inline-asm")); ++ ++ if (CodeGenOpts.StackProbeSize && CodeGenOpts.StackProbeSize != 4096) ++ getModule().addModuleFlag(llvm::Module::Min, "stack-probe-size", ++ CodeGenOpts.StackProbeSize); ++ + if (!CodeGenOpts.MemoryProfileOutput.empty()) { + llvm::LLVMContext &Ctx = TheModule.getContext(); + getModule().addModuleFlag( +@@ -2261,6 +2270,10 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, + if (CodeGenOpts.StackClashProtector) + B.addAttribute("probe-stack", "inline-asm"); + ++ if (CodeGenOpts.StackProbeSize && CodeGenOpts.StackProbeSize != 4096) ++ B.addAttribute("stack-probe-size", ++ std::to_string(CodeGenOpts.StackProbeSize)); ++ + if (!hasUnwindExceptions(LangOpts)) + B.addAttribute(llvm::Attribute::NoUnwind); + +diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp +index 6b5930990f11..25627c72f858 100644 +--- a/clang/lib/Driver/ToolChains/Clang.cpp ++++ b/clang/lib/Driver/ToolChains/Clang.cpp +@@ -3473,7 +3473,7 @@ static void RenderSCPOptions(const ToolChain &TC, const ArgList &Args, + return; + + if (!EffectiveTriple.isX86() && !EffectiveTriple.isSystemZ() && +- !EffectiveTriple.isPPC64()) ++ !EffectiveTriple.isPPC64() && !EffectiveTriple.isAArch64()) + return; + + Args.addOptInFlag(CmdArgs, options::OPT_fstack_clash_protection, +diff --git a/clang/test/CodeGen/stack-clash-protection.c b/clang/test/CodeGen/stack-clash-protection.c +index 67571f5cdb2c..dab9ee768c28 100644 +--- a/clang/test/CodeGen/stack-clash-protection.c ++++ b/clang/test/CodeGen/stack-clash-protection.c +@@ -1,8 +1,9 @@ + // Check the correct function attributes are generated +-// RUN: %clang_cc1 -triple x86_64-linux -O0 -S -emit-llvm -o- %s -fstack-clash-protection | FileCheck %s +-// RUN: %clang_cc1 -triple s390x-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection | FileCheck %s +-// RUN: %clang_cc1 -triple powerpc64le-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection | FileCheck %s +-// RUN: %clang_cc1 -triple powerpc64-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection | FileCheck %s ++// RUN: %clang_cc1 -triple x86_64-linux -O0 -S -emit-llvm -o- %s -fstack-clash-protection -mstack-probe-size=8192 | FileCheck %s ++// RUN: %clang_cc1 -triple s390x-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection -mstack-probe-size=8192 | FileCheck %s ++// RUN: %clang_cc1 -triple powerpc64le-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection -mstack-probe-size=8192 | FileCheck %s ++// RUN: %clang_cc1 -triple powerpc64-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection -mstack-probe-size=8192 | FileCheck %s ++// RUN: %clang_cc1 -triple aarch64-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection -mstack-probe-size=8192 | FileCheck %s + + // CHECK: define{{.*}} void @large_stack() #[[A:.*]] { + void large_stack(void) { +@@ -11,15 +12,18 @@ void large_stack(void) { + stack[i] = i; + } + +-// CHECK: define{{.*}} void @vla({{.*}}) #[[A:.*]] { ++// CHECK: define{{.*}} void @vla({{.*}}) #[[A]] { + void vla(int n) { + volatile int vla[n]; + __builtin_memset(&vla[0], 0, 1); + } + +-// CHECK: define{{.*}} void @builtin_alloca({{.*}}) #[[A:.*]] { ++// CHECK: define{{.*}} void @builtin_alloca({{.*}}) #[[A]] { + void builtin_alloca(int n) { + volatile void *mem = __builtin_alloca(n); + } + +-// CHECK: attributes #[[A]] = {{.*}} "probe-stack"="inline-asm" ++// CHECK: attributes #[[A]] = {{.*}}"probe-stack"="inline-asm" {{.*}}"stack-probe-size"="8192" ++ ++// CHECK: !{i32 4, !"probe-stack", !"inline-asm"} ++// CHECK: !{i32 8, !"stack-probe-size", i32 8192} +-- +2.42.0.windows.2 + diff --git a/clang.spec b/clang.spec index 1ab334c..8abb379 100644 --- a/clang.spec +++ b/clang.spec @@ -43,7 +43,7 @@ Name: %{pkg_name} Version: %{clang_version} -Release: 24 +Release: 25 Summary: A C language family front-end for LLVM License: NCSA @@ -72,6 +72,7 @@ Patch17: 0017-fix-for-missing-DENABLE_AUTOTUNER.patch Patch18: 0018-backport-Clang-Fix-build-with-GCC-14-on-ARM-78704.patch Patch19: 0019-AArch64-Support-HiSilicon-s-HIP09-Processor.patch Patch20: 0020-Backport-LoongArch-fix-and-add-some-new-support.patch +Patch21: 0021-Backport-clang-AArch64-Pass-down-stack-clash-protection-options-to-LLVM-Backend.patch # Patches for clang-tools-extra # See https://reviews.llvm.org/D120301 @@ -416,6 +417,9 @@ LD_LIBRARY_PATH=%{buildroot}/%{install_libdir} %{__ninja} check-all -C ./_build %{install_bindir}/git-clang-format %changelog +* Mon Sep 23 2024 rickyleung - 17.0.6-25 +- Backport the patch to support stack clash protection + * Mon Sep 23 2024 zhanglimin - 17.0.6-24 - [LoongArch] Backport some new support -- Gitee