Getting rid of alignment faults in userspace
Paul Brook
paul at codesourcery.com
Sat Jun 18 08:42:01 UTC 2011
> > > char buf[8];
> > > void *v = &buf[1];
> > > unsigned int *p = (unsigned int *)v;
> >
> > This does not (reliably) do what you expect. The compiler need not align
> > buf.
>
> Printing the value of p should clarify this.
>
> And, as we can see above, the "simple" accesses are left to the hardware
> to fix up. However, if the misaligned access is performed using a
> 64-bit value pointer, then the kernel will trap an exception and the
> access will be simulated.
I think you've missed my point. gcc may (though unlikely in this case) choose
to place buf at an odd address. In which case p will happen to be properly
aligned.
I'm not sure where you get "64-bit value pointer" from. *p is only a word
sized access, and memcpy is defined in terms of bytes so will only be promoted
to wider accesses when the compiler believes it is safe.
Paul
More information about the ubuntu-devel
mailing list