llvm / llvm-project

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

clang-check crashes with option --syntax-tree-dump for "default" function #97779

Open nickhuang99 opened 4 months ago

nickhuang99 commented 4 months ago

clang-check always crash for option --syntax-tree-dump when a function has key word like default or delete

`$ cat /tmp/test7.cpp 
struct Foo {
Foo() = default;
};
$ clang-check --syntax-tree-dump /tmp/test7.cpp
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "/tmp/test7.cpp"
No compilation database found in /tmp or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
 #0 0x00007449e5e3fd01 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xe3fd01)
 #1 0x00007449e5e3da0c llvm::sys::RunSignalHandlers() (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xe3da0c)
 #2 0x00007449e5e40236 (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xe40236)
 #3 0x00007449e4842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007449ee0b6abd (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x26b6abd)
 #5 0x00007449ee0bb1e2 (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x26bb1e2)
 #6 0x00007449ee08e683 (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x268e683)
 #7 0x00007449ee088e3b (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2688e3b)
 #8 0x00007449ee08c45b (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x268c45b)
 #9 0x00007449ee088c9f (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2688c9f)
#10 0x00007449ee09118b (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x269118b)
#11 0x00007449ee088f76 (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2688f76)
#12 0x00007449ee08777b clang::syntax::buildSyntaxTree(clang::syntax::Arena&, clang::ASTContext&) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x268777b)
#13 0x00000000004081ad (/usr/lib/llvm-14/bin/clang-check+0x4081ad)
#14 0x00007449ec404824 clang::ParseAST(clang::Sema&, bool, bool) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0xa04824)
#15 0x00007449edf14b57 clang::FrontendAction::Execute() (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2514b57)
#16 0x00007449ede6c3a6 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x246c3a6)
#17 0x00007449ee112423 clang::tooling::FrontendActionFactory::runInvocation(std::shared_ptr<clang::CompilerInvocation>, clang::FileManager*, std::shared_ptr<clang::PCHContainerOperations>, clang::DiagnosticConsumer*) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2712423)
#18 0x00007449ee112175 clang::tooling::ToolInvocation::runInvocation(char const*, clang::driver::Compilation*, std::shared_ptr<clang::CompilerInvocation>, std::shared_ptr<clang::PCHContainerOperations>) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2712175)
#19 0x00007449ee111183 clang::tooling::ToolInvocation::run() (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2711183)
#20 0x00007449ee113d0e clang::tooling::ClangTool::run(clang::tooling::ToolAction*) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2713d0e)
#21 0x000000000040731e (/usr/lib/llvm-14/bin/clang-check+0x40731e)
#22 0x00007449e4829d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#23 0x00007449e4829e40 call_init ./csu/../csu/libc-start.c:128:20
#24 0x00007449e4829e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#25 0x0000000000405e65 (/usr/lib/llvm-14/bin/clang-check+0x405e65)
Segmentation fault (core dumped)
$ 

`

asl commented 4 months ago

Does the issue reproduce with something less ancient? E.g. LLVM 18?

nickhuang99 commented 4 months ago

Yes, I download latest git repo and it is the same crash. Using debug build, this assert causes core dump: clang/lib/Tooling/Syntax/BuildTree.cpp:1750 const syntax::Token *syntax::TreeBuilder::findToken(SourceLocation L) const { if (L.isInvalid()) return nullptr; auto It = LocationToToken.find(L); assert(It != LocationToToken.end()); return It->second; }

FYI, using usual Release or RelDebInfo build type is a bit misleading as the crash point is when the SourceLocation is invalid or something like token not found by location etc. In short, only Debug build gives this issue at first place.

nickhuang99 commented 3 months ago

@asl Is this libLLVM.so.19.0git the evidence you are requiring to have? Currently I only have ubuntu 22.04 to test, but my gdb shows it is a kind of platform independent issue. i.e. BuildTree.cpp is using a different alglorithm than Parser.cpp has. Maybe this is the purpose to have an independent way to check clang parsing.

$ cat /tmp/test7.cpp 
struct Foo {
Foo() = default;
};
$ clang-check --syntax-tree-dump /tmp/test7.cpp --
 #0 0x0000735e578c51a0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/nick/workspace/llvm-project/install-release/bin/../lib/libLLVM.so.19.0git+0x14c51a0)
 #1 0x0000735e578c275e SignalHandler(int) Signals.cpp:0:0
 #2 0x0000735e56042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x0000735e5e684d84 clang::syntax::TreeBuilder::getDeclarationRange(clang::Decl*) BuildTree.cpp:0:0
 #4 0x0000735e5e687107 bool (anonymous namespace)::BuildTreeVisitor::processDeclaratorAndDeclaration<clang::DeclaratorDecl>(clang::DeclaratorDecl*) BuildTree.cpp:0:0
 #5 0x0000735e5e68f2cb clang::RecursiveASTVisitor<(anonymous namespace)::BuildTreeVisitor>::TraverseDeclContextHelper(clang::DeclContext*) (.part.0) BuildTree.cpp:0:0
 #6 0x0000735e5e68df63 clang::RecursiveASTVisitor<(anonymous namespace)::BuildTreeVisitor>::TraverseDecl(clang::Decl*) (.part.0) BuildTree.cpp:0:0
 #7 0x0000735e5e68f2cb clang::RecursiveASTVisitor<(anonymous namespace)::BuildTreeVisitor>::TraverseDeclContextHelper(clang::DeclContext*) (.part.0) BuildTree.cpp:0:0
 #8 0x0000735e5e6a6248 clang::RecursiveASTVisitor<(anonymous namespace)::BuildTreeVisitor>::TraverseTranslationUnitDecl(clang::TranslationUnitDecl*) BuildTree.cpp:0:0
 #9 0x0000735e5e6a1495 clang::syntax::buildSyntaxTree(clang::syntax::Arena&, clang::syntax::TokenBufferTokenManager&, clang::ASTContext&) (/home/nick/workspace/llvm-project/install-release/bin/../lib/libclang-cpp.so.19.0git+0x3aa1495)
#10 0x00005fb397ec62d7 (anonymous namespace)::DumpSyntaxTree::CreateASTConsumer(clang::CompilerInstance&, llvm::StringRef)::Consumer::HandleTranslationUnit(clang::ASTContext&) ClangCheck.cpp:0:0
#11 0x0000735e5c05bffc clang::ParseAST(clang::Sema&, bool, bool) (/home/nick/workspace/llvm-project/install-release/bin/../lib/libclang-cpp.so.19.0git+0x145bffc)
#12 0x0000735e5e530419 clang::FrontendAction::Execute() (/home/nick/workspace/llvm-project/install-release/bin/../lib/libclang-cpp.so.19.0git+0x3930419)
#13 0x0000735e5e4a0839 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/home/nick/workspace/llvm-project/install-release/bin/../lib/libclang-cpp.so.19.0git+0x38a0839)
#14 0x0000735e5e73ae44 clang::tooling::FrontendActionFactory::runInvocation(std::shared_ptr<clang::CompilerInvocation>, clang::FileManager*, std::shared_ptr<clang::PCHContainerOperations>, clang::DiagnosticConsumer*) (/home/nick/workspace/llvm-project/install-release/bin/../lib/libclang-cpp.so.19.0git+0x3b3ae44)
#15 0x0000735e5e734905 clang::tooling::ToolInvocation::runInvocation(char const*, clang::driver::Compilation*, std::shared_ptr<clang::CompilerInvocation>, std::shared_ptr<clang::PCHContainerOperations>) (/home/nick/workspace/llvm-project/install-release/bin/../lib/libclang-cpp.so.19.0git+0x3b34905)
#16 0x0000735e5e736a1b clang::tooling::ToolInvocation::run() (/home/nick/workspace/llvm-project/install-release/bin/../lib/libclang-cpp.so.19.0git+0x3b36a1b)
#17 0x0000735e5e738bcf clang::tooling::ClangTool::run(clang::tooling::ToolAction*) (/home/nick/workspace/llvm-project/install-release/bin/../lib/libclang-cpp.so.19.0git+0x3b38bcf)
#18 0x00005fb397ec7353 main (../install-release/bin/clang-check+0x7353)
#19 0x0000735e56029d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#20 0x0000735e56029e40 call_init ./csu/../csu/libc-start.c:128:20
#21 0x0000735e56029e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#22 0x00005fb397ec56b5 _start (../install-release/bin/clang-check+0x56b5)
Segmentation fault (core dumped)