mailing list of musl libc
 help / color / mirror / code / Atom feed
* broken gcc optimization for facilitynames
@ 2014-06-18 16:31 Clément Vasseur
  2014-06-19  3:34 ` Rich Felker
  0 siblings, 1 reply; 7+ messages in thread
From: Clément Vasseur @ 2014-06-18 16:31 UTC (permalink / raw)
  To: musl

Hello,

I might have found a gcc code generation problem with the facilitynames
implementation used in musl. See the following reduced test case:

$ cat test-facilitynames.c
#define SYSLOG_NAMES
#include <syslog.h>
int main(void)
{
    for (int i = 0; facilitynames[i].c_name; i++)
        if (facilitynames[i].c_name)
            return facilitynames[i].c_val;
}

$ musl-gcc -std=gnu99 -O1 test-facilitynames.c && ./a.out; echo $?
32

$ musl-gcc -std=gnu99 -O2 test-facilitynames.c && ./a.out; echo $?
1

I see similar results with gcc versions 4.6.1, 4.8.3 and 4.9.0 (with a
different return value with -O2).



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

* Re: broken gcc optimization for facilitynames
  2014-06-18 16:31 broken gcc optimization for facilitynames Clément Vasseur
@ 2014-06-19  3:34 ` Rich Felker
  2014-06-20 10:14   ` Christopher Meng
  2014-06-20 22:09   ` Clément Vasseur
  0 siblings, 2 replies; 7+ messages in thread
From: Rich Felker @ 2014-06-19  3:34 UTC (permalink / raw)
  To: musl

On Wed, Jun 18, 2014 at 04:31:58PM +0000, Clément Vasseur wrote:
> Hello,
> 
> I might have found a gcc code generation problem with the facilitynames
> implementation used in musl. See the following reduced test case:
> 
> $ cat test-facilitynames.c
> #define SYSLOG_NAMES
> #include <syslog.h>
> int main(void)
> {
>     for (int i = 0; facilitynames[i].c_name; i++)
>         if (facilitynames[i].c_name)
>             return facilitynames[i].c_val;
> }
> 
> $ musl-gcc -std=gnu99 -O1 test-facilitynames.c && ./a.out; echo $?
> 32
> 
> $ musl-gcc -std=gnu99 -O2 test-facilitynames.c && ./a.out; echo $?
> 1
> 
> I see similar results with gcc versions 4.6.1, 4.8.3 and 4.9.0 (with a
> different return value with -O2).

I can verify that I see this one on gcc 4.7.3. I don't see any UB in
the code so I'm pretty sure this is a gcc bug.

Note that we should really fix this horrible definition of
facilitynames (it bloats busybox by quite a bit), but it's a nice
demonstration of the gcc bug which we should also report and try to
get fixed..

Can you make a self-contained test case (copy the macros from musl's
syslog.h into the source file) and file a gcc bug report?

Rich


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

* Re: broken gcc optimization for facilitynames
  2014-06-19  3:34 ` Rich Felker
@ 2014-06-20 10:14   ` Christopher Meng
  2014-06-20 22:13     ` Clément Vasseur
  2014-06-20 22:09   ` Clément Vasseur
  1 sibling, 1 reply; 7+ messages in thread
From: Christopher Meng @ 2014-06-20 10:14 UTC (permalink / raw)
  To: musl

Hmm...

Clément, which gcc version are you using?


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

* Re: broken gcc optimization for facilitynames
  2014-06-19  3:34 ` Rich Felker
  2014-06-20 10:14   ` Christopher Meng
@ 2014-06-20 22:09   ` Clément Vasseur
  2014-06-20 22:51     ` Szabolcs Nagy
  2014-06-21  0:34     ` Rich Felker
  1 sibling, 2 replies; 7+ messages in thread
From: Clément Vasseur @ 2014-06-20 22:09 UTC (permalink / raw)
  To: musl

On 2014-06-19, Rich Felker <dalias@libc.org> wrote:
> On Wed, Jun 18, 2014 at 04:31:58PM +0000, Clément Vasseur wrote:
>> Hello,
>> 
>> I might have found a gcc code generation problem with the facilitynames
>> implementation used in musl. See the following reduced test case:
>> 
>> $ cat test-facilitynames.c
>> #define SYSLOG_NAMES
>> #include <syslog.h>
>> int main(void)
>> {
>>     for (int i = 0; facilitynames[i].c_name; i++)
>>         if (facilitynames[i].c_name)
>>             return facilitynames[i].c_val;
>> }
>> 
>> $ musl-gcc -std=gnu99 -O1 test-facilitynames.c && ./a.out; echo $?
>> 32
>> 
>> $ musl-gcc -std=gnu99 -O2 test-facilitynames.c && ./a.out; echo $?
>> 1
>> 
>> I see similar results with gcc versions 4.6.1, 4.8.3 and 4.9.0 (with a
>> different return value with -O2).
>
> I can verify that I see this one on gcc 4.7.3. I don't see any UB in
> the code so I'm pretty sure this is a gcc bug.
>
> Note that we should really fix this horrible definition of
> facilitynames (it bloats busybox by quite a bit), but it's a nice
> demonstration of the gcc bug which we should also report and try to
> get fixed..
>
> Can you make a self-contained test case (copy the macros from musl's
> syslog.h into the source file) and file a gcc bug report?

I made some progress towards a standalone test case. Here it is:

struct s1 { int v; };
struct s2 { int v; };

#define a ((struct s2 *)(struct s1 []){{ 42 }})

int main(void)
{
	for (int i = 0; a[i].v; i++)
		if (a[i].v)
			return a[i].v;
}

I also found out which optimization flag causes the broken output, it's
-fstrict-aliasing. I guess the issue here is whether casting `struct s1
[]` to `struct s2 *` violates the strict aliasing rules or not. Indeed,
compiling with -Wstrict-aliasing=1 shows a warning at this location.

Can someone pinpoint the exact location in the ISO C spec where there is
an explanation about the aliasing rules with this kind of pointer
compatibility?



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

* Re: broken gcc optimization for facilitynames
  2014-06-20 10:14   ` Christopher Meng
@ 2014-06-20 22:13     ` Clément Vasseur
  0 siblings, 0 replies; 7+ messages in thread
From: Clément Vasseur @ 2014-06-20 22:13 UTC (permalink / raw)
  To: musl

On 2014-06-20, Christopher Meng <cickumqt@gmail.com> wrote:
> Hmm...
>
> Clément, which gcc version are you using?

Like I said before: 4.6.1, 4.8.3 and 4.9.0.



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

* Re: Re: broken gcc optimization for facilitynames
  2014-06-20 22:09   ` Clément Vasseur
@ 2014-06-20 22:51     ` Szabolcs Nagy
  2014-06-21  0:34     ` Rich Felker
  1 sibling, 0 replies; 7+ messages in thread
From: Szabolcs Nagy @ 2014-06-20 22:51 UTC (permalink / raw)
  To: musl

* Cl?ment Vasseur <clement.vasseur@gmail.com> [2014-06-20 22:09:17 +0000]:
> 
> I made some progress towards a standalone test case. Here it is:
> 
> struct s1 { int v; };
> struct s2 { int v; };
> 
> #define a ((struct s2 *)(struct s1 []){{ 42 }})
> 
> int main(void)
> {
> 	for (int i = 0; a[i].v; i++)
> 		if (a[i].v)
> 			return a[i].v;
> }
> 
> I also found out which optimization flag causes the broken output, it's
> -fstrict-aliasing. I guess the issue here is whether casting `struct s1
> []` to `struct s2 *` violates the strict aliasing rules or not. Indeed,
> compiling with -Wstrict-aliasing=1 shows a warning at this location.
> 
> Can someone pinpoint the exact location in the ISO C spec where there is
> an explanation about the aliasing rules with this kind of pointer
> compatibility?

http://port70.net/~nsz/c/c11/n1570.html#6.5p7

different structs are not compatible types and
object with s1 effective type is accessed through
an expression with s2 type

(if a union of s1 and s2 were used then it would
be ok, because of the "common initial sequence"
rule, or if the first member of s1 had type s2,
because aggregate member can be aliased that way)

so yes this is an aliasing violation and syslog.h
should be fixed


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

* Re: Re: broken gcc optimization for facilitynames
  2014-06-20 22:09   ` Clément Vasseur
  2014-06-20 22:51     ` Szabolcs Nagy
@ 2014-06-21  0:34     ` Rich Felker
  1 sibling, 0 replies; 7+ messages in thread
From: Rich Felker @ 2014-06-21  0:34 UTC (permalink / raw)
  To: musl

On Fri, Jun 20, 2014 at 10:09:17PM +0000, Clément Vasseur wrote:
> On 2014-06-19, Rich Felker <dalias@libc.org> wrote:
> > On Wed, Jun 18, 2014 at 04:31:58PM +0000, Clément Vasseur wrote:
> >> Hello,
> >> 
> >> I might have found a gcc code generation problem with the facilitynames
> >> implementation used in musl. See the following reduced test case:
> >> 
> >> $ cat test-facilitynames.c
> >> #define SYSLOG_NAMES
> >> #include <syslog.h>
> >> int main(void)
> >> {
> >>     for (int i = 0; facilitynames[i].c_name; i++)
> >>         if (facilitynames[i].c_name)
> >>             return facilitynames[i].c_val;
> >> }
> >> 
> >> $ musl-gcc -std=gnu99 -O1 test-facilitynames.c && ./a.out; echo $?
> >> 32
> >> 
> >> $ musl-gcc -std=gnu99 -O2 test-facilitynames.c && ./a.out; echo $?
> >> 1
> >> 
> >> I see similar results with gcc versions 4.6.1, 4.8.3 and 4.9.0 (with a
> >> different return value with -O2).
> >
> > I can verify that I see this one on gcc 4.7.3. I don't see any UB in
> > the code so I'm pretty sure this is a gcc bug.
> >
> > Note that we should really fix this horrible definition of
> > facilitynames (it bloats busybox by quite a bit), but it's a nice
> > demonstration of the gcc bug which we should also report and try to
> > get fixed..
> >
> > Can you make a self-contained test case (copy the macros from musl's
> > syslog.h into the source file) and file a gcc bug report?
> 
> I made some progress towards a standalone test case. Here it is:
> 
> struct s1 { int v; };
> struct s2 { int v; };
> 
> #define a ((struct s2 *)(struct s1 []){{ 42 }})
> 
> int main(void)
> {
> 	for (int i = 0; a[i].v; i++)
> 		if (a[i].v)
> 			return a[i].v;
> }
> 
> I also found out which optimization flag causes the broken output, it's
> -fstrict-aliasing. I guess the issue here is whether casting `struct s1
> []` to `struct s2 *` violates the strict aliasing rules or not. Indeed,
> compiling with -Wstrict-aliasing=1 shows a warning at this location.

It's indeed an aliasing violation. And I don't see what the point of
it was in the first place. Whether c_name has type char * or const
char * has no bearing on whether the data can reside in .text/.rodata.

> Can someone pinpoint the exact location in the ISO C spec where there is
> an explanation about the aliasing rules with this kind of pointer
> compatibility?

C99 6.5 paragraph 7.

Rich


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

end of thread, other threads:[~2014-06-21  0:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-18 16:31 broken gcc optimization for facilitynames Clément Vasseur
2014-06-19  3:34 ` Rich Felker
2014-06-20 10:14   ` Christopher Meng
2014-06-20 22:13     ` Clément Vasseur
2014-06-20 22:09   ` Clément Vasseur
2014-06-20 22:51     ` Szabolcs Nagy
2014-06-21  0:34     ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).