public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/111210] New: Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7
@ 2023-08-28  7:13 shaohua.li at inf dot ethz.ch
  2023-08-28  7:29 ` [Bug c/111210] " amonakov at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: shaohua.li at inf dot ethz.ch @ 2023-08-28  7:13 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111210
           Summary: Wrong code at -Os on x86_64-linux-gnu since
                    r12-4849-gf19791565d7
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shaohua.li at inf dot ethz.ch
  Target Milestone: ---

gcc at -Os produces the wrong code.

Bisected to r12-4849-gf19791565d7

Compiler explorer: https://godbolt.org/z/fjcEvKdYT

$ cat a.c
int printf(const char *, ...);
int a;
int *b = &a;
int c(long *d) {
  if (*d)
    return *(int *)0;
  return *(int *)(d + 1);
}
int main() {
  long e[] = {0, 100};
  int f = c(e);
  *b = f;
  printf("%d\n", a);
}
$
$ gcc -O3 a.c && ./a.out
100
$ gcc -Os a.c && ./a.out
0
$

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

* [Bug c/111210] Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7
  2023-08-28  7:13 [Bug c/111210] New: Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7 shaohua.li at inf dot ethz.ch
@ 2023-08-28  7:29 ` amonakov at gcc dot gnu.org
  2023-08-28 12:46 ` shaohua.li at inf dot ethz.ch
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: amonakov at gcc dot gnu.org @ 2023-08-28  7:29 UTC (permalink / raw)
  To: gcc-bugs

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

Alexander Monakov <amonakov at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |amonakov at gcc dot gnu.org

--- Comment #1 from Alexander Monakov <amonakov at gcc dot gnu.org> ---
'c' is called with 'd' pointing to 'long e[2]', so

  return *(int *)(d + 1);

is an aliasing violation (dereferencing a pointer to an incompatible type).

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

* [Bug c/111210] Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7
  2023-08-28  7:13 [Bug c/111210] New: Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7 shaohua.li at inf dot ethz.ch
  2023-08-28  7:29 ` [Bug c/111210] " amonakov at gcc dot gnu.org
@ 2023-08-28 12:46 ` shaohua.li at inf dot ethz.ch
  2023-08-28 12:50 ` xry111 at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: shaohua.li at inf dot ethz.ch @ 2023-08-28 12:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Shaohua Li <shaohua.li at inf dot ethz.ch> ---
(In reply to Alexander Monakov from comment #1)
> 'c' is called with 'd' pointing to 'long e[2]', so
> 
>   return *(int *)(d + 1);
> 
> is an aliasing violation (dereferencing a pointer to an incompatible type).

Thanks for the quick diagnosis. I tried to enable -Wall -Wextra -pedantic but
got no warning about the test case. Could you share how you diagnose this
issue?

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

* [Bug c/111210] Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7
  2023-08-28  7:13 [Bug c/111210] New: Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7 shaohua.li at inf dot ethz.ch
  2023-08-28  7:29 ` [Bug c/111210] " amonakov at gcc dot gnu.org
  2023-08-28 12:46 ` shaohua.li at inf dot ethz.ch
@ 2023-08-28 12:50 ` xry111 at gcc dot gnu.org
  2023-08-28 12:55 ` amonakov at gcc dot gnu.org
  2023-08-28 12:58 ` shaohua.li at inf dot ethz.ch
  4 siblings, 0 replies; 6+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-08-28 12:50 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to Shaohua Li from comment #2)
> (In reply to Alexander Monakov from comment #1)
> > 'c' is called with 'd' pointing to 'long e[2]', so
> > 
> >   return *(int *)(d + 1);
> > 
> > is an aliasing violation (dereferencing a pointer to an incompatible type).
> 
> Thanks for the quick diagnosis. I tried to enable -Wall -Wextra -pedantic
> but got no warning about the test case. Could you share how you diagnose
> this issue?

The red banner in the bug creation page says clearly:

"Similarly, if compiling with -fno-strict-aliasing -fwrapv makes a difference,
your code probably is not correct."

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

* [Bug c/111210] Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7
  2023-08-28  7:13 [Bug c/111210] New: Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7 shaohua.li at inf dot ethz.ch
                   ` (2 preceding siblings ...)
  2023-08-28 12:50 ` xry111 at gcc dot gnu.org
@ 2023-08-28 12:55 ` amonakov at gcc dot gnu.org
  2023-08-28 12:58 ` shaohua.li at inf dot ethz.ch
  4 siblings, 0 replies; 6+ messages in thread
From: amonakov at gcc dot gnu.org @ 2023-08-28 12:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Alexander Monakov <amonakov at gcc dot gnu.org> ---
The testcase is small enough to notice the issue by inspection.

Note that you get the "expected" answer with -fno-strict-aliasing, and as
explained in https://gcc.gnu.org/bugs/ it is one of the things you should check
when submitting a bugreport:

Before reporting that GCC compiles your code incorrectly, compile it with gcc
-Wall -Wextra and see whether this shows anything wrong with your code.
Similarly, if compiling with -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations makes a difference, or if compiling with
-fsanitize=undefined produces any run-time errors, then your code is probably
not correct.

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

* [Bug c/111210] Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7
  2023-08-28  7:13 [Bug c/111210] New: Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7 shaohua.li at inf dot ethz.ch
                   ` (3 preceding siblings ...)
  2023-08-28 12:55 ` amonakov at gcc dot gnu.org
@ 2023-08-28 12:58 ` shaohua.li at inf dot ethz.ch
  4 siblings, 0 replies; 6+ messages in thread
From: shaohua.li at inf dot ethz.ch @ 2023-08-28 12:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Shaohua Li <shaohua.li at inf dot ethz.ch> ---
Thanks for all your comments!

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

end of thread, other threads:[~2023-08-28 12:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-28  7:13 [Bug c/111210] New: Wrong code at -Os on x86_64-linux-gnu since r12-4849-gf19791565d7 shaohua.li at inf dot ethz.ch
2023-08-28  7:29 ` [Bug c/111210] " amonakov at gcc dot gnu.org
2023-08-28 12:46 ` shaohua.li at inf dot ethz.ch
2023-08-28 12:50 ` xry111 at gcc dot gnu.org
2023-08-28 12:55 ` amonakov at gcc dot gnu.org
2023-08-28 12:58 ` shaohua.li at inf dot ethz.ch

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