Subject: Re: alignment crash in v6 ipfilter when receiving on gif
To: None <port-sparc64@NetBSD.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: port-sparc64
Date: 07/11/2007 15:36:42
>> +			memcpy(&ip6_src, &ip6->ip6_src,
>> +			       sizeof(struct in6_addr));
> Unless we force a function call for memcpy, the compiler can still
> assume that the source is 64bit aligned and ensure that the target is
> 64bit aligned and to 64bit operations for the copy!

...because it is of a type that requires alignment.

> IMHO this whole 'optimisation' in gcc sucks.  Having badly aligned
> pointers to structures is all too common in system code.

Then declare them correctly!  You should never overlay a struct with
alignment-requiring members onto a memory block that is not correctly
aligned.  That this has ever worked is an accident of historical
implementations; it has never been correct.  Makes me glad I don't
write code that way.

If you want to do this right, do something like

memcpy(&ip6_src,((char *)&ip6)+offsetof(ip6,ip6_src),sizeof(...));

(mutatis mutandis; I'm not sure I have all the syntax right).

If the IPv6 source address is not guaranteed aligned, don't use a
struct element that implies alignment.  (Or declare it
__attribute__((packed)) or whatever it is that tells gcc that its
alignment may not be as much as what its type normally calls for, if
you're willing to use gcc extensions.)

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B