llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.85k stars 11.47k forks source link

Crash in modernize-use-ranges check #100406

Closed matthew-f closed 1 month ago

matthew-f commented 1 month ago

The modernize-use-ranges check is crashing. This is the backtrace:

1.  <eof> parser at end of file
2.  ASTMatcher: Processing 'modernize-use-ranges' against:
    CallExpr : </home/xxx/xxx/source/trunk/500_tmp/enterprise/cloud/servers/user/logins.cpp:241:4, line:243:31>
--- Bound Nodes Begin ---
    ArgName0 - { DeclRefExpr : </home/xxx/xxx/source/trunk/500_tmp/enterprise/cloud/servers/user/logins.cpp:241:16> }
    CallExpr0:2:0 - { CallExpr : </home/xxx/xxx/source/trunk/500_tmp/enterprise/cloud/servers/user/logins.cpp:241:4, line:243:31> }
    FuncDecl - { FunctionDecl std::_V2::rotate : </usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_algo.h:1403:5 <Spelling=/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/x86_64-linux-gnu/c++/11/bits/c++config.h:169:32>, /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_algo.h:1416:5> }
--- Bound Nodes End ---
 #0 0x000059a2eef79530 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/local/llvm-20240723/bin/clang-tidy+0x48f6530)
 #1 0x000059a2eef76b0e SignalHandler(int) Signals.cpp:0:0
 #2 0x0000768f8a242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x000059a2ebfb09bc clang::tidy::utils::UseRangesCheck::check(clang::ast_matchers::MatchFinder::MatchResult const&) (/usr/local/llvm-20240723/bin/clang-tidy+0x192d9bc)
 #4 0x000059a2edab7e36 clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::MatchVisitor::visitMatch(clang::ast_matchers::BoundNodes const&) ASTMatchFinder.cpp:0:0
 #5 0x000059a2edae5b8f clang::ast_matchers::internal::BoundNodesTreeBuilder::visitMatches(clang::ast_matchers::internal::BoundNodesTreeBuilder::Visitor*) (/usr/local/llvm-20240723/bin/clang-tidy+0x3462b8f)
 #6 0x000059a2edab8a30 clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::matchWithFilter(clang::DynTypedNode const&) ASTMatchFinder.cpp:0:0
 #7 0x000059a2edadd4d0 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseObjCAvailabilityCheckExpr(clang::ObjCAvailabilityCheckExpr*, llvm::SmallVectorImpl<llvm::PointerIntPair<clang::Stmt*, 1u, bool, llvm::PointerLikeTypeTraits<clang::Stmt*>, llvm::PointerIntPairInfo<clang::Stmt*, 1u, llvm::PointerLikeTypeTraits<clang::Stmt*>>>>*) ASTMatchFinder.cpp:0:0
 #8 0x000059a2edad5950 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseStmt(clang::Stmt*, llvm::SmallVectorImpl<llvm::PointerIntPair<clang::Stmt*, 1u, bool, llvm::PointerLikeTypeTraits<clang::Stmt*>, llvm::PointerIntPairInfo<clang::Stmt*, 1u, llvm::PointerLikeTypeTraits<clang::Stmt*>>>>*) ASTMatchFinder.cpp:0:0
 #9 0x000059a2edad5adb clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseStmt(clang::Stmt*, llvm::SmallVectorImpl<llvm::PointerIntPair<clang::Stmt*, 1u, bool, llvm::PointerLikeTypeTraits<clang::Stmt*>, llvm::PointerIntPairInfo<clang::Stmt*, 1u, llvm::PointerLikeTypeTraits<clang::Stmt*>>>>*) (.constprop.0) ASTMatchFinder.cpp:0:0
#10 0x000059a2edadf90f clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseFunctionHelper(clang::FunctionDecl*) ASTMatchFinder.cpp:0:0
#11 0x000059a2edadfb83 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseCXXMethodDecl(clang::CXXMethodDecl*) ASTMatchFinder.cpp:0:0
#12 0x000059a2edad02cf clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) ASTMatchFinder.cpp:0:0
#13 0x000059a2edad03d9 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseDeclContextHelper(clang::DeclContext*) (.part.0) ASTMatchFinder.cpp:0:0
#14 0x000059a2edadbad8 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseTranslationUnitDecl(clang::TranslationUnitDecl*) ASTMatchFinder.cpp:0:0
#15 0x000059a2edad02cf clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) ASTMatchFinder.cpp:0:0
#16 0x000059a2edad0605 clang::ast_matchers::MatchFinder::matchAST(clang::ASTContext&) (/usr/local/llvm-20240723/bin/clang-tidy+0x344d605)
#17 0x000059a2ec84a900 clang::MultiplexConsumer::HandleTranslationUnit(clang::ASTContext&) (/usr/local/llvm-20240723/bin/clang-tidy+0x21c7900)
#18 0x000059a2ecad633c clang::ParseAST(clang::Sema&, bool, bool) (/usr/local/llvm-20240723/bin/clang-tidy+0x245333c)
#19 0x000059a2ec80a449 clang::FrontendAction::Execute() (/usr/local/llvm-20240723/bin/clang-tidy+0x2187449)
#20 0x000059a2ec777c39 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/local/llvm-20240723/bin/clang-tidy+0x20f4c39)
#21 0x000059a2ec01ca04 clang::tooling::FrontendActionFactory::runInvocation(std::shared_ptr<clang::CompilerInvocation>, clang::FileManager*, std::shared_ptr<clang::PCHContainerOperations>, clang::DiagnosticConsumer*) (/usr/local/llvm-20240723/bin/clang-tidy+0x1999a04)
#22 0x000059a2ebfbe9fe clang::tidy::runClangTidy(clang::tidy::ClangTidyContext&, clang::tooling::CompilationDatabase const&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, 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
#23 0x000059a2ec016035 clang::tooling::ToolInvocation::runInvocation(char const*, clang::driver::Compilation*, std::shared_ptr<clang::CompilerInvocation>, std::shared_ptr<clang::PCHContainerOperations>) (/usr/local/llvm-20240723/bin/clang-tidy+0x1993035)
#24 0x000059a2ec0185db clang::tooling::ToolInvocation::run() (/usr/local/llvm-20240723/bin/clang-tidy+0x19955db)
#25 0x000059a2ec01a78f clang::tooling::ClangTool::run(clang::tooling::ToolAction*) (/usr/local/llvm-20240723/bin/clang-tidy+0x199778f)
#26 0x000059a2ebfc7dea clang::tidy::runClangTidy(clang::tidy::ClangTidyContext&, clang::tooling::CompilationDatabase const&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>, bool, bool, llvm::StringRef) (/usr/local/llvm-20240723/bin/clang-tidy+0x1944dea)
#27 0x000059a2eb23425a clang::tidy::clangTidyMain(int, char const**) (/usr/local/llvm-20240723/bin/clang-tidy+0xbb125a)
#28 0x0000768f8a229d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#29 0x0000768f8a229e40 call_init ./csu/../csu/libc-start.c:128:20
#30 0x0000768f8a229e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#31 0x000059a2eb22a4d5 _start (/usr/local/llvm-20240723/bin/clang-tidy+0xba74d5)
Segmentation fault (core dumped)

The problem is the 'Qualified name is not found in 'Replaces'. As a hack, this fixes it:

auto Iter = Replaces.find(Qualified);

if (Iter == Replaces.end()) {
  return;
}

In the instance when it crashed, the 'Qualified' name was ::std::_V2::rotate. The 'Replaces' map does contain '::std::rotate'

I don't know this code at all, and from the assert it looks like this is not expected, so I'm afraid I'm not submitting a proper fix.

llvmbot commented 1 month ago

@llvm/issue-subscribers-clang-tidy

Author: None (matthew-f)

The modernize-use-ranges check is crashing. This is the backtrace: ``` 1. <eof> parser at end of file 2. ASTMatcher: Processing 'modernize-use-ranges' against: CallExpr : </home/xxx/xxx/source/trunk/500_tmp/enterprise/cloud/servers/user/logins.cpp:241:4, line:243:31> --- Bound Nodes Begin --- ArgName0 - { DeclRefExpr : </home/xxx/xxx/source/trunk/500_tmp/enterprise/cloud/servers/user/logins.cpp:241:16> } CallExpr0:2:0 - { CallExpr : </home/xxx/xxx/source/trunk/500_tmp/enterprise/cloud/servers/user/logins.cpp:241:4, line:243:31> } FuncDecl - { FunctionDecl std::_V2::rotate : </usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_algo.h:1403:5 <Spelling=/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/x86_64-linux-gnu/c++/11/bits/c++config.h:169:32>, /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_algo.h:1416:5> } --- Bound Nodes End --- #0 0x000059a2eef79530 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/local/llvm-20240723/bin/clang-tidy+0x48f6530) #1 0x000059a2eef76b0e SignalHandler(int) Signals.cpp:0:0 #2 0x0000768f8a242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520) #3 0x000059a2ebfb09bc clang::tidy::utils::UseRangesCheck::check(clang::ast_matchers::MatchFinder::MatchResult const&) (/usr/local/llvm-20240723/bin/clang-tidy+0x192d9bc) #4 0x000059a2edab7e36 clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::MatchVisitor::visitMatch(clang::ast_matchers::BoundNodes const&) ASTMatchFinder.cpp:0:0 #5 0x000059a2edae5b8f clang::ast_matchers::internal::BoundNodesTreeBuilder::visitMatches(clang::ast_matchers::internal::BoundNodesTreeBuilder::Visitor*) (/usr/local/llvm-20240723/bin/clang-tidy+0x3462b8f) #6 0x000059a2edab8a30 clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::matchWithFilter(clang::DynTypedNode const&) ASTMatchFinder.cpp:0:0 #7 0x000059a2edadd4d0 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseObjCAvailabilityCheckExpr(clang::ObjCAvailabilityCheckExpr*, llvm::SmallVectorImpl<llvm::PointerIntPair<clang::Stmt*, 1u, bool, llvm::PointerLikeTypeTraits<clang::Stmt*>, llvm::PointerIntPairInfo<clang::Stmt*, 1u, llvm::PointerLikeTypeTraits<clang::Stmt*>>>>*) ASTMatchFinder.cpp:0:0 #8 0x000059a2edad5950 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseStmt(clang::Stmt*, llvm::SmallVectorImpl<llvm::PointerIntPair<clang::Stmt*, 1u, bool, llvm::PointerLikeTypeTraits<clang::Stmt*>, llvm::PointerIntPairInfo<clang::Stmt*, 1u, llvm::PointerLikeTypeTraits<clang::Stmt*>>>>*) ASTMatchFinder.cpp:0:0 #9 0x000059a2edad5adb clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseStmt(clang::Stmt*, llvm::SmallVectorImpl<llvm::PointerIntPair<clang::Stmt*, 1u, bool, llvm::PointerLikeTypeTraits<clang::Stmt*>, llvm::PointerIntPairInfo<clang::Stmt*, 1u, llvm::PointerLikeTypeTraits<clang::Stmt*>>>>*) (.constprop.0) ASTMatchFinder.cpp:0:0 #10 0x000059a2edadf90f clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseFunctionHelper(clang::FunctionDecl*) ASTMatchFinder.cpp:0:0 #11 0x000059a2edadfb83 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseCXXMethodDecl(clang::CXXMethodDecl*) ASTMatchFinder.cpp:0:0 #12 0x000059a2edad02cf clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) ASTMatchFinder.cpp:0:0 #13 0x000059a2edad03d9 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseDeclContextHelper(clang::DeclContext*) (.part.0) ASTMatchFinder.cpp:0:0 #14 0x000059a2edadbad8 clang::RecursiveASTVisitor<clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor>::TraverseTranslationUnitDecl(clang::TranslationUnitDecl*) ASTMatchFinder.cpp:0:0 #15 0x000059a2edad02cf clang::ast_matchers::internal::(anonymous namespace)::MatchASTVisitor::TraverseDecl(clang::Decl*) ASTMatchFinder.cpp:0:0 #16 0x000059a2edad0605 clang::ast_matchers::MatchFinder::matchAST(clang::ASTContext&) (/usr/local/llvm-20240723/bin/clang-tidy+0x344d605) #17 0x000059a2ec84a900 clang::MultiplexConsumer::HandleTranslationUnit(clang::ASTContext&) (/usr/local/llvm-20240723/bin/clang-tidy+0x21c7900) #18 0x000059a2ecad633c clang::ParseAST(clang::Sema&, bool, bool) (/usr/local/llvm-20240723/bin/clang-tidy+0x245333c) #19 0x000059a2ec80a449 clang::FrontendAction::Execute() (/usr/local/llvm-20240723/bin/clang-tidy+0x2187449) #20 0x000059a2ec777c39 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/local/llvm-20240723/bin/clang-tidy+0x20f4c39) #21 0x000059a2ec01ca04 clang::tooling::FrontendActionFactory::runInvocation(std::shared_ptr<clang::CompilerInvocation>, clang::FileManager*, std::shared_ptr<clang::PCHContainerOperations>, clang::DiagnosticConsumer*) (/usr/local/llvm-20240723/bin/clang-tidy+0x1999a04) #22 0x000059a2ebfbe9fe clang::tidy::runClangTidy(clang::tidy::ClangTidyContext&, clang::tooling::CompilationDatabase const&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, 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 #23 0x000059a2ec016035 clang::tooling::ToolInvocation::runInvocation(char const*, clang::driver::Compilation*, std::shared_ptr<clang::CompilerInvocation>, std::shared_ptr<clang::PCHContainerOperations>) (/usr/local/llvm-20240723/bin/clang-tidy+0x1993035) #24 0x000059a2ec0185db clang::tooling::ToolInvocation::run() (/usr/local/llvm-20240723/bin/clang-tidy+0x19955db) #25 0x000059a2ec01a78f clang::tooling::ClangTool::run(clang::tooling::ToolAction*) (/usr/local/llvm-20240723/bin/clang-tidy+0x199778f) #26 0x000059a2ebfc7dea clang::tidy::runClangTidy(clang::tidy::ClangTidyContext&, clang::tooling::CompilationDatabase const&, llvm::ArrayRef<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>, bool, bool, llvm::StringRef) (/usr/local/llvm-20240723/bin/clang-tidy+0x1944dea) #27 0x000059a2eb23425a clang::tidy::clangTidyMain(int, char const**) (/usr/local/llvm-20240723/bin/clang-tidy+0xbb125a) #28 0x0000768f8a229d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16 #29 0x0000768f8a229e40 call_init ./csu/../csu/libc-start.c:128:20 #30 0x0000768f8a229e40 __libc_start_main ./csu/../csu/libc-start.c:379:5 #31 0x000059a2eb22a4d5 _start (/usr/local/llvm-20240723/bin/clang-tidy+0xba74d5) Segmentation fault (core dumped) ``` The problem is the 'Qualified name is not found in 'Replaces'. As a hack, this fixes it: ``` auto Iter = Replaces.find(Qualified); if (Iter == Replaces.end()) { return; } ``` In the instance when it crashed, the 'Qualified' name was `::std::_V2::rotate`. The 'Replaces' map does contain '::std::rotate' I don't know this code at all, and from the assert it looks like this is not expected, so I'm afraid I'm not submitting a proper fix.
njames93 commented 1 month ago

Sounds like the issue is its not properly handling inline namespaces

njames93 commented 1 month ago

@matthew-f If you wouldn't mind could you test that linked MR. I wasn't able to reproduce the crash with the test cases, but it seems like it should be fixed

matthew-f commented 1 month ago

Hi @njames93 . That fix does work on my test case. Thank you.

tru commented 1 month ago

Should this still be backported to 19? in that case someone needs to request a PR.

PiotrZSL commented 1 month ago

Yes, it need.

njames93 commented 1 month ago

Should this still be backported to 19? in that case someone needs to request a PR.

Can the fix from the linked PR not simply be cherry picked?

tru commented 1 month ago

In order for the release managers to keep up with the number of fixes that should be merged we want people to follow these instructions - https://llvm.org/docs/GitHub.html#backporting-fixes-to-the-release-branches

llvmbot commented 1 month ago

/pull-request llvm/llvm-project#101482