Skip to content

Commit

Permalink
[lld] Fix elf::unlinkAsync detached thread
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vitalybuka committed Feb 14, 2019
1 parent 0b2dca9 commit 2694810
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lld/ELF/Filesystem.cpp
Expand Up @@ -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
}

Expand Down

0 comments on commit 2694810

Please sign in to comment.