public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102303] New: optimizer elides -ftrapv overflow detection
@ 2021-09-13  9:24 john at mcfarlane dot name
  2021-09-13  9:31 ` [Bug c++/102303] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: john at mcfarlane dot name @ 2021-09-13  9:24 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102303
           Summary: optimizer elides -ftrapv overflow detection
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: john at mcfarlane dot name
  Target Milestone: ---

With flag, `-ftrapv`, the following program on CE with 'x86-64 gcc 11.2' has
exit status 139:

   int main()
   {
       int n = 0x7fffffff;
       return n+1;
   }

This is expected behavior: run-time overflow trapping. But with flags, `-ftrapv
-O1`, it has exit status 0. I wouldn't expect optimization to change the
behavior of this program, which appears to be either UB or as-if `-fwrapv`.

https://godbolt.org/z/jvj9javcY

Behavior is inconsistent with Clang 12.0. Behavior is inconsistent with
`-fsanitize=signed-integer-overflow -fsanitize-undefined-trap-on-error`.

Inconsistency goes as far back as the 5.x series, where trapping of this
overflow first appears. Docs for -ftrapv
(https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html) don't mention
optimization.

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

* [Bug c++/102303] optimizer elides -ftrapv overflow detection
  2021-09-13  9:24 [Bug c++/102303] New: optimizer elides -ftrapv overflow detection john at mcfarlane dot name
@ 2021-09-13  9:31 ` pinskia at gcc dot gnu.org
  2021-09-13 10:33 ` rguenth at gcc dot gnu.org
  2021-09-13 20:41 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-13  9:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is a known bug with -ftrapv where gcc widens everything to word mode
(64bit in this case) and uses libcalls to implement-ftrapv. There are a few
other bugs that say the same thing.

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

* [Bug c++/102303] optimizer elides -ftrapv overflow detection
  2021-09-13  9:24 [Bug c++/102303] New: optimizer elides -ftrapv overflow detection john at mcfarlane dot name
  2021-09-13  9:31 ` [Bug c++/102303] " pinskia at gcc dot gnu.org
@ 2021-09-13 10:33 ` rguenth at gcc dot gnu.org
  2021-09-13 20:41 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-09-13 10:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
No, the issue is that we apply constant folding and elide the trapping
operation which is generally considered OK by GCC (eliding traps, that is). 
For example
we also elide the trap in

   int main()
   {
       int n = 0x7fffffff;
       n+1;
       return 0;
   }

I would advise against the use of -ftrapv and instead use
-fsanitize=signed-integer-overflow -fsanitize-undefined-trap-on-error

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

* [Bug c++/102303] optimizer elides -ftrapv overflow detection
  2021-09-13  9:24 [Bug c++/102303] New: optimizer elides -ftrapv overflow detection john at mcfarlane dot name
  2021-09-13  9:31 ` [Bug c++/102303] " pinskia at gcc dot gnu.org
  2021-09-13 10:33 ` rguenth at gcc dot gnu.org
@ 2021-09-13 20:41 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-13 20:41 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is a dup of bug 61893.

*** This bug has been marked as a duplicate of bug 61893 ***

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

end of thread, other threads:[~2021-09-13 20:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-13  9:24 [Bug c++/102303] New: optimizer elides -ftrapv overflow detection john at mcfarlane dot name
2021-09-13  9:31 ` [Bug c++/102303] " pinskia at gcc dot gnu.org
2021-09-13 10:33 ` rguenth at gcc dot gnu.org
2021-09-13 20:41 ` pinskia at gcc dot gnu.org

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