Author: Jan Svoboda
Date: 2026-11-12T03:59:27Z
New Revision: 2934809c8aeea143924dd7e052367ab97df8912e

URL: 
https://github.com/llvm/llvm-project/commit/2934809c8aeea143924dd7e052367ab97df8912e
DIFF: 
https://github.com/llvm/llvm-project/commit/2934809c8aeea143924dd7e052367ab97df8912e.diff

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

`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.

Added: 
    

Modified: 
    clang/include/clang/DependencyScanning/DependencyScanningFilesystem.h
    clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp
    clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp

Removed: 
    


################################################################################
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..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"
@@ -610,3 +611,71 @@ 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>";
+    // 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.
+  {
+    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");
+  }
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to