ThreadSanitizer in 3.5 nightly appears to incorrectly schedule calls to atexit handlers

Hi -

I’m using the clang 3.5 builds from http://llvm.org/apt/ on Ubuntu 14.04:

$clang --version

Ubuntu clang version 3.5-1ubuntu1 (trunk) (based on LLVM 3.5)
Target: x86_64-pc-linux-gnu
Thread model: posix

The following program calls the Logger destructor after calling the registered atexit handler, since the logger object was constructed before the call to atexit:

$ cat ./tsan_vs_atexit.cpp
#include
#include

class Logger {
public:
Logger() {
std::printf(“Logger ctor\n”);
}

void log(const char* msg) {
std::printf(“%s”, msg);
}

~Logger() {
std::printf(“Logger dtor\n”);
}
};

Logger logger;

void log_from_atexit() {
logger.log(“In log_from_atexit\n”);
}

int main(int argc, char* argv) {
std::atexit(log_from_atexit);
return EXIT_SUCCESS;
}

$ clang++ -std=c++03 -fPIE -pie ./tsan_vs_atexit.cpp
$ ./a.out
Logger ctor
In log_from_atexit
Logger dtor

However, if I enable the thread sanitizer, now the atexit call is sequenced after the call to destroy logger.

$ clang++ -std=c++03 -fPIE -pie -fsanitize=thread ./tsan_vs_atexit.cpp
$ ./a.out
Logger ctor
Logger dtor
In log_from_atexit

Unless I’ve misunderstood the C++03 language around std::atexit that ordering is incorrect.

A quick google search didn’t find much. Is this a known issue, or is the ordering not specified in C++03 as I thought?

Thanks,
Andrew

I think you are right, and this is a bug in TSan. I’ve filed https://code.google.com/p/thread-sanitizer/issues/detail?id=74