[clang] [clang][deps] Don't share real paths from `LocalFS` via the shared cache (PR #241463)

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 19:06:58 PST 2026


https://github.com/jansvoboda11 updated https://github.com/llvm/llvm-project/pull/241463

>From 4b0c4686b93ff41bd30e47962f303ea690d99bf6 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Wed, 11 Nov 2026 11:35:31 -0800
Subject: [PATCH 1/2] [clang][deps] Don't share real paths from LocalFS via the
 shared cache

DependencyScanningWorkerFilesystem::getRealPath() computed uncached real
paths via the overlay of LocalFS and the global FS, but stored the result in
the shared cache. Since LocalFS is specific to the worker, this leaked its
real paths to other workers, and could hide LocalFS real paths behind results
cached by other workers.

Resolve real paths of files that exist in LocalFS through LocalFS and only
cache them locally, the same way getOrCreateFileSystemEntry() does for status
and contents. Compute all other real paths through the global FS.
---
 .../DependencyScanningFilesystem.h            |  8 ++-
 .../DependencyScanningFilesystem.cpp          | 13 +++-
 .../DependencyScanningFilesystemTest.cpp      | 67 +++++++++++++++++++
 3 files changed, 85 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/DependencyScanning/DependencyScanningFilesystem.h b/clang/include/clang/DependencyScanning/DependencyScanningFilesystem.h
index 69a6bb85fd560e..177c4f354a0086 100644
--- a/clang/include/clang/DependencyScanning/DependencyScanningFilesystem.h
+++ b/clang/include/clang/DependencyScanning/DependencyScanningFilesystem.h
@@ -415,8 +415,9 @@ class DependencyScanningWorkerFilesystem
 
   /// Per-filename state cached locally by this worker thread, so repeated
   /// queries can be served without touching the shared cache. The entry and
-  /// real path are arena-owned by the shared cache and outlive this worker, so
-  /// borrowing raw pointers here is safe.
+  /// real path are arena-owned by the shared cache (or by this worker for
+  /// results from \c LocalFS) and outlive the local cache, so borrowing raw
+  /// pointers here is safe.
   struct LocalEntry {
     const CachedFileSystemEntry *File = nullptr;
     const CachedRealPath *RealPath = nullptr;
@@ -444,6 +445,9 @@ class DependencyScanningWorkerFilesystem
   /// The backing storage for cached contents resolved from local FS only.
   llvm::SpecificBumpPtrAllocator<CachedFileContents> LocalContentsStorage;
 
+  /// The backing storage for cached real paths resolved from local FS only.
+  llvm::SpecificBumpPtrAllocator<CachedRealPath> LocalRealPathStorage;
+
   static const CachedFileSystemEntry *createEntry(
       llvm::ErrorOr<TentativeEntry> &Entry,
       llvm::SpecificBumpPtrAllocator<CachedFileContents> &ContentsStorage,
diff --git a/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp b/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp
index 0648867f613bf1..fb1a04402f2230 100644
--- a/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp
+++ b/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp
@@ -488,6 +488,17 @@ DependencyScanningWorkerFilesystem::getRealPath(const Twine &Path,
   if (Local.RealPath)
     return HandleCachedRealPath(*Local.RealPath);
 
+  // Resolve through the local VFS without going through the shared cache.
+  if (LocalFS && LocalFS->exists(OriginalFilename)) {
+    std::error_code EC = LocalFS->getRealPath(OriginalFilename, Output);
+    CachedRealPath RealPath = EC;
+    if (!EC)
+      RealPath = std::string(Output.begin(), Output.end());
+    Local.RealPath = new (LocalRealPathStorage.Allocate())
+        CachedRealPath(std::move(RealPath));
+    return HandleCachedRealPath(*Local.RealPath);
+  }
+
   // If we have the result in the shared cache, cache it locally.
   auto &Shard =
       Service.getSharedCache().getShardForFilename(*FilenameForLookup);
@@ -498,7 +509,7 @@ DependencyScanningWorkerFilesystem::getRealPath(const Twine &Path,
   }
 
   // If we don't know the real path, compute it...
-  std::error_code EC = getUnderlyingFS().getRealPath(OriginalFilename, Output);
+  std::error_code EC = BaseFS->getRealPath(OriginalFilename, Output);
   llvm::ErrorOr<llvm::StringRef> ComputedRealPath = EC;
   if (!EC)
     ComputedRealPath = StringRef{Output.data(), Output.size()};
diff --git a/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp b/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
index 9f1c966eb34d2d..f4ff4ec129dabb 100644
--- a/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
+++ b/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
@@ -610,3 +610,70 @@ TEST(DependencyScanningWorkerFilesystem, LocalFS) {
     EXPECT_EQ(InstrLocalFS->NumStatusCalls, 2u); // not using global cache.
   }
 }
+
+namespace {
+/// Returns real paths prefixed with "/local-real", to tell them apart from real
+/// paths returned by other filesystems.
+class LocalRealPathFileSystem : public llvm::vfs::ProxyFileSystem {
+public:
+  using ProxyFileSystem::ProxyFileSystem;
+
+  std::error_code getRealPath(const llvm::Twine &Path,
+                              llvm::SmallVectorImpl<char> &Output) override {
+    llvm::SmallString<128> RealPath;
+    if (std::error_code EC = ProxyFileSystem::getRealPath(Path, RealPath))
+      return EC;
+    llvm::StringRef Prefix = "/local-real";
+    Output.assign(Prefix.begin(), Prefix.end());
+    Output.append(RealPath.begin(), RealPath.end());
+    return {};
+  }
+};
+} // namespace
+
+TEST(DependencyScanningWorkerFilesystem, LocalFSRealPath) {
+  auto GlobalFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
+  GlobalFS->setCurrentWorkingDirectory("/");
+  GlobalFS->addFile("/both.c", 0, llvm::MemoryBuffer::getMemBuffer(""));
+  GlobalFS->addFile("/glob.c", 0, llvm::MemoryBuffer::getMemBuffer(""));
+
+  auto LocalInMemoryFS =
+      llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
+  LocalInMemoryFS->setCurrentWorkingDirectory("/");
+  LocalInMemoryFS->addFile("/both.c", 0, llvm::MemoryBuffer::getMemBuffer(""));
+  LocalInMemoryFS->addFile("/loc.c", 0, llvm::MemoryBuffer::getMemBuffer(""));
+  auto LocalFS =
+      llvm::makeIntrusiveRefCnt<LocalRealPathFileSystem>(LocalInMemoryFS);
+
+  DependencyScanningServiceOptions Opts;
+  DependencyScanningService Service(std::move(Opts));
+
+  auto GetRealPath = [](DependencyScanningWorkerFilesystem &DepFS,
+                        llvm::StringRef Path) -> std::string {
+    llvm::SmallString<128> Result;
+    if (DepFS.getRealPath(Path, Result))
+      return "<error>";
+    return std::string(Result);
+  };
+
+  // Populate the shared cache for "/glob.c" from a worker without LocalFS.
+  {
+    DependencyScanningWorkerFilesystem DepFS(Service, GlobalFS);
+    EXPECT_EQ(GetRealPath(DepFS, "/glob.c"), "/glob.c");
+  }
+
+  // LocalFS takes precedence over the global FS, like for status().
+  {
+    DependencyScanningWorkerFilesystem DepFS(Service, GlobalFS, LocalFS);
+    EXPECT_EQ(GetRealPath(DepFS, "/loc.c"), "/local-real/loc.c");
+    EXPECT_EQ(GetRealPath(DepFS, "/both.c"), "/local-real/both.c");
+    EXPECT_EQ(GetRealPath(DepFS, "/glob.c"), "/glob.c");
+  }
+
+  // Real paths from LocalFS don't leak to other workers via the shared cache.
+  {
+    DependencyScanningWorkerFilesystem DepFS(Service, GlobalFS);
+    EXPECT_EQ(GetRealPath(DepFS, "/loc.c"), "/loc.c");
+    EXPECT_EQ(GetRealPath(DepFS, "/both.c"), "/both.c");
+  }
+}

>From 52c498ef44a10429391ec0fbbcd11416596a99fe Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Wed, 11 Nov 2026 12:20:35 -0800
Subject: [PATCH 2/2] [clang][deps] Fix LocalFSRealPath test on Windows

InMemoryFileSystem returns real paths with native separators, so normalize
them before comparing.
---
 .../DependencyScanning/DependencyScanningFilesystemTest.cpp   | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp b/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
index f4ff4ec129dabb..0f2292ca262195 100644
--- a/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
+++ b/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
@@ -9,6 +9,7 @@
 #include "clang/DependencyScanning/DependencyScanningFilesystem.h"
 #include "clang/DependencyScanning/DependencyScanningService.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/Support/Path.h"
 #include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Testing/Support/SupportHelpers.h"
 #include "gtest/gtest.h"
@@ -653,7 +654,8 @@ TEST(DependencyScanningWorkerFilesystem, LocalFSRealPath) {
     llvm::SmallString<128> Result;
     if (DepFS.getRealPath(Path, Result))
       return "<error>";
-    return std::string(Result);
+    // InMemoryFileSystem returns real paths with native separators.
+    return llvm::sys::path::convert_to_slash(Result);
   };
 
   // Populate the shared cache for "/glob.c" from a worker without LocalFS.



More information about the cfe-commits mailing list