[Modules] Fix implicit modules 'could not read module signature' problem
[Modules] Fix implicit modules 'could not read module signature' problem

This fixes a problem that happens while creating / using the same PCH file
(with -fmodules enabled) from different compiler invocations concurrently. One
of the invocations might fail with the error message:

'truncated or malformed object: could not read module signature'

In addModule(), we populate a FileEntry for the PCM by calling
lookupModuleFile(), where clang opens the file immediately to ensure there is
no race between stat'ing and opening the file. At this point, we cache the size
for SomeModule.pcm. Another process might come around at this point, re-write
the PCM, changing its size. When we resume, next thing clang does is to call
getBufferForFile to populate the memory buffer:

--
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile,
                              bool ShouldCloseOpenFile) {
  uint64_t FileSize = Entry->getSize();
  // If there's a high enough chance that the file have changed since we
  // got its size, force a stat before opening it.
  if (isVolatile)
    FileSize = -1;

  StringRef Filename = Entry->getName();
  // If the file is already open, use the open file descriptor.
  if (Entry->File) {
    auto Result =
        Entry->File->getBuffer(Filename, FileSize,
                               /*RequiresNullTerminator=*/true, isVolatile);
  ....
--

If we don't force the reload above, we might end up getting a buffer with a
smaller size than we need, since the signature lives at the end of the PCM, and
potentialy past the size of the buffer, we hit the malformed message

rdar://problem/45337549
(cherry picked from commit a113643bc4fc0547e6b559c469bd3ddc86dae2c0)

apple/swift-clang,apple/swift-clang,apple/swift-clang,apple/swift-clang,apple/swift-clang,apple/swift-clang,apple/swift-clang,apple/swift-clang,apple/swift-clang,apple/swift-clang