When compiling with -mbackchain -fstack-clash-protection currently no
probes are emitted.  This patch adjusts the "allocate_stack" expander
to call anti_adjust_stack_and_probe_stack_clash when needed. In order
to do this I had to export that function from explow.c.

Ok for mainline?

2020-05-08  Andreas Krebbel  <kreb...@linux.ibm.com>

        * explow.c (anti_adjust_stack_and_probe_stack_clash): Remove
        prototype. Remove static.
        * explow.h (anti_adjust_stack_and_probe_stack_clash): Add
        prototype.
        * config/s390/s390.md ("allocate_stack"): Call
        anti_adjust_stack_and_probe_stack_clash when stack clash
        protection is enabled.

gcc/testsuite/ChangeLog:

2020-05-08  Andreas Krebbel  <kreb...@linux.ibm.com>

        * gcc.target/s390/stack-clash-3.c: New test.
---
 gcc/config/s390/s390.md                       |  7 ++++++-
 gcc/explow.c                                  |  3 +--
 gcc/explow.h                                  |  4 ++++
 gcc/testsuite/gcc.target/s390/stack-clash-3.c | 17 +++++++++++++++++
 4 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/s390/stack-clash-3.c

diff --git a/gcc/config/s390/s390.md b/gcc/config/s390/s390.md
index cf53ef1b791..908de587e17 100644
--- a/gcc/config/s390/s390.md
+++ b/gcc/config/s390/s390.md
@@ -11007,7 +11007,12 @@
   rtx temp = gen_reg_rtx (Pmode);
 
   emit_move_insn (temp, s390_back_chain_rtx ());
-  anti_adjust_stack (operands[1]);
+
+  if (flag_stack_clash_protection)
+    anti_adjust_stack_and_probe_stack_clash (operands[1]);
+  else
+    anti_adjust_stack (operands[1]);
+
   emit_move_insn (s390_back_chain_rtx (), temp);
 
   emit_move_insn (operands[0], virtual_stack_dynamic_rtx);
diff --git a/gcc/explow.c b/gcc/explow.c
index b838f035870..15c9cfb0318 100644
--- a/gcc/explow.c
+++ b/gcc/explow.c
@@ -43,7 +43,6 @@ along with GCC; see the file COPYING3.  If not see
 #include "output.h"
 
 static rtx break_out_memory_refs (rtx);
-static void anti_adjust_stack_and_probe_stack_clash (rtx);
 
 
 /* Truncate and perhaps sign-extend C as appropriate for MODE.  */
@@ -1948,7 +1947,7 @@ emit_stack_clash_protection_probe_loop_end (rtx loop_lab, 
rtx end_loop,
        allocate/probe beyond that because this probing style does not
        guarantee signal handling capability if the guard is hit.  */
 
-static void
+void
 anti_adjust_stack_and_probe_stack_clash (rtx size)
 {
   /* First ensure SIZE is Pmode.  */
diff --git a/gcc/explow.h b/gcc/explow.h
index cc44bf8520a..0df8c62b82a 100644
--- a/gcc/explow.h
+++ b/gcc/explow.h
@@ -69,6 +69,10 @@ extern void anti_adjust_stack (rtx);
 /* Add some bytes to the stack while probing it.  An rtx says how many. */
 extern void anti_adjust_stack_and_probe (rtx, bool);
 
+/* Add some bytes to the stack while probing it.  An rtx says how
+   many.  Add additional probes to prevent stack clashing attacks.  */
+extern void anti_adjust_stack_and_probe_stack_clash (rtx);
+
 /* Support for building allocation/probing loops for stack-clash
    protection of dyamically allocated stack space.  */
 extern void compute_stack_clash_protection_loop_data (rtx *, rtx *, rtx *,
diff --git a/gcc/testsuite/gcc.target/s390/stack-clash-3.c 
b/gcc/testsuite/gcc.target/s390/stack-clash-3.c
new file mode 100644
index 00000000000..929d3fbb365
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/stack-clash-3.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=z900 -fstack-clash-protection -mbackchain" } */
+
+extern void bar (char *);
+
+void
+foo ()
+{
+  char * mem = __builtin_alloca (20000);
+  bar (mem);
+}
+
+/* For alloca a common code routine emits the probes.  Make sure the
+   "probe_stack" expander is used in that case. We want to use mem
+   compares instead of stores.  */
+/* { dg-final { scan-assembler-times "cg\t" 5 { target lp64 } } } */
+/* { dg-final { scan-assembler-times "c\t" 5 { target { ! lp64 } } } } */
-- 
2.17.1

Reply via email to