diff options
author | Peter Collingbourne <peter@pcc.me.uk> | 2017-09-05 19:51:38 +0000 |
---|---|---|
committer | Peter Collingbourne <peter@pcc.me.uk> | 2017-09-05 19:51:38 +0000 |
commit | a3886c11ee55aa4cb683cbb34d9bf33e4de6fc63 (patch) | |
tree | 33d94efa7fab41efb583fd1e1d2eab230b173183 /lib/LTO | |
parent | 8c5b337a87c209b26c559510827172073bf4bb8a (diff) |
LTO: Try to open cache files before renaming them.
It appears that a potential race between the cache client and the cache
pruner that I thought was unlikely actually happened in practice [1].
Try to avoid the race condition by opening the temporary file before
renaming it. Do this only on non-Windows platforms because we cannot
rename open files on Windows using the sys::fs::rename function.
[1] https://luci-logdog.appspot.com/v/?s=chromium%2Fbb%2Fchromium.memory%2FLinux_CFI%2F1610%2F%2B%2Frecipes%2Fsteps%2Fcompile%2F0%2Fstdout
Differential Revision: https://reviews.llvm.org/D37410
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@312567 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/LTO')
-rw-r--r-- | lib/LTO/Caching.cpp | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/lib/LTO/Caching.cpp b/lib/LTO/Caching.cpp index e32e46c4c3c..3f10c154683 100644 --- a/lib/LTO/Caching.cpp +++ b/lib/LTO/Caching.cpp @@ -36,7 +36,7 @@ Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath, ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(EntryPath); if (MBOrErr) { - AddBuffer(Task, std::move(*MBOrErr)); + AddBuffer(Task, std::move(*MBOrErr), EntryPath); return AddStreamFn(); } @@ -60,22 +60,37 @@ Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath, EntryPath(std::move(EntryPath)), Task(Task) {} ~CacheStream() { - // FIXME: This code could race with the cache pruner, but it is unlikely - // that the cache pruner will choose to remove a newly created file. - // Make sure the file is closed before committing it. OS.reset(); - // This is atomic on POSIX systems. + +#ifdef _WIN32 + // Rename the file first on Windows because we cannot rename an open + // file on that platform using the sys::fs::rename function. + // FIXME: This code could race with the cache pruner, but it is unlikely + // that the cache pruner will choose to remove a newly created file. + // We should look at using the SetFileInformationByHandle function to + // rename the file while it is open. if (auto EC = sys::fs::rename(TempFilename, EntryPath)) report_fatal_error(Twine("Failed to rename temporary file ") + TempFilename + ": " + EC.message() + "\n"); ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(EntryPath); +#else + // Open the file first to avoid racing with a cache pruner. + ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = + MemoryBuffer::getFile(TempFilename); + + // 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"); +#endif + if (!MBOrErr) report_fatal_error(Twine("Failed to open cache file ") + EntryPath + ": " + MBOrErr.getError().message() + "\n"); - AddBuffer(Task, std::move(*MBOrErr)); + AddBuffer(Task, std::move(*MBOrErr), EntryPath); } }; |