From 6c34a0921352e7ed6058aadf4c950ec700375794 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Wed, 22 Apr 2009 09:01:43 +0000 Subject: [PATCH] sh.c (shift_insns_rtx, [...]): Truncate shift counts to avoid out-of-bounds array accesses. 2009-04-22 Paolo Bonzini * config/sh/sh.c (shift_insns_rtx, shiftcosts, gen_shifty_op, sh_dynamicalize_shift_p, shl_and_scr_length): Truncate shift counts to avoid out-of-bounds array accesses. From-SVN: r146553 --- gcc/ChangeLog | 6 ++++++ gcc/config/sh/sh.c | 17 +++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 2357c2a6181..d7270194d26 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,11 @@ 2009-04-22 Paolo Bonzini + * config/sh/sh.c (shift_insns_rtx, shiftcosts, gen_shifty_op, + sh_dynamicalize_shift_p, shl_and_scr_length): Truncate + shift counts to avoid out-of-bounds array accesses. + +2009-04-22 Paolo Bonzini + * config/sparc/sparc.h (POINTER_SIZE): Fix comment. (Pmode): Move above. diff --git a/gcc/config/sh/sh.c b/gcc/config/sh/sh.c index 9d573e044cb..ce45898ff2d 100644 --- a/gcc/config/sh/sh.c +++ b/gcc/config/sh/sh.c @@ -2247,7 +2247,7 @@ int shift_insns_rtx (rtx insn) { rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0)); - int shift_count = INTVAL (XEXP (set_src, 1)); + int shift_count = INTVAL (XEXP (set_src, 1)) & 31; enum rtx_code shift_code = GET_CODE (set_src); switch (shift_code) @@ -2286,9 +2286,10 @@ shiftcosts (rtx x) if (GET_CODE (XEXP (x, 1)) != CONST_INT) return SH_DYNAMIC_SHIFT_COST; - value = INTVAL (XEXP (x, 1)); + /* Otherwise, return the true cost in instructions. Cope with out of range + shift counts more or less arbitrarily. */ + value = INTVAL (XEXP (x, 1)) & 31; - /* Otherwise, return the true cost in instructions. */ if (GET_CODE (x) == ASHIFTRT) { int cost = ashiftrt_insns[value]; @@ -2637,7 +2638,7 @@ gen_shifty_op (int code, rtx *operands) int max, i; /* Truncate the shift count in case it is out of bounds. */ - value = value & 0x1f; + value = value & 31; if (value == 31) { @@ -2790,7 +2791,7 @@ expand_ashiftrt (rtx *operands) int sh_dynamicalize_shift_p (rtx count) { - return shift_insns[INTVAL (count)] > 1 + SH_DYNAMIC_SHIFT_COST; + return shift_insns[INTVAL (count) & 31] > 1 + SH_DYNAMIC_SHIFT_COST; } /* Try to find a good way to implement the combiner pattern @@ -2950,11 +2951,11 @@ int shl_and_scr_length (rtx insn) { rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0)); - int len = shift_insns[INTVAL (XEXP (set_src, 1))]; + int len = shift_insns[INTVAL (XEXP (set_src, 1)) & 31]; rtx op = XEXP (set_src, 0); - len += shift_insns[INTVAL (XEXP (op, 1))] + 1; + len += shift_insns[INTVAL (XEXP (op, 1)) & 31] + 1; op = XEXP (XEXP (op, 0), 0); - return len + shift_insns[INTVAL (XEXP (op, 1))]; + return len + shift_insns[INTVAL (XEXP (op, 1)) & 31]; } /* Generate rtl for instructions for which shl_and_kind advised a particular -- 2.11.4.GIT