public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/114700] middle-end optimization generates causes -fsanitize=undefined not to happen in some cases
Date: Mon, 15 Apr 2024 06:49:20 +0000	[thread overview]
Message-ID: <bug-114700-4-4bCSrPkWnX@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-114700-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114700

--- Comment #18 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Hu Lin from comment #17)
> (In reply to Jakub Jelinek from comment #16)
> > (In reply to Hu Lin from comment #11)
> > > I think it doesn't mean that's not a bug with -ftrapv, it should preserve
> > > all overflow traps. Because it doesn't work, we use -fsanitize=undefined
> > > instead of it.
> > > 
> > > refer: Gcc's trapv is known not always to work correctly.
> > 
> > No, -ftrapv isn't a debugging tool.  There is no overflow in the expression
> > that GCC actually evaluates (into which the expression has been optimized).
> > If you have overflow in an expression that is never used, GCC with -ftrapv
> > will also
> > eliminate it as unused and won't diagnose the trap.
> > -fsanitize=undefined behaves in that case actually the same with -O1 and
> > higher (intentionally, to decrease the cost of the sanitization).  So, one
> > needs to use -O0 -fsanitize=undefined to get as many cases of UB in the
> > program diagnosed as possible.
> 
> OK, that look like GCC's -ftrapv is not the same as clang's. Then my added
> condition should be (optimize || !TYPE_OVERFLOW_SANITIZED (type)). 

Why?  Just !TYPE_OVERFLOW_SANITIZED (type).

> > When a pattern already has one if, can't you just add that to the preexisting if rather than adding yet another one.
> 
> I made a mistake on this line, it should be
> +   (if (!TYPE_OVERFLOW_SANITIZED (type))
>      (if (!ANY_INTEGRAL_TYPE_P (type)
>          || TYPE_OVERFLOW_WRAPS (type))
>       (negate (view_convert @1))
>       (view_convert (negate @1))))
> 
> I can't just modify the preexisting if, the optimization shouldn't be used
> with -fsanitize=undefined.

TYPE_OVERFLOW_SANITIZED is
#define TYPE_OVERFLOW_SANITIZED(TYPE)                   \
  (INTEGRAL_TYPE_P (TYPE)                               \
   && !TYPE_OVERFLOW_WRAPS (TYPE)                       \
   && (flag_sanitize & SANITIZE_SI_OVERFLOW))
so, it isn't true for non-integral types, nor for TYPE_OVERFLOW_WRAPS types.
So, if you want to avoid the (view_convert (negate @1)), just add (if
!TYPE_OVERFLOW_SANITIZED (type)) above the (view_convert (negate @1)).  But in
each case, you want to be careful which exact type you want to check, type is
the type of
the outermost expression, otherwise TREE_TYPE (@0) etc.

  parent reply	other threads:[~2024-04-15  6:49 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-12  2:22 [Bug c/114700] New: Front-end optimization generates wrong code with -ftrapv lin1.hu at intel dot com
2024-04-12  2:34 ` [Bug middle-end/114700] " pinskia at gcc dot gnu.org
2024-04-12  2:47 ` lin1.hu at intel dot com
2024-04-12  2:47 ` lin1.hu at intel dot com
2024-04-12  3:21 ` pinskia at gcc dot gnu.org
2024-04-12  3:26 ` pinskia at gcc dot gnu.org
2024-04-12  3:35 ` pinskia at gcc dot gnu.org
2024-04-12  3:53 ` lin1.hu at intel dot com
2024-04-12  3:56 ` lin1.hu at intel dot com
2024-04-12  5:56 ` rguenth at gcc dot gnu.org
2024-04-12  6:33 ` [Bug middle-end/114700] Front-end optimization generates wrong code with -fsanitize=undefined xry111 at gcc dot gnu.org
2024-04-12  6:44 ` lin1.hu at intel dot com
2024-04-12  7:12 ` lin1.hu at intel dot com
2024-04-12  7:14 ` xry111 at gcc dot gnu.org
2024-04-12  8:51 ` lin1.hu at intel dot com
2024-04-12 10:15 ` [Bug middle-end/114700] middle-end " jakub at gcc dot gnu.org
2024-04-12 10:25 ` jakub at gcc dot gnu.org
2024-04-15  3:43 ` [Bug middle-end/114700] middle-end optimization generates causes -fsanitize=undefined not to happen in some cases lin1.hu at intel dot com
2024-04-15  6:49 ` jakub at gcc dot gnu.org [this message]
2024-04-15  7:44 ` lin1.hu at intel dot com
2024-04-17  1:50 ` lin1.hu at intel dot com

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=bug-114700-4-4bCSrPkWnX@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).