Blame SOURCES/gcc48-rh1469697-16.patch

22033d
commit 5d7a77ede3e91948ee125bd82533d7e692543fff
22033d
Author: Jeff Law <law@redhat.com>
22033d
Date:   Mon Oct 2 13:43:01 2017 -0600
22033d
22033d
    aarch64 support
22033d
22033d
diff --git a/gcc/config/aarch64/.aarch64.c.rej.swp b/gcc/config/aarch64/.aarch64.c.rej.swp
22033d
new file mode 100644
22033d
index 00000000000..b899e21b855
22033d
Binary files /dev/null and b/gcc/config/aarch64/.aarch64.c.rej.swp differ
22033d
diff --git a/gcc/config/aarch64/aarch64.c b/gcc/config/aarch64/aarch64.c
22033d
index cadf193cfcf..e08632ffa88 100644
22033d
--- a/gcc/config/aarch64/aarch64.c
22033d
+++ b/gcc/config/aarch64/aarch64.c
22033d
@@ -45,6 +45,8 @@
22033d
 #include "gimple.h"
22033d
 #include "optabs.h"
22033d
 #include "dwarf2.h"
22033d
+#include "params.h"
22033d
+#include "dumpfile.h"
22033d
 
22033d
 /* Classifies an address.
22033d
 
22033d
@@ -1696,7 +1698,14 @@ aarch64_output_probe_stack_range (rtx reg1, rtx reg2)
22033d
   output_asm_insn ("sub\t%0, %0, %1", xops);
22033d
 
22033d
   /* Probe at TEST_ADDR.  */
22033d
-  output_asm_insn ("str\txzr, [%0]", xops);
22033d
+  if (flag_stack_clash_protection)
22033d
+    {
22033d
+      gcc_assert (xops[0] == stack_pointer_rtx);
22033d
+      xops[1] = GEN_INT (PROBE_INTERVAL - 8);
22033d
+      output_asm_insn ("str\txzr, [%0, %1]", xops);
22033d
+    }
22033d
+  else
22033d
+    output_asm_insn ("str\txzr, [%0]", xops);
22033d
 
22033d
   /* Test if TEST_ADDR == LAST_ADDR.  */
22033d
   xops[1] = reg2;
22033d
@@ -2001,6 +2010,123 @@ aarch64_save_or_restore_callee_save_registers (HOST_WIDE_INT offset,
22033d
 				base_rtx, cfi_ops);
22033d
 }
22033d
 
22033d
+/* Allocate SIZE bytes of stack space using SCRATCH_REG as a scratch
22033d
+   register.  */
22033d
+
22033d
+static void
22033d
+aarch64_allocate_and_probe_stack_space (int scratchreg, HOST_WIDE_INT size)
22033d
+{
22033d
+  HOST_WIDE_INT probe_interval
22033d
+    = 1 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL);
22033d
+  HOST_WIDE_INT guard_size
22033d
+    = 1 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE);
22033d
+  HOST_WIDE_INT guard_used_by_caller = 1024;
22033d
+
22033d
+  /* SIZE should be large enough to require probing here.  ie, it
22033d
+     must be larger than GUARD_SIZE - GUARD_USED_BY_CALLER.
22033d
+
22033d
+     We can allocate GUARD_SIZE - GUARD_USED_BY_CALLER as a single chunk
22033d
+     without any probing.  */
22033d
+  gcc_assert (size >= guard_size - guard_used_by_caller);
22033d
+  aarch64_sub_sp (scratchreg, guard_size - guard_used_by_caller, true);
22033d
+  HOST_WIDE_INT orig_size = size;
22033d
+  size -= (guard_size - guard_used_by_caller);
22033d
+
22033d
+  HOST_WIDE_INT rounded_size = size & -probe_interval;
22033d
+  HOST_WIDE_INT residual = size - rounded_size;
22033d
+
22033d
+  /* We can handle a small number of allocations/probes inline.  Otherwise
22033d
+     punt to a loop.  */
22033d
+  if (rounded_size && rounded_size <= 4 * probe_interval)
22033d
+    {
22033d
+      /* We don't use aarch64_sub_sp here because we don't want to
22033d
+	 repeatedly load SCRATCHREG.  */
22033d
+      rtx scratch_rtx = gen_rtx_REG (Pmode, scratchreg);
22033d
+      if (probe_interval > ARITH_FACTOR)
22033d
+	emit_move_insn (scratch_rtx, GEN_INT (-probe_interval));
22033d
+      else
22033d
+	scratch_rtx = GEN_INT (-probe_interval);
22033d
+
22033d
+      for (HOST_WIDE_INT i = 0; i < rounded_size; i += probe_interval)
22033d
+	{
22033d
+	  rtx insn = emit_insn (gen_add2_insn (stack_pointer_rtx, scratch_rtx));
22033d
+          add_reg_note (insn, REG_STACK_CHECK, const0_rtx);
22033d
+
22033d
+	  if (probe_interval > ARITH_FACTOR)
22033d
+	    {
22033d
+	      RTX_FRAME_RELATED_P (insn) = 1;
22033d
+	      rtx adj = plus_constant (Pmode, stack_pointer_rtx, -probe_interval);
22033d
+	      add_reg_note (insn, REG_CFA_ADJUST_CFA,
22033d
+			    gen_rtx_SET (VOIDmode, stack_pointer_rtx, adj));
22033d
+	    }
22033d
+
22033d
+	  emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
22033d
+					   (probe_interval
22033d
+					    - GET_MODE_SIZE (word_mode))));
22033d
+	  emit_insn (gen_blockage ());
22033d
+	}
22033d
+      dump_stack_clash_frame_info (PROBE_INLINE, size != rounded_size);
22033d
+    }
22033d
+  else if (rounded_size)
22033d
+    {
22033d
+      /* Compute the ending address.  */
22033d
+      rtx temp = gen_rtx_REG (word_mode, scratchreg);
22033d
+      emit_move_insn (temp, GEN_INT (-rounded_size));
22033d
+      rtx insn = emit_insn (gen_add3_insn (temp, stack_pointer_rtx, temp));
22033d
+
22033d
+      /* For the initial allocation, we don't have a frame pointer
22033d
+	 set up, so we always need CFI notes.  If we're doing the
22033d
+	 final allocation, then we may have a frame pointer, in which
22033d
+	 case it is the CFA, otherwise we need CFI notes.
22033d
+
22033d
+	 We can determine which allocation we are doing by looking at
22033d
+	 the temporary register.  IP0 is the initial allocation, IP1
22033d
+	 is the final allocation.  */
22033d
+      if (scratchreg == IP0_REGNUM || !frame_pointer_needed)
22033d
+	{
22033d
+	  /* We want the CFA independent of the stack pointer for the
22033d
+	     duration of the loop.  */
22033d
+	  add_reg_note (insn, REG_CFA_DEF_CFA,
22033d
+			plus_constant (Pmode, temp,
22033d
+				       (rounded_size + (orig_size - size))));
22033d
+	  RTX_FRAME_RELATED_P (insn) = 1;
22033d
+	}
22033d
+
22033d
+      /* This allocates and probes the stack.
22033d
+
22033d
+	 It also probes at a 4k interval regardless of the value of
22033d
+	 PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL.  */
22033d
+      insn = emit_insn (gen_probe_stack_range (stack_pointer_rtx,
22033d
+					       stack_pointer_rtx, temp));
22033d
+
22033d
+      /* Now reset the CFA register if needed.  */
22033d
+      if (scratchreg == IP0_REGNUM || !frame_pointer_needed)
22033d
+	{
22033d
+	  add_reg_note (insn, REG_CFA_DEF_CFA,
22033d
+			plus_constant (Pmode, stack_pointer_rtx,
22033d
+				       (rounded_size + (orig_size - size))));
22033d
+	  RTX_FRAME_RELATED_P (insn) = 1;
22033d
+	}
22033d
+
22033d
+      emit_insn (gen_blockage ());
22033d
+      dump_stack_clash_frame_info (PROBE_LOOP, size != rounded_size);
22033d
+    }
22033d
+  else
22033d
+    dump_stack_clash_frame_info (PROBE_INLINE, size != rounded_size);
22033d
+
22033d
+  /* Handle any residuals.
22033d
+     Note that any residual must be probed.  */
22033d
+  if (residual)
22033d
+    {
22033d
+      aarch64_sub_sp (scratchreg, residual, true);
22033d
+      add_reg_note (get_last_insn (), REG_STACK_CHECK, const0_rtx);
22033d
+      emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
22033d
+				       (residual - GET_MODE_SIZE (word_mode))));
22033d
+      emit_insn (gen_blockage ());
22033d
+    }
22033d
+  return;
22033d
+}
22033d
+
22033d
 /* AArch64 stack frames generated by this compiler look like:
22033d
 
22033d
 	+-------------------------------+
22033d
@@ -2073,6 +2199,44 @@ aarch64_expand_prologue (void)
22033d
 	       - original_frame_size
22033d
 	       - cfun->machine->frame.saved_regs_size);
22033d
 
22033d
+  /* We do not fully protect aarch64 against stack clash style attacks
22033d
+     as doing so would be prohibitively expensive with less utility over
22033d
+     time as newer compilers are deployed.
22033d
+
22033d
+     We assume the guard is at least 64k.  Furthermore, we assume that
22033d
+     the caller has not pushed the stack pointer more than 1k into
22033d
+     the guard.  A caller that pushes the stack pointer than 1k into
22033d
+     the guard is considered invalid.
22033d
+
22033d
+     Note that the caller's ability to push the stack pointer into the
22033d
+     guard is a function of the number and size of outgoing arguments and/or
22033d
+     dynamic stack allocations due to the mandatory save of the link register
22033d
+     in the caller's frame.
22033d
+
22033d
+     With those assumptions the callee can allocate up to 63k of stack
22033d
+     space without probing.
22033d
+
22033d
+     When probing is needed, we emit a probe at the start of the prologue
22033d
+     and every PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes thereafter.
22033d
+
22033d
+     We have to track how much space has been allocated, but we do not
22033d
+     track stores into the stack as implicit probes except for the
22033d
+     fp/lr store.  */
22033d
+  HOST_WIDE_INT guard_size
22033d
+    = 1 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE);
22033d
+  HOST_WIDE_INT guard_used_by_caller = 1024;
22033d
+  HOST_WIDE_INT final_adjust = crtl->outgoing_args_size;
22033d
+  HOST_WIDE_INT initial_adjust = frame_size;
22033d
+
22033d
+  if (flag_stack_clash_protection)
22033d
+    {
22033d
+      if (initial_adjust == 0)
22033d
+	dump_stack_clash_frame_info (NO_PROBE_NO_FRAME, false);
22033d
+      else if (offset < guard_size - guard_used_by_caller
22033d
+	       && final_adjust < guard_size - guard_used_by_caller)
22033d
+	dump_stack_clash_frame_info (NO_PROBE_SMALL_FRAME, true);
22033d
+    }
22033d
+
22033d
   /* Store pairs and load pairs have a range only -512 to 504.  */
22033d
   if (offset >= 512)
22033d
     {
22033d
@@ -2089,7 +2253,10 @@ aarch64_expand_prologue (void)
22033d
       frame_size -= (offset + crtl->outgoing_args_size);
22033d
       fp_offset = 0;
22033d
 
22033d
-      if (frame_size >= 0x1000000)
22033d
+      if (flag_stack_clash_protection
22033d
+	  && frame_size >= guard_size - guard_used_by_caller)
22033d
+	aarch64_allocate_and_probe_stack_space (IP0_REGNUM, frame_size);
22033d
+      else if (frame_size >= 0x1000000)
22033d
 	{
22033d
 	  rtx op0 = gen_rtx_REG (Pmode, IP0_REGNUM);
22033d
 	  emit_move_insn (op0, GEN_INT (-frame_size));
22033d
@@ -2206,10 +2373,30 @@ aarch64_expand_prologue (void)
22033d
     {
22033d
       if (crtl->outgoing_args_size > 0)
22033d
 	{
22033d
-	  insn = emit_insn (gen_add2_insn
22033d
-			    (stack_pointer_rtx,
22033d
-			     GEN_INT (- crtl->outgoing_args_size)));
22033d
-	  RTX_FRAME_RELATED_P (insn) = 1;
22033d
+	  if (flag_stack_clash_protection)
22033d
+	    {
22033d
+	      /* First probe if the final adjustment is larger than the
22033d
+		 guard size less the amount of guard reserved for use by
22033d
+		 the caller's outgoing args.  */
22033d
+	      if (final_adjust >= guard_size - guard_used_by_caller)
22033d
+		aarch64_allocate_and_probe_stack_space (IP1_REGNUM,
22033d
+						        final_adjust);
22033d
+	      else
22033d
+		aarch64_sub_sp (IP1_REGNUM, final_adjust, !frame_pointer_needed);
22033d
+
22033d
+	      /* We must also probe if the final adjustment is larger than the
22033d
+		 guard that is assumed used by the caller.  This may be
22033d
+		 sub-optimal.  */
22033d
+	      if (final_adjust >= guard_used_by_caller)
22033d
+		{
22033d
+		  if (dump_file)
22033d
+		    fprintf (dump_file,
22033d
+			     "Stack clash aarch64 large outgoing arg, probing\n");
22033d
+		  emit_stack_probe (stack_pointer_rtx);
22033d
+		}
22033d
+	    }
22033d
+	  else
22033d
+	    aarch64_sub_sp (IP1_REGNUM, final_adjust, !frame_pointer_needed);
22033d
 	}
22033d
     }
22033d
 }
22033d
@@ -5088,6 +5275,12 @@ aarch64_override_options (void)
22033d
 #endif
22033d
     }
22033d
 
22033d
+  /* We assume the guard page is 64k.  */
22033d
+  maybe_set_param_value (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE,
22033d
+			 16,
22033d
+			 global_options.x_param_values,
22033d
+			 global_options_set.x_param_values);
22033d
+
22033d
   aarch64_override_options_after_change ();
22033d
 }
22033d
 
22033d
@@ -8161,6 +8354,28 @@ aarch64_vectorize_vec_perm_const_ok (enum machine_mode vmode,
22033d
   return ret;
22033d
 }
22033d
 
22033d
+/* It has been decided that to allow up to 1kb of outgoing argument
22033d
+   space to be allocated w/o probing.  If more than 1kb of outgoing
22033d
+   argment space is allocated, then it must be probed and the last
22033d
+   probe must occur no more than 1kbyte away from the end of the
22033d
+   allocated space.
22033d
+
22033d
+   This implies that the residual part of an alloca allocation may
22033d
+   need probing in cases where the generic code might not otherwise
22033d
+   think a probe is needed.
22033d
+
22033d
+   This target hook returns TRUE when allocating RESIDUAL bytes of
22033d
+   alloca space requires an additional probe, otherwise FALSE is
22033d
+   returned.  */
22033d
+
22033d
+static bool
22033d
+aarch64_stack_clash_protection_final_dynamic_probe (rtx residual)
22033d
+{
22033d
+  return (residual == CONST0_RTX (Pmode)
22033d
+	  || GET_CODE (residual) != CONST_INT
22033d
+	  || INTVAL (residual) >= 1024);
22033d
+}
22033d
+
22033d
 #undef TARGET_ADDRESS_COST
22033d
 #define TARGET_ADDRESS_COST aarch64_address_cost
22033d
 
22033d
@@ -8378,6 +8593,10 @@ aarch64_vectorize_vec_perm_const_ok (enum machine_mode vmode,
22033d
 #undef TARGET_FIXED_CONDITION_CODE_REGS
22033d
 #define TARGET_FIXED_CONDITION_CODE_REGS aarch64_fixed_condition_code_regs
22033d
 
22033d
+#undef TARGET_STACK_CLASH_PROTECTION_FINAL_DYNAMIC_PROBE
22033d
+#define TARGET_STACK_CLASH_PROTECTION_FINAL_DYNAMIC_PROBE \
22033d
+  aarch64_stack_clash_protection_final_dynamic_probe
22033d
+
22033d
 struct gcc_target targetm = TARGET_INITIALIZER;
22033d
 
22033d
 #include "gt-aarch64.h"
22033d
diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md
22033d
index a085c6acaf5..5485a5f70b1 100644
22033d
--- a/gcc/config/aarch64/aarch64.md
22033d
+++ b/gcc/config/aarch64/aarch64.md
22033d
@@ -3401,7 +3401,7 @@
22033d
 )
22033d
 
22033d
 (define_insn "probe_stack_range"
22033d
-  [(set (match_operand:DI 0 "register_operand" "=r")
22033d
+  [(set (match_operand:DI 0 "register_operand" "=rk")
22033d
 	(unspec_volatile:DI [(match_operand:DI 1 "register_operand" "0")
22033d
 			     (match_operand:DI 2 "register_operand" "r")]
22033d
 			      UNSPECV_PROBE_STACK_RANGE))]
22033d
diff --git a/gcc/testsuite/gcc.target/aarch64/stack-check-12.c b/gcc/testsuite/gcc.target/aarch64/stack-check-12.c
22033d
new file mode 100644
22033d
index 00000000000..2ce38483b6b
22033d
--- /dev/null
22033d
+++ b/gcc/testsuite/gcc.target/aarch64/stack-check-12.c
22033d
@@ -0,0 +1,20 @@
22033d
+/* { dg-do compile } */
22033d
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
22033d
+/* { dg-require-effective-target supports_stack_clash_protection } */
22033d
+
22033d
+extern void arf (unsigned long int *, unsigned long int *);
22033d
+void
22033d
+frob ()
22033d
+{
22033d
+  unsigned long int num[1000];
22033d
+  unsigned long int den[1000];
22033d
+  arf (den, num);
22033d
+}
22033d
+
22033d
+/* This verifies that the scheduler did not break the dependencies
22033d
+   by adjusting the offsets within the probe and that the scheduler
22033d
+   did not reorder around the stack probes.  */
22033d
+/* { dg-final { scan-assembler-times "sub\\tsp, sp, #4096\\n\\tstr\\txzr, .sp, 4088." 3 } } */
22033d
+
22033d
+
22033d
+
22033d
diff --git a/gcc/testsuite/gcc.target/aarch64/stack-check-13.c b/gcc/testsuite/gcc.target/aarch64/stack-check-13.c
22033d
new file mode 100644
22033d
index 00000000000..d8886835989
22033d
--- /dev/null
22033d
+++ b/gcc/testsuite/gcc.target/aarch64/stack-check-13.c
22033d
@@ -0,0 +1,28 @@
22033d
+/* { dg-do compile } */
22033d
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
22033d
+/* { dg-require-effective-target supports_stack_clash_protection } */
22033d
+
22033d
+#define ARG32(X) X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X
22033d
+#define ARG192(X) ARG32(X),ARG32(X),ARG32(X),ARG32(X),ARG32(X),ARG32(X)
22033d
+void out1(ARG192(__int128));
22033d
+int t1(int);
22033d
+
22033d
+int t3(int x)
22033d
+{
22033d
+  if (x < 1000)
22033d
+    return t1 (x) + 1;
22033d
+
22033d
+  out1 (ARG192(1));
22033d
+  return 0;
22033d
+}
22033d
+
22033d
+
22033d
+
22033d
+/* This test creates a large (> 1k) outgoing argument area that needs
22033d
+   to be probed.  We don't test the exact size of the space or the
22033d
+   exact offset to make the test a little less sensitive to trivial
22033d
+   output changes.  */
22033d
+/* { dg-final { scan-assembler-times "sub\\tsp, sp, #....\\n\\tstr\\txzr, \\\[sp" 1 } } */
22033d
+
22033d
+
22033d
+
22033d
diff --git a/gcc/testsuite/gcc.target/aarch64/stack-check-14.c b/gcc/testsuite/gcc.target/aarch64/stack-check-14.c
22033d
new file mode 100644
22033d
index 00000000000..59ffe01376d
22033d
--- /dev/null
22033d
+++ b/gcc/testsuite/gcc.target/aarch64/stack-check-14.c
22033d
@@ -0,0 +1,25 @@
22033d
+/* { dg-do compile } */
22033d
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
22033d
+/* { dg-require-effective-target supports_stack_clash_protection } */
22033d
+
22033d
+int t1(int);
22033d
+
22033d
+int t2(int x)
22033d
+{
22033d
+  char *p = __builtin_alloca (4050);
22033d
+  x = t1 (x);
22033d
+  return p[x];
22033d
+}
22033d
+
22033d
+
22033d
+/* This test has a constant sized alloca that is smaller than the
22033d
+   probe interval.  But it actually requires two probes instead
22033d
+   of one because of the optimistic assumptions we made in the
22033d
+   aarch64 prologue code WRT probing state. 
22033d
+
22033d
+   The form can change quite a bit so we just check for two
22033d
+   probes without looking at the actual address.  */
22033d
+/* { dg-final { scan-assembler-times "str\\txzr," 2 } } */
22033d
+
22033d
+
22033d
+
22033d
diff --git a/gcc/testsuite/gcc.target/aarch64/stack-check-15.c b/gcc/testsuite/gcc.target/aarch64/stack-check-15.c
22033d
new file mode 100644
22033d
index 00000000000..e06db6dc2f0
22033d
--- /dev/null
22033d
+++ b/gcc/testsuite/gcc.target/aarch64/stack-check-15.c
22033d
@@ -0,0 +1,24 @@
22033d
+/* { dg-do compile } */
22033d
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
22033d
+/* { dg-require-effective-target supports_stack_clash_protection } */
22033d
+
22033d
+int t1(int);
22033d
+
22033d
+int t2(int x)
22033d
+{
22033d
+  char *p = __builtin_alloca (x);
22033d
+  x = t1 (x);
22033d
+  return p[x];
22033d
+}
22033d
+
22033d
+
22033d
+/* This test has a variable sized alloca.  It requires 3 probes.
22033d
+   One in the loop, one for the residual and at the end of the
22033d
+   alloca area. 
22033d
+
22033d
+   The form can change quite a bit so we just check for two
22033d
+   probes without looking at the actual address.  */
22033d
+/* { dg-final { scan-assembler-times "str\\txzr," 3 } } */
22033d
+
22033d
+
22033d
+
22033d
diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp
22033d
index aba99513ed0..a8451c98b08 100644
22033d
--- a/gcc/testsuite/lib/target-supports.exp
22033d
+++ b/gcc/testsuite/lib/target-supports.exp
22033d
@@ -5420,14 +5420,9 @@ proc check_effective_target_autoincdec { } {
22033d
 # 
22033d
 proc check_effective_target_supports_stack_clash_protection { } {
22033d
 
22033d
-   # Temporary until the target bits are fully ACK'd.
22033d
-#  if { [istarget aarch*-*-*] } {
22033d
-#	return 1
22033d
-#  }
22033d
-
22033d
     if { [istarget x86_64-*-*] || [istarget i?86-*-*] 
22033d
 	  || [istarget powerpc*-*-*] || [istarget rs6000*-*-*]
22033d
-	  || [istarget s390*-*-*] } {
22033d
+	  || [istarget aarch64*-**] || [istarget s390*-*-*] } {
22033d
 	return 1
22033d
     }
22033d
   return 0