Skip to content

Commit 585384a

Browse files
committed
Avoid calling ParseCommandLineOptions in BackendUtil if possible
Calling `ParseCommandLineOptions` should only be called from `main` as the CommandLine setup code isn't thread-safe. As BackendUtil is part of the generic Clang FrontendAction logic, a process which has several threads executing Clang FrontendActions will randomly crash in the unsafe setup code. This patch avoids calling the function unless either the debug-pass option or limit-float-precision option is set. Without these two options set the `ParseCommandLineOptions` call doesn't do anything beside parsing the command line `clang` which doesn't set any options. See also D99652 where LLDB received a workaround for this crash. Reviewed By: JDevlieghere Differential Revision: https://reviews.llvm.org/D99740 (cherry picked from commit 60854c3)
1 parent 8d4a055 commit 585384a

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,15 @@ static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
860860
BackendArgs.push_back("-limit-float-precision");
861861
BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
862862
}
863+
// Check for the default "clang" invocation that won't set any cl::opt values.
864+
// Skip trying to parse the command line invocation to avoid the issues
865+
// described below.
866+
if (BackendArgs.size() == 1)
867+
return;
863868
BackendArgs.push_back(nullptr);
869+
// FIXME: The command line parser below is not thread-safe and shares a global
870+
// state, so this call might crash or overwrite the options of another Clang
871+
// instance in the same process.
864872
llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
865873
BackendArgs.data());
866874
}

0 commit comments

Comments
 (0)