tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Memory alignment not respected in thread-local objects


My today's problem was to evaluate crashes in compiler-rt's xray tests. 
After some debugging, I've came to the conclusion that the primary cause
is that TLS allocated for aligned objects isn't actually aligned.

Take the following sample (C++11) program:

  #include <cstdio>

  struct alignas(64) a {
    int x;

  void b() {
    thread_local a as_tls;

    printf("as tls: %p\n", &as_tls);

  int main() {
    return 0;

Both when compiled with g++ and with clang++, I get:

  as tls: 0x7f7ff7efe068

which is certainly not aligned on 64-byte boundary.  I have tested
the same program on Linux and FreeBSD, and on both platforms I get
properly aligned addresses.  Furthermore, if I change the alignment
value, I see the address changing on Linux and FreeBSD but not
on NetBSD.

This is problematic to me since clang/llvm seem to assume that
the object will actually be aligned, and emit code (e.g. MOVAPS
instructions) that segfaults when dealing with unaligned data.

Is this a fault of our implementation?  Could someone help me
figure out how to solve this?

Best regards,
Michał Górny

Attachment: signature.asc
Description: This is a digitally signed message part

Home | Main Index | Thread Index | Old Index