public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95326] New: UBsan can not detect signed-integer-overflow correctly
@ 2020-05-26  4:14 haoxintu at gmail dot com
  2020-05-26  5:23 ` [Bug c++/95326] GCC " haoxintu at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: haoxintu at gmail dot com @ 2020-05-26  4:14 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95326
           Summary: UBsan can not detect signed-integer-overflow correctly
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

This case example.cpp

#include <iostream>

volatile wchar_t g_volatile_wchar = 2082494266;
volatile unsigned short g_volatile_ushort = 31503U;

int main () {
    unsigned long l_var_ulong = 526562505494506029UL;
    const wchar_t l_const_wchar = 1188246531;
    g_volatile_ushort = l_var_ulong + l_const_wchar * g_volatile_wchar;
    std::cout << "hello" << std::endl;
    return 0;
}

can not detect signed-integer-overflow runtime error in gcc-trunk

$g++ -fsanitize=signed-integer-overflow example.cpp ; ./a.out
hello

But in clang-trunk
$clang++ -fsanitize=signed-integer-overflow test.cc ; ./a.out
example.cpp:9:53: runtime error: signed integer overflow: 1188246531 *
2082494266 cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:9:53 in 
hello

godbolt clang-trunk : https://godbolt.org/z/s4-AqW
godbolt gcc-trunk : https://godbolt.org/z/k7NhG6

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

* [Bug c++/95326] GCC can not detect signed-integer-overflow correctly
  2020-05-26  4:14 [Bug c++/95326] New: UBsan can not detect signed-integer-overflow correctly haoxintu at gmail dot com
@ 2020-05-26  5:23 ` haoxintu at gmail dot com
  2020-05-26  7:06 ` [Bug middle-end/95326] " pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: haoxintu at gmail dot com @ 2020-05-26  5:23 UTC (permalink / raw)
  To: gcc-bugs

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

Haoxin Tu <haoxintu at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|11.0                        |10.0

--- Comment #1 from Haoxin Tu <haoxintu at gmail dot com> ---
I also found another case example.cpp

#include <iostream>

unsigned short g_var_ushort = 52781U;
const unsigned int g_const_uint = 2331271054U;

int main () {
    //test_function();
    short l_var_short = -14674;
    volatile unsigned char l_volatile_uchar = 151U;
    long l_var_long = 572469544701421587L;

    g_var_ushort = l_volatile_uchar - l_var_short % g_const_uint * l_var_long;
    std::cout << "hello" << std::endl;
    return 0;
}

$g++ -fsanitize=signed-integer-overflow example.cpp ; ./a.out
hello

$clang++ -fsanitize=signed-integer-overflow example.cpp ; ./a.out
example.cpp:12:66: runtime error: signed integer overflow: 1963681568 *
572469544701421587 cannot be represented in type 'long'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior example.cpp:12:66 in 
hello

My g++ version is
$g++ --version
g++ (GCC) 10.0.1 20200420 (experimental)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I have tested all GCC versions including GCC-8, GCC-9, GCC-10, and GCC-Trunk in
Godbolt, they can not detect this UB as well.

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

* [Bug middle-end/95326] GCC can not detect signed-integer-overflow correctly
  2020-05-26  4:14 [Bug c++/95326] New: UBsan can not detect signed-integer-overflow correctly haoxintu at gmail dot com
  2020-05-26  5:23 ` [Bug c++/95326] GCC " haoxintu at gmail dot com
@ 2020-05-26  7:06 ` pinskia at gcc dot gnu.org
  2020-05-26 12:41 ` haoxintu at gmail dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-05-26  7:06 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c++                         |middle-end
   Last reconfirmed|                            |2020-05-26
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The problem is GCC is shortening the multiple to be unsigned short early on.
That is doing:
    long tt;
    tt = l_volatile_uchar - l_var_short % g_const_uint * l_var_long;
    l_var_short = tt;


Shows the issue.

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

* [Bug middle-end/95326] GCC can not detect signed-integer-overflow correctly
  2020-05-26  4:14 [Bug c++/95326] New: UBsan can not detect signed-integer-overflow correctly haoxintu at gmail dot com
  2020-05-26  5:23 ` [Bug c++/95326] GCC " haoxintu at gmail dot com
  2020-05-26  7:06 ` [Bug middle-end/95326] " pinskia at gcc dot gnu.org
@ 2020-05-26 12:41 ` haoxintu at gmail dot com
  2020-05-26 15:24 ` pinskia at gcc dot gnu.org
  2024-03-10  3:35 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: haoxintu at gmail dot com @ 2020-05-26 12:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Haoxin Tu <haoxintu at gmail dot com> ---
Would GCC shorten the multiple to be other types early as well? I also find
some cases in "short" type with the same symptom. I am still testing now and
don't know there are any cases in rest of types(such as char, int, long...)
have the problem.

By the way, should I file a new bug report when I found another type (for
example,int) that GCC can not detect the signed-integer-overflow UB issues?

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

* [Bug middle-end/95326] GCC can not detect signed-integer-overflow correctly
  2020-05-26  4:14 [Bug c++/95326] New: UBsan can not detect signed-integer-overflow correctly haoxintu at gmail dot com
                   ` (2 preceding siblings ...)
  2020-05-26 12:41 ` haoxintu at gmail dot com
@ 2020-05-26 15:24 ` pinskia at gcc dot gnu.org
  2024-03-10  3:35 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-05-26 15:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Haoxin Tu from comment #3)
> Would GCC shorten the multiple to be other types early as well? I also find
> some cases in "short" type with the same symptom. I am still testing now and
> don't know there are any cases in rest of types(such as char, int, long...)
> have the problem.

Yes and I suspect it is the same issue.  the shortening is happening in convert
before the instrumentation of the undefined sanitizer is done.

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

* [Bug middle-end/95326] GCC can not detect signed-integer-overflow correctly
  2020-05-26  4:14 [Bug c++/95326] New: UBsan can not detect signed-integer-overflow correctly haoxintu at gmail dot com
                   ` (3 preceding siblings ...)
  2020-05-26 15:24 ` pinskia at gcc dot gnu.org
@ 2024-03-10  3:35 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-10  3:35 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |13.1.0
      Known to work|                            |12.3.0
         Resolution|---                         |DUPLICATE
             Status|NEW                         |RESOLVED

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed for GCC 13 by r13-4988-g8692b15ae7c05e so a dup of bug 108256.

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

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

end of thread, other threads:[~2024-03-10  3:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-26  4:14 [Bug c++/95326] New: UBsan can not detect signed-integer-overflow correctly haoxintu at gmail dot com
2020-05-26  5:23 ` [Bug c++/95326] GCC " haoxintu at gmail dot com
2020-05-26  7:06 ` [Bug middle-end/95326] " pinskia at gcc dot gnu.org
2020-05-26 12:41 ` haoxintu at gmail dot com
2020-05-26 15:24 ` pinskia at gcc dot gnu.org
2024-03-10  3:35 ` 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).