Blame SOURCES/gcc48-rh1469697-2.patch

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