jeudi 1 juin 2017

Segmentation fault in capturing aligned variables in lambdas

This little code snippet segfaults with g++ 6.2.0 and clang++ 3.8.1 with:

clang++ -std=c++11 -O3 -mavx -pthread or g++ -std=c++11 -O3 -mavx -pthread

#include <thread>
#include <iostream>

class alignas(32) AlignedObject {
public:
  float dummy[8];
};

int main() {
  while (true) {
    std::thread([](){
      AlignedObject x;
      std::cout << &x;
      std::thread([x](){
        std::cout << &x;
      }).join();
    }).join();
  }

  return 0;
}

Looking at the disassembly, both compilers are inserting vmovaps instructions that are failing, suggesting that compiler-generated objects somewhere aren't being aligned properly. It works fine if -mavx is removed since the instruction doesn't get used anymore. Is this a compiler bug or is this code relying on undefined behaviour?

Aucun commentaire:

Enregistrer un commentaire