public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Is this a possible wrong-code issue?
@ 2022-03-12  4:19 Haoxin Tu
  2022-03-12  4:33 ` Andrew Pinski
  0 siblings, 1 reply; 3+ messages in thread
From: Haoxin Tu @ 2022-03-12  4:19 UTC (permalink / raw)
  To: gcc

Dear developers,

May I seek your confirmation to check whether the following program
triggers a true wrong-code issue or not? I don't want to make noise to the
bug repository so I'd like to seek your confirmation here first.

The following case makes GCC outputs differ under  -O1 below vs -O1 above
optimization levels. Here is the program (s.c):
```
#include <stdint.h>
union {
int32_t a;
uint16_t b;
} c;
static uint16_t *d = &c.b;
int32_t *e = &c.a;
int32_t **f = &e;
int32_t ***g = &f;
int32_t *h = &c.a;
int64_t i() {
int8_t j = 5;
*h = j;
***g = *d;
return 0;
}
int main() {
c.a = 1;
c.b = 1;
i();
printf("%d\n", c.a);
}
```

```
$gcc-trunk -O0 -w s.c ; ./a.out
5
$gcc-trunk -O1 -w s.c ; ./a.out
5
$gcc-trunk -O2 -w s.c ; ./a.out
1
$gcc-trunk -O3 -w s.c ; ./a.out
1
$gcc-trunk -Os -w s.c ; ./a.out
1
```

What surprised me was that almost all GCC versions produce different
results (please check more details here: https://godbolt.org/z/vTzhhvnGE).
Besides, clang also has the same issue with -O0 vs the above versions. So,
does this program have any UB? If not, I would like to file new bug reports
then. Thank you so much!


Best regards,
Haoxin

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

* Re: Is this a possible wrong-code issue?
  2022-03-12  4:19 Is this a possible wrong-code issue? Haoxin Tu
@ 2022-03-12  4:33 ` Andrew Pinski
  2022-03-12  4:42   ` Haoxin Tu
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Pinski @ 2022-03-12  4:33 UTC (permalink / raw)
  To: Haoxin Tu; +Cc: GCC Mailing List

On Fri, Mar 11, 2022 at 8:21 PM Haoxin Tu via Gcc <gcc@gcc.gnu.org> wrote:
>
> Dear developers,
>
> May I seek your confirmation to check whether the following program
> triggers a true wrong-code issue or not? I don't want to make noise to the
> bug repository so I'd like to seek your confirmation here first.
>
> The following case makes GCC outputs differ under  -O1 below vs -O1 above
> optimization levels. Here is the program (s.c):
> ```
> #include <stdint.h>
> union {
> int32_t a;
> uint16_t b;
> } c;
> static uint16_t *d = &c.b;
> int32_t *e = &c.a;
> int32_t **f = &e;
> int32_t ***g = &f;
> int32_t *h = &c.a;
> int64_t i() {
> int8_t j = 5;
> *h = j;
> ***g = *d;
> return 0;
> }
> int main() {
> c.a = 1;
> c.b = 1;
> i();
> printf("%d\n", c.a);
> }
> ```
>
> ```
> $gcc-trunk -O0 -w s.c ; ./a.out
> 5
> $gcc-trunk -O1 -w s.c ; ./a.out
> 5
> $gcc-trunk -O2 -w s.c ; ./a.out
> 1
> $gcc-trunk -O3 -w s.c ; ./a.out
> 1
> $gcc-trunk -Os -w s.c ; ./a.out
> 1
> ```
>
> What surprised me was that almost all GCC versions produce different
> results (please check more details here: https://godbolt.org/z/vTzhhvnGE).
> Besides, clang also has the same issue with -O0 vs the above versions. So,
> does this program have any UB? If not, I would like to file new bug reports
> then. Thank you so much!

Yes it looks like a strict aliasing issue though GCC does not
implement some aliasing rules sometimes with unions, see PR 82224.

Thanks,
Adnrew Pinski

>
>
> Best regards,
> Haoxin

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

* Re: Is this a possible wrong-code issue?
  2022-03-12  4:33 ` Andrew Pinski
@ 2022-03-12  4:42   ` Haoxin Tu
  0 siblings, 0 replies; 3+ messages in thread
From: Haoxin Tu @ 2022-03-12  4:42 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: GCC Mailing List

Hi Andrew,

I see. Thanks for your speedy reply for clearing my confusion!

Have a great day!

Best regards,
Haoxin

Andrew Pinski <pinskia@gmail.com> 于2022年3月12日周六 12:33写道:

> On Fri, Mar 11, 2022 at 8:21 PM Haoxin Tu via Gcc <gcc@gcc.gnu.org> wrote:
> >
> > Dear developers,
> >
> > May I seek your confirmation to check whether the following program
> > triggers a true wrong-code issue or not? I don't want to make noise to
> the
> > bug repository so I'd like to seek your confirmation here first.
> >
> > The following case makes GCC outputs differ under  -O1 below vs -O1 above
> > optimization levels. Here is the program (s.c):
> > ```
> > #include <stdint.h>
> > union {
> > int32_t a;
> > uint16_t b;
> > } c;
> > static uint16_t *d = &c.b;
> > int32_t *e = &c.a;
> > int32_t **f = &e;
> > int32_t ***g = &f;
> > int32_t *h = &c.a;
> > int64_t i() {
> > int8_t j = 5;
> > *h = j;
> > ***g = *d;
> > return 0;
> > }
> > int main() {
> > c.a = 1;
> > c.b = 1;
> > i();
> > printf("%d\n", c.a);
> > }
> > ```
> >
> > ```
> > $gcc-trunk -O0 -w s.c ; ./a.out
> > 5
> > $gcc-trunk -O1 -w s.c ; ./a.out
> > 5
> > $gcc-trunk -O2 -w s.c ; ./a.out
> > 1
> > $gcc-trunk -O3 -w s.c ; ./a.out
> > 1
> > $gcc-trunk -Os -w s.c ; ./a.out
> > 1
> > ```
> >
> > What surprised me was that almost all GCC versions produce different
> > results (please check more details here: https://godbolt.org/z/vTzhhvnGE
> ).
> > Besides, clang also has the same issue with -O0 vs the above versions.
> So,
> > does this program have any UB? If not, I would like to file new bug
> reports
> > then. Thank you so much!
>
> Yes it looks like a strict aliasing issue though GCC does not
> implement some aliasing rules sometimes with unions, see PR 82224.
>
> Thanks,
> Adnrew Pinski
>
> >
> >
> > Best regards,
> > Haoxin
>

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

end of thread, other threads:[~2022-03-12  4:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-12  4:19 Is this a possible wrong-code issue? Haoxin Tu
2022-03-12  4:33 ` Andrew Pinski
2022-03-12  4:42   ` Haoxin Tu

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).