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