public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/115582] New: [regression 15] wrong code when accessing members of incompatible type structure
@ 2024-06-21 13:48 tangyixuan at mail dot dlut.edu.cn
  2024-06-21 14:05 ` [Bug tree-optimization/115582] [15 regression] " sjames at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: tangyixuan at mail dot dlut.edu.cn @ 2024-06-21 13:48 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115582
           Summary: [regression 15] wrong code when accessing members of
                    incompatible type structure
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tangyixuan at mail dot dlut.edu.cn
  Target Milestone: ---

Hi, I find GCC produces wrong code under optimizations when pointers point to
incompatible type structure but the member types are compatible.

Compiler Explorer: https://godbolt.org/z/jjY599Yfe

$ cat s.c

int printf(const char *, ...);
struct s {int x; } __attribute__((packed));
struct t {int x; };

void swap(struct s* p, struct t* q)
{
  p->x = q->x;
  q->x = p->x;
}
int main()
{    
  struct t a[2];
  a[0].x = 1234;
  a[1].x = 5678;
  swap ((struct s *)((char *)a + 1), a);
  printf("%d",a[0].x);
  return 0;
}

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

* [Bug tree-optimization/115582] [15 regression] wrong code when accessing members of incompatible type structure
  2024-06-21 13:48 [Bug tree-optimization/115582] New: [regression 15] wrong code when accessing members of incompatible type structure tangyixuan at mail dot dlut.edu.cn
@ 2024-06-21 14:05 ` sjames at gcc dot gnu.org
  2024-06-21 14:11 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: sjames at gcc dot gnu.org @ 2024-06-21 14:05 UTC (permalink / raw)
  To: gcc-bugs

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

Sam James <sjames at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[regression 15] wrong code  |[15 regression] wrong code
                   |when accessing members of   |when accessing members of
                   |incompatible type structure |incompatible type structure

--- Comment #1 from Sam James <sjames at gcc dot gnu.org> ---
Please give compiler options to reproduce.

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

* [Bug tree-optimization/115582] [15 regression] wrong code when accessing members of incompatible type structure
  2024-06-21 13:48 [Bug tree-optimization/115582] New: [regression 15] wrong code when accessing members of incompatible type structure tangyixuan at mail dot dlut.edu.cn
  2024-06-21 14:05 ` [Bug tree-optimization/115582] [15 regression] " sjames at gcc dot gnu.org
@ 2024-06-21 14:11 ` pinskia at gcc dot gnu.org
  2024-06-21 14:13 ` sjames at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-21 14:11 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2024-06-21

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The godbolt is at -O3.

But these two structures are not compatible at all.
In c structs are not compatible just because they have the same fields. So they
can't alias.

Why do you think they can alias?

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

* [Bug tree-optimization/115582] [15 regression] wrong code when accessing members of incompatible type structure
  2024-06-21 13:48 [Bug tree-optimization/115582] New: [regression 15] wrong code when accessing members of incompatible type structure tangyixuan at mail dot dlut.edu.cn
  2024-06-21 14:05 ` [Bug tree-optimization/115582] [15 regression] " sjames at gcc dot gnu.org
  2024-06-21 14:11 ` pinskia at gcc dot gnu.org
@ 2024-06-21 14:13 ` sjames at gcc dot gnu.org
  2024-06-21 15:39 ` tangyixuan at mail dot dlut.edu.cn
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: sjames at gcc dot gnu.org @ 2024-06-21 14:13 UTC (permalink / raw)
  To: gcc-bugs

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

Sam James <sjames at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sjames at gcc dot gnu.org

--- Comment #3 from Sam James <sjames at gcc dot gnu.org> ---
Ah, I was comparing -O0 vs -O{2,3} & -fno-sa given it's aliasing. I didn't
think it was well-defined but wanted you to chip in.

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

* [Bug tree-optimization/115582] [15 regression] wrong code when accessing members of incompatible type structure
  2024-06-21 13:48 [Bug tree-optimization/115582] New: [regression 15] wrong code when accessing members of incompatible type structure tangyixuan at mail dot dlut.edu.cn
                   ` (2 preceding siblings ...)
  2024-06-21 14:13 ` sjames at gcc dot gnu.org
@ 2024-06-21 15:39 ` tangyixuan at mail dot dlut.edu.cn
  2024-06-21 15:56 ` pinskia at gcc dot gnu.org
  2024-06-27  1:37 ` xry111 at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: tangyixuan at mail dot dlut.edu.cn @ 2024-06-21 15:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from tangyixuan <tangyixuan at mail dot dlut.edu.cn> ---
Thanks for your concerns. The type of "a.x" is converted to char in "(char *)a
+ 1", and it may be confused in clang under -O0 vs -O{2,3}. gcc-15 outputs the
"int" value, but the previous versions like gcc-5.3 produce the "char" value I
guess.

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

* [Bug tree-optimization/115582] [15 regression] wrong code when accessing members of incompatible type structure
  2024-06-21 13:48 [Bug tree-optimization/115582] New: [regression 15] wrong code when accessing members of incompatible type structure tangyixuan at mail dot dlut.edu.cn
                   ` (3 preceding siblings ...)
  2024-06-21 15:39 ` tangyixuan at mail dot dlut.edu.cn
@ 2024-06-21 15:56 ` pinskia at gcc dot gnu.org
  2024-06-27  1:37 ` xry111 at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-21 15:56 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The cast to char* does nothing except getting a pointer. Violation of aliasing
rules in c/c++ is all about what type is doing the access.

In this case the accesses are via the two structs. Note the fields might have
the same type and both can be accessed via int without any undefinedness, they
cannot be accessed via the each other struct types.

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

* [Bug tree-optimization/115582] [15 regression] wrong code when accessing members of incompatible type structure
  2024-06-21 13:48 [Bug tree-optimization/115582] New: [regression 15] wrong code when accessing members of incompatible type structure tangyixuan at mail dot dlut.edu.cn
                   ` (4 preceding siblings ...)
  2024-06-21 15:56 ` pinskia at gcc dot gnu.org
@ 2024-06-27  1:37 ` xry111 at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-06-27  1:37 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xry111 at gcc dot gnu.org

--- Comment #6 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
FWIW there are too many online articles discussing aliasing rule but failing to
get the concept correct.  As Andrew has explained the aliasing rule is all
about the type used for accessing a lvalue, it's not related to pointer
casting, at all.

Checking against pointer casting is just a heruistic way for finding aliasing
bugs and it may have false positives or false negatives.  This renders
-Wstrict-aliasing almost useless.

If a pointer cast to (T *) made the type alias with T, the aliasing rule would
be meaningless because in a different TU (that the compiler cannot see) any
pointer can be casted to anything.  For example, *everything* is casted to
(char *) in Glibc memcpy() thus *everything* would be aliasing.  This would be
obviously irrational.

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

end of thread, other threads:[~2024-06-27  1:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-21 13:48 [Bug tree-optimization/115582] New: [regression 15] wrong code when accessing members of incompatible type structure tangyixuan at mail dot dlut.edu.cn
2024-06-21 14:05 ` [Bug tree-optimization/115582] [15 regression] " sjames at gcc dot gnu.org
2024-06-21 14:11 ` pinskia at gcc dot gnu.org
2024-06-21 14:13 ` sjames at gcc dot gnu.org
2024-06-21 15:39 ` tangyixuan at mail dot dlut.edu.cn
2024-06-21 15:56 ` pinskia at gcc dot gnu.org
2024-06-27  1:37 ` xry111 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).