Skip to content

Commit 0a1d473

Browse files
committed
Rename, re-layer, add build setting
1 parent bd875f1 commit 0a1d473

File tree

18 files changed

+121
-113
lines changed

18 files changed

+121
-113
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ void clang::emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts,
14361436
std::unique_ptr<llvm::Module> EmptyModule;
14371437
if (!CGOpts.ThinLTOIndexFile.empty()) {
14381438
// FIXME(sandboxing): Figure out how to support distributed indexing.
1439-
auto BypassSandbox = sys::sandbox_scoped_disable();
1439+
auto BypassSandbox = sys::sandbox::scopedDisable();
14401440
// If we are performing a ThinLTO importing compile, load the function index
14411441
// into memory and pass it into runThinLTOBackend, which will run the
14421442
// function importer and invoke LTO passes.

clang/lib/Driver/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ bool Driver::getCrashDiagnosticFile(StringRef ReproCrashFilename,
18721872
assert(llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin() &&
18731873
"Only knows about .crash files on Darwin");
18741874
// This is not a formal output of the compiler, let's bypass the sandbox.
1875-
auto BypassSandbox = sandbox_scoped_disable();
1875+
auto BypassSandbox = sandbox::scopedDisable();
18761876

18771877
// The .crash file can be found on at ~/Library/Logs/DiagnosticReports/
18781878
// (or /Library/Logs/DiagnosticReports for root) and has the filename pattern

clang/lib/Driver/Job.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ int CC1Command::Execute(ArrayRef<std::optional<StringRef>> Redirects,
429429

430430
// Enabling the sandbox here allows us to restore its previous state even when
431431
// this cc1 invocation crashes.
432-
auto EnableSandbox = llvm::sys::sandbox_scoped_enable();
432+
auto EnableSandbox = llvm::sys::sandbox::scopedEnable();
433433

434434
llvm::CrashRecoveryContext CRC;
435435
CRC.DumpStackAndCleanupOnFailure = true;

clang/lib/Serialization/GlobalModuleIndex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ GlobalModuleIndex::~GlobalModuleIndex() {
252252
std::pair<GlobalModuleIndex *, llvm::Error>
253253
GlobalModuleIndex::readIndex(StringRef Path) {
254254
// This is a compiler-internal input/output, let's bypass the sandbox.
255-
auto BypassSandbox = llvm::sys::sandbox_scoped_disable();
255+
auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
256256

257257
// Load the index file, if it's there.
258258
llvm::SmallString<128> IndexPath;
@@ -848,7 +848,7 @@ GlobalModuleIndex::writeIndex(FileManager &FileMgr,
848848
const PCHContainerReader &PCHContainerRdr,
849849
StringRef Path) {
850850
// This is a compiler-internal input/output, let's bypass the sandbox.
851-
auto BypassSandbox = llvm::sys::sandbox_scoped_disable();
851+
auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
852852

853853
llvm::SmallString<128> IndexPath;
854854
IndexPath += Path;

clang/lib/Serialization/ModuleCache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void clang::maybePruneImpl(StringRef Path, time_t PruneInterval,
2929
return;
3030

3131
// This is a compiler-internal input/output, let's bypass the sandbox.
32-
auto BypassSandbox = llvm::sys::sandbox_scoped_disable();
32+
auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
3333

3434
llvm::SmallString<128> TimestampFile(Path);
3535
llvm::sys::path::append(TimestampFile, "modules.timestamp");
@@ -120,7 +120,7 @@ class CrossProcessModuleCache : public ModuleCache {
120120

121121
std::time_t getModuleTimestamp(StringRef ModuleFilename) override {
122122
// This is a compiler-internal input/output, let's bypass the sandbox.
123-
auto SandboxBypass = llvm::sys::sandbox_scoped_disable();
123+
auto SandboxBypass = llvm::sys::sandbox::scopedDisable();
124124
std::string TimestampFilename =
125125
serialization::ModuleFile::getTimestampFilename(ModuleFilename);
126126
llvm::sys::fs::file_status Status;

clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ void HTMLDiagnostics::FlushDiagnosticsImpl(
259259
void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
260260
FilesMade *filesMade) {
261261
// FIXME(sandboxing): Remove this by adopting `llvm::vfs::OutputBackend`.
262-
auto SandboxBypass = llvm::sys::sandbox_scoped_disable();
262+
auto SandboxBypass = llvm::sys::sandbox::scopedDisable();
263263

264264
// Create the HTML directory if it is missing.
265265
if (!createdDir) {

clang/tools/driver/cc1_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
274274

275275
/// Create the actual file system.
276276
auto VFS = [] {
277-
auto BypassSandbox = llvm::sys::sandbox_scoped_disable();
277+
auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
278278
return llvm::vfs::getRealFileSystem();
279279
}();
280280
Clang->createVirtualFileSystem(std::move(VFS), DiagsBuffer);
@@ -309,7 +309,7 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
309309
// results now. This happens in -disable-free mode.
310310
{
311311
// This isn't a formal input or output of the compiler.
312-
auto BypassSandbox = llvm::sys::sandbox_scoped_disable();
312+
auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
313313
std::unique_ptr<raw_ostream> IOFile = llvm::CreateInfoOutputFile();
314314
if (Clang->getCodeGenOpts().TimePassesJson) {
315315
*IOFile << "{\n";

clang/tools/driver/cc1as_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
428428

429429
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = [=] {
430430
// FIXME(sandboxing): Make this a proper input file.
431-
auto BypassSandbox = sys::sandbox_scoped_disable();
431+
auto BypassSandbox = sys::sandbox::scopedDisable();
432432
return MemoryBuffer::getFileOrSTDIN(Opts.InputFile, /*IsText=*/true);
433433
}();
434434

@@ -677,7 +677,7 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
677677
DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DiagClient);
678678

679679
auto VFS = [] {
680-
auto BypassSandbox = sys::sandbox_scoped_disable();
680+
auto BypassSandbox = sys::sandbox::scopedDisable();
681681
return vfs::getRealFileSystem();
682682
}();
683683

llvm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ option(LLVM_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
697697

698698
option(LLVM_ENABLE_DUMP "Enable dump functions even when assertions are disabled" OFF)
699699
option(LLVM_UNREACHABLE_OPTIMIZE "Optimize llvm_unreachable() as undefined behavior (default), guaranteed trap when OFF" ON)
700+
option(LLVM_ENABLE_IO_SANDBOX "Enable IO sandboxing in supported tools" OFF)
700701

701702
if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
702703
option(LLVM_ENABLE_ASSERTIONS "Enable assertions" OFF)

llvm/include/llvm/Support/IOSandbox.h

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,74 @@
99
#ifndef LLVM_SUPPORT_IOSANDBOX_H
1010
#define LLVM_SUPPORT_IOSANDBOX_H
1111

12+
// Always enable IO sandboxing in debug/assert builds for development,
13+
// but allow enablement even for release/no-assert builds for production.
14+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_IO_SANDBOX)
15+
16+
#include "llvm/Support/ErrorHandling.h"
1217
#include "llvm/Support/SaveAndRestore.h"
1318

14-
namespace llvm::sys {
15-
SaveAndRestore<bool> sandbox_scoped_enable();
16-
SaveAndRestore<bool> sandbox_scoped_disable();
17-
void sandbox_violation_if_enabled();
18-
} // namespace llvm::sys
19+
namespace llvm::sys::sandbox {
20+
inline thread_local bool Enabled = false;
21+
inline SaveAndRestore<bool> scopedEnable() { return {Enabled, true}; }
22+
inline SaveAndRestore<bool> scopedDisable() { return {Enabled, false}; }
23+
inline void violationIfEnabled() {
24+
if (Enabled)
25+
reportFatalInternalError("IO sandbox violation");
26+
}
27+
} // namespace llvm::sys::sandbox
28+
29+
#else
30+
31+
namespace llvm::sys::sandbox {
32+
inline int scopedEnable() {}
33+
inline int scopedDisable() {}
34+
inline void violationIfEnabled() {}
35+
} // namespace llvm::sys::sandbox
36+
37+
#endif
38+
39+
namespace llvm::sys::sandbox {
40+
/// Facility for seamlessly interposing function calls and sandbox enforcement.
41+
/// This is intended for creating static functors like so:
42+
///
43+
/// // before
44+
/// #include <unistd.h>
45+
/// namespace x {
46+
/// void perform_read() { read(); } // not sandboxed
47+
/// }
48+
///
49+
/// // after
50+
/// #include <unistd.h>
51+
/// namespace x {
52+
/// static constexpr auto read = llvm::sys::sandbox::interpose(::read);
53+
/// void perform_read() { read(); } // sandboxed
54+
/// }
55+
template <class FnTy> struct Interposed;
56+
57+
template <class RetTy, class... ArgTy> struct Interposed<RetTy (*)(ArgTy...)> {
58+
RetTy (*Fn)(ArgTy...);
59+
60+
RetTy operator()(ArgTy... Arg) const {
61+
violationIfEnabled();
62+
return Fn(std::forward<ArgTy>(Arg)...);
63+
}
64+
};
65+
66+
template <class RetTy, class... ArgTy>
67+
struct Interposed<RetTy (*)(ArgTy..., ...)> {
68+
RetTy (*Fn)(ArgTy..., ...);
69+
70+
template <class... CVarArgTy>
71+
RetTy operator()(ArgTy... Arg, CVarArgTy... CVarArg) const {
72+
violationIfEnabled();
73+
return Fn(std::forward<ArgTy>(Arg)..., std::forward<CVarArgTy>(CVarArg)...);
74+
}
75+
};
76+
77+
template <class FnTy> constexpr auto interpose(FnTy Fn) {
78+
return Interposed<FnTy>{Fn};
79+
}
80+
} // namespace llvm::sys::sandbox
1981

2082
#endif

0 commit comments

Comments
 (0)