[lld] Fix elf::unlinkAsync detached thread

Summary:
So this patch just make sure that the thread is at least stated
before we return from main.

If we just detach then the thread may be actually be stated just after
the process returned from main and it's calling atexit handers. Then the thread may try to create own function static variable and it will
add new at exit handlers confusing libc.

GLIBC before 2.27 had race in that case which corrupted atexit handlers
list. Support for this use-case for other implementation is also unclear,
so we can try just avoid that.

PR40162

Reviewers: ruiu, espindola

Subscribers: emaste, arichardson, jfb, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D58246

llvm-svn: 354078
This commit is contained in:
Vitaly Buka
2019-02-14 23:29:28 +00:00
parent 0b2dca9f83
commit 2694810153

View File

@@ -58,9 +58,26 @@ void elf::unlinkAsync(StringRef Path) {
std::error_code EC = sys::fs::openFileForRead(Path, FD);
sys::fs::remove(Path);
if (EC)
return;
// close and therefore remove TempPath in background.
if (!EC)
std::thread([=] { ::close(FD); }).detach();
std::mutex M;
std::condition_variable CV;
bool Started = false;
std::thread([&, FD] {
{
std::lock_guard<std::mutex> L(M);
Started = true;
CV.notify_all();
}
::close(FD);
}).detach();
// GLIBC 2.26 and earlier have race condition that crashes an entire process
// if the main thread calls exit(2) while other thread is starting up.
std::unique_lock<std::mutex> L(M);
CV.wait(L, [&] { return Started; });
#endif
}