Xcode 11: stack_not_16_byte_aligned_error with AVX code

There seems to be a regression in Xcode 11 beta, when AVX is enabled. The C library doesn't make the same assertions regarding alignment as the compiler.


Here is a C-reduced test case:


#include
#include
#include
#include
#include
typedef struct {
char d[16];
void *e;
struct { char b[5536]; } f;
} i;
void g(void) {
struct addrinfo hints, *k;
memset(&hints, 0, sizeof hints);
getaddrinfo(NULL, NULL, &hints, &k);
}
int main(void) {
puts("Hello world");
fflush(stdout);
close(open("/dev/null", O_RDONLY));
i context;
context.e = open;
printf("%p\n", context.d);
g();
return 0;
}


When compiled with AVX optimizations (e.g. -mavx or more commonly -march=native), this crashes even before `main()` is executed:


cc -mavx -O2 a.c && ./a.out
lldb ./a.out
run
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
frame #0: 0x00007fff65e73316 libdyld.dylib`stack_not_16_byte_aligned_error libdyld.dylib`stack_not_16_byte_aligned_error:
-> 0x7fff65e73316 <+0>: movdqa %xmm0, (%rsp)


Without AVX optimizations, or with Xcode 10, this doesn't happen.


This bug affects real-world applications such as libsodium and dsvpn.


A workaround is to use `-ffreestanding`.

i just want to say that this has be EXTREMELY helpfull


thank you @snowcat @Kevindb


-------------------


additional background in my case:


i was attempting to build linux headers with GCC-9 and was getting the same problem as with xcode (gcc-9 was built with xcode 11 command line tools).... the solution was to build with normal xcode and set `MACOSX_DEPLOYMENT_TARGET=10.14` to build linux headers properly


note:

i also tried building GCC-8 with `8.3.0-xcode-bug-_Atomic-fix.patch` and see if i can still use a GCC-based toolchain... ill update here if gcc-8 is a valid workaround.....

Xcode 11: stack_not_16_byte_aligned_error with AVX code
 
 
Q