All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Brown <david@westcontrol.com>
To: Michal Soltys <soltys@ziu.info>
Cc: David Brown <david.brown@hesbynett.no>,
	linux-raid@vger.kernel.org, Jes.Sorensen@redhat.com,
	dledford@redhat.com
Subject: Re: [PATCH 1/1] Work around gcc-4.7's strict aliasing checks
Date: Fri, 13 Jan 2012 13:20:51 +0100	[thread overview]
Message-ID: <4F1021A3.6020203@westcontrol.com> (raw)
In-Reply-To: <4F0EE3F3.9050906@ziu.info>

On 12/01/2012 14:45, Michal Soltys wrote:
> On 12.01.2012 08:14, David Brown wrote:
>> On 12/01/12 00:43, NeilBrown wrote:
>>> On Thu, 5 Jan 2012 12:16:41 +0100 Jes.Sorensen@redhat.com wrote:
>>>
>>>> From: Jes Sorensen<Jes.Sorensen@redhat.com>
>>>>
>>>> -	info->array.ctime	  = DECADE + __be32_to_cpu(*(__u32*)
>>>> -							 (ddf->anchor.guid+16));
>>>> +	cptr = (__u32 *)(ddf->anchor.guid + 16);
>>>> +	info->array.ctime	  = DECADE + __be32_to_cpu(*cptr);
>
> But going through intermediate (yet incompatible) pointer still breaks
> the aliasing rules - unless the above is related to how 4.7+ in
> particular do things.
>
> Somewhat similar code - on 4.5.2 and 4.4.1 though, of which the former
> will warn about possible break only with the most aggressive (broad)
> -Wstrict-aliasing=1 (and the latter at all levels):
>
> int main(void)
> {
> 	struct s {
> 		uint16_t d[128];
> 	} tab;
> 	uint32_t *p32;
>
> 	tab.d[8] = 0x5678;
> 	tab.d[9] = 0x1234;
>
> 	p32 = (uint32_t *)&tab.d[8];
> 	printf("%X\n", *p32);
>
> 	return 0;
> }
>
>
> Will output 0 on my machines when compiled with -O2 or higher (or -O1
> -fstrict-aliasing) - unless -fno-strict-aliasing is added.
>
> The code clearly breaks the rules, so no wonder optimizations relying on
> them messed up. I'm not sure if/how much changed since then, so perhaps
> it would behave fine on 4.7+.
>
>>
>> The basic rule is that the compiler can assume that objects whose type
>> has different sizes, cannot appear at the same address. Unions are one
>> way to avoid this.
>>
>
> Following this lengthy (but quite interesting) thread it's not (was not
> ?) allowed either:
> http://thread.gmane.org/gmane.comp.gcc.devel/111111
>
> It's 2 years old thread, still the rules and interpretation seem pretty
> well entrenched.
>
> OTOH: __attribute__((__may_alias__)) should(?) always give consistent
> (expected) results with type definitions that are used as aliases. Maybe it
> would be better to go by this route.

I couldn't find that attribute in the gcc manual.

>
> In context of mdadm - I wonder if simply -fno-strict-aliasing wouldn't
> be the overall best thing to do.

I don't know much about the style requirements for kernel code, but it 
might be best to specify this using "#pragma GCC optimize 
("no-strict-aliasing")" to force it into the source code independently 
of any global compiler options.


  reply	other threads:[~2012-01-13 12:20 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-05 11:16 [PATCH 0/1] gcc-4.7 build fix Jes.Sorensen
2012-01-05 11:16 ` [PATCH 1/1] Work around gcc-4.7's strict aliasing checks Jes.Sorensen
2012-01-11 23:43   ` NeilBrown
2012-01-12  7:14     ` David Brown
2012-01-12 13:45       ` Michal Soltys
2012-01-13 12:20         ` David Brown [this message]
2012-01-13 14:21           ` Michal Soltys
2012-01-13 17:48             ` David Brown
2012-01-12  9:24     ` Jes Sorensen
2012-01-22  0:49     ` Michal Soltys
2012-01-22  0:49       ` [PATCH] compile cleanly with -Wstrict-aliasing=1 Michal Soltys
2012-01-23 12:50       ` [PATCH 1/1] Work around gcc-4.7's strict aliasing checks Jes Sorensen
2012-01-24  0:59         ` Michal Soltys
2012-01-24  9:18           ` David Brown

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=4F1021A3.6020203@westcontrol.com \
    --to=david@westcontrol.com \
    --cc=Jes.Sorensen@redhat.com \
    --cc=david.brown@hesbynett.no \
    --cc=dledford@redhat.com \
    --cc=linux-raid@vger.kernel.org \
    --cc=soltys@ziu.info \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.