Bug 81705 - [8 Regression] UBSAN: yet another false positive
Summary: [8 Regression] UBSAN: yet another false positive
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 8.0
: P1 normal
Target Milestone: 8.0
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks: yarpgen
  Show dependency treegraph
 
Reported: 2017-08-03 19:11 UTC by Dmitry Babokin
Modified: 2021-11-01 23:07 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work: 7.1.1
Known to fail: 8.0
Last reconfirmed: 2017-08-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitry Babokin 2017-08-03 19:11:23 UTC
gcc trunk, rev250857, x86_64.

After recent fix for #81148, this case is the last one failing UBSAN false positive that I see on my radars.

> cat f.cpp
int var_4 = -1716607962;
int var_14 = 943738830;
volatile int a;
int main() {
//  (-(-1716607962) - 516151698) - -(9403738830)
  a = (-var_4 - 516151698) - -var_14;
  return 0;
}

> g++ -fsanitize=undefined f.cpp -o out; ./out
f.cpp:6:28: runtime error: signed integer overflow: -943738830 + -1716607962 cannot be represented in type 'int'
f.cpp:6:28: runtime error: signed integer overflow: -516151698 - 1634620504 cannot be represented in type 'int'
Comment 1 Marek Polacek 2017-08-04 07:13:40 UTC
Confirmed.
Comment 2 Marek Polacek 2017-08-04 07:36:38 UTC
I don't see this with g++-7 which has

(void) (a = (-516151698 - NON_LVALUE_EXPR <var_4>) - -NON_LVALUE_EXPR <var_14>)

trunks produces

(void) (a = -516151698 - (-NON_LVALUE_EXPR <var_14> + NON_LVALUE_EXPR <var_4>))
Comment 3 Richard Biener 2017-08-04 08:15:47 UTC
Created by associate, thus mine.
Comment 4 Richard Biener 2017-08-04 08:20:56 UTC
Oops.  Simple mistake in my recent patch.

Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c    (revision 250865)
+++ gcc/fold-const.c    (working copy)
@@ -9629,7 +9629,9 @@ fold_binary_loc (location_t loc,
              else if ((var0 && minus_var1
                        && ! operand_equal_p (var0, minus_var1, 0))
                       || (minus_var0 && var1
-                          && ! operand_equal_p (minus_var0, var1, 0)))
+                          && ! operand_equal_p (minus_var0, var1, 0))
+                      || (minus_var0 && minus_var1
+                          && ! operand_equal_p (minus_var0, minus_var1, 0)))
                ok = false;
            }
Comment 5 Richard Biener 2017-08-04 10:33:51 UTC
Fixed.
Comment 6 Richard Biener 2017-08-04 10:34:11 UTC
Author: rguenth
Date: Fri Aug  4 10:33:39 2017
New Revision: 250866

URL: https://gcc.gnu.org/viewcvs?rev=250866&root=gcc&view=rev
Log:
2017-08-04  Richard Biener  <rguenther@suse.de>

	PR middle-end/81705
	* fold-const.c (fold_binary_loc): Properly restrict
	minus_var0 && minus_var1 case when associating undefined overflow
	entities.

	* c-c++-common/ubsan/pr81705.c: New testcase.

Added:
    trunk/gcc/testsuite/c-c++-common/ubsan/pr81705.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 Dmitry Babokin 2017-08-04 22:01:06 UTC
That's an excellent new! This means UBSAN becomes finally fully functional in GCC. No known false positives anymore (on quite large test base). Great job and thank you!
Comment 8 rguenther@suse.de 2017-08-05 06:02:20 UTC
On August 5, 2017 12:01:06 AM GMT+02:00, babokin at gmail dot com <gcc-bugzilla@gcc.gnu.org> wrote:
>https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81705
>
>--- Comment #7 from Dmitry Babokin <babokin at gmail dot com> ---
>That's an excellent new! This means UBSAN becomes finally fully
>functional in
>GCC. No known false positives anymore (on quite large test base). Great
>job and
>thank you!

Thank you for reporting the issues!
Comment 9 Aldy Hernandez 2017-09-13 16:28:18 UTC
Author: aldyh
Date: Wed Sep 13 16:27:46 2017
New Revision: 252286

URL: https://gcc.gnu.org/viewcvs?rev=252286&root=gcc&view=rev
Log:
2017-08-04  Richard Biener  <rguenther@suse.de>

	PR middle-end/81705
	* fold-const.c (fold_binary_loc): Properly restrict
	minus_var0 && minus_var1 case when associating undefined overflow
	entities.

	* c-c++-common/ubsan/pr81705.c: New testcase.

Added:
    branches/range-gen2/gcc/testsuite/c-c++-common/ubsan/pr81705.c
Modified:
    branches/range-gen2/gcc/ChangeLog
    branches/range-gen2/gcc/fold-const.c
    branches/range-gen2/gcc/testsuite/ChangeLog