Blame SOURCES/gcc48-rh1469697-2.patch

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