Skip to content

[clang] Allow enabling sandbox for direct -cc1 invocations#174653

Merged
jansvoboda11 merged 3 commits intollvm:mainfrom
jansvoboda11:cc1-sandbox
Jan 7, 2026
Merged

[clang] Allow enabling sandbox for direct -cc1 invocations#174653
jansvoboda11 merged 3 commits intollvm:mainfrom
jansvoboda11:cc1-sandbox

Conversation

@jansvoboda11
Copy link
Contributor

This PR enables the FS sandbox for direct clang -cc1 invocations. #165350 unintentionally implemented the sandbox only for the code path where clang -cc1 gets invoked after being expanded from a driver command line, which reduced the expected test coverage.

@github-actions
Copy link

github-actions bot commented Jan 7, 2026

🪟 Windows x64 Test Results

  • 53354 tests passed
  • 2217 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Jan 7, 2026

🐧 Linux x64 Test Results

  • 112555 tests passed
  • 4598 tests skipped

✅ The build succeeded and all tests passed.

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

llvmbot commented Jan 7, 2026

@llvm/pr-subscribers-clang

Author: Jan Svoboda (jansvoboda11)

Changes

This PR enables the FS sandbox for direct clang -cc1 invocations. #165350 unintentionally implemented the sandbox only for the code path where clang -cc1 gets invoked after being expanded from a driver command line, which reduced the expected test coverage.


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

2 Files Affected:

  • (modified) clang/tools/driver/cc1gen_reproducer_main.cpp (+3)
  • (modified) clang/tools/driver/driver.cpp (+8-1)
diff --git a/clang/tools/driver/cc1gen_reproducer_main.cpp b/clang/tools/driver/cc1gen_reproducer_main.cpp
index 14548c39975da..851d252015c44 100644
--- a/clang/tools/driver/cc1gen_reproducer_main.cpp
+++ b/clang/tools/driver/cc1gen_reproducer_main.cpp
@@ -116,6 +116,9 @@ generateReproducerForInvocationArguments(
     ArrayRef<const char *> Argv, const ClangInvocationInfo &Info,
     const llvm::ToolContext &ToolContext,
     IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
+  // The driver is not expected to be free of sandbox violations.
+  auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
+
   using namespace driver;
   auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(Argv[0]);
 
diff --git a/clang/tools/driver/driver.cpp b/clang/tools/driver/driver.cpp
index 1e2c9884ba63d..490136961ebc6 100644
--- a/clang/tools/driver/driver.cpp
+++ b/clang/tools/driver/driver.cpp
@@ -38,6 +38,7 @@
 #include "llvm/Support/CrashRecoveryContext.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/IOSandbox.h"
 #include "llvm/Support/LLVMDriver.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/PrettyStackTrace.h"
@@ -264,8 +265,14 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
   }
 
   // Handle -cc1 integrated tools.
-  if (Args.size() >= 2 && StringRef(Args[1]).starts_with("-cc1"))
+  if (Args.size() >= 2 && StringRef(Args[1]).starts_with("-cc1")) {
+    // Note that this only enables the sandbox for direct -cc1 invocations and
+    // out-of-process -cc1 invocations launched by the driver. For in-process
+    // -cc1 invocations launched by the driver, the sandbox is enabled in
+    // CC1Command::Execute() for better crash recovery.
+    auto EnableSandbox = llvm::sys::sandbox::scopedEnable();
     return ExecuteCC1Tool(Args, ToolContext, VFS);
+  }
 
   // Handle options that need handling before the real command line parsing in
   // Driver::BuildCompilation()

Copy link
Collaborator

@benlangmuir benlangmuir left a comment

Choose a reason for hiding this comment

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

Thanks

@jansvoboda11 jansvoboda11 merged commit 3911c6a into llvm:main Jan 7, 2026
11 checks passed
@jansvoboda11 jansvoboda11 deleted the cc1-sandbox branch January 7, 2026 23:07
kshitijvp pushed a commit to kshitijvp/llvm-project that referenced this pull request Jan 9, 2026
…4653)

This PR enables the FS sandbox for direct `clang -cc1` invocations.
llvm#165350 unintentionally
implemented the sandbox only for the code path where `clang -cc1` gets
invoked after being expanded from a driver command line, which reduced
the expected test coverage.
@adams381
Copy link
Contributor

Hi @jan-svo,

Thank you for the work on enabling the IO sandbox for direct clang -cc1 invocations. We're working on the ClangIR (CIR) project and discovered that several of our tests are incompatible with LLVM_ENABLE_IO_SANDBOX=ON.

What We're Seeing

15 CIR tests fail with fatal error: error in backend: IO sandbox violation when the sandbox is enabled.

Example test pattern:

clang -cc1 -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare \
  test.c -o test.cir 2> before-lp.cir

Error:

fatal error: error in backend: IO sandbox violation
Exit Code: 70

Why We're Using This Pattern

These tests use MLIR's --mlir-print-ir-before=<pass> flag to capture IR at different compilation stages, allowing us to verify that transformations are working correctly. The output goes to stderr, which is redirected to a file for FileCheck verification.

Affected Tests

All 16 CIR tests using --mlir-print-ir-before:

  • clang/test/CIR/CodeGen/array-dtor.cpp
  • clang/test/CIR/CodeGen/array-ctor.cpp
  • clang/test/CIR/CodeGen/dynamic-cast.cpp
  • clang/test/CIR/CodeGen/complex-cast.cpp
  • clang/test/CIR/CodeGen/complex-mul-div.cpp
  • clang/test/CIR/CodeGen/complex-unary.cpp
  • clang/test/CIR/CodeGen/binassign.c
  • clang/test/CIR/CodeGen/global-ctor-dtor.cpp
  • clang/test/CIR/CodeGen/global-init.cpp
  • clang/test/CIR/CodeGen/switch_flat_op.cpp
  • clang/test/CIR/CodeGen/global-array-dtor.cpp
  • clang/test/CIR/CodeGen/pointer-to-member-func.cpp
  • clang/test/CIR/CodeGen/pointer-to-data-member-cast.cpp
  • clang/test/CIR/CodeGen/global-constant-storage.cpp
  • clang/test/CIR/CodeGen/pointer-to-data-member.cpp
  • clang/test/CIR/CodeGen/pointer-to-data-member-cmp.cpp

Requesting Guidance

We agree that tests should be sandbox-clean. Could you advise on the best approach to fix this?

Option 1: Should MLIR debug output flags (--mlir-print-ir-before, --mlir-print-ir-after) bypass the sandbox for stderr writes, similar to the bypasses added in #175097 and #175220?

Option 2: Should we restructure these tests to avoid stderr redirection? If so, is there a recommended pattern for verifying IR at intermediate compilation stages?

Option 3: Is there another approach we should consider?

Environment Details

  • Default behavior: LLVM_ENABLE_IO_SANDBOX=OFF (tests pass)
  • With sandbox enabled: LLVM_ENABLE_IO_SANDBOX=ON (tests fail)
  • Commit: 3911c6a2535736a365dd28163dd18daaa4398f74

We want to ensure CIR tests work correctly with the sandbox enabled. Any guidance would be appreciated!

Priyanshu3820 pushed a commit to Priyanshu3820/llvm-project that referenced this pull request Jan 18, 2026
…4653)

This PR enables the FS sandbox for direct `clang -cc1` invocations.
llvm#165350 unintentionally
implemented the sandbox only for the code path where `clang -cc1` gets
invoked after being expanded from a driver command line, which reduced
the expected test coverage.
tahonermann added a commit to tahonermann/intel-llvm that referenced this pull request Jan 27, 2026
The LLVM sandbox was enabled by default for direct clang -cc1 invocations
by llvm/llvm-project#174653. Subsequent build
failures building the sycl_web branch of the intel/llvm repository lead
to the default enablement being reverted in merge commit
63ea3d6 to be investigated later.
Subsequent investigation has not succeeded in reproducing the reported
build failures. This change reenables the previous default behavior.

Jira: CMPLRLLVM-72683
offsake pushed a commit to intel/llvm that referenced this pull request Jan 29, 2026
…s. (#21146)

The LLVM sandbox was enabled by default for direct clang -cc1
invocations by llvm/llvm-project#174653.
Subsequent build failures building the sycl_web branch of the intel/llvm
repository lead to the default enablement being reverted in merge commit
63ea3d6 to be investigated later.
Subsequent investigation has not succeeded in reproducing the reported
build failures. This change reenables the previous default behavior.

Jira: CMPLRLLVM-72683
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.

4 participants