public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Thibault Castel <thibault.castel@gmail.com>
To: gcc-help@gcc.gnu.org
Cc: axel-freyn@gmx.de
Subject: Re: Re: Weird strict-aliasing break
Date: Mon, 30 May 2011 12:45:00 -0000	[thread overview]
Message-ID: <BANLkTik_ky5jN2r+85OkULe6_AX=vL7q7w@mail.gmail.com> (raw)

> From: Axel Freyn <axel-freyn@gmx.de>
> To: gcc-help@gcc.gnu.org
> 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 ;)

             reply	other threads:[~2011-05-30 12:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-30 12:45 Thibault Castel [this message]
2011-05-30 17:27 ` Jonathan Wakely
2011-05-30 13:08 Thibault Castel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='BANLkTik_ky5jN2r+85OkULe6_AX=vL7q7w@mail.gmail.com' \
    --to=thibault.castel@gmail.com \
    --cc=axel-freyn@gmx.de \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).