llvm / llvm-project

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

[Clang][C++] Class with no destructor because of error recovery crashes if a derived class is aggregate initialized #97230

Closed MitalAshok closed 2 months ago

MitalAshok commented 3 months ago
struct X {
    ~X() = defaul;
};
struct Y : X {} y1{};

https://godbolt.org/z/877vsf8jb

<source>:2:5: error: initializer on function does not look like a pure-specifier
    2 |     ~X() = defaul;
      |     ^      ~~~~~~
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.  Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++20 -O3 <source>
1.  <source>:4:21: current parser token ';'
 #0 0x0000000003a30b08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3a30b08)
 #1 0x0000000003a2e7ec llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3a2e7ec)
 #2 0x0000000003974058 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x000072549e042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x0000000007664c14 clang::Decl::AccessDeclContextCheck() const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7664c14)
 #5 0x0000000006583d21 clang::Sema::CheckDestructorAccess(clang::SourceLocation, clang::CXXDestructorDecl*, clang::PartialDiagnostic const&, clang::QualType) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6583d21)
 #6 0x0000000006b8d052 checkDestructorReference(clang::QualType, clang::SourceLocation, clang::Sema&) SemaInit.cpp:0:0
 #7 0x0000000006bb2028 (anonymous namespace)::InitListChecker::CheckStructUnionTypes(clang::InitializedEntity const&, clang::InitListExpr*, clang::QualType, llvm::iterator_range<clang::CXXBaseSpecifier const*>, clang::DeclContext::specific_decl_iterator<clang::FieldDecl>, bool, unsigned int&, clang::InitListExpr*, unsigned int&, bool) SemaInit.cpp:0:0
 #8 0x0000000006bb081d (anonymous namespace)::InitListChecker::CheckListElementTypes(clang::InitializedEntity const&, clang::InitListExpr*, clang::QualType&, bool, unsigned int&, clang::InitListExpr*, unsigned int&, bool) (.constprop.0) SemaInit.cpp:0:0
 #9 0x0000000006b9f92a (anonymous namespace)::InitListChecker::CheckExplicitInitList(clang::InitializedEntity const&, clang::InitListExpr*, clang::QualType&, clang::InitListExpr*, bool) (.constprop.0) SemaInit.cpp:0:0
#10 0x0000000006ba032e (anonymous namespace)::InitListChecker::InitListChecker(clang::Sema&, clang::InitializedEntity const&, clang::InitListExpr*, clang::QualType&, bool, bool, bool, llvm::SmallVectorImpl<clang::QualType>*) SemaInit.cpp:0:0
#11 0x0000000006ba240b clang::InitializationSequence::Perform(clang::Sema&, clang::InitializedEntity const&, clang::InitializationKind const&, llvm::MutableArrayRef<clang::Expr*>, clang::QualType*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6ba240b)
#12 0x00000000067507b1 clang::Sema::AddInitializerToDecl(clang::Decl*, clang::Expr*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x67507b1)
#13 0x00000000063e345c clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63e345c)
#14 0x00000000063f239a clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63f239a)
#15 0x00000000063b073e clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63b073e)
#16 0x00000000063b0f9e clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63b0f9e)
#17 0x00000000063b76c7 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63b76c7)
#18 0x00000000063b85af clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63b85af)
#19 0x00000000063abdaa clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63abdaa)
#20 0x000000000431b9c8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x431b9c8)
#21 0x00000000045a28d9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x45a28d9)
#22 0x000000000452869e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x452869e)
#23 0x000000000468ae0e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x468ae0e)
#24 0x0000000000c6f7b5 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc6f7b5)
#25 0x0000000000c689da ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#26 0x000000000435f159 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#27 0x0000000003974504 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3974504)
#28 0x000000000435f74f clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#29 0x0000000004325195 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4325195)
#30 0x0000000004325bfd clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4325bfd)
#31 0x000000000432d615 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x432d615)
#32 0x0000000000c6cb85 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc6cb85)
#33 0x0000000000b45704 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xb45704)
#34 0x000072549e029d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#35 0x000072549e029e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#36 0x0000000000c6848e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc6848e)

This is a crash on invalid code.

Also a fixit for typos of = delete/= default rather than complaining that it doesn't look like = 0 would be nice.

llvmbot commented 3 months ago

@llvm/issue-subscribers-clang-frontend

Author: Mital Ashok (MitalAshok)

```c++ struct X { ~X() = defaul; }; struct Y : X {} y1{}; ``` https://godbolt.org/z/877vsf8jb ``` <source>:2:5: error: initializer on function does not look like a pure-specifier 2 | ~X() = defaul; | ^ ~~~~~~ PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script. Stack dump: 0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++20 -O3 <source> 1. <source>:4:21: current parser token ';' #0 0x0000000003a30b08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3a30b08) #1 0x0000000003a2e7ec llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3a2e7ec) #2 0x0000000003974058 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0 #3 0x000072549e042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520) #4 0x0000000007664c14 clang::Decl::AccessDeclContextCheck() const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7664c14) #5 0x0000000006583d21 clang::Sema::CheckDestructorAccess(clang::SourceLocation, clang::CXXDestructorDecl*, clang::PartialDiagnostic const&, clang::QualType) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6583d21) #6 0x0000000006b8d052 checkDestructorReference(clang::QualType, clang::SourceLocation, clang::Sema&) SemaInit.cpp:0:0 #7 0x0000000006bb2028 (anonymous namespace)::InitListChecker::CheckStructUnionTypes(clang::InitializedEntity const&, clang::InitListExpr*, clang::QualType, llvm::iterator_range<clang::CXXBaseSpecifier const*>, clang::DeclContext::specific_decl_iterator<clang::FieldDecl>, bool, unsigned int&, clang::InitListExpr*, unsigned int&, bool) SemaInit.cpp:0:0 #8 0x0000000006bb081d (anonymous namespace)::InitListChecker::CheckListElementTypes(clang::InitializedEntity const&, clang::InitListExpr*, clang::QualType&, bool, unsigned int&, clang::InitListExpr*, unsigned int&, bool) (.constprop.0) SemaInit.cpp:0:0 #9 0x0000000006b9f92a (anonymous namespace)::InitListChecker::CheckExplicitInitList(clang::InitializedEntity const&, clang::InitListExpr*, clang::QualType&, clang::InitListExpr*, bool) (.constprop.0) SemaInit.cpp:0:0 #10 0x0000000006ba032e (anonymous namespace)::InitListChecker::InitListChecker(clang::Sema&, clang::InitializedEntity const&, clang::InitListExpr*, clang::QualType&, bool, bool, bool, llvm::SmallVectorImpl<clang::QualType>*) SemaInit.cpp:0:0 #11 0x0000000006ba240b clang::InitializationSequence::Perform(clang::Sema&, clang::InitializedEntity const&, clang::InitializationKind const&, llvm::MutableArrayRef<clang::Expr*>, clang::QualType*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6ba240b) #12 0x00000000067507b1 clang::Sema::AddInitializerToDecl(clang::Decl*, clang::Expr*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x67507b1) #13 0x00000000063e345c clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63e345c) #14 0x00000000063f239a clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63f239a) #15 0x00000000063b073e clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63b073e) #16 0x00000000063b0f9e clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63b0f9e) #17 0x00000000063b76c7 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63b76c7) #18 0x00000000063b85af clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63b85af) #19 0x00000000063abdaa clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63abdaa) #20 0x000000000431b9c8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x431b9c8) #21 0x00000000045a28d9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x45a28d9) #22 0x000000000452869e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x452869e) #23 0x000000000468ae0e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x468ae0e) #24 0x0000000000c6f7b5 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc6f7b5) #25 0x0000000000c689da ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0 #26 0x000000000435f159 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0 #27 0x0000000003974504 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3974504) #28 0x000000000435f74f clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0 #29 0x0000000004325195 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4325195) #30 0x0000000004325bfd clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4325bfd) #31 0x000000000432d615 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x432d615) #32 0x0000000000c6cb85 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc6cb85) #33 0x0000000000b45704 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xb45704) #34 0x000072549e029d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90) #35 0x000072549e029e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40) #36 0x0000000000c6848e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc6848e) ``` This is a crash on invalid code. Also a fixit for typos of `= delete`/`= default` rather than complaining that it doesn't look like `= 0` would be nice.
shafik commented 3 months ago

I think the problem is in SemaInit:

https://github.com/llvm/llvm-project/blob/8598bcb9934dca16ea16d87304e00defc85d986c/clang/lib/Sema/SemaInit.cpp#L1988-L1991

It looks like outside of SemaInit after calling SemaRef.LookupDestructor(...) code checks the return value. I think we need something like:

if (!Destructor)
  return false;