Skip to content

Commit 60854c3

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
1 parent 232d3a3 commit 60854c3

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
@@ -871,7 +871,15 @@ static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
871871
BackendArgs.push_back("-limit-float-precision");
872872
BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
873873
}
874+
// Check for the default "clang" invocation that won't set any cl::opt values.
875+
// Skip trying to parse the command line invocation to avoid the issues
876+
// described below.
877+
if (BackendArgs.size() == 1)
878+
return;
874879
BackendArgs.push_back(nullptr);
880+
// FIXME: The command line parser below is not thread-safe and shares a global
881+
// state, so this call might crash or overwrite the options of another Clang
882+
// instance in the same process.
875883
llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
876884
BackendArgs.data());
877885
}

0 commit comments

Comments
 (0)