[clang] Allow enabling sandbox for direct -cc1 invocations#174653
[clang] Allow enabling sandbox for direct -cc1 invocations#174653jansvoboda11 merged 3 commits intollvm:mainfrom
-cc1 invocations#174653Conversation
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
|
@llvm/pr-subscribers-clang Author: Jan Svoboda (jansvoboda11) ChangesThis PR enables the FS sandbox for direct Full diff: https://github.com/llvm/llvm-project/pull/174653.diff 2 Files Affected:
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()
|
…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.
|
Hi @jan-svo, Thank you for the work on enabling the IO sandbox for direct What We're Seeing15 CIR tests fail with Example test pattern: clang -cc1 -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare \
test.c -o test.cir 2> before-lp.cirError: Why We're Using This PatternThese tests use MLIR's Affected TestsAll 16 CIR tests using
Requesting GuidanceWe agree that tests should be sandbox-clean. Could you advise on the best approach to fix this? Option 1: Should MLIR debug output flags ( 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
We want to ensure CIR tests work correctly with the sandbox enabled. Any guidance would be appreciated! |
…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.
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
…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
This PR enables the FS sandbox for direct
clang -cc1invocations. #165350 unintentionally implemented the sandbox only for the code path whereclang -cc1gets invoked after being expanded from a driver command line, which reduced the expected test coverage.