dead loop in clang::dataflow::WatchedLiteralsSolverImpl::solve(long long) when running bugprone-unchecked-optional-access tidy-check
Reproduce step:
- build https://github.com/ccache/ccache with compile_commands.json
- with 17.02 clangd check src/ccache.cpp
Stack trace
thread #1, queue = 'com.apple.main-thread', stop reason = instruction step over
* frame #0: 0x00000001063b1a44 clangd`clang::dataflow::WatchedLiteralsSolverImpl::solve(long long) && + 228
frame #1: 0x00000001063b17a4 clangd`clang::dataflow::WatchedLiteralsSolver::solve(llvm::ArrayRef<clang::dataflow::Formula const*>) + 64
frame #2: 0x000000010639bc84 clangd`clang::dataflow::DataflowAnalysisContext::isUnsatisfiable(llvm::SetVector<clang::dataflow::Formula const*, std::__1::vector<clang::dataflow::Formula const*, std::__1::allocator<clang::dataflow::Formula const*>>, llvm::DenseSet<clang::dataflow::Formula const*, llvm::DenseMapInfo<clang::dataflow::Formula const*, void>>, 0u>) + 180
frame #3: 0x000000010639b8d8 clangd`clang::dataflow::DataflowAnalysisContext::flowConditionImplies(clang::dataflow::Atom, clang::dataflow::Formula const&) + 196
frame #4: 0x000000010638853c clangd`clang::dataflow::UncheckedOptionalAccessModel::compare(clang::QualType, clang::dataflow::Value const&, clang::dataflow::Environment const&, clang::dataflow::Value const&, clang::dataflow::Environment const&) + 280
frame #5: 0x0000000106388978 clangd`clang::dataflow::UncheckedOptionalAccessModel::widen(clang::QualType, clang::dataflow::Value&, clang::dataflow::Environment const&, clang::dataflow::Value&, clang::dataflow::Environment&) + 44
frame #6: 0x00000001063a1588 clangd`clang::dataflow::Environment::widen(clang::dataflow::Environment const&, clang::dataflow::Environment::ValueModel&) + 404
frame #7: 0x00000001063aee8c clangd`clang::dataflow::runTypeErasedDataflowAnalysis(clang::dataflow::ControlFlowContext const&, clang::dataflow::TypeErasedDataflowAnalysis&, clang::dataflow::Environment const&, std::__1::function<void (clang::CFGElement const&, clang::dataflow::TypeErasedDataflowAnalysisState const&)>) + 1340
frame #8: 0x000000010635f5b4 clangd`llvm::Expected<std::__1::vector<std::__1::optional<clang::dataflow::DataflowAnalysisState<clang::dataflow::UncheckedOptionalAccessModel::Lattice>>, std::__1::allocator<std::__1::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::__1::function<void (clang::CFGElement const&, clang::dataflow::DataflowAnalysisState<clang::dataflow::UncheckedOptionalAccessModel::Lattice> const&)>) + 216
frame #9: 0x000000010635f13c clangd`clang::tidy::bugprone::UncheckedOptionalAccessCheck::check(clang::ast_matchers::MatchFinder::MatchResult const&) + 464
frame #10: 0x0000000106c42058 clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::MatchVisitor::visitMatch(clang::ast_matchers::BoundNodes const&) + 132
frame #11: 0x0000000106c6d1a0 clangd`clang::ast_matchers::internal::BoundNodesTreeBuilder::visitMatches(clang::ast_matchers::internal::BoundNodesTreeBuilder::Visitor*) + 256
frame #12: 0x0000000106c419ac clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::matchWithFilter(clang::DynTypedNode const&) + 1104
frame #13: 0x0000000106c433fc clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) + 276
frame #14: 0x0000000106c46728 clangd`clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseDecl(clang::Decl*) + 13004
frame #15: 0x0000000106c43408 clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) + 288
frame #16: 0x0000000106c226bc clangd`clang::ast_matchers::MatchFinder::matchAST(clang::ASTContext&) + 748
frame #17: 0x0000000105fca154 clangd`clang::clangd::ParsedAST::build(llvm::StringRef, clang::clangd::ParseInputs const&, std::__1::unique_ptr<clang::CompilerInvocation, std::__1::default_delete<clang::CompilerInvocation>>, llvm::ArrayRef<clang::clangd::Diag>, std::__1::shared_ptr<clang::clangd::PreambleData const>) + 7508
frame #18: 0x0000000105e63994 clangd`clang::clangd::check(llvm::StringRef, clang::clangd::ThreadsafeFS const&, clang::clangd::ClangdLSPServer::Options const&) + 7376
frame #19: 0x0000000105e53294 clangd`clang::clangd::clangdMain(int, char**) + 8664
frame #20: 0x000000018659bf28 dyld`start + 2236
@llvm/issue-subscribers-clangd
Author: Andy Chen (andy-yx-chen)
Stack trace
thread #<!-- -->1, queue = 'com.apple.main-thread', stop reason = instruction step over
* frame #<!-- -->0: 0x00000001063b1a44 clangd`clang::dataflow::WatchedLiteralsSolverImpl::solve(long long) && + 228
frame #<!-- -->1: 0x00000001063b17a4 clangd`clang::dataflow::WatchedLiteralsSolver::solve(llvm::ArrayRef<clang::dataflow::Formula const*>) + 64
frame #<!-- -->2: 0x000000010639bc84 clangd`clang::dataflow::DataflowAnalysisContext::isUnsatisfiable(llvm::SetVector<clang::dataflow::Formula const*, std::__1::vector<clang::dataflow::Formula const*, std::__1::allocator<clang::dataflow::Formula const*>>, llvm::DenseSet<clang::dataflow::Formula const*, llvm::DenseMapInfo<clang::dataflow::Formula const*, void>>, 0u>) + 180
frame #<!-- -->3: 0x000000010639b8d8 clangd`clang::dataflow::DataflowAnalysisContext::flowConditionImplies(clang::dataflow::Atom, clang::dataflow::Formula const&) + 196
frame #<!-- -->4: 0x000000010638853c clangd`clang::dataflow::UncheckedOptionalAccessModel::compare(clang::QualType, clang::dataflow::Value const&, clang::dataflow::Environment const&, clang::dataflow::Value const&, clang::dataflow::Environment const&) + 280
frame #<!-- -->5: 0x0000000106388978 clangd`clang::dataflow::UncheckedOptionalAccessModel::widen(clang::QualType, clang::dataflow::Value&, clang::dataflow::Environment const&, clang::dataflow::Value&, clang::dataflow::Environment&) + 44
frame #<!-- -->6: 0x00000001063a1588 clangd`clang::dataflow::Environment::widen(clang::dataflow::Environment const&, clang::dataflow::Environment::ValueModel&) + 404
frame #<!-- -->7: 0x00000001063aee8c clangd`clang::dataflow::runTypeErasedDataflowAnalysis(clang::dataflow::ControlFlowContext const&, clang::dataflow::TypeErasedDataflowAnalysis&, clang::dataflow::Environment const&, std::__1::function<void (clang::CFGElement const&, clang::dataflow::TypeErasedDataflowAnalysisState const&)>) + 1340
frame #<!-- -->8: 0x000000010635f5b4 clangd`llvm::Expected<std::__1::vector<std::__1::optional<clang::dataflow::DataflowAnalysisState<clang::dataflow::UncheckedOptionalAccessModel::Lattice>>, std::__1::allocator<std::__1::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::__1::function<void (clang::CFGElement const&, clang::dataflow::DataflowAnalysisState<clang::dataflow::UncheckedOptionalAccessModel::Lattice> const&)>) + 216
frame #<!-- -->9: 0x000000010635f13c clangd`clang::tidy::bugprone::UncheckedOptionalAccessCheck::check(clang::ast_matchers::MatchFinder::MatchResult const&) + 464
frame #<!-- -->10: 0x0000000106c42058 clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::MatchVisitor::visitMatch(clang::ast_matchers::BoundNodes const&) + 132
frame #<!-- -->11: 0x0000000106c6d1a0 clangd`clang::ast_matchers::internal::BoundNodesTreeBuilder::visitMatches(clang::ast_matchers::internal::BoundNodesTreeBuilder::Visitor*) + 256
frame #<!-- -->12: 0x0000000106c419ac clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::matchWithFilter(clang::DynTypedNode const&) + 1104
frame #<!-- -->13: 0x0000000106c433fc clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) + 276
frame #<!-- -->14: 0x0000000106c46728 clangd`clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseDecl(clang::Decl*) + 13004
frame #<!-- -->15: 0x0000000106c43408 clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) + 288
frame #<!-- -->16: 0x0000000106c226bc clangd`clang::ast_matchers::MatchFinder::matchAST(clang::ASTContext&) + 748
frame #<!-- -->17: 0x0000000105fca154 clangd`clang::clangd::ParsedAST::build(llvm::StringRef, clang::clangd::ParseInputs const&, std::__1::unique_ptr<clang::CompilerInvocation, std::__1::default_delete<clang::CompilerInvocation>>, llvm::ArrayRef<clang::clangd::Diag>, std::__1::shared_ptr<clang::clangd::PreambleData const>) + 7508
frame #<!-- -->18: 0x0000000105e63994 clangd`clang::clangd::check(llvm::StringRef, clang::clangd::ThreadsafeFS const&, clang::clangd::ClangdLSPServer::Options const&) + 7376
frame #<!-- -->19: 0x0000000105e53294 clangd`clang::clangd::clangdMain(int, char**) + 8664
frame #<!-- -->20: 0x000000018659bf28 dyld`start + 2236
@llvm/issue-subscribers-clang-tidy
Author: Andy Chen (andy-yx-chen)
Stack trace
thread #<!-- -->1, queue = 'com.apple.main-thread', stop reason = instruction step over
* frame #<!-- -->0: 0x00000001063b1a44 clangd`clang::dataflow::WatchedLiteralsSolverImpl::solve(long long) && + 228
frame #<!-- -->1: 0x00000001063b17a4 clangd`clang::dataflow::WatchedLiteralsSolver::solve(llvm::ArrayRef<clang::dataflow::Formula const*>) + 64
frame #<!-- -->2: 0x000000010639bc84 clangd`clang::dataflow::DataflowAnalysisContext::isUnsatisfiable(llvm::SetVector<clang::dataflow::Formula const*, std::__1::vector<clang::dataflow::Formula const*, std::__1::allocator<clang::dataflow::Formula const*>>, llvm::DenseSet<clang::dataflow::Formula const*, llvm::DenseMapInfo<clang::dataflow::Formula const*, void>>, 0u>) + 180
frame #<!-- -->3: 0x000000010639b8d8 clangd`clang::dataflow::DataflowAnalysisContext::flowConditionImplies(clang::dataflow::Atom, clang::dataflow::Formula const&) + 196
frame #<!-- -->4: 0x000000010638853c clangd`clang::dataflow::UncheckedOptionalAccessModel::compare(clang::QualType, clang::dataflow::Value const&, clang::dataflow::Environment const&, clang::dataflow::Value const&, clang::dataflow::Environment const&) + 280
frame #<!-- -->5: 0x0000000106388978 clangd`clang::dataflow::UncheckedOptionalAccessModel::widen(clang::QualType, clang::dataflow::Value&, clang::dataflow::Environment const&, clang::dataflow::Value&, clang::dataflow::Environment&) + 44
frame #<!-- -->6: 0x00000001063a1588 clangd`clang::dataflow::Environment::widen(clang::dataflow::Environment const&, clang::dataflow::Environment::ValueModel&) + 404
frame #<!-- -->7: 0x00000001063aee8c clangd`clang::dataflow::runTypeErasedDataflowAnalysis(clang::dataflow::ControlFlowContext const&, clang::dataflow::TypeErasedDataflowAnalysis&, clang::dataflow::Environment const&, std::__1::function<void (clang::CFGElement const&, clang::dataflow::TypeErasedDataflowAnalysisState const&)>) + 1340
frame #<!-- -->8: 0x000000010635f5b4 clangd`llvm::Expected<std::__1::vector<std::__1::optional<clang::dataflow::DataflowAnalysisState<clang::dataflow::UncheckedOptionalAccessModel::Lattice>>, std::__1::allocator<std::__1::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::__1::function<void (clang::CFGElement const&, clang::dataflow::DataflowAnalysisState<clang::dataflow::UncheckedOptionalAccessModel::Lattice> const&)>) + 216
frame #<!-- -->9: 0x000000010635f13c clangd`clang::tidy::bugprone::UncheckedOptionalAccessCheck::check(clang::ast_matchers::MatchFinder::MatchResult const&) + 464
frame #<!-- -->10: 0x0000000106c42058 clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::MatchVisitor::visitMatch(clang::ast_matchers::BoundNodes const&) + 132
frame #<!-- -->11: 0x0000000106c6d1a0 clangd`clang::ast_matchers::internal::BoundNodesTreeBuilder::visitMatches(clang::ast_matchers::internal::BoundNodesTreeBuilder::Visitor*) + 256
frame #<!-- -->12: 0x0000000106c419ac clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::matchWithFilter(clang::DynTypedNode const&) + 1104
frame #<!-- -->13: 0x0000000106c433fc clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) + 276
frame #<!-- -->14: 0x0000000106c46728 clangd`clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseDecl(clang::Decl*) + 13004
frame #<!-- -->15: 0x0000000106c43408 clangd`clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) + 288
frame #<!-- -->16: 0x0000000106c226bc clangd`clang::ast_matchers::MatchFinder::matchAST(clang::ASTContext&) + 748
frame #<!-- -->17: 0x0000000105fca154 clangd`clang::clangd::ParsedAST::build(llvm::StringRef, clang::clangd::ParseInputs const&, std::__1::unique_ptr<clang::CompilerInvocation, std::__1::default_delete<clang::CompilerInvocation>>, llvm::ArrayRef<clang::clangd::Diag>, std::__1::shared_ptr<clang::clangd::PreambleData const>) + 7508
frame #<!-- -->18: 0x0000000105e63994 clangd`clang::clangd::check(llvm::StringRef, clang::clangd::ThreadsafeFS const&, clang::clangd::ClangdLSPServer::Options const&) + 7376
frame #<!-- -->19: 0x0000000105e53294 clangd`clang::clangd::clangdMain(int, char**) + 8664
frame #<!-- -->20: 0x000000018659bf28 dyld`start + 2236
Added clang-tidy label because the hang is inside the bugprone-unchecked-optional-access clang-tidy check.
There are existing issues on file for hangs in that checker (#62085, #55530).
As a clangd user, a workaround is to disable the checker for clangd by adding:
Diagnostics:
ClangTidy:
Remove: bugprone-unchecked-optional-access
to .clangd.
Added clang-tidy label because the hang is inside the bugprone-unchecked-optional-access clang-tidy check.
There are existing issues on file for hangs in that checker (#62085, #55530).
As a clangd user, a workaround is to disable the checker for clangd by adding:
Diagnostics: ClangTidy: Remove: bugprone-unchecked-optional-accessto
.clangd.
this works for me, thanks!
Root cause was not fixed yet.
@EugeneZelenko sorry for the noise here, i marked this one as fixed because symptoms in clangd are "fixed" (what a broad meaning that word has..) and because there are other issues mentioned in https://github.com/llvm/llvm-project/issues/69369#issuecomment-1767710887 for tracking the problems in the tidy-check.
I've renamed the issue at least to not mention clangd, feel free to keep/close it.
Still no fix for root cause.
Still no fix for root cause.
Root case is the constraint on SAT solver(I think). Currently, satisfiability algorithm used in FlowSensitiveAnalysis is based on DPLL framework and can't solve large formula which contains a few thousand variable.
Root case is the constraint on SAT solver(I think). Currently, satisfiability algorithm used in
FlowSensitiveAnalysisis based on DPLL framework and can't solve large formula which contains a few thousand variable.
Can we make it so that hitting the limits of the solver results in a false negative (no diagnostic issued) rather than a hang?
Root case is the constraint on SAT solver(I think). Currently, satisfiability algorithm used in
FlowSensitiveAnalysisis based on DPLL framework and can't solve large formula which contains a few thousand variable.Can we make it so that hitting the limits of the solver results in a false negative (no diagnostic issued) rather than a hang?
Actually, UncheckedOptionalAccessCheck use default iteration number(1'000'000'000) as the iteration limit, change it like this
if (llvm::Expected<llvm::SmallVector<SourceLocation>> Locs =
dataflow::diagnoseFunction<UncheckedOptionalAccessModel,
SourceLocation>(*FuncDecl, *Result.Context,
Diagnoser, /*# of iteration= */ 1000))
for (const SourceLocation &Loc : *Locs)
diag(Loc, "unchecked access to optional value");
else
llvm::consumeError(Locs.takeError());
would avoid hanging and return timeout(reach limitation), but this will produce false positive(not very sure) based on current implementation.