[PATCH] D96007: [AArch64] Enable stack clash protection for AArch64 linux in clang
Oliver Stannard (Linaro) via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 4 02:31:15 PST 2021
ostannard created this revision.
ostannard added reviewers: serge-sans-paille, jnspaulsson, bzEq, tnfchris.
Herald added subscribers: danielkiss, kristof.beyls.
ostannard requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
This allows the -fstack-clash-protection option to be used for AArch64
Linux.
Linux for AArch64 uses a 64k stack guard, instead of the 4k guard
assumed by the backend, so we set the stack-probe-size function
attribute to configure this, and the -mstack-probe-size= option can be
used to override this.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D96007
Files:
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/test/CodeGen/stack-clash-protection.c
clang/test/Driver/stack-clash-protection.c
Index: clang/test/Driver/stack-clash-protection.c
===================================================================
--- clang/test/Driver/stack-clash-protection.c
+++ clang/test/Driver/stack-clash-protection.c
@@ -21,12 +21,20 @@
// SCP-ll-win64-NOT: attributes {{.*}} "probe-stack"="inline-asm"
// SCP-ll-win64: argument unused during compilation: '-fstack-clash-protection'
+// RUN: %clang -target aarch64-unknown-linux -fstack-clash-protection -S -emit-llvm -o- %s | FileCheck %s -check-prefix=SCP-aarch64
+// SCP-aarch64: attributes {{.*}} "probe-stack"="inline-asm"
+// SCP-aarch64-SAME: "stack-probe-size"="65536"
+
int foo(int c) {
int r;
__asm__("sub %0, %%rsp"
:
: "rm"(c)
+#ifdef __aarch64__
+ : "sp");
+#else
: "rsp");
+#endif
__asm__("mov %%rsp, %0"
: "=rm"(r)::);
return r;
Index: clang/test/CodeGen/stack-clash-protection.c
===================================================================
--- clang/test/CodeGen/stack-clash-protection.c
+++ clang/test/CodeGen/stack-clash-protection.c
@@ -3,6 +3,9 @@
// 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 aarch64-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection | FileCheck %s
+// RUN: %clang_cc1 -triple aarch64_be-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection | FileCheck %s
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection -mstack-probe-size=65536 | FileCheck %s --check-prefix=CHECK --check-prefix=LARGE-GUARD
// CHECK: define{{.*}} void @large_stack() #[[A:.*]] {
void large_stack() {
@@ -23,3 +26,5 @@
}
// CHECK: attributes #[[A]] = {{.*}} "probe-stack"="inline-asm"
+// LARGE-GUARD-SAME: "stack-probe-size"="65536"
+// CHECK-NOT: "stack-probe-size"
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3091,12 +3091,20 @@
return;
if (!EffectiveTriple.isX86() && !EffectiveTriple.isSystemZ() &&
- !EffectiveTriple.isPPC64())
+ !EffectiveTriple.isPPC64() && !EffectiveTriple.isAArch64())
return;
if (Args.hasFlag(options::OPT_fstack_clash_protection,
- options::OPT_fno_stack_clash_protection, false))
+ options::OPT_fno_stack_clash_protection, false)) {
CmdArgs.push_back("-fstack-clash-protection");
+
+ if (Args.hasArg(options::OPT_mstack_probe_size)) {
+ StringRef Size = Args.getLastArgValue(options::OPT_mstack_probe_size);
+ CmdArgs.push_back(Args.MakeArgString("-mstack-probe-size=" + Size));
+ } else if (EffectiveTriple.isAArch64()) {
+ CmdArgs.push_back("-mstack-probe-size=65536");
+ }
+ }
}
static void RenderTrivialAutoVarInitOptions(const Driver &D,
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -1664,8 +1664,12 @@
if (CodeGenOpts.UnwindTables)
B.addAttribute(llvm::Attribute::UWTable);
- if (CodeGenOpts.StackClashProtector)
+ if (CodeGenOpts.StackClashProtector) {
B.addAttribute("probe-stack", "inline-asm");
+ if (CodeGenOpts.StackProbeSize != 4096)
+ B.addAttribute("stack-probe-size",
+ llvm::utostr(CodeGenOpts.StackProbeSize));
+ }
if (!hasUnwindExceptions(LangOpts))
B.addAttribute(llvm::Attribute::NoUnwind);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96007.321355.patch
Type: text/x-patch
Size: 3823 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210204/6f399f7f/attachment.bin>
More information about the llvm-commits
mailing list