Blame SOURCES/gcc48-rh1469697-2.patch

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