[PATCH] D39868: Make sure the temporary file is almost always deleted

Rafael Ávila de Espíndola via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 9 13:45:35 PST 2017


rafael created this revision.
Herald added subscribers: arichardson, emaste.

It is really hard to cover restarts in a debug, SIGKILL or power failures.

RemoveFileOnSignal takes care of crashes.

So what is left is making sure all regular exits delete the file. This patch does that by moving the buffer to error handling. That is a bit of hack, but seemed better than to generalize it to take a callback on construction.

Sending for review with just ELF. It something like this is OK I will update COFF too.


https://reviews.llvm.org/D39868

Files:
  Common/ErrorHandler.cpp
  ELF/Writer.cpp
  include/lld/Common/ErrorHandler.h


Index: include/lld/Common/ErrorHandler.h
===================================================================
--- include/lld/Common/ErrorHandler.h
+++ include/lld/Common/ErrorHandler.h
@@ -31,6 +31,7 @@
 #include "lld/Common/LLVM.h"
 
 #include "llvm/Support/Error.h"
+#include "llvm/Support/FileOutputBuffer.h"
 
 namespace lld {
 
@@ -52,6 +53,8 @@
   void message(const Twine &Msg);
   void warn(const Twine &Msg);
 
+  std::unique_ptr<llvm::FileOutputBuffer> OutputBuffer;
+
 private:
   void print(StringRef S, raw_ostream::Colors C);
 };
Index: ELF/Writer.cpp
===================================================================
--- ELF/Writer.cpp
+++ ELF/Writer.cpp
@@ -22,7 +22,6 @@
 #include "lld/Common/Threads.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSwitch.h"
-#include "llvm/Support/FileOutputBuffer.h"
 #include <climits>
 
 using namespace llvm;
@@ -38,6 +37,7 @@
 // The writer writes a SymbolTable result to a file.
 template <class ELFT> class Writer {
 public:
+  Writer() : Buffer(errorHandler().OutputBuffer) {}
   typedef typename ELFT::Shdr Elf_Shdr;
   typedef typename ELFT::Ehdr Elf_Ehdr;
   typedef typename ELFT::Phdr Elf_Phdr;
@@ -70,7 +70,7 @@
   void writeSectionsBinary();
   void writeBuildId();
 
-  std::unique_ptr<FileOutputBuffer> Buffer;
+  std::unique_ptr<FileOutputBuffer> &Buffer;
 
   void addRelIpltSymbols();
   void addStartEndSymbols();
Index: Common/ErrorHandler.cpp
===================================================================
--- Common/ErrorHandler.cpp
+++ Common/ErrorHandler.cpp
@@ -46,6 +46,9 @@
 }
 
 void lld::exitLld(int Val) {
+  // Delete the output buffer so that any tempory file is deleted.
+  errorHandler().OutputBuffer.reset();
+
   // Dealloc/destroy ManagedStatic variables before calling
   // _exit(). In a non-LTO build, this is a nop. In an LTO
   // build allows us to get the output of -time-passes.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39868.122326.patch
Type: text/x-patch
Size: 1899 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171109/8c086187/attachment.bin>


More information about the llvm-commits mailing list