Re: Re: Weird strict-aliasing break

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> From:ÂAxel Freyn <axel-freyn@xxxxxx>
> To:Âgcc-help@xxxxxxxxxxx
> Date:ÂMon, 30 May 2011 11:19:54 +0200
> Subject:ÂRe: Weird strict-aliasing break
> Hi Thibault,
>
> On Mon, May 30, 2011 at 10:34:40AM +0200, Thibault Castel wrote:
>> Hi,
>>
>> I have a weird warning when compiling my program with g++:
>> Â Â"dereferencing pointer âNAMEâ does break strict-aliasing rules"
>>
>> So, I tried to investigate a little bit and find some clues : I do a
>> memory mapping from a char buffer to a C struct to have access to
>> different datas into the buffer. Pretty casual. But, when I try to
>> access one of my inner field, the warning triggers... Huh...
>>
>> To understand a bit more, I wrote a litte test program outside of my
>> project (really huge). See it in attachment. And I don't really
>> understand, because the warning is triggered only line 77, not on line
>> 55. Only when I try to access the inner fields of a mapped structured
>> included into another structure.
>>
>> I successfully avoid the warning with "__attribute__((__may_alias__))"
>> but this "cheat" bother me because :
>> Â- I don't want to fake the compiler
>> Â- I need to compile my huge project both under Linux (gcc/g++) and
>> Windows (Visual IDE)
>> Â- This warning is triggered a hundred times in my huge project (old C
>> project slowly turning into C++ :/)
>>
>> I already saw some messages on this list about this warning. But the
>> difference here is my program IS working. pFoo->a and pFoo->b have
>> correct values.
>>
>> I tried different scenarios
>> Â- grow or reduce the raw array
>> Â- change the definition order into TData
>>
>> [...]
>>
>> Thanks a lot for any clue or explanation
>
> I'm not 100% sure -- however here is my opinion:
>
> Â- the strict aliasing rules assume that you never access an object
> Â through two pointers of different type "at the same time", so
> Â your code in line 54 violates strict aliasing, too.
>

I assume that too, so I didn't understand why it didn't complain about
this line.

> Â- if you violate strict aliasing, that MIGHT introduce problems
> Â (especially during optimization, and if you change values). However,
> Â it might work without any problems.
>

Yes, and here, I don't have any problem because it's only a memory
mapping (as we all did in C in the past to read system structs for
example)


> Â- gcc warns not about ALL, but only about SOME strict-aliasing
> Â violations. You can adjust the warnings by -Wstrict-aliasing=1 (or
> Â 2 or 3). With "-Wstrict-aliasing=2", I also obtain the warning
> warn-strict.cpp:54: warning: dereferencing type-punned pointer will break strict-aliasing rules

I tried that too, but it's another warning on another line.
This new warning is triggered on line 54 and 76 and my warning is
always triggered only on line 77 (where the pointer is dereferenced)
and not on line 55 :/

> Â I think, gcc tries to warn only when it "expects" problems. So
> Â probably gcc is "quite sure" that line 54 does not introduce
> Â problems (for example gcc 4.7 on x86_64 does not warn at all for
> Â your code).

That's because of this behavior on new version of gcc I post this
thread here. I don't know if it's gcc-related or code-related.
(Don't misunderstand : my code BREAKS strict aliasing rules, but the
two pieces of code (line 55 and line 77) don't have the same behavior,
and I don't understand why)

> Â- you can avoid all problems with gcc by using "-fno-strict-aliasing",
> Â which tells gcc not to assume strict aliasing during it's
> Â optimizations: with this option, everything should be safe.

But it's also written (I don't remember where, I'm trying to find the
source) that gcc behavior is undefined when -O2 flag and
-fno-strict-aliasing flag are used together.
I forgot to say that my warning is not triggered with -O0 but appears
only in -O2.

> I don't know of any portable way how to avoid this possible problem (except
> correcting the code...)

I want to correct the code, but I don't know how ! I really don't like
the __attribute__ directive

>
> Axel

Thanks ;)



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux