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

Richard Smith richard at metafoo.co.uk
Fri Jan 18 11:04:59 PST 2013


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



More information about the cfe-dev mailing list