diff options
author | Peter Collingbourne <peter@pcc.me.uk> | 2017-03-16 18:20:06 +0000 |
---|---|---|
committer | Peter Collingbourne <peter@pcc.me.uk> | 2017-03-16 18:20:06 +0000 |
commit | cb46a6baf7424cb9e2c3085a0d622b0378dd314f (patch) | |
tree | d4ce0fd08828b519f072227a3ec870387eeb8a18 /llvm/lib/LTO | |
parent | dc855221afec792251ba910d4964bf255057a273 (diff) | |
download | bcm5719-llvm-cb46a6baf7424cb9e2c3085a0d622b0378dd314f.tar.gz bcm5719-llvm-cb46a6baf7424cb9e2c3085a0d622b0378dd314f.zip |
LTO: Create temporary cache files in the cache directory instead of $TMPDIR.
This fixes a race condition where another linker process can observe a
partially written file if we copy it from another file system, and allows
the link to be independent of the amount of free disk space in $TMPDIR.
Differential Revision: https://reviews.llvm.org/D31045
llvm-svn: 297970
Diffstat (limited to 'llvm/lib/LTO')
-rw-r--r-- | llvm/lib/LTO/Caching.cpp | 36 |
1 files changed, 8 insertions, 28 deletions
diff --git a/llvm/lib/LTO/Caching.cpp b/llvm/lib/LTO/Caching.cpp index fc898c49998..b7542049dbf 100644 --- a/llvm/lib/LTO/Caching.cpp +++ b/llvm/lib/LTO/Caching.cpp @@ -21,31 +21,6 @@ using namespace llvm; using namespace llvm::lto; -static void commitEntry(StringRef TempFilename, StringRef EntryPath) { - // Rename to final destination (hopefully race condition won't matter here) - auto EC = sys::fs::rename(TempFilename, EntryPath); - if (EC) { - // Renaming failed, probably not the same filesystem, copy and delete. - // FIXME: Avoid needing to do this by creating the temporary file in the - // cache directory. - { - auto ReloadedBufferOrErr = MemoryBuffer::getFile(TempFilename); - if (auto EC = ReloadedBufferOrErr.getError()) - report_fatal_error(Twine("Failed to open temp file '") + TempFilename + - "': " + EC.message() + "\n"); - - raw_fd_ostream OS(EntryPath, EC, sys::fs::F_None); - if (EC) - report_fatal_error(Twine("Failed to open ") + EntryPath + - " to save cached entry\n"); - // I'm not sure what are the guarantee if two processes are doing this - // at the same time. - OS << (*ReloadedBufferOrErr)->getBuffer(); - } - sys::fs::remove(TempFilename); - } -} - Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath, AddFileFn AddFile) { if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath)) @@ -78,7 +53,10 @@ Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath, ~CacheStream() { // Make sure the file is closed before committing it. OS.reset(); - commitEntry(TempFilename, EntryPath); + // This is atomic on POSIX systems. + if (auto EC = sys::fs::rename(TempFilename, EntryPath)) + report_fatal_error(Twine("Failed to rename temporary file ") + + TempFilename + ": " + EC.message() + "\n"); AddFile(Task, EntryPath); } }; @@ -86,9 +64,11 @@ Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath, return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> { // Write to a temporary to avoid race condition int TempFD; - SmallString<64> TempFilename; + SmallString<64> TempFilenameModel, TempFilename; + sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o"); std::error_code EC = - sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename); + sys::fs::createUniqueFile(TempFilenameModel, TempFD, TempFilename, + sys::fs::owner_read | sys::fs::owner_write); if (EC) { errs() << "Error: " << EC.message() << "\n"; report_fatal_error("ThinLTO: Can't get a temporary file"); |