Skip to content

Commit 3911c6a

Browse files
authored
[clang] Allow enabling sandbox for direct -cc1 invocations (llvm#174653)
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.
1 parent 539cf92 commit 3911c6a

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

clang/tools/driver/cc1gen_reproducer_main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ generateReproducerForInvocationArguments(
116116
ArrayRef<const char *> Argv, const ClangInvocationInfo &Info,
117117
const llvm::ToolContext &ToolContext,
118118
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
119+
// The driver is not expected to be free of sandbox violations.
120+
auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
121+
119122
using namespace driver;
120123
auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(Argv[0]);
121124

clang/tools/driver/driver.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "llvm/Support/CrashRecoveryContext.h"
3939
#include "llvm/Support/ErrorHandling.h"
4040
#include "llvm/Support/FileSystem.h"
41+
#include "llvm/Support/IOSandbox.h"
4142
#include "llvm/Support/LLVMDriver.h"
4243
#include "llvm/Support/Path.h"
4344
#include "llvm/Support/PrettyStackTrace.h"
@@ -264,8 +265,14 @@ int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
264265
}
265266

266267
// Handle -cc1 integrated tools.
267-
if (Args.size() >= 2 && StringRef(Args[1]).starts_with("-cc1"))
268+
if (Args.size() >= 2 && StringRef(Args[1]).starts_with("-cc1")) {
269+
// Note that this only enables the sandbox for direct -cc1 invocations and
270+
// out-of-process -cc1 invocations launched by the driver. For in-process
271+
// -cc1 invocations launched by the driver, the sandbox is enabled in
272+
// CC1Command::Execute() for better crash recovery.
273+
auto EnableSandbox = llvm::sys::sandbox::scopedEnable();
268274
return ExecuteCC1Tool(Args, ToolContext, VFS);
275+
}
269276

270277
// Handle options that need handling before the real command line parsing in
271278
// Driver::BuildCompilation()

0 commit comments

Comments
 (0)