[PATCH] D96007: [AArch64] Enable stack clash protection for AArch64 linux in clang

Oliver Stannard (Linaro) via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 5 02:38:27 PST 2021


ostannard updated this revision to Diff 321689.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D96007/new/

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.321689.patch
Type: text/x-patch
Size: 3823 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210205/e3b6f6b0/attachment.bin>


More information about the cfe-commits mailing list