llvm-project/llvm/lib/LTO
Peter Collingbourne 881ba10465 LTO: Keep file handles open for memory mapped files.
On Windows we've observed that if you open a file, write to it, map it into
memory and close the file handle, the contents of the memory mapping can
sometimes be incorrect. That was what we did when adding an entry to the
ThinLTO cache using the TempFile and MemoryBuffer classes, and it was causing
intermittent build failures on Chromium's ThinLTO bots on Windows. More
details are in the associated Chromium bug (crbug.com/786127).

We can prevent this from happening by keeping a handle to the file open while
the mapping is active. So this patch changes the mapped_file_region class to
duplicate the file handle when mapping the file and close it upon unmapping it.

One gotcha is that the file handle that we keep open must not have been
created with FILE_FLAG_DELETE_ON_CLOSE, as otherwise the operating system
will prevent other processes from opening the file. We can achieve this
by avoiding the use of FILE_FLAG_DELETE_ON_CLOSE altogether.  Instead,
we use SetFileInformationByHandle with FileDispositionInfo to manage the
delete-on-close bit. This lets us remove the hack that we used to use to
clear the delete-on-close bit on a file opened with FILE_FLAG_DELETE_ON_CLOSE.

A downside of using SetFileInformationByHandle/FileDispositionInfo as
opposed to FILE_FLAG_DELETE_ON_CLOSE is that it prevents us from using
CreateFile to open the file while the flag is set, even within the same
process. This doesn't seem to matter for almost every client of TempFile,
except for LockFileManager, which calls sys::fs::create_link to create a
hard link from the lock file, and in the process of doing so tries to open
the file. To prevent this change from breaking LockFileManager I changed it
to stop using TempFile by effectively reverting r318550.

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

llvm-svn: 334630
2018-06-13 18:03:14 +00:00
..
CMakeLists.txt Support: Add a VCSRevision.h header file. 2017-04-13 01:26:12 +00:00
Caching.cpp LTO: Keep file handles open for memory mapped files. 2018-06-13 18:03:14 +00:00
LLVMBuild.txt NFC: Move ObjectMemoryBuffer to support 2018-04-15 05:17:14 +00:00
LTO.cpp [ThinLTO] Rename index IsAnalysis flag to HaveGVs (NFC) 2018-06-06 22:22:01 +00:00
LTOBackend.cpp IRGen: Write .dwo files when -split-dwarf-file is used together with -fthinlto-index. 2018-05-31 18:25:59 +00:00
LTOCodeGenerator.cpp Revert "[LTO] Support filtering by hotness threshold" 2018-03-08 01:13:10 +00:00
LTOModule.cpp Move TargetLoweringObjectFile from CodeGen to Target to fix layering 2018-03-23 23:58:19 +00:00
ThinLTOCodeGenerator.cpp [ThinLTO] Rename index IsAnalysis flag to HaveGVs (NFC) 2018-06-06 22:22:01 +00:00
UpdateCompilerUsed.cpp Remove redundant includes from lib/LTO. 2017-12-13 21:30:53 +00:00