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

Richard Smith richard at metafoo.co.uk
Fri Jan 18 11:14:22 PST 2013


On Fri, Jan 18, 2013 at 11:09 AM, Tim Northover <t.p.northover at gmail.com> wrote:
>> The Standard simply says that access should be aligned at least to the
>> natural alignment of the type. You are free to overalign but not to
>> underalign.
>>
>> Various compilers provide extensions to "pack" structures and a number of
>> CPUs support unaligned accesses, but the Standard is concerned about ALL
>> architectures and thus Standard compliant code should conform to alignment.
>
> Well, if you were writing standard compliant code you wouldn't be
> using packed structs in the first place. I suppose the question I'd
> have is how Clang intends packed structs to fit in.
>
> Saying that the programmer can use them but their code immediately
> becomes undefined behaviour by doing so doesn't seem tenable to me.
>
> Some kind of type qualifier seems to be the obvious candidate: if
> "__attribute__((packed)) size_t" has no alignment requirements then
> most obvious properties follow (you can access fields directly, but
> not take pointers). But there may be issues with overloads and other
> things I've only vaguely heard about.
>
> Does anyone know what our official intent is, out of curiosity?

This is a GCC extension; our intent is to match GCC. That is:

Direct access to, and modification of, fields of packed structs is OK.
Taking addresses of them yields a pointer which you can't use for any
operations which require more aligment than the pointer actually has.



More information about the cfe-dev mailing list