summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2020-01-29 12:22:03 +0100
committerHans Wennborg <hans@chromium.org>2020-01-29 22:09:11 +0100
commitb28b38a59d3d287afb16e63c883e62876471ea2e (patch)
tree751e9b28aaa54e121113f1b2c60b362e572b5603
parentb0536b58c1b40ac946bb653d13a63e4372c2ca17 (diff)
downloadbcm5719-llvm-b28b38a59d3d287afb16e63c883e62876471ea2e.tar.gz
bcm5719-llvm-b28b38a59d3d287afb16e63c883e62876471ea2e.zip
[clangd] Don't mmap source files on all platforms --> don't crash on git checkout
Summary: Previously we mmapped on unix and not on windows: on windows mmap takes an exclusive lock on the file and prevents the user saving changes! The failure mode on linux is a bit more subtle: if the file is changed on disk but the SourceManager sticks around, then subsequent operations on the SourceManager will fail as invariants are violated (e.g. null-termination). This commonly manifests as crashes after switching git branches with many files open in clangd. Nominally mmap is for performance here, and we should be willing to give some up to stop crashing. Measurements on my system (linux+desktop+SSD) at least show no measurable regression on an a fairly IO-heavy workload: drop disk caches, open SemaOverload.cpp, wait for first diagnostics. for i in `seq 100`; do for variant in mmap volatile; do echo 3 | sudo tee /proc/sys/vm/drop_caches /usr/bin/time --append --quiet -o ~/timings -f "%C %E" \ bin/clangd.$variant -sync -background-index=0 < /tmp/mirror > /dev/null done done bin/clangd.mmap -sync -background-index=0 0:07.60 bin/clangd.volatile -sync -background-index=0 0:07.89 bin/clangd.mmap -sync -background-index=0 0:07.44 bin/clangd.volatile -sync -background-index=0 0:07.89 bin/clangd.mmap -sync -background-index=0 0:07.42 bin/clangd.volatile -sync -background-index=0 0:07.50 bin/clangd.mmap -sync -background-index=0 0:07.90 bin/clangd.volatile -sync -background-index=0 0:07.53 bin/clangd.mmap -sync -background-index=0 0:07.64 bin/clangd.volatile -sync -background-index=0 0:07.55 bin/clangd.mmap -sync -background-index=0 0:07.75 bin/clangd.volatile -sync -background-index=0 0:07.47 bin/clangd.mmap -sync -background-index=0 0:07.90 bin/clangd.volatile -sync -background-index=0 0:07.50 bin/clangd.mmap -sync -background-index=0 0:07.81 bin/clangd.volatile -sync -background-index=0 0:07.95 bin/clangd.mmap -sync -background-index=0 0:07.55 bin/clangd.volatile -sync -background-index=0 0:07.65 bin/clangd.mmap -sync -background-index=0 0:08.15 bin/clangd.volatile -sync -background-index=0 0:07.54 bin/clangd.mmap -sync -background-index=0 0:07.78 bin/clangd.volatile -sync -background-index=0 0:07.61 bin/clangd.mmap -sync -background-index=0 0:07.78 bin/clangd.volatile -sync -background-index=0 0:07.55 bin/clangd.mmap -sync -background-index=0 0:07.41 bin/clangd.volatile -sync -background-index=0 0:07.40 bin/clangd.mmap -sync -background-index=0 0:07.54 bin/clangd.volatile -sync -background-index=0 0:07.42 bin/clangd.mmap -sync -background-index=0 0:07.45 bin/clangd.volatile -sync -background-index=0 0:07.49 bin/clangd.mmap -sync -background-index=0 0:07.95 bin/clangd.volatile -sync -background-index=0 0:07.66 bin/clangd.mmap -sync -background-index=0 0:08.04 Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D73617 (cherry picked from commit b500c49cd4f81f067cda721049cb1fd72a5e7bf5)
-rw-r--r--clang-tools-extra/clangd/FSProvider.cpp17
-rw-r--r--clang-tools-extra/clangd/FSProvider.h1
2 files changed, 8 insertions, 10 deletions
diff --git a/clang-tools-extra/clangd/FSProvider.cpp b/clang-tools-extra/clangd/FSProvider.cpp
index 5d1434bf6f0..80d6be005cc 100644
--- a/clang-tools-extra/clangd/FSProvider.cpp
+++ b/clang-tools-extra/clangd/FSProvider.cpp
@@ -19,7 +19,10 @@ namespace clangd {
namespace {
/// Always opens files in the underlying filesystem as "volatile", meaning they
-/// won't be memory-mapped. This avoid locking the files on Windows.
+/// won't be memory-mapped. Memory-mapping isn't desirable for clangd:
+/// - edits to the underlying files change contents MemoryBuffers owned by
+// SourceManager, breaking its invariants and leading to crashes
+/// - it locks files on windows, preventing edits
class VolatileFileSystem : public llvm::vfs::ProxyFileSystem {
public:
explicit VolatileFileSystem(llvm::IntrusiveRefCntPtr<FileSystem> FS)
@@ -34,7 +37,7 @@ public:
if (!File)
return File;
// Try to guess preamble files, they can be memory-mapped even on Windows as
- // clangd has exclusive access to those.
+ // clangd has exclusive access to those and nothing else should touch them.
llvm::StringRef FileName = llvm::sys::path::filename(Path);
if (FileName.startswith("preamble-") && FileName.endswith(".pch"))
return File;
@@ -70,15 +73,11 @@ private:
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
clang::clangd::RealFileSystemProvider::getFileSystem() const {
-// Avoid using memory-mapped files on Windows, they cause file locking issues.
-// FIXME: Try to use a similar approach in Sema instead of relying on
-// propagation of the 'isVolatile' flag through all layers.
-#ifdef _WIN32
+ // Avoid using memory-mapped files.
+ // FIXME: Try to use a similar approach in Sema instead of relying on
+ // propagation of the 'isVolatile' flag through all layers.
return new VolatileFileSystem(
llvm::vfs::createPhysicalFileSystem().release());
-#else
- return llvm::vfs::createPhysicalFileSystem().release();
-#endif
}
} // namespace clangd
} // namespace clang
diff --git a/clang-tools-extra/clangd/FSProvider.h b/clang-tools-extra/clangd/FSProvider.h
index 22957892506..1fe3ba864a3 100644
--- a/clang-tools-extra/clangd/FSProvider.h
+++ b/clang-tools-extra/clangd/FSProvider.h
@@ -30,7 +30,6 @@ public:
class RealFileSystemProvider : public FileSystemProvider {
public:
- // FIXME: returns the single real FS instance, which is not threadsafe.
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
getFileSystem() const override;
};
OpenPOWER on IntegriCloud