public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/97053] New: an O3 codegen bug
@ 2020-09-15  2:50 cuzdav at gmail dot com
  2020-09-15  6:21 ` [Bug tree-optimization/97053] [10/11 Regression] an O2, " rguenth at gcc dot gnu.org
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: cuzdav at gmail dot com @ 2020-09-15  2:50 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97053
           Summary: an O3 codegen bug
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cuzdav at gmail dot com
  Target Milestone: ---

Created attachment 49217
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49217&action=edit
preprocessed code in case something changes after submitting

"Wrong" output codegen bug.  Very sensitive, almost any code change prevents
the problem, including O2 or less, as well as running under ubsan or asan,
reordering variables, and in some cases changing the values involved (such as
setting p=0 instead of 1)

Copying a buffer of 8 bytes of data (null terminated) results in a 6-bytes (of
data) null terminated "string", losing 2 bytes from the end.

A variable "len" holds the strlen() of this string, reports 6, but some code
affected by optimizer seems to think it is a larger value.

Expected output:

  runtime len=8
  orig=ABCDXXXX
  copy=ABCDXXXX

actual output:

  runtime len=6
  orig=ABCDXXXX
  copy=ABCDXX


Even though above the actual output shows len=6 when printing "len", a separate
line of code is not present in the assembly (presumably optimized away under a
confused compile-time eval.)

    if (len < 8)
        printf("len < 8\n"); // but len == 6


Problem visible in 10.1 and 10.2 at -O2 and -O3, but not -O1 or less, nor in in
9.x or earlier.  

CODE: 

#include <cstdio>
#include <cstring>

struct Data
{
    short n;
    char name[8 + 1];
    int p;
    char s;
    int b;
};

int main(int argc, char** argv)
{
    int p = 1;  
    char orig[9]{"XXXXXXXX"};
    std::memcpy(orig, "ABCD", 4);

    Data data{};
    data.n = 5u;

    // this should copy ABCDXXXX but loses final 2 XX's
    std::memcpy(data.name, orig, 8); 

    data.s = 'X';
    data.b = p;

    const size_t len = strlen(data.name);
    if (len < 8) 
        printf("len < 8\n"); // does not print

    std::printf("runtime len=%lu\n", len); // this, however, prints 6
    std::printf("orig=%s\ncopy=%s\n", orig, data.name);
}


Live on godbolt:  https://godbolt.org/z/v6xqh8

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

end of thread, other threads:[~2020-09-18  9:58 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-15  2:50 [Bug c++/97053] New: an O3 codegen bug cuzdav at gmail dot com
2020-09-15  6:21 ` [Bug tree-optimization/97053] [10/11 Regression] an O2, " rguenth at gcc dot gnu.org
2020-09-15  7:08 ` ebotcazou at gcc dot gnu.org
2020-09-15  8:21 ` jakub at gcc dot gnu.org
2020-09-15  9:08 ` jakub at gcc dot gnu.org
2020-09-15  9:53 ` jakub at gcc dot gnu.org
2020-09-15 10:55 ` jakub at gcc dot gnu.org
2020-09-15 13:23 ` jakub at gcc dot gnu.org
2020-09-15 14:58 ` jakub at gcc dot gnu.org
2020-09-16  7:45 ` cvs-commit at gcc dot gnu.org
2020-09-16  7:50 ` cvs-commit at gcc dot gnu.org
2020-09-16  7:55 ` jakub at gcc dot gnu.org
2020-09-18  9:52 ` cvs-commit at gcc dot gnu.org
2020-09-18  9:58 ` cvs-commit 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).