Bug 101521 - -ftrapv should become something like -fsanitize=undefined -fsanitize-undefined-trap-on-error
Summary: -ftrapv should become something like -fsanitize=undefined -fsanitize-undefine...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 94934 (view as bug list)
Depends on: 77891 78473
Blocks: 13421 35412 54848 57859 61893 77779 94930 94935 105295 110253 52490
  Show dependency treegraph
 
Reported: 2021-07-19 22:52 UTC by Andrew Pinski
Modified: 2023-06-15 01:45 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-07-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2021-07-19 22:52:13 UTC
As mentioned a few times -ftrapv is broken in many cases it would be better if it become something like -fsanitize=undefined -fsanitize-undefined-trap-on-error.
Comment 1 Richard Biener 2021-07-20 09:48:37 UTC
Confirmed.  In particular this would free the compiler from caring about -ftrapv when generating expressions on its own.  It was suggested that this transform can be done at gimplification time using the IFN_{ADD,SUB,MUL}_OVERFLOW machinery
(notably negate and division are missing here but easy to test for).
Comment 2 Jakub Jelinek 2021-07-20 10:06:21 UTC
Note, -fsanitize=undefined -fsanitize-undefined-trap-on-error actually doesn't use IFN_{ADD,SUB,MUL}_OVERFLOW, those are used for the __builtin_*_overflow* builtins, but its own IFN_UBSAN_CHECK_{ADD,SUB,MUL} ones.
Those are for the IL simpler than IFN_{ADD,SUB,MUL}_OVERFLOW, they just return their value instead of _Complex containing both the value and overflow flag - the abort operation (whether __builtin_trap () or some libubsan API call) is implicit in it.
I think best would be to introduce for -ftrapv another set of ifns, and treat those mostly as IFN_UBSAN_CHECK_{ADD,SUB,MUL}, except that the abort operation
would be always __builtin_trap regardless of -f{,no-}sanitize-undefined-trap-on-error and perhaps the expansion could at least for -Os or for longer sequences try to use the existing libgcc APIs when they are available.
Negate is for ubsan handled as IFN_UBSAN_CHECK_SUB (0, x), yes, division would need a new ifn.
Comment 3 Andrew Pinski 2021-08-03 03:22:06 UTC
*** Bug 94934 has been marked as a duplicate of this bug. ***
Comment 4 Andrew Pinski 2021-08-15 12:05:48 UTC
the request for a division overflow function is PR 78473.