Hi!

On Wed, Nov 15, 2017 at 08:31:00AM +0100, Martin Liška wrote:
> On 11/08/2017 05:31 PM, Jeff Law wrote:
> > I don't see an updated patch in this thread?  THe last message I see is
> > this one where you indicate you're going to tweak the patch and re-test.
> > 
> > Jeff
> 
> Yes, I tweaked and tested following patch.
> 
> Martin

> >From a369ac78b887e219a375e17d6817c1f744e71779 Mon Sep 17 00:00:00 2001
> From: marxin <mli...@suse.cz>
> Date: Thu, 19 Oct 2017 13:38:01 +0200
> Subject: [PATCH] Fix UBSAN errors in dse.c (PR rtl-optimization/82044).
> 
> gcc/ChangeLog:
> 
> 2017-10-19  Martin Liska  <mli...@suse.cz>
> 
>       PR rtl-optimization/82044
>       PR tree-optimization/82042
>       * dse.c (check_mem_read_rtx): Check for overflow.

Unfortunately this patch broke i686-linux bootstrap, during stage2
libgcc configure fails due to numerous ICEs.

There are 2 problems with the patch:
1) if the mode of the read is BLKmode, then width is set to -1,
so offset > HOST_WIDE_INT_MAX - width invokes UB at compile time
and is true for any offset > HOST_WIDE_INT_MIN if the compiler wraps
the result around.
2) clear_rhs_from_active_local_stores () is the punt action in record_store,
but not in check_mem_read_rtx, where e.g. a few lines above it if
canon_address fails it does add_wild_read instead.

The following patch fixes those two issues and adds similar overflow
check to record_store too (in that spot width is always non-negative, so
we don't need a special width == -1 handling).

Bootstrapped successfully on i686-linux, ok for trunk if it passes regtest
there (and pending x86_64-linux bootstrap + regtest)?

2017-11-21  Jakub Jelinek  <ja...@redhat.com>

        PR rtl-optimization/82044
        PR tree-optimization/82042
        * dse.c (record_store): Check for overflow.
        (check_mem_read_rtx): Properly check for overflow if width == -1, call
        add_wild_read instead of clear_rhs_from_active_local_stores on
        overflow and log it into dump_file.

--- gcc/dse.c.jj        2017-11-21 23:18:18.000000000 +0100
+++ gcc/dse.c   2017-11-21 23:28:08.952439915 +0100
@@ -1342,6 +1342,12 @@ record_store (rtx body, bb_info_t bb_inf
   else
     width = GET_MODE_SIZE (GET_MODE (mem));
 
+  if (offset > HOST_WIDE_INT_MAX - width)
+    {
+      clear_rhs_from_active_local_stores ();
+      return 0;
+    }
+
   if (group_id >= 0)
     {
       /* In the restrictive case where the base is a constant or the
@@ -1981,9 +1987,13 @@ check_mem_read_rtx (rtx *loc, bb_info_t
   else
     width = GET_MODE_SIZE (GET_MODE (mem));
 
-  if (offset > HOST_WIDE_INT_MAX - width)
+  if (width == -1
+      ? offset == HOST_WIDE_INT_MIN
+      : offset > HOST_WIDE_INT_MAX - width)
     {
-      clear_rhs_from_active_local_stores ();
+      if (dump_file && (dump_flags & TDF_DETAILS))
+       fprintf (dump_file, " adding wild read, due to overflow.\n");
+      add_wild_read (bb_info);
       return;
     }
 


        Jakub

Reply via email to