[cfe-dev] -fsanitize=undefined and alignment on x86_64

Martin Martin martin at silverliningsystems.com
Fri Jan 18 11:14:23 PST 2013


This is essentially what I was doing.

So that's potentially bad even on x86_64, because optimizations are free to
assume the lower order bits of the pointer are zero?


On Fri, Jan 18, 2013 at 2:04 PM, Richard Smith <richard at metafoo.co.uk>wrote:

> On Fri, Jan 18, 2013 at 4:21 AM, Martin Martin
> <martin at silverliningsystems.com> wrote:
> > Hi all,
> >
> > sanitize=undefined is giving me an error message about unaligned access
> to a
> > size_t.  It's in a packed structure, and indeed is not 8 byte aligned
> > (although it is 4 byte aligned.)  This is in Linux on an x86_64
> > architecture.
> >
> > My question is: why is this undefined behavior?  Can't modern Intel and
> AMD
> > processors accesses unaligned ints, although with a possible performance
> > penalty?  So this would be a performance problem, but not correctness?
>  Is
> > the problem that the compiler could use SSE or some other instructions
> that
> > require alignment and will seg fault or give wrong results with unaligned
> > access?
>
> Please can you provide a code sample which triggers the issue? Packed
> structs are supported by -fsanitize=alignment. However, note that
> taking the address of a misaligned member of a packed struct will not
> in general work:
>
> struct __attribute__((packed)) S {
>   char c;
>   long l;
> } s;
> int f() { return s.l; } // ok
> int g() { return *&s.l; } // undefined behavior: long* access requires
> 8 byte alignment
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130118/d145f391/attachment.html>


More information about the cfe-dev mailing list