diff options
author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2017-03-17 22:55:13 +0000 |
---|---|---|
committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2017-03-17 22:55:13 +0000 |
commit | 4c3cdee2a5047d05201424bf9f80b6664619550d (patch) | |
tree | 29e05bfdeabc3b67f7a99baba973f00b6ce60b27 /include/clang/Basic/MemoryBufferCache.h | |
parent | 594a067a96498e6e2840d3332f79cb03339b7b78 (diff) |
Modules: Cache PCMs in memory and avoid a use-after-free
Clang's internal build system for implicit modules uses lock files to
ensure that after a process writes a PCM it will read the same one back
in (without contention from other -cc1 commands). Since PCMs are read
from disk repeatedly while invalidating, building, and importing, the
lock is not released quickly. Furthermore, the LockFileManager is not
robust in every environment. Other -cc1 commands can stall until
timeout (after about eight minutes).
This commit changes the lock file from being necessary for correctness
to a (possibly dubious) performance hack. The remaining benefit is to
reduce duplicate work in competing -cc1 commands which depend on the
same module. Follow-up commits will change the internal build system to
continue after a timeout, and reduce the timeout. Perhaps we should
reconsider blocking at all.
This also fixes a use-after-free, when one part of a compilation
validates a PCM and starts using it, and another tries to swap out the
PCM for something new.
The PCMCache is a new type called MemoryBufferCache, which saves memory
buffers based on their filename. Its ownership is shared by the
CompilerInstance and ModuleManager.
- The ModuleManager stores PCMs there that it loads from disk, never
touching the disk if the cache is hot.
- When modules fail to validate, they're removed from the cache.
- When a CompilerInstance is spawned to build a new module, each
already-loaded PCM is assumed to be valid, and is frozen to avoid
the use-after-free.
- Any newly-built module is written directly to the cache to avoid the
round-trip to the filesystem, making lock files unnecessary for
correctness.
Original patch by Manman Ren; most testcases by Adrian Prantl!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@298165 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/clang/Basic/MemoryBufferCache.h')
-rw-r--r-- | include/clang/Basic/MemoryBufferCache.h | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/include/clang/Basic/MemoryBufferCache.h b/include/clang/Basic/MemoryBufferCache.h new file mode 100644 index 0000000000..c79c3c40e4 --- /dev/null +++ b/include/clang/Basic/MemoryBufferCache.h @@ -0,0 +1,80 @@ +//===- MemoryBufferCache.h - Cache for loaded memory buffers ----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_BASIC_MEMORYBUFFERCACHE_H +#define LLVM_CLANG_BASIC_MEMORYBUFFERCACHE_H + +#include "llvm/ADT/IntrusiveRefCntPtr.h" +#include "llvm/ADT/StringMap.h" +#include <memory> + +namespace llvm { +class MemoryBuffer; +} // end namespace llvm + +namespace clang { + +/// Manage memory buffers across multiple users. +/// +/// Ensures that multiple users have a consistent view of each buffer. This is +/// used by \a CompilerInstance when building PCMs to ensure that each \a +/// ModuleManager sees the same files. +/// +/// \a finalizeCurrentBuffers() should be called before creating a new user. +/// This locks in the current buffers, ensuring that no buffer that has already +/// been accessed can be purged, preventing use-after-frees. +class MemoryBufferCache : public llvm::RefCountedBase<MemoryBufferCache> { + struct BufferEntry { + std::unique_ptr<llvm::MemoryBuffer> Buffer; + + /// Track the timeline of when this was added to the cache. + unsigned Index; + }; + + /// Cache of buffers. + llvm::StringMap<BufferEntry> Buffers; + + /// Monotonically increasing index. + unsigned NextIndex = 0; + + /// Bumped to prevent "older" buffers from being removed. + unsigned FirstRemovableIndex = 0; + +public: + /// Store the Buffer under the Filename. + /// + /// \pre There is not already buffer is not already in the cache. + /// \return a reference to the buffer as a convenience. + llvm::MemoryBuffer &addBuffer(llvm::StringRef Filename, + std::unique_ptr<llvm::MemoryBuffer> Buffer); + + /// Try to remove a buffer from the cache. + /// + /// \return false on success, iff \c !isBufferFinal(). + bool tryToRemoveBuffer(llvm::StringRef Filename); + + /// Get a pointer to the buffer if it exists; else nullptr. + llvm::MemoryBuffer *lookupBuffer(llvm::StringRef Filename); + + /// Check whether the buffer is final. + /// + /// \return true iff \a finalizeCurrentBuffers() has been called since the + /// buffer was added. This prevents buffers from being removed. + bool isBufferFinal(llvm::StringRef Filename); + + /// Finalize the current buffers in the cache. + /// + /// Should be called when creating a new user to ensure previous uses aren't + /// invalidated. + void finalizeCurrentBuffers(); +}; + +} // end namespace clang + +#endif // LLVM_CLANG_BASIC_MEMORYBUFFERCACHE_H |