|
|
22033d |
commit a3e2ba88eb09c1eed2f7ed6e17660b345464bb90
|
|
|
22033d |
Author: law <law@138bc75d-0d04-0410-961f-82ee72b054a4>
|
|
|
22033d |
Date: Wed Sep 20 05:05:12 2017 +0000
|
|
|
22033d |
|
|
|
22033d |
2017-09-18 Jeff Law <law@redhat.com>
|
|
|
22033d |
|
|
|
22033d |
* explow.c: Include "params.h" and "dumpfile.h".
|
|
|
22033d |
(anti_adjust_stack_and_probe_stack_clash): New function.
|
|
|
22033d |
(get_stack_check_protect): Likewise.
|
|
|
22033d |
(compute_stack_clash_protection_loop_data): Likewise.
|
|
|
22033d |
(emit_stack_clash_protection_loop_start): Likewise.
|
|
|
22033d |
(emit_stack_clash_protection_loop_end): Likewise.
|
|
|
22033d |
(allocate_dynamic_stack_space): Use get_stack_check_protect.
|
|
|
22033d |
Use anti_adjust_stack_and_probe_stack_clash.
|
|
|
22033d |
* explow.h (compute_stack_clash_protection_loop_data): Prototype.
|
|
|
22033d |
(emit_stack_clash_protection_loop_start): Likewise.
|
|
|
22033d |
(emit_stack_clash_protection_loop_end): Likewise.
|
|
|
22033d |
* rtl.h (get_stack_check_protect): Prototype.
|
|
|
22033d |
* target.def (stack_clash_protection_final_dynamic_probe): New hook.
|
|
|
22033d |
* targhooks.c (default_stack_clash_protection_final_dynamic_probe): New.
|
|
|
22033d |
* targhooks.h (default_stack_clash_protection_final_dynamic_probe):
|
|
|
22033d |
Prototype.
|
|
|
22033d |
* doc/tm.texi.in (TARGET_STACK_CLASH_PROTECTION_FINAL_DYNAMIC_PROBE):
|
|
|
22033d |
Add @hook.
|
|
|
22033d |
* doc/tm.texi: Rebuilt.
|
|
|
22033d |
* config/alpha/alpha.c (alpha_expand_prologue): Likewise.
|
|
|
22033d |
* config/i386/i386.c (ix86_expand_prologue): Likewise.
|
|
|
22033d |
* config/ia64/ia64.c (ia64_expand_prologue): Likewise.
|
|
|
22033d |
* config/mips/mips.c (mips_expand_prologue): Likewise.
|
|
|
22033d |
* config/rs6000/rs6000.c (rs6000_emit_prologue): Likewise.
|
|
|
22033d |
* config/sparc/sparc.c (sparc_expand_prologue): Likewise.
|
|
|
22033d |
(sparc_flat_expand_prologue): Likewise.
|
|
|
22033d |
|
|
|
22033d |
* gcc.dg/stack-check-3.c: New test.
|
|
|
22033d |
|
|
|
22033d |
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@252995 138bc75d-0d04-0410-961f-82ee72b054a4
|
|
|
22033d |
|
|
|
22033d |
diff --git a/gcc/config/alpha/alpha.c b/gcc/config/alpha/alpha.c
|
|
|
22033d |
index 2874b8454a9..5402f5213d6 100644
|
|
|
22033d |
--- a/gcc/config/alpha/alpha.c
|
|
|
22033d |
+++ b/gcc/config/alpha/alpha.c
|
|
|
22033d |
@@ -7625,7 +7625,7 @@ alpha_expand_prologue (void)
|
|
|
22033d |
|
|
|
22033d |
probed_size = frame_size;
|
|
|
22033d |
if (flag_stack_check)
|
|
|
22033d |
- probed_size += STACK_CHECK_PROTECT;
|
|
|
22033d |
+ probed_size += get_stack_check_protect ();
|
|
|
22033d |
|
|
|
22033d |
if (probed_size <= 32768)
|
|
|
22033d |
{
|
|
|
22033d |
diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
|
|
|
22033d |
index e36726ba722..d996fd160e8 100644
|
|
|
22033d |
--- a/gcc/config/i386/i386.c
|
|
|
22033d |
+++ b/gcc/config/i386/i386.c
|
|
|
22033d |
@@ -10544,12 +10544,12 @@ ix86_expand_prologue (void)
|
|
|
22033d |
HOST_WIDE_INT size = allocate;
|
|
|
22033d |
|
|
|
22033d |
if (TARGET_64BIT && size >= (HOST_WIDE_INT) 0x80000000)
|
|
|
22033d |
- size = 0x80000000 - STACK_CHECK_PROTECT - 1;
|
|
|
22033d |
+ size = 0x80000000 - get_stack_check_protect () - 1;
|
|
|
22033d |
|
|
|
22033d |
if (TARGET_STACK_PROBE)
|
|
|
22033d |
- ix86_emit_probe_stack_range (0, size + STACK_CHECK_PROTECT);
|
|
|
22033d |
+ ix86_emit_probe_stack_range (0, size + get_stack_check_protect ());
|
|
|
22033d |
else
|
|
|
22033d |
- ix86_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
|
|
|
22033d |
+ ix86_emit_probe_stack_range (get_stack_check_protect (), size);
|
|
|
22033d |
}
|
|
|
22033d |
}
|
|
|
22033d |
|
|
|
22033d |
diff --git a/gcc/config/ia64/ia64.c b/gcc/config/ia64/ia64.c
|
|
|
22033d |
index 50bbad6661c..390983936e8 100644
|
|
|
22033d |
--- a/gcc/config/ia64/ia64.c
|
|
|
22033d |
+++ b/gcc/config/ia64/ia64.c
|
|
|
22033d |
@@ -3435,7 +3435,7 @@ ia64_expand_prologue (void)
|
|
|
22033d |
current_function_static_stack_size = current_frame_info.total_size;
|
|
|
22033d |
|
|
|
22033d |
if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
|
|
|
22033d |
- ia64_emit_probe_stack_range (STACK_CHECK_PROTECT,
|
|
|
22033d |
+ ia64_emit_probe_stack_range (get_stack_check_protect (),
|
|
|
22033d |
current_frame_info.total_size,
|
|
|
22033d |
current_frame_info.n_input_regs
|
|
|
22033d |
+ current_frame_info.n_local_regs);
|
|
|
22033d |
diff --git a/gcc/config/mips/mips.c b/gcc/config/mips/mips.c
|
|
|
22033d |
index 41c5d6b6b1f..9b7eb678f19 100644
|
|
|
22033d |
--- a/gcc/config/mips/mips.c
|
|
|
22033d |
+++ b/gcc/config/mips/mips.c
|
|
|
22033d |
@@ -10746,7 +10746,7 @@ mips_expand_prologue (void)
|
|
|
22033d |
current_function_static_stack_size = size;
|
|
|
22033d |
|
|
|
22033d |
if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && size)
|
|
|
22033d |
- mips_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
|
|
|
22033d |
+ mips_emit_probe_stack_range (get_stack_check_protect (), size);
|
|
|
22033d |
|
|
|
22033d |
/* Save the registers. Allocate up to MIPS_MAX_FIRST_STACK_STEP
|
|
|
22033d |
bytes beforehand; this is enough to cover the register save area
|
|
|
22033d |
diff --git a/gcc/config/rs6000/rs6000.c b/gcc/config/rs6000/rs6000.c
|
|
|
22033d |
index 15583055895..a9052c6becf 100644
|
|
|
22033d |
--- a/gcc/config/rs6000/rs6000.c
|
|
|
22033d |
+++ b/gcc/config/rs6000/rs6000.c
|
|
|
22033d |
@@ -23214,7 +23214,8 @@ rs6000_emit_prologue (void)
|
|
|
22033d |
current_function_static_stack_size = info->total_size;
|
|
|
22033d |
|
|
|
22033d |
if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && info->total_size)
|
|
|
22033d |
- rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, info->total_size);
|
|
|
22033d |
+ rs6000_emit_probe_stack_range (get_stack_check_protect (),
|
|
|
22033d |
+ info->total_size);
|
|
|
22033d |
|
|
|
22033d |
if (TARGET_FIX_AND_CONTINUE)
|
|
|
22033d |
{
|
|
|
22033d |
diff --git a/gcc/config/sparc/sparc.c b/gcc/config/sparc/sparc.c
|
|
|
22033d |
index e5d326cdf23..e5e93c80261 100644
|
|
|
22033d |
--- a/gcc/config/sparc/sparc.c
|
|
|
22033d |
+++ b/gcc/config/sparc/sparc.c
|
|
|
22033d |
@@ -5431,7 +5431,7 @@ sparc_expand_prologue (void)
|
|
|
22033d |
current_function_static_stack_size = size;
|
|
|
22033d |
|
|
|
22033d |
if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && size)
|
|
|
22033d |
- sparc_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
|
|
|
22033d |
+ sparc_emit_probe_stack_range (get_stack_check_protect (), size);
|
|
|
22033d |
|
|
|
22033d |
if (size == 0)
|
|
|
22033d |
; /* do nothing. */
|
|
|
22033d |
@@ -5533,7 +5533,7 @@ sparc_flat_expand_prologue (void)
|
|
|
22033d |
current_function_static_stack_size = size;
|
|
|
22033d |
|
|
|
22033d |
if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && size)
|
|
|
22033d |
- sparc_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
|
|
|
22033d |
+ sparc_emit_probe_stack_range (get_stack_check_protect (), size);
|
|
|
22033d |
|
|
|
22033d |
if (sparc_save_local_in_regs_p)
|
|
|
22033d |
emit_save_or_restore_local_in_regs (stack_pointer_rtx, SPARC_STACK_BIAS,
|
|
|
22033d |
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
|
|
|
22033d |
index 6b18a2724bc..eeef757bf5b 100644
|
|
|
22033d |
--- a/gcc/doc/tm.texi
|
|
|
22033d |
+++ b/gcc/doc/tm.texi
|
|
|
22033d |
@@ -3571,6 +3571,10 @@ GCC computed the default from the values of the above macros and you will
|
|
|
22033d |
normally not need to override that default.
|
|
|
22033d |
@end defmac
|
|
|
22033d |
|
|
|
22033d |
+@deftypefn {Target Hook} bool TARGET_STACK_CLASH_PROTECTION_FINAL_DYNAMIC_PROBE (rtx @var{residual})
|
|
|
22033d |
+Some targets make optimistic assumptions about the state of stack probing when they emit their prologues. On such targets a probe into the end of any dynamically allocated space is likely required for safety against stack clash style attacks. Define this variable to return nonzero if such a probe is required or zero otherwise. You need not define this macro if it would always have the value zero.
|
|
|
22033d |
+@end deftypefn
|
|
|
22033d |
+
|
|
|
22033d |
@need 2000
|
|
|
22033d |
@node Frame Registers
|
|
|
22033d |
@subsection Registers That Address the Stack Frame
|
|
|
22033d |
diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in
|
|
|
22033d |
index 7d0b3c73b2f..6707ca87236 100644
|
|
|
22033d |
--- a/gcc/doc/tm.texi.in
|
|
|
22033d |
+++ b/gcc/doc/tm.texi.in
|
|
|
22033d |
@@ -3539,6 +3539,8 @@ GCC computed the default from the values of the above macros and you will
|
|
|
22033d |
normally not need to override that default.
|
|
|
22033d |
@end defmac
|
|
|
22033d |
|
|
|
22033d |
+@hook TARGET_STACK_CLASH_PROTECTION_FINAL_DYNAMIC_PROBE
|
|
|
22033d |
+
|
|
|
22033d |
@need 2000
|
|
|
22033d |
@node Frame Registers
|
|
|
22033d |
@subsection Registers That Address the Stack Frame
|
|
|
22033d |
diff --git a/gcc/explow.c b/gcc/explow.c
|
|
|
22033d |
index 7da8bc75f19..2526e8513b7 100644
|
|
|
22033d |
--- a/gcc/explow.c
|
|
|
22033d |
+++ b/gcc/explow.c
|
|
|
22033d |
@@ -40,8 +40,11 @@ along with GCC; see the file COPYING3. If not see
|
|
|
22033d |
#include "target.h"
|
|
|
22033d |
#include "common/common-target.h"
|
|
|
22033d |
#include "output.h"
|
|
|
22033d |
+#include "params.h"
|
|
|
22033d |
+#include "dumpfile.h"
|
|
|
22033d |
|
|
|
22033d |
static rtx break_out_memory_refs (rtx);
|
|
|
22033d |
+static void anti_adjust_stack_and_probe_stack_clash (rtx);
|
|
|
22033d |
|
|
|
22033d |
|
|
|
22033d |
/* Truncate and perhaps sign-extend C as appropriate for MODE. */
|
|
|
22033d |
@@ -1140,6 +1143,29 @@ update_nonlocal_goto_save_area (void)
|
|
|
22033d |
emit_stack_save (SAVE_NONLOCAL, &r_save);
|
|
|
22033d |
}
|
|
|
22033d |
|
|
|
22033d |
+/* Return the number of bytes to "protect" on the stack for -fstack-check.
|
|
|
22033d |
+
|
|
|
22033d |
+ "protect" in the context of -fstack-check means how many bytes we
|
|
|
22033d |
+ should always ensure are available on the stack. More importantly
|
|
|
22033d |
+ this is how many bytes are skipped when probing the stack.
|
|
|
22033d |
+
|
|
|
22033d |
+ On some targets we want to reuse the -fstack-check prologue support
|
|
|
22033d |
+ to give a degree of protection against stack clashing style attacks.
|
|
|
22033d |
+
|
|
|
22033d |
+ In that scenario we do not want to skip bytes before probing as that
|
|
|
22033d |
+ would render the stack clash protections useless.
|
|
|
22033d |
+
|
|
|
22033d |
+ So we never use STACK_CHECK_PROTECT directly. Instead we indirect though
|
|
|
22033d |
+ this helper which allows us to provide different values for
|
|
|
22033d |
+ -fstack-check and -fstack-clash-protection. */
|
|
|
22033d |
+HOST_WIDE_INT
|
|
|
22033d |
+get_stack_check_protect (void)
|
|
|
22033d |
+{
|
|
|
22033d |
+ if (flag_stack_clash_protection)
|
|
|
22033d |
+ return 0;
|
|
|
22033d |
+ return STACK_CHECK_PROTECT;
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
/* Return an rtx representing the address of an area of memory dynamically
|
|
|
22033d |
pushed on the stack.
|
|
|
22033d |
|
|
|
22033d |
@@ -1393,7 +1419,7 @@ allocate_dynamic_stack_space (rtx size, unsigned size_align,
|
|
|
22033d |
probe_stack_range (STACK_OLD_CHECK_PROTECT + STACK_CHECK_MAX_FRAME_SIZE,
|
|
|
22033d |
size);
|
|
|
22033d |
else if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
|
|
|
22033d |
- probe_stack_range (STACK_CHECK_PROTECT, size);
|
|
|
22033d |
+ probe_stack_range (get_stack_check_protect (), size);
|
|
|
22033d |
|
|
|
22033d |
/* Don't let anti_adjust_stack emit notes. */
|
|
|
22033d |
suppress_reg_args_size = true;
|
|
|
22033d |
@@ -1451,6 +1477,8 @@ allocate_dynamic_stack_space (rtx size, unsigned size_align,
|
|
|
22033d |
|
|
|
22033d |
if (flag_stack_check && STACK_CHECK_MOVING_SP)
|
|
|
22033d |
anti_adjust_stack_and_probe (size, false);
|
|
|
22033d |
+ else if (flag_stack_clash_protection)
|
|
|
22033d |
+ anti_adjust_stack_and_probe_stack_clash (size);
|
|
|
22033d |
else
|
|
|
22033d |
anti_adjust_stack (size);
|
|
|
22033d |
|
|
|
22033d |
@@ -1712,6 +1740,219 @@ probe_stack_range (HOST_WIDE_INT first, rtx size)
|
|
|
22033d |
}
|
|
|
22033d |
}
|
|
|
22033d |
|
|
|
22033d |
+/* Compute parameters for stack clash probing a dynamic stack
|
|
|
22033d |
+ allocation of SIZE bytes.
|
|
|
22033d |
+
|
|
|
22033d |
+ We compute ROUNDED_SIZE, LAST_ADDR, RESIDUAL and PROBE_INTERVAL.
|
|
|
22033d |
+
|
|
|
22033d |
+ Additionally we conditionally dump the type of probing that will
|
|
|
22033d |
+ be needed given the values computed. */
|
|
|
22033d |
+
|
|
|
22033d |
+void
|
|
|
22033d |
+compute_stack_clash_protection_loop_data (rtx *rounded_size, rtx *last_addr,
|
|
|
22033d |
+ rtx *residual,
|
|
|
22033d |
+ HOST_WIDE_INT *probe_interval,
|
|
|
22033d |
+ rtx size)
|
|
|
22033d |
+{
|
|
|
22033d |
+ /* Round SIZE down to STACK_CLASH_PROTECTION_PROBE_INTERVAL */
|
|
|
22033d |
+ *probe_interval
|
|
|
22033d |
+ = 1 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL);
|
|
|
22033d |
+ *rounded_size = simplify_gen_binary (AND, Pmode, size,
|
|
|
22033d |
+ GEN_INT (-*probe_interval));
|
|
|
22033d |
+
|
|
|
22033d |
+ /* Compute the value of the stack pointer for the last iteration.
|
|
|
22033d |
+ It's just SP + ROUNDED_SIZE. */
|
|
|
22033d |
+ rtx rounded_size_op = force_operand (*rounded_size, NULL_RTX);
|
|
|
22033d |
+ *last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
|
|
|
22033d |
+ stack_pointer_rtx,
|
|
|
22033d |
+ rounded_size_op),
|
|
|
22033d |
+ NULL_RTX);
|
|
|
22033d |
+
|
|
|
22033d |
+ /* Compute any residuals not allocated by the loop above. Residuals
|
|
|
22033d |
+ are just the ROUNDED_SIZE - SIZE. */
|
|
|
22033d |
+ *residual = simplify_gen_binary (MINUS, Pmode, size, *rounded_size);
|
|
|
22033d |
+
|
|
|
22033d |
+ /* Dump key information to make writing tests easy. */
|
|
|
22033d |
+ if (dump_file)
|
|
|
22033d |
+ {
|
|
|
22033d |
+ if (*rounded_size == CONST0_RTX (Pmode))
|
|
|
22033d |
+ fprintf (dump_file,
|
|
|
22033d |
+ "Stack clash skipped dynamic allocation and probing loop.\n");
|
|
|
22033d |
+ else if (GET_CODE (*rounded_size) == CONST_INT
|
|
|
22033d |
+ && INTVAL (*rounded_size) <= 4 * *probe_interval)
|
|
|
22033d |
+ fprintf (dump_file,
|
|
|
22033d |
+ "Stack clash dynamic allocation and probing inline.\n");
|
|
|
22033d |
+ else if (GET_CODE (*rounded_size) == CONST_INT)
|
|
|
22033d |
+ fprintf (dump_file,
|
|
|
22033d |
+ "Stack clash dynamic allocation and probing in "
|
|
|
22033d |
+ "rotated loop.\n");
|
|
|
22033d |
+ else
|
|
|
22033d |
+ fprintf (dump_file,
|
|
|
22033d |
+ "Stack clash dynamic allocation and probing in loop.\n");
|
|
|
22033d |
+
|
|
|
22033d |
+ if (*residual != CONST0_RTX (Pmode))
|
|
|
22033d |
+ fprintf (dump_file,
|
|
|
22033d |
+ "Stack clash dynamic allocation and probing residuals.\n");
|
|
|
22033d |
+ else
|
|
|
22033d |
+ fprintf (dump_file,
|
|
|
22033d |
+ "Stack clash skipped dynamic allocation and "
|
|
|
22033d |
+ "probing residuals.\n");
|
|
|
22033d |
+ }
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+/* Emit the start of an allocate/probe loop for stack
|
|
|
22033d |
+ clash protection.
|
|
|
22033d |
+
|
|
|
22033d |
+ LOOP_LAB and END_LAB are returned for use when we emit the
|
|
|
22033d |
+ end of the loop.
|
|
|
22033d |
+
|
|
|
22033d |
+ LAST addr is the value for SP which stops the loop. */
|
|
|
22033d |
+void
|
|
|
22033d |
+emit_stack_clash_protection_probe_loop_start (rtx *loop_lab,
|
|
|
22033d |
+ rtx *end_lab,
|
|
|
22033d |
+ rtx last_addr,
|
|
|
22033d |
+ bool rotated)
|
|
|
22033d |
+{
|
|
|
22033d |
+ /* Essentially we want to emit any setup code, the top of loop
|
|
|
22033d |
+ label and the comparison at the top of the loop. */
|
|
|
22033d |
+ *loop_lab = gen_label_rtx ();
|
|
|
22033d |
+ *end_lab = gen_label_rtx ();
|
|
|
22033d |
+
|
|
|
22033d |
+ emit_label (*loop_lab);
|
|
|
22033d |
+ if (!rotated)
|
|
|
22033d |
+ emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, EQ, NULL_RTX,
|
|
|
22033d |
+ Pmode, 1, *end_lab);
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+/* Emit the end of a stack clash probing loop.
|
|
|
22033d |
+
|
|
|
22033d |
+ This consists of just the jump back to LOOP_LAB and
|
|
|
22033d |
+ emitting END_LOOP after the loop. */
|
|
|
22033d |
+
|
|
|
22033d |
+void
|
|
|
22033d |
+emit_stack_clash_protection_probe_loop_end (rtx loop_lab, rtx end_loop,
|
|
|
22033d |
+ rtx last_addr, bool rotated)
|
|
|
22033d |
+{
|
|
|
22033d |
+ if (rotated)
|
|
|
22033d |
+ emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, NE, NULL_RTX,
|
|
|
22033d |
+ Pmode, 1, loop_lab);
|
|
|
22033d |
+ else
|
|
|
22033d |
+ emit_jump (loop_lab);
|
|
|
22033d |
+
|
|
|
22033d |
+ emit_label (end_loop);
|
|
|
22033d |
+
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+/* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
|
|
|
22033d |
+ while probing it. This pushes when SIZE is positive. SIZE need not
|
|
|
22033d |
+ be constant.
|
|
|
22033d |
+
|
|
|
22033d |
+ This is subtly different than anti_adjust_stack_and_probe to try and
|
|
|
22033d |
+ prevent stack-clash attacks
|
|
|
22033d |
+
|
|
|
22033d |
+ 1. It must assume no knowledge of the probing state, any allocation
|
|
|
22033d |
+ must probe.
|
|
|
22033d |
+
|
|
|
22033d |
+ Consider the case of a 1 byte alloca in a loop. If the sum of the
|
|
|
22033d |
+ allocations is large, then this could be used to jump the guard if
|
|
|
22033d |
+ probes were not emitted.
|
|
|
22033d |
+
|
|
|
22033d |
+ 2. It never skips probes, whereas anti_adjust_stack_and_probe will
|
|
|
22033d |
+ skip probes on the first couple PROBE_INTERVALs on the assumption
|
|
|
22033d |
+ they're done elsewhere.
|
|
|
22033d |
+
|
|
|
22033d |
+ 3. It only allocates and probes SIZE bytes, it does not need to
|
|
|
22033d |
+ allocate/probe beyond that because this probing style does not
|
|
|
22033d |
+ guarantee signal handling capability if the guard is hit. */
|
|
|
22033d |
+
|
|
|
22033d |
+static void
|
|
|
22033d |
+anti_adjust_stack_and_probe_stack_clash (rtx size)
|
|
|
22033d |
+{
|
|
|
22033d |
+ /* First ensure SIZE is Pmode. */
|
|
|
22033d |
+ if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
|
|
|
22033d |
+ size = convert_to_mode (Pmode, size, 1);
|
|
|
22033d |
+
|
|
|
22033d |
+ /* We can get here with a constant size on some targets. */
|
|
|
22033d |
+ rtx rounded_size, last_addr, residual;
|
|
|
22033d |
+ HOST_WIDE_INT probe_interval;
|
|
|
22033d |
+ compute_stack_clash_protection_loop_data (&rounded_size, &last_addr,
|
|
|
22033d |
+ &residual, &probe_interval, size);
|
|
|
22033d |
+
|
|
|
22033d |
+ if (rounded_size != CONST0_RTX (Pmode))
|
|
|
22033d |
+ {
|
|
|
22033d |
+ if (INTVAL (rounded_size) <= 4 * probe_interval)
|
|
|
22033d |
+ {
|
|
|
22033d |
+ for (HOST_WIDE_INT i = 0;
|
|
|
22033d |
+ i < INTVAL (rounded_size);
|
|
|
22033d |
+ i += probe_interval)
|
|
|
22033d |
+ {
|
|
|
22033d |
+ anti_adjust_stack (GEN_INT (probe_interval));
|
|
|
22033d |
+
|
|
|
22033d |
+ /* The prologue does not probe residuals. Thus the offset
|
|
|
22033d |
+ here to probe just beyond what the prologue had already
|
|
|
22033d |
+ allocated. */
|
|
|
22033d |
+ emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
|
|
|
22033d |
+ (probe_interval
|
|
|
22033d |
+ - GET_MODE_SIZE (word_mode))));
|
|
|
22033d |
+ emit_insn (gen_blockage ());
|
|
|
22033d |
+ }
|
|
|
22033d |
+ }
|
|
|
22033d |
+ else
|
|
|
22033d |
+ {
|
|
|
22033d |
+ rtx loop_lab, end_loop;
|
|
|
22033d |
+ bool rotate_loop = GET_CODE (rounded_size) == CONST_INT;
|
|
|
22033d |
+ emit_stack_clash_protection_probe_loop_start (&loop_lab, &end_loop,
|
|
|
22033d |
+ last_addr, rotate_loop);
|
|
|
22033d |
+
|
|
|
22033d |
+ anti_adjust_stack (GEN_INT (probe_interval));
|
|
|
22033d |
+
|
|
|
22033d |
+ /* The prologue does not probe residuals. Thus the offset here
|
|
|
22033d |
+ to probe just beyond what the prologue had already allocated. */
|
|
|
22033d |
+ emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
|
|
|
22033d |
+ (probe_interval
|
|
|
22033d |
+ - GET_MODE_SIZE (word_mode))));
|
|
|
22033d |
+
|
|
|
22033d |
+ emit_stack_clash_protection_probe_loop_end (loop_lab, end_loop,
|
|
|
22033d |
+ last_addr, rotate_loop);
|
|
|
22033d |
+ emit_insn (gen_blockage ());
|
|
|
22033d |
+ }
|
|
|
22033d |
+ }
|
|
|
22033d |
+
|
|
|
22033d |
+ if (residual != CONST0_RTX (Pmode))
|
|
|
22033d |
+ {
|
|
|
22033d |
+ rtx x = force_reg (Pmode, plus_constant (Pmode, residual,
|
|
|
22033d |
+ -GET_MODE_SIZE (word_mode)));
|
|
|
22033d |
+ anti_adjust_stack (residual);
|
|
|
22033d |
+ emit_stack_probe (gen_rtx_PLUS (Pmode, stack_pointer_rtx, x));
|
|
|
22033d |
+ emit_insn (gen_blockage ());
|
|
|
22033d |
+ }
|
|
|
22033d |
+
|
|
|
22033d |
+ /* Some targets make optimistic assumptions in their prologues about
|
|
|
22033d |
+ how the caller may have probed the stack. Make sure we honor
|
|
|
22033d |
+ those assumptions when needed. */
|
|
|
22033d |
+ if (size != CONST0_RTX (Pmode)
|
|
|
22033d |
+ && targetm.stack_clash_protection_final_dynamic_probe (residual))
|
|
|
22033d |
+ {
|
|
|
22033d |
+ /* Ideally we would just probe at *sp. However, if SIZE is not
|
|
|
22033d |
+ a compile-time constant, but is zero at runtime, then *sp
|
|
|
22033d |
+ might hold live data. So probe at *sp if we know that
|
|
|
22033d |
+ an allocation was made, otherwise probe into the red zone
|
|
|
22033d |
+ which is obviously undesirable. */
|
|
|
22033d |
+ if (GET_CODE (size) == CONST_INT)
|
|
|
22033d |
+ {
|
|
|
22033d |
+ emit_stack_probe (stack_pointer_rtx);
|
|
|
22033d |
+ emit_insn (gen_blockage ());
|
|
|
22033d |
+ }
|
|
|
22033d |
+ else
|
|
|
22033d |
+ {
|
|
|
22033d |
+ emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
|
|
|
22033d |
+ -GET_MODE_SIZE (word_mode)));
|
|
|
22033d |
+ emit_insn (gen_blockage ());
|
|
|
22033d |
+ }
|
|
|
22033d |
+ }
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+
|
|
|
22033d |
/* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
|
|
|
22033d |
while probing it. This pushes when SIZE is positive. SIZE need not
|
|
|
22033d |
be constant. If ADJUST_BACK is true, adjust back the stack pointer
|
|
|
22033d |
diff --git a/gcc/rtl.h b/gcc/rtl.h
|
|
|
22033d |
index 91f3387c701..ab8ec27418d 100644
|
|
|
22033d |
--- a/gcc/rtl.h
|
|
|
22033d |
+++ b/gcc/rtl.h
|
|
|
22033d |
@@ -1756,6 +1756,17 @@ extern int currently_expanding_to_rtl;
|
|
|
22033d |
/* In explow.c */
|
|
|
22033d |
extern HOST_WIDE_INT trunc_int_for_mode (HOST_WIDE_INT, enum machine_mode);
|
|
|
22033d |
extern rtx plus_constant (enum machine_mode, rtx, HOST_WIDE_INT);
|
|
|
22033d |
+extern HOST_WIDE_INT get_stack_check_protect (void);
|
|
|
22033d |
+
|
|
|
22033d |
+/* Support for building allocation/probing loops for stack-clash
|
|
|
22033d |
+ protection of dyamically allocated stack space. */
|
|
|
22033d |
+extern void compute_stack_clash_protection_loop_data (rtx *, rtx *, rtx *,
|
|
|
22033d |
+ HOST_WIDE_INT *, rtx);
|
|
|
22033d |
+extern void emit_stack_clash_protection_probe_loop_start (rtx *, rtx *,
|
|
|
22033d |
+ rtx, bool);
|
|
|
22033d |
+extern void emit_stack_clash_protection_probe_loop_end (rtx, rtx,
|
|
|
22033d |
+ rtx, bool);
|
|
|
22033d |
+
|
|
|
22033d |
|
|
|
22033d |
/* In rtl.c */
|
|
|
22033d |
extern rtx rtx_alloc_stat (RTX_CODE MEM_STAT_DECL);
|
|
|
22033d |
diff --git a/gcc/target.def b/gcc/target.def
|
|
|
22033d |
index 4d6081c3121..eb2bd46f7a1 100644
|
|
|
22033d |
--- a/gcc/target.def
|
|
|
22033d |
+++ b/gcc/target.def
|
|
|
22033d |
@@ -2580,6 +2580,13 @@ DEFHOOK
|
|
|
22033d |
void, (void),
|
|
|
22033d |
hook_void_void)
|
|
|
22033d |
|
|
|
22033d |
+DEFHOOK
|
|
|
22033d |
+(stack_clash_protection_final_dynamic_probe,
|
|
|
22033d |
+ "Some targets make optimistic assumptions about the state of stack probing when they emit their prologues. On such targets a probe into the end of any dynamically allocated space is likely required for safety against stack clash style attacks. Define this variable to return nonzero if such a probe is required or zero otherwise. You need not define this macro if it would always have the value zero.",
|
|
|
22033d |
+ bool, (rtx residual),
|
|
|
22033d |
+ default_stack_clash_protection_final_dynamic_probe)
|
|
|
22033d |
+
|
|
|
22033d |
+
|
|
|
22033d |
/* Functions specific to the C family of frontends. */
|
|
|
22033d |
#undef HOOK_PREFIX
|
|
|
22033d |
#define HOOK_PREFIX "TARGET_C_"
|
|
|
22033d |
diff --git a/gcc/targhooks.c b/gcc/targhooks.c
|
|
|
22033d |
index f6aa9907225..be23875538d 100644
|
|
|
22033d |
--- a/gcc/targhooks.c
|
|
|
22033d |
+++ b/gcc/targhooks.c
|
|
|
22033d |
@@ -1557,4 +1557,10 @@ default_canonicalize_comparison (int *, rtx *, rtx *, bool)
|
|
|
22033d |
{
|
|
|
22033d |
}
|
|
|
22033d |
|
|
|
22033d |
+bool
|
|
|
22033d |
+default_stack_clash_protection_final_dynamic_probe (rtx residual ATTRIBUTE_UNUSED)
|
|
|
22033d |
+{
|
|
|
22033d |
+ return 0;
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
#include "gt-targhooks.h"
|
|
|
22033d |
diff --git a/gcc/targhooks.h b/gcc/targhooks.h
|
|
|
22033d |
index b64274d3ff9..4acf33fae08 100644
|
|
|
22033d |
--- a/gcc/targhooks.h
|
|
|
22033d |
+++ b/gcc/targhooks.h
|
|
|
22033d |
@@ -195,3 +195,4 @@ extern const char *default_pch_valid_p (const void *, size_t);
|
|
|
22033d |
extern void default_asm_output_ident_directive (const char*);
|
|
|
22033d |
|
|
|
22033d |
extern bool default_member_type_forces_blk (const_tree, enum machine_mode);
|
|
|
22033d |
+extern bool default_stack_clash_protection_final_dynamic_probe (rtx);
|
|
|
22033d |
diff --git a/gcc/testsuite/gcc.dg/stack-check-3.c b/gcc/testsuite/gcc.dg/stack-check-3.c
|
|
|
22033d |
new file mode 100644
|
|
|
22033d |
index 00000000000..58fb65649ee
|
|
|
22033d |
--- /dev/null
|
|
|
22033d |
+++ b/gcc/testsuite/gcc.dg/stack-check-3.c
|
|
|
22033d |
@@ -0,0 +1,86 @@
|
|
|
22033d |
+/* The goal here is to ensure that dynamic allocations via vlas or
|
|
|
22033d |
+ alloca calls receive probing.
|
|
|
22033d |
+
|
|
|
22033d |
+ Scanning the RTL or assembly code seems like insanity here as does
|
|
|
22033d |
+ checking for particular allocation sizes and probe offsets. For
|
|
|
22033d |
+ now we just verify that there's an allocation + probe loop and
|
|
|
22033d |
+ residual allocation + probe for f?. */
|
|
|
22033d |
+
|
|
|
22033d |
+/* { dg-do compile } */
|
|
|
22033d |
+/* { dg-options "-O2 -fstack-clash-protection -fdump-rtl-expand -fno-optimize-sibling-calls --param stack-clash-protection-probe-interval=4096 --param stack-clash-protection-guard-size=4096" } */
|
|
|
22033d |
+/* { dg-require-effective-target supports_stack_clash_protection } */
|
|
|
22033d |
+
|
|
|
22033d |
+__attribute__((noinline, noclone)) void
|
|
|
22033d |
+foo (char *p)
|
|
|
22033d |
+{
|
|
|
22033d |
+ asm volatile ("" : : "r" (p) : "memory");
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+/* Simple VLA, no other locals. */
|
|
|
22033d |
+__attribute__((noinline, noclone)) void
|
|
|
22033d |
+f0 (int x)
|
|
|
22033d |
+{
|
|
|
22033d |
+ char vla[x];
|
|
|
22033d |
+ foo (vla);
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+/* Simple VLA, small local frame. */
|
|
|
22033d |
+__attribute__((noinline, noclone)) void
|
|
|
22033d |
+f1 (int x)
|
|
|
22033d |
+{
|
|
|
22033d |
+ char locals[128];
|
|
|
22033d |
+ char vla[x];
|
|
|
22033d |
+ foo (vla);
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+/* Small constant alloca, no other locals. */
|
|
|
22033d |
+__attribute__((noinline, noclone)) void
|
|
|
22033d |
+f2 (int x)
|
|
|
22033d |
+{
|
|
|
22033d |
+ char *vla = __builtin_alloca (128);
|
|
|
22033d |
+ foo (vla);
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+/* Big constant alloca, small local frame. */
|
|
|
22033d |
+__attribute__((noinline, noclone)) void
|
|
|
22033d |
+f3 (int x)
|
|
|
22033d |
+{
|
|
|
22033d |
+ char locals[128];
|
|
|
22033d |
+ char *vla = __builtin_alloca (16384);
|
|
|
22033d |
+ foo (vla);
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+/* Big constant alloca, small local frame. */
|
|
|
22033d |
+__attribute__((noinline, noclone)) void
|
|
|
22033d |
+f3a (int x)
|
|
|
22033d |
+{
|
|
|
22033d |
+ char locals[128];
|
|
|
22033d |
+ char *vla = __builtin_alloca (32768);
|
|
|
22033d |
+ foo (vla);
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+/* Nonconstant alloca, no other locals. */
|
|
|
22033d |
+__attribute__((noinline, noclone)) void
|
|
|
22033d |
+f4 (int x)
|
|
|
22033d |
+{
|
|
|
22033d |
+ char *vla = __builtin_alloca (x);
|
|
|
22033d |
+ foo (vla);
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+/* Nonconstant alloca, small local frame. */
|
|
|
22033d |
+__attribute__((noinline, noclone)) void
|
|
|
22033d |
+f5 (int x)
|
|
|
22033d |
+{
|
|
|
22033d |
+ char locals[128];
|
|
|
22033d |
+ char *vla = __builtin_alloca (x);
|
|
|
22033d |
+ foo (vla);
|
|
|
22033d |
+}
|
|
|
22033d |
+
|
|
|
22033d |
+/* { dg-final { scan-rtl-dump-times "allocation and probing residuals" 7 "expand" } } */
|
|
|
22033d |
+
|
|
|
22033d |
+
|
|
|
22033d |
+/* { dg-final { scan-rtl-dump-times "allocation and probing in loop" 7 "expand" { target callee_realigns_stack } } } */
|
|
|
22033d |
+/* { dg-final { scan-rtl-dump-times "allocation and probing in loop" 4 "expand" { target { ! callee_realigns_stack } } } } */
|
|
|
22033d |
+/* { dg-final { scan-rtl-dump-times "allocation and probing in rotated loop" 1 "expand" { target { ! callee_realigns_stack } } } } */
|
|
|
22033d |
+/* { dg-final { scan-rtl-dump-times "allocation and probing inline" 1 "expand" { target { ! callee_realigns_stack } } } } */
|
|
|
22033d |
+/* { dg-final { scan-rtl-dump-times "skipped dynamic allocation and probing loop" 1 "expand" { target { ! callee_realigns_stack } } } } */
|