Blame SOURCES/gcc8-rh1512529-aarch64.patch

2985e0
--- gcc/config/aarch64/aarch64.c
2985e0
+++ gcc/config/aarch64/aarch64.c
2985e0
@@ -3799,7 +3799,14 @@ aarch64_output_probe_stack_range (rtx reg1, rtx reg2)
2985e0
   output_asm_insn ("sub\t%0, %0, %1", xops);
2985e0
 
2985e0
   /* Probe at TEST_ADDR.  */
2985e0
-  output_asm_insn ("str\txzr, [%0]", xops);
2985e0
+  if (flag_stack_clash_protection)
2985e0
+    {
2985e0
+      gcc_assert (xops[0] == stack_pointer_rtx);
2985e0
+      xops[1] = GEN_INT (PROBE_INTERVAL - 8);
2985e0
+      output_asm_insn ("str\txzr, [%0, %1]", xops);
2985e0
+    }
2985e0
+  else
2985e0
+    output_asm_insn ("str\txzr, [%0]", xops);
2985e0
 
2985e0
   /* Test if TEST_ADDR == LAST_ADDR.  */
2985e0
   xops[1] = reg2;
2985e0
@@ -4589,6 +4596,133 @@ aarch64_set_handled_components (sbitmap components)
2985e0
       cfun->machine->reg_is_wrapped_separately[regno] = true;
2985e0
 }
2985e0
 
2985e0
+/* Allocate POLY_SIZE bytes of stack space using TEMP1 and TEMP2 as scratch
2985e0
+   registers.  */
2985e0
+
2985e0
+static void
2985e0
+aarch64_allocate_and_probe_stack_space (rtx temp1, rtx temp2,
2985e0
+					poly_int64 poly_size)
2985e0
+{
2985e0
+  HOST_WIDE_INT size;
2985e0
+  if (!poly_size.is_constant (&size))
2985e0
+    {
2985e0
+      sorry ("stack probes for SVE frames");
2985e0
+      return;
2985e0
+    }
2985e0
+
2985e0
+  HOST_WIDE_INT probe_interval
2985e0
+    = 1 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL);
2985e0
+  HOST_WIDE_INT guard_size
2985e0
+    = 1 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE);
2985e0
+  HOST_WIDE_INT guard_used_by_caller = 1024;
2985e0
+
2985e0
+  /* SIZE should be large enough to require probing here.  ie, it
2985e0
+     must be larger than GUARD_SIZE - GUARD_USED_BY_CALLER.
2985e0
+
2985e0
+     We can allocate GUARD_SIZE - GUARD_USED_BY_CALLER as a single chunk
2985e0
+     without any probing.  */
2985e0
+  gcc_assert (size >= guard_size - guard_used_by_caller);
2985e0
+  aarch64_sub_sp (temp1, temp2, guard_size - guard_used_by_caller, true);
2985e0
+  HOST_WIDE_INT orig_size = size;
2985e0
+  size -= (guard_size - guard_used_by_caller);
2985e0
+
2985e0
+  HOST_WIDE_INT rounded_size = size & -probe_interval;
2985e0
+  HOST_WIDE_INT residual = size - rounded_size;
2985e0
+
2985e0
+  /* We can handle a small number of allocations/probes inline.  Otherwise
2985e0
+     punt to a loop.  */
2985e0
+  if (rounded_size && rounded_size <= 4 * probe_interval)
2985e0
+    {
2985e0
+      /* We don't use aarch64_sub_sp here because we don't want to
2985e0
+	 repeatedly load TEMP1.  */
2985e0
+      rtx step = GEN_INT (-probe_interval);
2985e0
+      if (probe_interval > ARITH_FACTOR)
2985e0
+	{
2985e0
+	  emit_move_insn (temp1, step);
2985e0
+	  step = temp1;
2985e0
+	}
2985e0
+
2985e0
+      for (HOST_WIDE_INT i = 0; i < rounded_size; i += probe_interval)
2985e0
+	{
2985e0
+	  rtx_insn *insn = emit_insn (gen_add2_insn (stack_pointer_rtx, step));
2985e0
+          add_reg_note (insn, REG_STACK_CHECK, const0_rtx);
2985e0
+
2985e0
+	  if (probe_interval > ARITH_FACTOR)
2985e0
+	    {
2985e0
+	      RTX_FRAME_RELATED_P (insn) = 1;
2985e0
+	      rtx adj = plus_constant (Pmode, stack_pointer_rtx, -probe_interval);
2985e0
+	      add_reg_note (insn, REG_CFA_ADJUST_CFA,
2985e0
+			    gen_rtx_SET (stack_pointer_rtx, adj));
2985e0
+	    }
2985e0
+
2985e0
+	  emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
2985e0
+					   (probe_interval
2985e0
+					    - GET_MODE_SIZE (word_mode))));
2985e0
+	  emit_insn (gen_blockage ());
2985e0
+	}
2985e0
+      dump_stack_clash_frame_info (PROBE_INLINE, size != rounded_size);
2985e0
+    }
2985e0
+  else if (rounded_size)
2985e0
+    {
2985e0
+      /* Compute the ending address.  */
2985e0
+      unsigned int scratchreg = REGNO (temp1);
2985e0
+      emit_move_insn (temp1, GEN_INT (-rounded_size));
2985e0
+      rtx_insn *insn
2985e0
+	 = emit_insn (gen_add3_insn (temp1, stack_pointer_rtx, temp1));
2985e0
+
2985e0
+      /* For the initial allocation, we don't have a frame pointer
2985e0
+	 set up, so we always need CFI notes.  If we're doing the
2985e0
+	 final allocation, then we may have a frame pointer, in which
2985e0
+	 case it is the CFA, otherwise we need CFI notes.
2985e0
+
2985e0
+	 We can determine which allocation we are doing by looking at
2985e0
+	 the temporary register.  IP0 is the initial allocation, IP1
2985e0
+	 is the final allocation.  */
2985e0
+      if (scratchreg == IP0_REGNUM || !frame_pointer_needed)
2985e0
+	{
2985e0
+	  /* We want the CFA independent of the stack pointer for the
2985e0
+	     duration of the loop.  */
2985e0
+	  add_reg_note (insn, REG_CFA_DEF_CFA,
2985e0
+			plus_constant (Pmode, temp1,
2985e0
+				       (rounded_size + (orig_size - size))));
2985e0
+	  RTX_FRAME_RELATED_P (insn) = 1;
2985e0
+	}
2985e0
+
2985e0
+      /* This allocates and probes the stack.
2985e0
+
2985e0
+	 It also probes at a 4k interval regardless of the value of
2985e0
+	 PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL.  */
2985e0
+      insn = emit_insn (gen_probe_stack_range (stack_pointer_rtx,
2985e0
+					       stack_pointer_rtx, temp1));
2985e0
+
2985e0
+      /* Now reset the CFA register if needed.  */
2985e0
+      if (scratchreg == IP0_REGNUM || !frame_pointer_needed)
2985e0
+	{
2985e0
+	  add_reg_note (insn, REG_CFA_DEF_CFA,
2985e0
+			plus_constant (Pmode, stack_pointer_rtx,
2985e0
+				       (rounded_size + (orig_size - size))));
2985e0
+	  RTX_FRAME_RELATED_P (insn) = 1;
2985e0
+	}
2985e0
+
2985e0
+      emit_insn (gen_blockage ());
2985e0
+      dump_stack_clash_frame_info (PROBE_LOOP, size != rounded_size);
2985e0
+    }
2985e0
+  else
2985e0
+    dump_stack_clash_frame_info (PROBE_INLINE, size != rounded_size);
2985e0
+
2985e0
+  /* Handle any residuals.
2985e0
+     Note that any residual must be probed.  */
2985e0
+  if (residual)
2985e0
+    {
2985e0
+      aarch64_sub_sp (temp1, temp2, residual, true);
2985e0
+      add_reg_note (get_last_insn (), REG_STACK_CHECK, const0_rtx);
2985e0
+      emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
2985e0
+				       (residual - GET_MODE_SIZE (word_mode))));
2985e0
+      emit_insn (gen_blockage ());
2985e0
+    }
2985e0
+  return;
2985e0
+}
2985e0
+
2985e0
 /* Add a REG_CFA_EXPRESSION note to INSN to say that register REG
2985e0
    is saved at BASE + OFFSET.  */
2985e0
 
2985e0
@@ -4686,7 +4820,54 @@ aarch64_expand_prologue (void)
2985e0
   rtx ip0_rtx = gen_rtx_REG (Pmode, IP0_REGNUM);
2985e0
   rtx ip1_rtx = gen_rtx_REG (Pmode, IP1_REGNUM);
2985e0
 
2985e0
-  aarch64_sub_sp (ip0_rtx, ip1_rtx, initial_adjust, true);
2985e0
+  /* We do not fully protect aarch64 against stack clash style attacks
2985e0
+     as doing so would be prohibitively expensive with less utility over
2985e0
+     time as newer compilers are deployed.
2985e0
+
2985e0
+     We assume the guard is at least 64k.  Furthermore, we assume that
2985e0
+     the caller has not pushed the stack pointer more than 1k into
2985e0
+     the guard.  A caller that pushes the stack pointer than 1k into
2985e0
+     the guard is considered invalid.
2985e0
+
2985e0
+     Note that the caller's ability to push the stack pointer into the
2985e0
+     guard is a function of the number and size of outgoing arguments and/or
2985e0
+     dynamic stack allocations due to the mandatory save of the link register
2985e0
+     in the caller's frame.
2985e0
+
2985e0
+     With those assumptions the callee can allocate up to 63k of stack
2985e0
+     space without probing.
2985e0
+
2985e0
+     When probing is needed, we emit a probe at the start of the prologue
2985e0
+     and every PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes thereafter.
2985e0
+
2985e0
+     We have to track how much space has been allocated, but we do not
2985e0
+     track stores into the stack as implicit probes except for the
2985e0
+     fp/lr store.  */
2985e0
+  HOST_WIDE_INT guard_size
2985e0
+    = 1 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE);
2985e0
+  HOST_WIDE_INT guard_used_by_caller = 1024;
2985e0
+  if (flag_stack_clash_protection)
2985e0
+    {
2985e0
+      if (known_eq (frame_size, 0))
2985e0
+	dump_stack_clash_frame_info (NO_PROBE_NO_FRAME, false);
2985e0
+      else if (known_lt (initial_adjust, guard_size - guard_used_by_caller)
2985e0
+	       && known_lt (final_adjust, guard_size - guard_used_by_caller))
2985e0
+	dump_stack_clash_frame_info (NO_PROBE_SMALL_FRAME, true);
2985e0
+    }
2985e0
+
2985e0
+  /* In theory we should never have both an initial adjustment
2985e0
+     and a callee save adjustment.  Verify that is the case since the
2985e0
+     code below does not handle it for -fstack-clash-protection.  */
2985e0
+  gcc_assert (known_eq (initial_adjust, 0) || callee_adjust == 0);
2985e0
+
2985e0
+  /* Only probe if the initial adjustment is larger than the guard
2985e0
+     less the amount of the guard reserved for use by the caller's
2985e0
+     outgoing args.  */
2985e0
+  if (flag_stack_clash_protection
2985e0
+      && maybe_ge (initial_adjust, guard_size - guard_used_by_caller))
2985e0
+    aarch64_allocate_and_probe_stack_space (ip0_rtx, ip1_rtx, initial_adjust);
2985e0
+  else
2985e0
+    aarch64_sub_sp (ip0_rtx, ip1_rtx, initial_adjust, true);
2985e0
 
2985e0
   if (callee_adjust != 0)
2985e0
     aarch64_push_regs (reg1, reg2, callee_adjust);
2985e0
@@ -4742,7 +4923,31 @@ aarch64_expand_prologue (void)
2985e0
 			     callee_adjust != 0 || emit_frame_chain);
2985e0
   aarch64_save_callee_saves (DFmode, callee_offset, V0_REGNUM, V31_REGNUM,
2985e0
 			     callee_adjust != 0 || emit_frame_chain);
2985e0
-  aarch64_sub_sp (ip1_rtx, ip0_rtx, final_adjust, !frame_pointer_needed);
2985e0
+
2985e0
+  /* We may need to probe the final adjustment as well.  */
2985e0
+  if (flag_stack_clash_protection && maybe_ne (final_adjust, 0))
2985e0
+    {
2985e0
+      /* First probe if the final adjustment is larger than the guard size
2985e0
+	 less the amount of the guard reserved for use by the caller's
2985e0
+	 outgoing args.  */
2985e0
+      if (maybe_ge (final_adjust, guard_size - guard_used_by_caller))
2985e0
+	aarch64_allocate_and_probe_stack_space (ip1_rtx, ip0_rtx,
2985e0
+						final_adjust);
2985e0
+      else
2985e0
+	aarch64_sub_sp (ip1_rtx, ip0_rtx, final_adjust, !frame_pointer_needed);
2985e0
+
2985e0
+      /* We must also probe if the final adjustment is larger than the guard
2985e0
+	 that is assumed used by the caller.  This may be sub-optimal.  */
2985e0
+      if (maybe_ge (final_adjust, guard_used_by_caller))
2985e0
+	{
2985e0
+	  if (dump_file)
2985e0
+	    fprintf (dump_file,
2985e0
+		     "Stack clash aarch64 large outgoing arg, probing\n");
2985e0
+	  emit_stack_probe (stack_pointer_rtx);
2985e0
+	}
2985e0
+    }
2985e0
+  else
2985e0
+    aarch64_sub_sp (ip1_rtx, ip0_rtx, final_adjust, !frame_pointer_needed);
2985e0
 }
2985e0
 
2985e0
 /* Return TRUE if we can use a simple_return insn.
2985e0
@@ -10476,6 +10681,12 @@ aarch64_override_options_internal (struct gcc_options *opts)
2985e0
       && opts->x_optimize >= aarch64_tune_params.prefetch->default_opt_level)
2985e0
     opts->x_flag_prefetch_loop_arrays = 1;
2985e0
 
2985e0
+  /* We assume the guard page is 64k.  */
2985e0
+  maybe_set_param_value (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE,
2985e0
+			 16,
2985e0
+			 opts->x_param_values,
2985e0
+			 global_options_set.x_param_values);
2985e0
+
2985e0
   aarch64_override_options_after_change_1 (opts);
2985e0
 }
2985e0
 
2985e0
@@ -17161,6 +17372,28 @@ aarch64_sched_can_speculate_insn (rtx_insn *insn)
2985e0
     }
2985e0
 }
2985e0
 
2985e0
+/* It has been decided that to allow up to 1kb of outgoing argument
2985e0
+   space to be allocated w/o probing.  If more than 1kb of outgoing
2985e0
+   argment space is allocated, then it must be probed and the last
2985e0
+   probe must occur no more than 1kbyte away from the end of the
2985e0
+   allocated space.
2985e0
+
2985e0
+   This implies that the residual part of an alloca allocation may
2985e0
+   need probing in cases where the generic code might not otherwise
2985e0
+   think a probe is needed.
2985e0
+
2985e0
+   This target hook returns TRUE when allocating RESIDUAL bytes of
2985e0
+   alloca space requires an additional probe, otherwise FALSE is
2985e0
+   returned.  */
2985e0
+
2985e0
+static bool
2985e0
+aarch64_stack_clash_protection_final_dynamic_probe (rtx residual)
2985e0
+{
2985e0
+  return (residual == CONST0_RTX (Pmode)
2985e0
+	  || GET_CODE (residual) != CONST_INT
2985e0
+	  || INTVAL (residual) >= 1024);
2985e0
+}
2985e0
+
2985e0
 /* Implement TARGET_COMPUTE_PRESSURE_CLASSES.  */
2985e0
 
2985e0
 static int
2985e0
@@ -17669,6 +17902,10 @@ aarch64_libgcc_floating_mode_supported_p
2985e0
 #undef TARGET_CONSTANT_ALIGNMENT
2985e0
 #define TARGET_CONSTANT_ALIGNMENT aarch64_constant_alignment
2985e0
 
2985e0
+#undef TARGET_STACK_CLASH_PROTECTION_FINAL_DYNAMIC_PROBE
2985e0
+#define TARGET_STACK_CLASH_PROTECTION_FINAL_DYNAMIC_PROBE \
2985e0
+  aarch64_stack_clash_protection_final_dynamic_probe
2985e0
+
2985e0
 #undef TARGET_COMPUTE_PRESSURE_CLASSES
2985e0
 #define TARGET_COMPUTE_PRESSURE_CLASSES aarch64_compute_pressure_classes
2985e0
 
2985e0
--- gcc/config/aarch64/aarch64.md
2985e0
+++ gcc/config/aarch64/aarch64.md
2985e0
@@ -5812,7 +5812,7 @@
2985e0
 )
2985e0
 
2985e0
 (define_insn "probe_stack_range"
2985e0
-  [(set (match_operand:DI 0 "register_operand" "=r")
2985e0
+  [(set (match_operand:DI 0 "register_operand" "=rk")
2985e0
 	(unspec_volatile:DI [(match_operand:DI 1 "register_operand" "0")
2985e0
 			     (match_operand:DI 2 "register_operand" "r")]
2985e0
 			      UNSPECV_PROBE_STACK_RANGE))]
2985e0
--- gcc/testsuite/gcc.target/aarch64/stack-check-12.c
2985e0
+++ gcc/testsuite/gcc.target/aarch64/stack-check-12.c
2985e0
@@ -0,0 +1,20 @@
2985e0
+/* { dg-do compile } */
2985e0
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
2985e0
+/* { dg-require-effective-target supports_stack_clash_protection } */
2985e0
+
2985e0
+extern void arf (unsigned long int *, unsigned long int *);
2985e0
+void
2985e0
+frob ()
2985e0
+{
2985e0
+  unsigned long int num[1000];
2985e0
+  unsigned long int den[1000];
2985e0
+  arf (den, num);
2985e0
+}
2985e0
+
2985e0
+/* This verifies that the scheduler did not break the dependencies
2985e0
+   by adjusting the offsets within the probe and that the scheduler
2985e0
+   did not reorder around the stack probes.  */
2985e0
+/* { dg-final { scan-assembler-times "sub\\tsp, sp, #4096\\n\\tstr\\txzr, .sp, 4088." 3 } } */
2985e0
+
2985e0
+
2985e0
+
2985e0
--- gcc/testsuite/gcc.target/aarch64/stack-check-13.c
2985e0
+++ gcc/testsuite/gcc.target/aarch64/stack-check-13.c
2985e0
@@ -0,0 +1,28 @@
2985e0
+/* { dg-do compile } */
2985e0
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
2985e0
+/* { dg-require-effective-target supports_stack_clash_protection } */
2985e0
+
2985e0
+#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
2985e0
+#define ARG192(X) ARG32(X),ARG32(X),ARG32(X),ARG32(X),ARG32(X),ARG32(X)
2985e0
+void out1(ARG192(__int128));
2985e0
+int t1(int);
2985e0
+
2985e0
+int t3(int x)
2985e0
+{
2985e0
+  if (x < 1000)
2985e0
+    return t1 (x) + 1;
2985e0
+
2985e0
+  out1 (ARG192(1));
2985e0
+  return 0;
2985e0
+}
2985e0
+
2985e0
+
2985e0
+
2985e0
+/* This test creates a large (> 1k) outgoing argument area that needs
2985e0
+   to be probed.  We don't test the exact size of the space or the
2985e0
+   exact offset to make the test a little less sensitive to trivial
2985e0
+   output changes.  */
2985e0
+/* { dg-final { scan-assembler-times "sub\\tsp, sp, #....\\n\\tstr\\txzr, \\\[sp" 1 } } */
2985e0
+
2985e0
+
2985e0
+
2985e0
--- gcc/testsuite/gcc.target/aarch64/stack-check-14.c
2985e0
+++ gcc/testsuite/gcc.target/aarch64/stack-check-14.c
2985e0
@@ -0,0 +1,25 @@
2985e0
+/* { dg-do compile } */
2985e0
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
2985e0
+/* { dg-require-effective-target supports_stack_clash_protection } */
2985e0
+
2985e0
+int t1(int);
2985e0
+
2985e0
+int t2(int x)
2985e0
+{
2985e0
+  char *p = __builtin_alloca (4050);
2985e0
+  x = t1 (x);
2985e0
+  return p[x];
2985e0
+}
2985e0
+
2985e0
+
2985e0
+/* This test has a constant sized alloca that is smaller than the
2985e0
+   probe interval.  But it actually requires two probes instead
2985e0
+   of one because of the optimistic assumptions we made in the
2985e0
+   aarch64 prologue code WRT probing state. 
2985e0
+
2985e0
+   The form can change quite a bit so we just check for two
2985e0
+   probes without looking at the actual address.  */
2985e0
+/* { dg-final { scan-assembler-times "str\\txzr," 2 } } */
2985e0
+
2985e0
+
2985e0
+
2985e0
--- gcc/testsuite/gcc.target/aarch64/stack-check-15.c
2985e0
+++ gcc/testsuite/gcc.target/aarch64/stack-check-15.c
2985e0
@@ -0,0 +1,24 @@
2985e0
+/* { dg-do compile } */
2985e0
+/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-guard-size=12" } */
2985e0
+/* { dg-require-effective-target supports_stack_clash_protection } */
2985e0
+
2985e0
+int t1(int);
2985e0
+
2985e0
+int t2(int x)
2985e0
+{
2985e0
+  char *p = __builtin_alloca (x);
2985e0
+  x = t1 (x);
2985e0
+  return p[x];
2985e0
+}
2985e0
+
2985e0
+
2985e0
+/* This test has a variable sized alloca.  It requires 3 probes.
2985e0
+   One in the loop, one for the residual and at the end of the
2985e0
+   alloca area. 
2985e0
+
2985e0
+   The form can change quite a bit so we just check for two
2985e0
+   probes without looking at the actual address.  */
2985e0
+/* { dg-final { scan-assembler-times "str\\txzr," 3 } } */
2985e0
+
2985e0
+
2985e0
+
2985e0
--- gcc/testsuite/lib/target-supports.exp
2985e0
+++ gcc/testsuite/lib/target-supports.exp
2985e0
@@ -9201,14 +9201,9 @@ proc check_effective_target_autoincdec { } {
2985e0
 # 
2985e0
 proc check_effective_target_supports_stack_clash_protection { } {
2985e0
 
2985e0
-   # Temporary until the target bits are fully ACK'd.
2985e0
-#  if { [istarget aarch*-*-*] } {
2985e0
-#	return 1
2985e0
-#  }
2985e0
-
2985e0
     if { [istarget x86_64-*-*] || [istarget i?86-*-*] 
2985e0
 	  || [istarget powerpc*-*-*] || [istarget rs6000*-*-*]
2985e0
-	  || [istarget s390*-*-*] } {
2985e0
+	  || [istarget aarch64*-**] || [istarget s390*-*-*] } {
2985e0
 	return 1
2985e0
     }
2985e0
   return 0
2985e0
@@ -9217,9 +9212,9 @@ proc check_effective_target_supports_stack_clash_protection { } {
2985e0
 # Return 1 if the target creates a frame pointer for non-leaf functions
2985e0
 # Note we ignore cases where we apply tail call optimization here.
2985e0
 proc check_effective_target_frame_pointer_for_non_leaf { } {
2985e0
-  if { [istarget aarch*-*-*] } {
2985e0
-	return 1
2985e0
-  }
2985e0
+#  if { [istarget aarch*-*-*] } {
2985e0
+#	return 1
2985e0
+#  }
2985e0
 
2985e0
   # Solaris/x86 defaults to -fno-omit-frame-pointer.
2985e0
   if { [istarget i?86-*-solaris*] || [istarget x86_64-*-solaris*] } {