[lld] r354078 - [lld] Fix elf::unlinkAsync detached thread

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 14 15:29:28 PST 2019

Author: vitalybuka
Date: Thu Feb 14 15:29:28 2019
New Revision: 354078

URL: http://llvm.org/viewvc/llvm-project?rev=354078&view=rev
[lld] Fix elf::unlinkAsync detached thread

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.


Reviewers: ruiu, espindola

Subscribers: emaste, arichardson, jfb, llvm-commits

Tags: #llvm

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


Modified: lld/trunk/ELF/Filesystem.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Filesystem.cpp?rev=354078&r1=354077&r2=354078&view=diff
--- lld/trunk/ELF/Filesystem.cpp (original)
+++ lld/trunk/ELF/Filesystem.cpp Thu Feb 14 15:29:28 2019
@@ -58,9 +58,26 @@ void elf::unlinkAsync(StringRef Path) {
   std::error_code EC = sys::fs::openFileForRead(Path, FD);
+  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; });

More information about the llvm-commits mailing list