Hi!

With -ftrapv -fnon-call-exceptions -fexceptions +/-/* arithmetics can have
EH edges, and ubsan code didn't take it into account.
Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk/6.2?

2016-08-12  Jakub Jelinek  <ja...@redhat.com>

        PR c/71512
        * ubsan.c (instrument_si_overflow): Pass true instead of false
        to gsi_replace.
        (pass_ubsan::execute): Call gimple_purge_dead_eh_edges at the end
        of bbs.  Return TODO_cleanup_cfg if any returned true.

        * g++.dg/ubsan/pr71512.C: New test.
        * c-c++-common/ubsan/pr71512-1.c: New test.
        * c-c++-common/ubsan/pr71512-2.c: New test.

--- gcc/ubsan.c.jj      2016-07-19 18:44:25.000000000 +0200
+++ gcc/ubsan.c 2016-08-12 14:13:02.965390346 +0200
@@ -1292,7 +1292,7 @@ instrument_si_overflow (gimple_stmt_iter
                                      ? IFN_UBSAN_CHECK_SUB
                                      : IFN_UBSAN_CHECK_MUL, 2, a, b);
       gimple_call_set_lhs (g, lhs);
-      gsi_replace (&gsi, g, false);
+      gsi_replace (&gsi, g, true);
       break;
     case NEGATE_EXPR:
       /* Represent i = -u;
@@ -1302,7 +1302,7 @@ instrument_si_overflow (gimple_stmt_iter
       b = gimple_assign_rhs1 (stmt);
       g = gimple_build_call_internal (IFN_UBSAN_CHECK_SUB, 2, a, b);
       gimple_call_set_lhs (g, lhs);
-      gsi_replace (&gsi, g, false);
+      gsi_replace (&gsi, g, true);
       break;
     case ABS_EXPR:
       /* Transform i = ABS_EXPR<u>;
@@ -1955,6 +1955,7 @@ pass_ubsan::execute (function *fun)
 {
   basic_block bb;
   gimple_stmt_iterator gsi;
+  unsigned int ret = 0;
 
   initialize_sanitizer_builtins ();
 
@@ -2013,8 +2014,10 @@ pass_ubsan::execute (function *fun)
 
          gsi_next (&gsi);
        }
+      if (gimple_purge_dead_eh_edges (bb))
+       ret = TODO_cleanup_cfg;
     }
-  return 0;
+  return ret;
 }
 
 } // anon namespace
--- gcc/testsuite/g++.dg/ubsan/pr71512.C.jj     2016-08-12 14:20:55.547288099 
+0200
+++ gcc/testsuite/g++.dg/ubsan/pr71512.C        2016-08-12 14:20:07.000000000 
+0200
@@ -0,0 +1,20 @@
+/* PR c/71512 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -ftrapv -fnon-call-exceptions -fsanitize=undefined" } */
+
+bool
+foo (int *x, int *y, int *z)
+{
+  try
+    {
+      x[0] = y[0] + z[0];
+      x[1] = y[1] - z[1];
+      x[2] = y[2] * z[2];
+      x[3] = -y[3];
+    }
+  catch (...)
+    {
+      return true;
+    }
+  return false;
+}
--- gcc/testsuite/c-c++-common/ubsan/pr71512-1.c.jj     2016-08-12 
14:22:49.729807245 +0200
+++ gcc/testsuite/c-c++-common/ubsan/pr71512-1.c        2016-08-12 
14:22:40.423927934 +0200
@@ -0,0 +1,5 @@
+/* PR c/71512 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fnon-call-exceptions -ftrapv -fexceptions 
-fsanitize=undefined" } */
+
+#include "../../gcc.dg/pr44545.c"
--- gcc/testsuite/c-c++-common/ubsan/pr71512-2.c.jj     2016-08-12 
14:22:49.000000000 +0200
+++ gcc/testsuite/c-c++-common/ubsan/pr71512-2.c        2016-08-12 
14:23:30.541277953 +0200
@@ -0,0 +1,5 @@
+/* PR c/71512 */
+/* { dg-do compile } */
+/* { dg-options "-O -fexceptions -fnon-call-exceptions -ftrapv 
-fsanitize=undefined" } */
+
+#include "../../gcc.dg/pr47086.c"

        Jakub

Reply via email to