Possible clang bug with aligned structs

I've found a problem where clang generates code which crashes. The simplest case seems to happen when trying to use a vector type or aligned struct in a struct with the aligned and packed attributes, where the first item in the struct is smaller than the aligned struct. On assignment to the vector field, the program crashes with gdb reporting it as "Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: 13 at address: 0x0000000000000000"

The reduced test case crash occurs when building WITHOUT optimizations (i.e. -O0), but doesn't with higher levels (-O1, -O2, or -O3). However, in the larger program I was working with when I encountered the problem, it was broken with and without optimizations. Both the test case and the program I was working on worked fine with gcc and llvm-gcc.

Both lldb and gdb are terribly confused in functions where this problem occurs, reporting wrong values for the arguments passed in which don't agree with values printed out.

Attached is a small test case. It consists of 4 variants on the same concept, with some more notes about what I've found about the problem. With any of the special vector extension types, or the struct with the aligned attribute, clang generates code which crashes when assigning to that field within the larger struct. Tested on x86_64 Linux and OS X, clang 2.8, and gcc-4.5, and (GCC) 4.2.1 (Apple Inc. build 5664).

Build / run it with:

$ clang -O0 aligned_struct_bug.c && ./a.out

aligned_struct_bug.c (2.85 KB)

Hi Matt,

This definitely looks like a bug, please file a report at
llvm.org/bugs for tracking purposes.

- Daniel