On Wed, 26 Feb 2025, Jakub Jelinek wrote:

> Hi!
> 
> This PR is about ubsan error on the c - cx1 + cy1 evaluation in the first
> hunk.
> 
> The following patch hopefully fixes that by doing the additions/subtractions
> in poly_uint64 rather than poly_int64.  Or shall we instead perform it in
> offset_int and watch for overflows and punt somehow for those?

I think when we have the offset computation overflow ignoring such
overflow will make the memrefs_conflict_p give possibly wrong
answers.  In the PR you say cselib now has those
-9223372036854775807ish offsets from sp, why does it do alias queries
with those clearly invalid offsets?

So yes, I think we need to punt on overflow.  Using poly_offset_int
should work but it comes at a cost.  Does poly_wide_int have the same
wide-int like overflow overloads?

> Or shall we just treat this way only the first case where it is
> adding/subtracting 3 numbers and not just 2, so there is at least a chance
> the overflow is just temporary?
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux (but just normal
> bootstrap, not ubsan one), ok for trunk?
> 
> 2025-02-26  Jakub Jelinek  <ja...@redhat.com>
> 
>       PR middle-end/118819
>       * alias.cc (memrefs_conflict_p): Perform arithmetics on c in
>       poly_uint64 type rather than poly_int64 to avoid compile time
>       UB.
> 
> --- gcc/alias.cc.jj   2025-01-02 11:23:24.000000000 +0100
> +++ gcc/alias.cc      2025-02-25 12:43:16.655507666 +0100
> @@ -2537,12 +2537,14 @@ memrefs_conflict_p (poly_int64 xsize, rt
>           {
>             if (poly_int_rtx_p (y1, &cy1))
>               return memrefs_conflict_p (xsize, x0, ysize, y0,
> -                                        c - cx1 + cy1);
> +                                        (poly_uint64) c - cx1 + cy1);
>             else
> -             return memrefs_conflict_p (xsize, x0, ysize, y, c - cx1);
> +             return memrefs_conflict_p (xsize, x0, ysize, y,
> +                                        (poly_uint64) c - cx1);
>           }
>         else if (poly_int_rtx_p (y1, &cy1))
> -         return memrefs_conflict_p (xsize, x, ysize, y0, c + cy1);
> +         return memrefs_conflict_p (xsize, x, ysize, y0,
> +                                    (poly_uint64) c + cy1);
>  
>         return -1;
>       }
> @@ -2563,7 +2565,8 @@ memrefs_conflict_p (poly_int64 xsize, rt
>  
>        poly_int64 cy1;
>        if (poly_int_rtx_p (y1, &cy1))
> -     return memrefs_conflict_p (xsize, x, ysize, y0, c + cy1);
> +     return memrefs_conflict_p (xsize, x, ysize, y0,
> +                                (poly_uint64) c + cy1);
>        else
>       return -1;
>      }
> @@ -2643,7 +2646,7 @@ memrefs_conflict_p (poly_int64 xsize, rt
>        poly_int64 cx, cy;
>        if (poly_int_rtx_p (x, &cx) && poly_int_rtx_p (y, &cy))
>       {
> -       c += cy - cx;
> +       c += (poly_uint64) cy - cx;
>         return offset_overlap_p (c, xsize, ysize);
>       }
>  
> 
>       Jakub
> 
> 

-- 
Richard Biener <rguent...@suse.de>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)

Reply via email to