Skip to content

Commit 3be8250

Browse files
authored
[clang-repl] Fixing vulnerabilities with respect to orc runtime (llvm#165852)
Fixed `getORCRuntimePath` function to respect `getCompilerRTPath` and `getRuntimePath`
1 parent 5af27f8 commit 3be8250

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -394,36 +394,48 @@ Interpreter::outOfProcessJITBuilder(JITConfig Config) {
394394

395395
llvm::Expected<std::string>
396396
Interpreter::getOrcRuntimePath(const driver::ToolChain &TC) {
397-
std::optional<std::string> CompilerRTPath = TC.getCompilerRTPath();
398-
std::optional<std::string> ResourceDir = TC.getRuntimePath();
397+
const std::array<const char *, 3> OrcRTLibNames = {
398+
"liborc_rt.a", "liborc_rt_osx.a", "liborc_rt-x86_64.a"};
399+
400+
auto findInDir = [&](llvm::StringRef Base) -> std::optional<std::string> {
401+
for (const char *LibName : OrcRTLibNames) {
402+
llvm::SmallString<256> CandidatePath(Base);
403+
llvm::sys::path::append(CandidatePath, LibName);
404+
if (llvm::sys::fs::exists(CandidatePath))
405+
return std::string(CandidatePath.str());
406+
}
407+
return std::nullopt;
408+
};
409+
410+
std::string SearchedPaths;
399411

400-
if (!CompilerRTPath) {
412+
if (std::optional<std::string> CompilerRTPath = TC.getCompilerRTPath()) {
413+
if (auto Found = findInDir(*CompilerRTPath))
414+
return *Found;
415+
SearchedPaths += *CompilerRTPath;
416+
} else {
401417
return llvm::make_error<llvm::StringError>("CompilerRT path not found",
402418
std::error_code());
403419
}
404420

405-
const std::array<const char *, 3> OrcRTLibNames = {
406-
"liborc_rt.a", "liborc_rt_osx.a", "liborc_rt-x86_64.a"};
407-
408-
for (const char *LibName : OrcRTLibNames) {
409-
llvm::SmallString<256> CandidatePath((*CompilerRTPath).c_str());
410-
llvm::sys::path::append(CandidatePath, LibName);
411-
412-
if (llvm::sys::fs::exists(CandidatePath)) {
413-
return CandidatePath.str().str();
414-
}
421+
if (std::optional<std::string> ResourceDir = TC.getRuntimePath()) {
422+
if (auto Found = findInDir(*ResourceDir))
423+
return *Found;
424+
if (!SearchedPaths.empty())
425+
SearchedPaths += "; ";
426+
SearchedPaths += *ResourceDir;
427+
} else {
428+
return llvm::make_error<llvm::StringError>("ResourceDir path not found",
429+
std::error_code());
415430
}
416431

417432
return llvm::make_error<llvm::StringError>(
418-
llvm::Twine("OrcRuntime library not found in: ") + (*CompilerRTPath),
433+
llvm::Twine("OrcRuntime library not found in: ") + SearchedPaths,
419434
std::error_code());
420435
}
421436

422437
llvm::Expected<std::unique_ptr<Interpreter>>
423438
Interpreter::create(std::unique_ptr<CompilerInstance> CI, JITConfig Config) {
424-
llvm::Error Err = llvm::Error::success();
425-
426-
std::unique_ptr<llvm::orc::LLJITBuilder> JB;
427439

428440
if (Config.IsOutOfProcess) {
429441
const TargetInfo &TI = CI->getTarget();
@@ -453,6 +465,9 @@ Interpreter::create(std::unique_ptr<CompilerInstance> CI, JITConfig Config) {
453465
}
454466
}
455467

468+
llvm::Error Err = llvm::Error::success();
469+
std::unique_ptr<llvm::orc::LLJITBuilder> JB;
470+
456471
auto Interp = std::unique_ptr<Interpreter>(new Interpreter(
457472
std::move(CI), Err, std::move(JB), /*Consumer=*/nullptr, Config));
458473
if (auto E = std::move(Err))

clang/tools/clang-repl/ClangRepl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ int main(int argc, const char **argv) {
309309
clang::Interpreter::JITConfig Config;
310310
Config.IsOutOfProcess = !OOPExecutor.empty() || !OOPExecutorConnect.empty();
311311
Config.OOPExecutor = OOPExecutor;
312+
Config.OrcRuntimePath = OrcRuntimePath;
312313
auto SizeOrErr = getSlabAllocSize(SlabAllocateSizeString);
313314
if (!SizeOrErr) {
314315
llvm::logAllUnhandledErrors(SizeOrErr.takeError(), llvm::errs(), "error: ");

0 commit comments

Comments
 (0)