Skip to content

[clang] Prevent sandbox violations in CrossTranslationUnitContext#175097

Merged
jansvoboda11 merged 3 commits intollvm:mainfrom
jansvoboda11:ctu-sandbox
Jan 12, 2026
Merged

[clang] Prevent sandbox violations in CrossTranslationUnitContext#175097
jansvoboda11 merged 3 commits intollvm:mainfrom
jansvoboda11:ctu-sandbox

Conversation

@jansvoboda11
Copy link
Contributor

@jansvoboda11 jansvoboda11 commented Jan 8, 2026

This uses the VFS to load a file instead of using MemoryBuffer::getBufferForFile() directly to avoid sandbox violation. Sandbox is then disabled for CreateASTUnitFromCommandLine() which invokes the driver which is not expected to be free of sandbox violations.

@github-actions
Copy link

github-actions bot commented Jan 8, 2026

🪟 Windows x64 Test Results

  • 53380 tests passed
  • 2216 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Jan 8, 2026

🐧 Linux x64 Test Results

  • 112578 tests passed
  • 4602 tests skipped

✅ The build succeeded and all tests passed.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jan 9, 2026
@llvmbot
Copy link
Member

llvmbot commented Jan 9, 2026

@llvm/pr-subscribers-clang

Author: Jan Svoboda (jansvoboda11)

Changes

This PR propagates the VFS where necessary from CrossTranslationUnitContext::ASTLoader to avoid sandbox violations.


Full diff: https://github.com/llvm/llvm-project/pull/175097.diff

1 Files Affected:

  • (modified) clang/lib/CrossTU/CrossTranslationUnit.cpp (+4-1)
diff --git a/clang/lib/CrossTU/CrossTranslationUnit.cpp b/clang/lib/CrossTU/CrossTranslationUnit.cpp
index a3fc2cf6bfb3c..7496deb74cb67 100644
--- a/clang/lib/CrossTU/CrossTranslationUnit.cpp
+++ b/clang/lib/CrossTU/CrossTranslationUnit.cpp
@@ -24,6 +24,7 @@
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Option/ArgList.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/IOSandbox.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/YAMLParser.h"
@@ -620,6 +621,8 @@ CrossTranslationUnitContext::ASTLoader::loadFromSource(
   auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(DiagID, *DiagOpts,
                                                             DiagClient);
 
+  // This runs the driver which isn't expected to be free of sandbox violations.
+  auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
   return CreateASTUnitFromCommandLine(
       CommandLineArgs.begin(), (CommandLineArgs.end()),
       CI.getPCHContainerOperations(), DiagOpts, Diags,
@@ -710,7 +713,7 @@ llvm::Error CrossTranslationUnitContext::ASTLoader::lazyInitInvocationList() {
     return llvm::make_error<IndexError>(PreviousParsingResult);
 
   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileContent =
-      llvm::MemoryBuffer::getFile(InvocationListFilePath);
+      CI.getVirtualFileSystem().getBufferForFile(InvocationListFilePath);
   if (!FileContent) {
     PreviousParsingResult = index_error_code::invocation_list_file_not_found;
     return llvm::make_error<IndexError>(PreviousParsingResult);

@jansvoboda11 jansvoboda11 changed the title [clang] Use the VFS in CrossTranslationUnitContext [clang] Prevent sandbox violations in CrossTranslationUnitContext Jan 9, 2026
@jansvoboda11
Copy link
Contributor Author

This fixes these tests with sandboxing enabled:

Clang.Analysis/ctu-on-demand-parsing.c
Clang.Analysis/ctu-on-demand-parsing.cpp

DiagClient);

// This runs the driver which isn't expected to be free of sandbox violations.
auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change here LGTM, but do you think this deserves a FIXME? I'm not clear whether this is okay or not long term. From a VFS usage perspective calling back to the driver may or may not be okay if you don't pass in a VFS. From a caching perspective it would need special handling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. We already have a sandbox disablement due to the driver here:

// The driver is not expected to be free of sandbox violations.
auto BypassSandbox = llvm::sys::sandbox::scopedDisable();

and that doesn't have a FIXME. I'm not sure how actionable these FIXMEs would be, since the driver performs lots of FS operations and some don't have an equivalent on the VFS layer (access() comes to mind). Currently there'd be little upside in resolving the FIXMEs (even though it makes sense conceptually).

I don't have a strong opinion, but if we decide to go forward with the FIXMEs, wouldn't it make sense to put them on the driver and have the sandbox disablement somewhere in the clangDriver library?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really concerned about the driver itself, that is when it is parsing arguments for and then coordinating the frontend execution, it's the possibility of the driver being called back by the frontend specifically that seems (potentially) problematic if it allows the frontend to indirectly depend on unsandboxed FS access. Probably this is more of a caching issue than a general VFS issue, and it would need special handling to cache correctly anyway, so maybe it's fine to ignore it for now?

@jansvoboda11 jansvoboda11 merged commit a698f7e into llvm:main Jan 12, 2026
11 checks passed
@jansvoboda11 jansvoboda11 deleted the ctu-sandbox branch January 12, 2026 16:12
Priyanshu3820 pushed a commit to Priyanshu3820/llvm-project that referenced this pull request Jan 18, 2026
…lvm#175097)

This uses the VFS to load a file instead of using
`MemoryBuffer::getBufferForFile()` directly to avoid sandbox violation.
Sandbox is then disabled for `CreateASTUnitFromCommandLine()` which
invokes the driver which is not expected to be free of sandbox
violations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants