From 2694810153cf992823eb45253d26b8567424438f Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Thu, 14 Feb 2019 23:29:28 +0000 Subject: [PATCH] [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 --- lld/ELF/Filesystem.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lld/ELF/Filesystem.cpp b/lld/ELF/Filesystem.cpp index 957534a..5c8475f 100644 --- a/lld/ELF/Filesystem.cpp +++ b/lld/ELF/Filesystem.cpp @@ -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 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 L(M); + CV.wait(L, [&] { return Started; }); #endif } -- 2.7.4