public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Eric Botcazou <ebotcazou@adacore.com>
To: Richard Biener <richard.guenther@gmail.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [patch] Fix segfault caused by spurious constant overflow
Date: Fri, 31 May 2019 16:17:00 -0000	[thread overview]
Message-ID: <3613688.ht5AtZ0BRG@polaris> (raw)
In-Reply-To: <CAFiYyc2ogaZS6hAjownaJkbG1j7m1Aoufnt3K5tRJ1Zo6djfJQ@mail.gmail.com>

> Hmm, ISTR we had such mitigations in place (or have) elsewhere keying
> on the most significant bit set instead of power-of-two.

fold_plusminus_mult_expr only factors out for power-of-two:

      if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
	  /* The remainder should not be a constant, otherwise we
	     end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
	     increased the number of multiplications necessary.  */
	  && TREE_CODE (arg10) != INTEGER_CST)

> But your case
> likely recurses and runs into the extract_multiv limiting to eventually
> stop, even for (N + 4) * 8, right?

Yes, it oscillates between extract_multiv and fold_plusminus_mult_expr until 
reaching the maximal depth.

> If so shouldn't we prevent this even for !TYPE_OVERFLOW_WRAPS?  Also
> 
> +         && !(tree_fits_shwi_p (c)
> +              && exact_log2 (absu_hwi (tree_to_shwi (c))) > 0))

The code only distributes for TYPE_OVERFLOW_WRAPS though:

      /* The last case is if we are a multiply.  In that case, we can
	 apply the distributive law to commute the multiply and addition
	 if the multiplication of the constants doesn't overflow
	 and overflow is defined.  With undefined overflow
	 op0 * c might overflow, while (op0 + orig_op1) * c doesn't.  */
      if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
	return fold_build2 (tcode, ctype,
			    fold_build2 (code, ctype,
					 fold_convert (ctype, op0),
					 fold_convert (ctype, c)),
			    op1);

> is better written as
> 
>            && exact_log2 (wi::to_wide (c)) > 0
> 
> not sure why the sizetype constant for you fits in a signed HWI
> or you need to compute its absolute value.  Eventually you
> need to use wi::abs(wide_int::from (wi::to_wide (c), TYPE_PRECISION
> (TREE_TYPE (c)), SIGNED))
> or so.

This is just mirrored on what fold_plusminus_mult_expr does.

-- 
Eric Botcazou

  reply	other threads:[~2019-05-31 16:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-31 10:31 Eric Botcazou
2019-05-31 11:44 ` Richard Biener
2019-05-31 16:17   ` Eric Botcazou [this message]
2019-06-03 10:38   ` Eric Botcazou
2019-06-04 15:06     ` Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3613688.ht5AtZ0BRG@polaris \
    --to=ebotcazou@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=richard.guenther@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).