|
| 1 | +From 60854c328d8729b2ef10b9bb4dcbcc282f43c5e7 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Raphael Isemann < [email protected]> |
| 3 | +Date: Thu, 1 Apr 2021 18:41:44 +0200 |
| 4 | +Subject: [PATCH] Avoid calling ParseCommandLineOptions in BackendUtil if |
| 5 | + possible |
| 6 | + |
| 7 | +Calling `ParseCommandLineOptions` should only be called from `main` as the |
| 8 | +CommandLine setup code isn't thread-safe. As BackendUtil is part of the |
| 9 | +generic Clang FrontendAction logic, a process which has several threads executing |
| 10 | +Clang FrontendActions will randomly crash in the unsafe setup code. |
| 11 | + |
| 12 | +This patch avoids calling the function unless either the debug-pass option or |
| 13 | +limit-float-precision option is set. Without these two options set the |
| 14 | +`ParseCommandLineOptions` call doesn't do anything beside parsing |
| 15 | +the command line `clang` which doesn't set any options. |
| 16 | + |
| 17 | +See also D99652 where LLDB received a workaround for this crash. |
| 18 | + |
| 19 | +Reviewed By: JDevlieghere |
| 20 | + |
| 21 | +Differential Revision: https://reviews.llvm.org/D99740 |
| 22 | +--- |
| 23 | + clang/lib/CodeGen/BackendUtil.cpp | 8 ++++++++ |
| 24 | + 1 file changed, 8 insertions(+) |
| 25 | + |
| 26 | +diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp |
| 27 | +index 41eafd13d97c..00d92e7beadd 100644 |
| 28 | +--- a/clang/lib/CodeGen/BackendUtil.cpp |
| 29 | ++++ b/clang/lib/CodeGen/BackendUtil.cpp |
| 30 | +@@ -871,7 +871,15 @@ static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) { |
| 31 | + BackendArgs.push_back("-limit-float-precision"); |
| 32 | + BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); |
| 33 | + } |
| 34 | ++ // Check for the default "clang" invocation that won't set any cl::opt values. |
| 35 | ++ // Skip trying to parse the command line invocation to avoid the issues |
| 36 | ++ // described below. |
| 37 | ++ if (BackendArgs.size() == 1) |
| 38 | ++ return; |
| 39 | + BackendArgs.push_back(nullptr); |
| 40 | ++ // FIXME: The command line parser below is not thread-safe and shares a global |
| 41 | ++ // state, so this call might crash or overwrite the options of another Clang |
| 42 | ++ // instance in the same process. |
| 43 | + llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, |
| 44 | + BackendArgs.data()); |
| 45 | + } |
| 46 | +-- |
| 47 | +2.29.2 |
| 48 | + |
0 commit comments