[Bug c/99431] New: Return value of __builtin_memset() incorrect if partially optimized away

jonathon.reinhart at gmail dot com gcc-bugzilla@gcc.gnu.org
Sat Mar 6 16:56:20 GMT 2021


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

            Bug ID: 99431
           Summary: Return value of __builtin_memset() incorrect if
                    partially optimized away
           Product: gcc
           Version: 9.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jonathon.reinhart at gmail dot com
  Target Milestone: ---

Created attachment 50319
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50319&action=edit
Minimal example

Please see attached code or https://godbolt.org/z/oTMhsd.

If the address of a struct object is passed to __builtin_memset(), and some
leading fields are assigned elsewhere in the function, GCC may effectively
adjust the portion of the object written to by the memset(), to eliminate
redundant writes. However, the return value of memset(), which should always
match the first argument, is also (incorrectly) adjusted, leading to an invalid
result.

Specifically, this happens with -Os. I attempted to narrow this down to a
specific set of optimizations by testing everything listed in the output of
this command, but that was insufficient to reproduce the problem:

  gcc -Q -Os --help=optimizers

I found it interesting, although not surprising, that adding this between the
__builtin_memset() call and the "other" assignments caused the problem to go
away: 

    __asm__("" :::"memory");

GCC versions tested:
  10.2 OK
  10.1 OK
  9.3  Broken
  8.3  Broken
  7.3  Broken
  7.2  Broke
  7.1  Broken
  6.3  OK


More information about the Gcc-bugs mailing list