public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix UBSAN errors in ipa-cp.
@ 2020-09-23 12:19 Martin Liška
  2020-09-23 12:32 ` Jan Hubicka
  0 siblings, 1 reply; 8+ messages in thread
From: Martin Liška @ 2020-09-23 12:19 UTC (permalink / raw)
  To: gcc-patches; +Cc: Martin Jambor, Jan Hubicka

I see the following UBSAN errors:

./xgcc -B. /home/marxin/Programming/gcc/gcc/testsuite/g++.dg/ipa/pr96806.C -std=c++11 -O -fipa-cp -fipa-cp-clone --param=ipa-cp-max-recursive-depth=94 --param=logical-op-non-short-circuit=0
/home/marxin/Programming/gcc2/gcc/ipa-cp.c:3866:20: runtime error: signed integer overflow: 64 + 2147483584 cannot be represented in type 'int'
/home/marxin/Programming/gcc2/gcc/ipa-cp.c:3843:16: runtime error: signed integer overflow: -2147483648 + -2147483648 cannot be represented in type 'int'
/home/marxin/Programming/gcc2/gcc/ipa-cp.c:3864:20: runtime error: signed integer overflow: 1 + 2147483647 cannot be represented in type 'int'

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

gcc/ChangeLog:

	* ipa-cp.c (safe_add): Handle also very small negative values.
	(value_topo_info::propagate_effects): Use properly safe_add.
---
  gcc/ipa-cp.c | 11 +++++++----
  1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/gcc/ipa-cp.c b/gcc/ipa-cp.c
index b3e7d41ea10..e39ee28726d 100644
--- a/gcc/ipa-cp.c
+++ b/gcc/ipa-cp.c
@@ -3832,13 +3832,15 @@ propagate_constants_topo (class ipa_topo_info *topo)
  
  
  /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
-   the bigger one if otherwise.  */
+   the bigger one if otherwise.  Similarly for negative numbers.  */
  
  static int
  safe_add (int a, int b)
  {
    if (a > INT_MAX/2 || b > INT_MAX/2)
      return a > b ? a : b;
+  else if (a < -INT_MAX/2 || b < -INT_MAX/2)
+    return a > b ? b : a;
    else
      return a + b;
  }
@@ -3861,9 +3863,10 @@ value_topo_info<valtype>::propagate_effects ()
  
        for (val = base; val; val = val->scc_next)
  	{
-	  time = safe_add (time,
-			   val->local_time_benefit + val->prop_time_benefit);
-	  size = safe_add (size, val->local_size_cost + val->prop_size_cost);
+	  time = safe_add (time, val->local_time_benefit);
+	  time = safe_add (time, val->prop_time_benefit);
+	  size = safe_add (size, val->local_size_cost);
+	  size = safe_add (size, val->prop_size_cost);
  	}
  
        for (val = base; val; val = val->scc_next)
-- 
2.28.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-09-24  7:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-23 12:19 [PATCH] Fix UBSAN errors in ipa-cp Martin Liška
2020-09-23 12:32 ` Jan Hubicka
2020-09-23 12:39   ` Martin Liška
2020-09-23 13:50     ` Martin Liška
2020-09-23 13:57       ` Jan Hubicka
2020-09-23 14:32         ` Martin Jambor
2020-09-23 15:02       ` Martin Jambor
2020-09-24  7:20         ` Martin Liška

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).