clang-tidy (llvm-17) crash in dataflow analysis
On our machines (Linux) clang-tidy (llvm 17) crashes when processing
#include <optional>
#include <string>
std::optional<int> g();
void f() {
std::string replace_fields[25];
const std::optional<int> c = g();
c.has_value();
}
with stack dump
Stack dump:
0. Program arguments: clang-tidy-17 adrid059.cpp
1. <eof> parser at end of file
2. ASTMatcher: Processing 'bugprone-unchecked-optional-access' against:
FunctionDecl f : </home/kromanov1/CORE/adrid059/adrid059/adrid059.cpp:6:1, line:11:1>
--- Bound Nodes Begin ---
T - { BuiltinType : int }
fun - { FunctionDecl f : </home/kromanov1/CORE/adrid059/adrid059/adrid059.cpp:6:1, line:11:1> }
--- Bound Nodes End ---
3. runTypeErasedDataflowAnalysis
Decl:
FunctionDecl 0x75c22a0 </home/kromanov1/CORE/adrid059/adrid059/adrid059.cpp:6:1, line:11:1> line:6:6 f 'void ()'
`-CompoundStmt 0x7615830 <col:10, line:11:1>
|-DeclStmt 0x75c2580 <line:7:5, col:35>
| `-VarDecl 0x75c2498 <col:5, col:34> col:17 replace_fields 'std::string[25]' callinit destroyed
| `-CXXConstructExpr 0x75c2558 <col:17> 'std::string[25]' 'void () noexcept'
|-DeclStmt 0x7614e68 <line:9:5, col:37>
| `-VarDecl 0x75c2690 <col:5, col:36> col:30 used c 'const std::optional<int>':'const std::optional<int>' cinit
| `-ImplicitCastExpr 0x7614e50 <col:34, col:36> 'const std::optional<int>':'const std::optional<int>' <NoOp>
| `-CallExpr 0x75c27d0 <col:34, col:36> 'std::optional<int>':'std::optional<int>'
| `-ImplicitCastExpr 0x75c27b8 <col:34> 'std::optional<int> (*)()' <FunctionToPointerDecay>
| `-DeclRefExpr 0x75c2740 <col:34> 'std::optional<int> ()' lvalue Function 0x75c21d8 'g' 'std::optional<int> ()'
`-CXXMemberCallExpr 0x7615278 <line:10:5, col:17> 'bool'
`-MemberExpr 0x7614ea0 <col:5, col:7> '<bound member function type>' .has_value 0x7610b30
`-DeclRefExpr 0x7614e80 <col:5> 'const std::optional<int>':'const std::optional<int>' lvalue Var 0x75c2690 'c' 'const std::optional<int>':'const std::optional<int>'
CFG:
[B2 (ENTRY)]
Succs (1): B1
[B1]
1: (CXXConstructExpr, std::string[25])
2: std::string replace_fields[25];
3: g
4: [B1.3] (ImplicitCastExpr, FunctionToPointerDecay, std::optional<int> (*)(void))
5: [B1.4]()
6: [B1.5] (ImplicitCastExpr, NoOp, const std::optional<int>)
7: const std::optional<int> c = g();
8: c
9: [B1.8].has_value
10: [B1.9]()
11: [B1.2].~std::string[25]() (Implicit destructor)
Preds (1): B2
Succs (1): B0
[B0 (EXIT)]
Preds (1): B1
4. transferCFGBlock: Element [B1.1]
Stmt:
CXXConstructExpr 0x75c2558 'std::string[25]' 'void (void) noexcept'
#0 0x0000000003f7917b llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x3f7917b)
#1 0x0000000003f767a4 SignalHandler(int) Signals.cpp:0:0
#2 0x00007f29c0411630 __restore_rt sigaction.c:0:0
#3 0x00000000013dd80b clang::StmtVisitorBase<llvm::make_const_ptr, clang::dataflow::(anonymous namespace)::TransferVisitor, void>::Visit(clang::Stmt const*) Transfer.cpp:0:0
#4 0x00000000013ddaee clang::dataflow::transfer(clang::dataflow::StmtToEnvMap const&, clang::Stmt const&, clang::dataflow::Environment&) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x13ddaee)
#5 0x00000000013c7507 clang::dataflow::transferCFGBlock(clang::CFGBlock const&, clang::dataflow::(anonymous namespace)::AnalysisContext&, std::function<void (clang::CFGElement const&, clang::dataflow::TypeErasedDataflowAnalysisState const&)>) TypeErasedDataflowAnalysis.cpp:0:0
#6 0x00000000013c8792 clang::dataflow::runTypeErasedDataflowAnalysis(clang::dataflow::ControlFlowContext const&, clang::dataflow::TypeErasedDataflowAnalysis&, clang::dataflow::Environment const&, std::function<void (clang::CFGElement const&, clang::dataflow::TypeErasedDataflowAnalysisState const&)>) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x13c8792)
#7 0x0000000000df4f1c llvm::Expected<std::vector<std::optional<clang::dataflow::DataflowAnalysisState<clang::dataflow::UncheckedOptionalAccessModel::Lattice>>, std::allocator<std::optional<clang::dataflow::DataflowAnalysisState<clang::dataflow::UncheckedOptionalAccessModel::Lattice>>>>> clang::dataflow::runDataflowAnalysis<clang::dataflow::UncheckedOptionalAccessModel>(clang::dataflow::ControlFlowContext const&, clang::dataflow::UncheckedOptionalAccessModel&, clang::dataflow::Environment const&, std::function<void (clang::CFGElement const&, clang::dataflow::DataflowAnalysisState<clang::dataflow::UncheckedOptionalAccessModel::Lattice> const&)>) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0xdf4f1c)
#8 0x0000000000df5460 clang::tidy::bugprone::analyzeFunction(clang::FunctionDecl const&, clang::ASTContext&, clang::dataflow::UncheckedOptionalAccessModelOptions) UncheckedOptionalAccessCheck.cpp:0:0
#9 0x0000000000df58d7 clang::tidy::bugprone::UncheckedOptionalAccessCheck::check(clang::ast_matchers::MatchFinder::MatchResult const&) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0xdf58d7)
#10 0x0000000002d78179 clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::MatchVisitor::visitMatch(clang::ast_matchers::BoundNodes const&) ASTMatchFinder.cpp:0:0
#11 0x0000000002da9397 clang::ast_matchers::internal::BoundNodesTreeBuilder::visitMatches(clang::ast_matchers::internal::BoundNodesTreeBuilder::Visitor*) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x2da9397)
#12 0x0000000002d78e14 clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::matchWithFilter(clang::DynTypedNode const&) ASTMatchFinder.cpp:0:0
#13 0x0000000002d97aac clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) ASTMatchFinder.cpp:0:0
#14 0x0000000002d97ba9 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseDeclContextHelper(clang::DeclContext*) (.part.0) ASTMatchFinder.cpp:0:0
#15 0x0000000002da15d7 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseTranslationUnitDecl(clang::TranslationUnitDecl*) ASTMatchFinder.cpp:0:0
#16 0x0000000002d97ab7 clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) ASTMatchFinder.cpp:0:0
#17 0x0000000002d97dd4 clang::ast_matchers::MatchFinder::matchAST(clang::ASTContext&) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x2d97dd4)
#18 0x0000000001c9ee98 clang::MultiplexConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x1c9ee98)
#19 0x0000000001f3a5cd clang::ParseAST(clang::Sema&, bool, bool) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x1f3a5cd)
#20 0x0000000001c60c39 clang::FrontendAction::Execute() (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x1c60c39)
#21 0x0000000001bdf59b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x1bdf59b)
#22 0x000000000151d20a clang::tooling::FrontendActionFactory::runInvocation(std::shared_ptr<clang::CompilerInvocation>, clang::FileManager*, std::shared_ptr<clang::PCHContainerOperations>, clang::DiagnosticConsumer*) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x151d20a)
#23 0x00000000014b9812 clang::tidy::runClangTidy(clang::tidy::ClangTidyContext&, clang::tooling::CompilationDatabase const&, llvm::ArrayRef<std::string>, llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>, bool, bool, llvm::StringRef)::ActionFactory::runInvocation(std::shared_ptr<clang::CompilerInvocation>, clang::FileManager*, std::shared_ptr<clang::PCHContainerOperations>, clang::DiagnosticConsumer*) ClangTidy.cpp:0:0
#24 0x00000000015151f3 clang::tooling::ToolInvocation::runInvocation(char const*, clang::driver::Compilation*, std::shared_ptr<clang::CompilerInvocation>, std::shared_ptr<clang::PCHContainerOperations>) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x15151f3)
#25 0x00000000015187b0 clang::tooling::ToolInvocation::run() (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x15187b0)
#26 0x000000000151aeca clang::tooling::ClangTool::run(clang::tooling::ToolAction*) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x151aeca)
#27 0x00000000014c7988 clang::tidy::runClangTidy(clang::tidy::ClangTidyContext&, clang::tooling::CompilationDatabase const&, llvm::ArrayRef<std::string>, llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>, bool, bool, llvm::StringRef) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0x14c7988)
#28 0x0000000000c2bc18 clang::tidy::clangTidyMain(int, char const**) (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0xc2bc18)
#29 0x00007f29bf42a555 __libc_start_main (/lib64/libc.so.6+0x22555)
#30 0x0000000000c2382d _start (/opt/bb/libexec/clang-tidy-17.0/bin/clang-tidy+0xc2382d)
Segmentation fault
Could you please 18 or main branch? 18 has relevant fixes: https://releases.llvm.org/18.1.0/tools/clang/tools/extra/docs/ReleaseNotes.html.
This does not reproduce on my Linux with the trunk version.
Does not reproduce with clang-tidy-18 and main.
However it does reproduce with clang-tidy-17 downloaded from LLVM via apt-get.
Reliable command line is
clang-tidy-17 --checks=* dataflow.cpp
@ksromanov: Only most recent release is maintained. There were some fixes in dataflow analysis recently.
@ksromanov: Only most recent release is maintained. There were some fixes in dataflow analysis recently.
Thanks. Please feel free to close this issue.
I suspect that the problem is resolved in https://github.com/llvm/llvm-project/commit/e6e83cbcc748a55a7eddce67b228298820cb9315
Since, another variation of the program crashes clang-tidy-17:
#include <optional>
#include <string>
std::optional<int> g();
struct Z {
int a;
};
void f() {
Z replace_fields[1];
const std::optional<int> c = g();
c.has_value();
}
replacing array replace_fields with a single value hides the crash.
I believe this has been fixed as well. See, for exapmle, https://godbolt.org/z/q6Pcaovrr. Can you check on the latest release (or head)?
I believe this has been fixed as well. See, for exapmle, https://godbolt.org/z/q6Pcaovrr. Can you check on the latest release (or head)?
Yes, of course it has been fixed.
I am just trying to identify the exact commit, where the problem in the top-level comment got fixed. Since I can replace std::string with the simple struct and it seems to crash in the exactly the same place, it looks like the both crashes (with std::string and struct) come from the same source. The latter crash is fixed by https://github.com/llvm/llvm-project/commit/e6e83cbcc748a55a7eddce67b228298820cb9315 , so I think, that the initial crash is also resolved by the same commit.
So, it makes sense to backport this commit to our internal build of clang-17.
Confirming — cherry-picking https://github.com/llvm/llvm-project/commit/e6e83cbcc748a55a7eddce67b228298820cb9315 did help. The patched version of clang-tidy-17 does not crash on either of the examples above.