public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102937] New: Miscompilation with -O3 and aliasing of char* and size_t
@ 2021-10-25 21:02 drohr at jwdt dot org
  2021-10-25 21:17 ` [Bug c++/102937] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: drohr at jwdt dot org @ 2021-10-25 21:02 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102937
           Summary: Miscompilation with -O3 and aliasing of char* and
                    size_t
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: drohr at jwdt dot org
  Target Milestone: ---

Created attachment 51663
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51663&action=edit
testcase

The attached testcase miscompiles with GCC 11.2 when using -O3. It compiles
correctly with -O3, and also with GCC 10.3 using -O3. For reference the code is
also here: https://godbolt.org/z/cjr584sMo

The program prints a ptr twice, which should have been modified in between, but
this is not the case, thus it prints twice a nullptr:
FOO 0x(nil)
BAR 0x(nil)

By either commenting in the printf in line 51 (or also by outcommenting the
call to computePointerWithAlignment in line 50) the miscompilation disappears,
and the output is
FOO 0x(nil)
test 0x800
BAR 0x0x800

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

* [Bug c++/102937] Miscompilation with -O3 and aliasing of char* and size_t
  2021-10-25 21:02 [Bug c++/102937] New: Miscompilation with -O3 and aliasing of char* and size_t drohr at jwdt dot org
@ 2021-10-25 21:17 ` pinskia at gcc dot gnu.org
  2021-10-25 21:19 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-25 21:17 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The code is undefined according to the C++ aliasing rules.  Access pointer
types as size_t is undefined.

Here is a simple fix to the code itself:
  template <size_t alignment = GPUCA_BUFFER_ALIGNMENT, class S>
  static inline S* getPointerWithAlignment(char*& basePtr, size_t nEntries = 1)
  {
    auto t = reinterpret_cast<size_t>(basePtr);
    auto t1= getPointerWithAlignment<alignment,
S>(reinterpret_cast<size_t&>(basePtr), nEntries);
    basePtr = reinterpret_cast<decltype(basePtr)>(t);
    return t1;
  }

  template <size_t alignment = GPUCA_BUFFER_ALIGNMENT, class T, class S>
  static inline void computePointerWithAlignment(T*& basePtr, S*& objPtr,
size_t nEntries = 1)
  {
    size_t t = reinterpret_cast<size_t>(basePtr);
    objPtr = getPointerWithAlignment<alignment, S>(t, nEntries);
    basePtr = reinterpret_cast<T*>(t);
  }

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

* [Bug c++/102937] Miscompilation with -O3 and aliasing of char* and size_t
  2021-10-25 21:02 [Bug c++/102937] New: Miscompilation with -O3 and aliasing of char* and size_t drohr at jwdt dot org
  2021-10-25 21:17 ` [Bug c++/102937] " pinskia at gcc dot gnu.org
@ 2021-10-25 21:19 ` pinskia at gcc dot gnu.org
  2021-10-25 21:44 ` drohr at jwdt dot org
  2021-10-25 21:52 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-25 21:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
That is the following is undefined:
char *a = NULL;
size_t &t = reinterpret_cast<size_t&>(a);
t = 0x8;
printf("%p\n", a);

GCC does give a warning in the above case too:

<source>: In function 'int main()':
<source>:69:39: warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
   69 | size_t &t = reinterpret_cast<size_t&>(a);
      |                                       ^

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

* [Bug c++/102937] Miscompilation with -O3 and aliasing of char* and size_t
  2021-10-25 21:02 [Bug c++/102937] New: Miscompilation with -O3 and aliasing of char* and size_t drohr at jwdt dot org
  2021-10-25 21:17 ` [Bug c++/102937] " pinskia at gcc dot gnu.org
  2021-10-25 21:19 ` pinskia at gcc dot gnu.org
@ 2021-10-25 21:44 ` drohr at jwdt dot org
  2021-10-25 21:52 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: drohr at jwdt dot org @ 2021-10-25 21:44 UTC (permalink / raw)
  To: gcc-bugs

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

David Rohr <drohr at jwdt dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |FIXED
                 CC|                            |drohr at jwdt dot org

--- Comment #3 from David Rohr <drohr at jwdt dot org> ---
thanks, makes me feel pretty stupid...
I thought it is not a problem since char* may alias, but apparently size_t* may
not alias char**... if I understood correctly...

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

* [Bug c++/102937] Miscompilation with -O3 and aliasing of char* and size_t
  2021-10-25 21:02 [Bug c++/102937] New: Miscompilation with -O3 and aliasing of char* and size_t drohr at jwdt dot org
                   ` (2 preceding siblings ...)
  2021-10-25 21:44 ` drohr at jwdt dot org
@ 2021-10-25 21:52 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-25 21:52 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|FIXED                       |INVALID

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to David Rohr from comment #3)
> thanks, makes me feel pretty stupid...
> I thought it is not a problem since char* may alias, but apparently size_t*
> may not alias char**... if I understood correctly...

No, you are still misunderstand aliasing rules.

Basically you can access any type via a character type.
That is:
char *a = (char*)t;
a[n] = ...;
access *t

since you are doing basically:
char *a;
size_t *t = (size_t)&a;
*t = ....
access a

size_t only alias size_t and the signed version of what the type size_t was
typedef of (size_t is unsigned).
In the above case, you access "char*" as size_t which is undefined.  if you
instead accessed it as char rather than size_t then the code would be well
defined.
That is:
char *a;
char *b = (char*)&a;
b[0] = ...
b[1] = ...
b[2] = ...
....
access a

There are other ways fixing the issue by using memcpy (or an union but an union
in this case is harder to use correctly) than extra assignments.

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

end of thread, other threads:[~2021-10-25 21:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-25 21:02 [Bug c++/102937] New: Miscompilation with -O3 and aliasing of char* and size_t drohr at jwdt dot org
2021-10-25 21:17 ` [Bug c++/102937] " pinskia at gcc dot gnu.org
2021-10-25 21:19 ` pinskia at gcc dot gnu.org
2021-10-25 21:44 ` drohr at jwdt dot org
2021-10-25 21:52 ` 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).