Blame SOURCES/gcc48-rh1469697-2.patch

4dd737
commit 6427208ee82548346a2f42a8ac83fdd2f823fde2
4dd737
Author: law <law@138bc75d-0d04-0410-961f-82ee72b054a4>
4dd737
Date:   Wed Sep 20 04:56:54 2017 +0000
4dd737
4dd737
            * common.opt (-fstack-clash-protection): New option.
4dd737
            * flag-types.h (enum stack_check_type): Note difference between
4dd737
            -fstack-check= and -fstack-clash-protection.
4dd737
            * params.def (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE): New PARAM.
4dd737
            (PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL): Likewise.
4dd737
            * toplev.c (process_options): Issue warnings/errors for cases
4dd737
            not handled with -fstack-clash-protection.
4dd737
            * doc/invoke.texi (-fstack-clash-protection): Document new option.
4dd737
            (-fstack-check): Note additional problem with -fstack-check=generic.
4dd737
            Note that -fstack-check is primarily for Ada and refer users
4dd737
            to -fstack-clash-protection for stack-clash-protection.
4dd737
            Document new params for stack clash protection.
4dd737
    
4dd737
            * gcc.dg/stack-check-2.c: New test.
4dd737
            * lib/target-supports.exp
4dd737
            (check_effective_target_supports_stack_clash_protection): New function.
4dd737
            (check_effective_target_frame_pointer_for_non_leaf): Likewise.
4dd737
            (check_effective_target_caller_implicit_probes): Likewise.
4dd737
    
4dd737
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@252994 138bc75d-0d04-0410-961f-82ee72b054a4
4dd737
4dd737
diff --git a/gcc/common.opt b/gcc/common.opt
4dd737
index 16846c13b62..0c335cb12cd 100644
4dd737
--- a/gcc/common.opt
4dd737
+++ b/gcc/common.opt
4dd737
@@ -1911,13 +1911,18 @@ Common Report Var(flag_variable_expansion_in_unroller) Optimization
4dd737
 Apply variable expansion when loops are unrolled
4dd737
 
4dd737
 fstack-check=
4dd737
-Common Report RejectNegative Joined
4dd737
--fstack-check=[no|generic|specific]	Insert stack checking code into the program
4dd737
+Common Report RejectNegative Joined Optimization
4dd737
+-fstack-check=[no|generic|specific]	Insert stack checking code into the program.
4dd737
 
4dd737
 fstack-check
4dd737
 Common Alias(fstack-check=, specific, no)
4dd737
 Insert stack checking code into the program.  Same as -fstack-check=specific
4dd737
 
4dd737
+fstack-clash-protection
4dd737
+Common Report Var(flag_stack_clash_protection) Optimization
4dd737
+Insert code to probe each page of stack space as it is allocated to protect
4dd737
+from stack-clash style attacks.
4dd737
+
4dd737
 fstack-limit
4dd737
 Common Var(common_deferred_options) Defer
4dd737
 
4dd737
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
4dd737
index f7a15ca190e..313a6c5ff76 100644
4dd737
--- a/gcc/doc/invoke.texi
4dd737
+++ b/gcc/doc/invoke.texi
4dd737
@@ -9406,6 +9406,21 @@ compilation for profile feedback and one for compilation without.  The value
4dd737
 for compilation with profile feedback needs to be more conservative (higher) in
4dd737
 order to make tracer effective.
4dd737
 
4dd737
+@item stack-clash-protection-guard-size
4dd737
+Specify the size of the operating system provided stack guard as
4dd737
+2 raised to @var{num} bytes.  The default value is 12 (4096 bytes).
4dd737
+Acceptable values are between 12 and 30.  Higher values may reduce the
4dd737
+number of explicit probes, but a value larger than the operating system
4dd737
+provided guard will leave code vulnerable to stack clash style attacks.
4dd737
+
4dd737
+@item stack-clash-protection-probe-interval
4dd737
+Stack clash protection involves probing stack space as it is allocated.  This
4dd737
+param controls the maximum distance between probes into the stack as 2 raised
4dd737
+to @var{num} bytes.  Acceptable values are between 10 and 16 and defaults to
4dd737
+12.  Higher values may reduce the number of explicit probes, but a value
4dd737
+larger than the operating system provided guard will leave code vulnerable to
4dd737
+stack clash style attacks.
4dd737
+
4dd737
 @item max-cse-path-length
4dd737
 
4dd737
 The maximum number of basic blocks on path that CSE considers.
4dd737
@@ -20949,7 +20964,8 @@ target support in the compiler but comes with the following drawbacks:
4dd737
 @enumerate
4dd737
 @item
4dd737
 Modified allocation strategy for large objects: they are always
4dd737
-allocated dynamically if their size exceeds a fixed threshold.
4dd737
+allocated dynamically if their size exceeds a fixed threshold.  Note this
4dd737
+may change the semantics of some code.
4dd737
 
4dd737
 @item
4dd737
 Fixed limit on the size of the static frame of functions: when it is
4dd737
@@ -20964,6 +20980,27 @@ generic implementation, code performance is hampered.
4dd737
 Note that old-style stack checking is also the fallback method for
4dd737
 @code{specific} if no target support has been added in the compiler.
4dd737
 
4dd737
+@samp{-fstack-check=} is designed for Ada's needs to detect infinite recursion
4dd737
+and stack overflows.  @samp{specific} is an excellent choice when compiling
4dd737
+Ada code.  It is not generally sufficient to protect against stack-clash
4dd737
+attacks.  To protect against those you want @samp{-fstack-clash-protection}.
4dd737
+
4dd737
+@item -fstack-clash-protection
4dd737
+@opindex fstack-clash-protection
4dd737
+Generate code to prevent stack clash style attacks.  When this option is
4dd737
+enabled, the compiler will only allocate one page of stack space at a time
4dd737
+and each page is accessed immediately after allocation.  Thus, it prevents
4dd737
+allocations from jumping over any stack guard page provided by the
4dd737
+operating system.
4dd737
+
4dd737
+Most targets do not fully support stack clash protection.  However, on
4dd737
+those targets @option{-fstack-clash-protection} will protect dynamic stack
4dd737
+allocations.  @option{-fstack-clash-protection} may also provide limited
4dd737
+protection for static stack allocations if the target supports
4dd737
+@option{-fstack-check=specific}.
4dd737
+
4dd737
+
4dd737
+
4dd737
 @item -fstack-limit-register=@var{reg}
4dd737
 @itemx -fstack-limit-symbol=@var{sym}
4dd737
 @itemx -fno-stack-limit
4dd737
diff --git a/gcc/flag-types.h b/gcc/flag-types.h
4dd737
index 4fc5d33348e..21e943d38fa 100644
4dd737
--- a/gcc/flag-types.h
4dd737
+++ b/gcc/flag-types.h
4dd737
@@ -139,7 +139,14 @@ enum excess_precision
4dd737
   EXCESS_PRECISION_STANDARD
4dd737
 };
4dd737
 
4dd737
-/* Type of stack check.  */
4dd737
+/* Type of stack check.
4dd737
+
4dd737
+   Stack checking is designed to detect infinite recursion and stack
4dd737
+   overflows for Ada programs.  Furthermore stack checking tries to ensure
4dd737
+   in that scenario that enough stack space is left to run a signal handler.
4dd737
+
4dd737
+   -fstack-check= does not prevent stack-clash style attacks.  For that
4dd737
+   you want -fstack-clash-protection.  */
4dd737
 enum stack_check_type
4dd737
 {
4dd737
   /* Do not check the stack.  */
4dd737
diff --git a/gcc/params.def b/gcc/params.def
4dd737
index e51b847a7c4..e668624b0cb 100644
4dd737
--- a/gcc/params.def
4dd737
+++ b/gcc/params.def
4dd737
@@ -208,6 +208,16 @@ DEFPARAM(PARAM_STACK_FRAME_GROWTH,
4dd737
 	 "Maximal stack frame growth due to inlining (in percent)",
4dd737
 	 1000, 0, 0)
4dd737
 
4dd737
+DEFPARAM(PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE,
4dd737
+	 "stack-clash-protection-guard-size",
4dd737
+	 "Size of the stack guard expressed as a power of two.",
4dd737
+	 12, 12, 30)
4dd737
+
4dd737
+DEFPARAM(PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL,
4dd737
+	 "stack-clash-protection-probe-interval",
4dd737
+	 "Interval in which to probe the stack expressed as a power of two.",
4dd737
+	 12, 10, 16)
4dd737
+
4dd737
 /* The GCSE optimization will be disabled if it would require
4dd737
    significantly more memory than this value.  */
4dd737
 DEFPARAM(PARAM_MAX_GCSE_MEMORY,
4dd737
diff --git a/gcc/testsuite/gcc.dg/stack-check-2.c b/gcc/testsuite/gcc.dg/stack-check-2.c
4dd737
new file mode 100644
4dd737
index 00000000000..196c4bbfbdd
4dd737
--- /dev/null
4dd737
+++ b/gcc/testsuite/gcc.dg/stack-check-2.c
4dd737
@@ -0,0 +1,66 @@
4dd737
+/* The goal here is to ensure that we never consider a call to a noreturn
4dd737
+   function as a potential tail call.
4dd737
+
4dd737
+   Right now GCC discovers potential tail calls by looking at the
4dd737
+   predecessors of the exit block.  A call to a non-return function
4dd737
+   has no successors and thus can never match that first filter.
4dd737
+
4dd737
+   But that could change one day and we want to catch it.  The problem
4dd737
+   is the compiler could potentially optimize a tail call to a nonreturn
4dd737
+   function, even if the caller has a frame.  That breaks the assumption
4dd737
+   that calls probe *sp when saving the return address that some targets
4dd737
+   depend on to elide stack probes.  */
4dd737
+
4dd737
+/* { dg-do compile } */
4dd737
+/* { dg-options "-O2 -fstack-clash-protection -fdump-tree-tailc -fdump-tree-optimized" } */
4dd737
+/* { dg-require-effective-target supports_stack_clash_protection } */
4dd737
+
4dd737
+extern void foo (void) __attribute__ ((__noreturn__));
4dd737
+
4dd737
+
4dd737
+void
4dd737
+test_direct_1 (void)
4dd737
+{
4dd737
+  foo ();
4dd737
+}
4dd737
+
4dd737
+void
4dd737
+test_direct_2 (void)
4dd737
+{
4dd737
+  return foo ();
4dd737
+}
4dd737
+
4dd737
+void (*indirect)(void)__attribute__ ((noreturn));
4dd737
+
4dd737
+
4dd737
+void
4dd737
+test_indirect_1 ()
4dd737
+{
4dd737
+  (*indirect)();
4dd737
+}
4dd737
+
4dd737
+void
4dd737
+test_indirect_2 (void)
4dd737
+{
4dd737
+  return (*indirect)();;
4dd737
+}
4dd737
+
4dd737
+
4dd737
+typedef void (*pvfn)() __attribute__ ((noreturn));
4dd737
+
4dd737
+void (*indirect_casted)(void);
4dd737
+
4dd737
+void
4dd737
+test_indirect_casted_1 ()
4dd737
+{
4dd737
+  (*(pvfn)indirect_casted)();
4dd737
+}
4dd737
+
4dd737
+void
4dd737
+test_indirect_casted_2 (void)
4dd737
+{
4dd737
+  return (*(pvfn)indirect_casted)();
4dd737
+}
4dd737
+/* { dg-final { scan-tree-dump-not "tail call" "tailc" } } */
4dd737
+/* { dg-final { scan-tree-dump-not "tail call" "optimized" } } */
4dd737
+
4dd737
diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp
4dd737
index ef371ad7efd..821cea9cb33 100644
4dd737
--- a/gcc/testsuite/lib/target-supports.exp
4dd737
+++ b/gcc/testsuite/lib/target-supports.exp
4dd737
@@ -5392,3 +5392,95 @@ proc check_effective_target_fenv_exceptions {} {
4dd737
 	}
4dd737
     } "-std=gnu99"]
4dd737
 }
4dd737
+
4dd737
+# Return 1 if the target supports the auto_inc_dec optimization pass.
4dd737
+proc check_effective_target_autoincdec { } {
4dd737
+    if { ![check_no_compiler_messages auto_incdec assembly { void f () { }
4dd737
+	 } "-O2 -fdump-rtl-auto_inc_dec" ] } {
4dd737
+      return 0
4dd737
+    }
4dd737
+
4dd737
+    set dumpfile [glob -nocomplain "auto_incdec[pid].c.\[0-9\]\[0-9\]\[0-9\]r.auto_inc_dec"]
4dd737
+    if { [file exists $dumpfile ] } {
4dd737
+	file delete $dumpfile
4dd737
+	return 1
4dd737
+    }
4dd737
+    return 0
4dd737
+}
4dd737
+
4dd737
+# Return 1 if the target has support for stack probing designed
4dd737
+# to avoid stack-clash style attacks.
4dd737
+#
4dd737
+# This is used to restrict the stack-clash mitigation tests to
4dd737
+# just those targets that have been explicitly supported.
4dd737
+# 
4dd737
+# In addition to the prologue work on those targets, each target's
4dd737
+# properties should be described in the functions below so that
4dd737
+# tests do not become a mess of unreadable target conditions.
4dd737
+# 
4dd737
+proc check_effective_target_supports_stack_clash_protection { } {
4dd737
+
4dd737
+   # Temporary until the target bits are fully ACK'd.
4dd737
+#  if { [istarget aarch*-*-*] || [istarget x86_64-*-*]
4dd737
+#       || [istarget i?86-*-*] || [istarget s390*-*-*]
4dd737
+#       || [istarget powerpc*-*-*] || [istarget rs6000*-*-*] } {
4dd737
+#	return 1
4dd737
+#  }
4dd737
+  return 0
4dd737
+}
4dd737
+
4dd737
+# Return 1 if the target creates a frame pointer for non-leaf functions
4dd737
+# Note we ignore cases where we apply tail call optimization here.
4dd737
+proc check_effective_target_frame_pointer_for_non_leaf { } {
4dd737
+  if { [istarget aarch*-*-*] } {
4dd737
+	return 1
4dd737
+  }
4dd737
+  return 0
4dd737
+}
4dd737
+
4dd737
+# Return 1 if the target's calling sequence or its ABI
4dd737
+# create implicit stack probes at or prior to function entry.
4dd737
+proc check_effective_target_caller_implicit_probes { } {
4dd737
+
4dd737
+  # On x86/x86_64 the call instruction itself pushes the return
4dd737
+  # address onto the stack.  That is an implicit probe of *sp.
4dd737
+  if { [istarget x86_64-*-*] || [istarget i?86-*-*] } {
4dd737
+	return 1
4dd737
+  }
4dd737
+
4dd737
+  # On PPC, the ABI mandates that the address of the outer
4dd737
+  # frame be stored at *sp.  Thus each allocation of stack
4dd737
+  # space is itself an implicit probe of *sp.
4dd737
+  if { [istarget powerpc*-*-*] || [istarget rs6000*-*-*] } {
4dd737
+	return 1
4dd737
+  }
4dd737
+
4dd737
+  # s390's ABI has a register save area allocated by the
4dd737
+  # caller for use by the callee.  The mere existence does
4dd737
+  # not constitute a probe by the caller, but when the slots
4dd737
+  # used by the callee those stores are implicit probes.
4dd737
+  if { [istarget s390*-*-*] } {
4dd737
+	return 1
4dd737
+  }
4dd737
+
4dd737
+  # Not strictly true on aarch64, but we have agreed that we will
4dd737
+  # consider any function that pushes SP more than 3kbytes into
4dd737
+  # the guard page as broken.  This essentially means that we can
4dd737
+  # consider the aarch64 as having a caller implicit probe at
4dd737
+  # *(sp + 1k).
4dd737
+  if { [istarget aarch64*-*-*] } {
4dd737
+	return 1;
4dd737
+  }
4dd737
+
4dd737
+  return 0
4dd737
+}
4dd737
+
4dd737
+# Targets that potentially realign the stack pointer often cause residual
4dd737
+# stack allocations and make it difficult to elimination loops or residual
4dd737
+# allocations for dynamic stack allocations
4dd737
+proc check_effective_target_callee_realigns_stack { } {
4dd737
+  if { [istarget x86_64-*-*] || [istarget i?86-*-*] } {
4dd737
+	return 1
4dd737
+  }
4dd737
+  return 0
4dd737
+}
4dd737
diff --git a/gcc/toplev.c b/gcc/toplev.c
4dd737
index 26f2ffb362c..1def163f8b9 100644
4dd737
--- a/gcc/toplev.c
4dd737
+++ b/gcc/toplev.c
4dd737
@@ -1520,6 +1520,28 @@ process_options (void)
4dd737
       flag_associative_math = 0;
4dd737
     }
4dd737
 
4dd737
+#ifndef STACK_GROWS_DOWNWARD
4dd737
+  /* -fstack-clash-protection is not currently supported on targets
4dd737
+     where the stack grows up.  */
4dd737
+  if (flag_stack_clash_protection)
4dd737
+    {
4dd737
+      warning_at (UNKNOWN_LOCATION, 0,
4dd737
+		  "%<-fstack-clash-protection%> is not supported on targets "
4dd737
+		  "where the stack grows from lower to higher addresses");
4dd737
+      flag_stack_clash_protection = 0;
4dd737
+    }
4dd737
+#endif
4dd737
+
4dd737
+  /* We can not support -fstack-check= and -fstack-clash-protection at
4dd737
+     the same time.  */
4dd737
+  if (flag_stack_check != NO_STACK_CHECK && flag_stack_clash_protection)
4dd737
+    {
4dd737
+      warning_at (UNKNOWN_LOCATION, 0,
4dd737
+		  "%<-fstack-check=%> and %<-fstack-clash_protection%> are "
4dd737
+		  "mutually exclusive.  Disabling %<-fstack-check=%>");
4dd737
+      flag_stack_check = NO_STACK_CHECK;
4dd737
+    }
4dd737
+
4dd737
   /* With -fcx-limited-range, we do cheap and quick complex arithmetic.  */
4dd737
   if (flag_cx_limited_range)
4dd737
     flag_complex_method = 0;