llvm / llvm-project

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

clang++ segment fault/assertion in clang::Sema::ActOnTagFinishDefinition #48526

Open llvmbot opened 3 years ago

llvmbot commented 3 years ago
Bugzilla Link 49182
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @fhahn,@zygoloid

Extended Description

Following segment fault in frontend has borken many downstream applications (e.g., creduce).

It is tested on version 11.0.0 and 6.0.0.

$ ./clang++ test.cc test.cc:4:28: error: C++ requires a type specifier for all declarations typeof({union {x = 0}}) ^ test.cc:4:33: error: expected ';' at end of declaration list typeof({union {x = 0}}) ^ ; test.cc:4:34: error: expected ';' after union typeof({union {x = 0}}) ^ ; test.cc:5:9: error: expected member name or ';' after declaration specifiers } ^ test.cc:5:10: error: expected ';' after union } ^ ;

​0 0x00007fc75ec6d0ea llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/usr/lib/llvm-6.0/bin/../lib/libLLVM-6.0.so.1+0x81e0ea)

​1 0x00007fc75ec6b366 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-6.0/bin/../lib/libLLVM-6.0.so.1+0x81c366)

​2 0x00007fc75ec6b49b (/usr/lib/llvm-6.0/bin/../lib/libLLVM-6.0.so.1+0x81c49b)

​3 0x00007fc761efd8a0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x128a0)

​4 0x0000557648d5f4b7 clang::Sema::ActOnTagFinishDefinition(clang::Scope, clang::Decl, clang::SourceRange) (/usr/lib/llvm-6.0/bin/clang+0xf994b7)

​5 0x0000557648b0cac0 clang::Parser::ParseCXXMemberSpecification(clang::SourceLocation, clang::SourceLocation, clang::Parser::ParsedAttributesWithRange&, unsigned int, clang::Decl*) (/usr/lib/llvm-6.0/bin/clang+0xd46ac0)

​6 0x0000557648b0f6c5 clang::Parser::ParseClassSpecifier(clang::tok::TokenKind, clang::SourceLocation, clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, bool, clang::Parser::DeclSpecContext, clang::Parser::ParsedAttributesWithRange&) (/usr/lib/llvm-6.0/bin/clang+0xd496c5)

​7 0x0000557648ae9569 clang::Parser::ParseDeclarationSpecifiers(clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext, clang::Parser::LateParsedAttrList*) (/usr/lib/llvm-6.0/bin/clang+0xd23569)

​8 0x0000557648af7ec1 clang::Parser::ParseSimpleDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::Parser::ParsedAttributesWithRange&, bool, clang::Parser::ForRangeInit*) (/usr/lib/llvm-6.0/bin/clang+0xd31ec1)

​9 0x0000557648af825b clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::Parser::ParsedAttributesWithRange&) (/usr/lib/llvm-6.0/bin/clang+0xd3225b)

​10 0x0000557648b5cd15 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt, 32u>&, clang::Parser::AllowedConstructsKind, clang::SourceLocation, clang::Parser::ParsedAttributesWithRange&) (/usr/lib/llvm-6.0/bin/clang+0xd96d15)

​11 0x0000557648b5d3c8 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt, 32u>&, clang::Parser::AllowedConstructsKind, clang::SourceLocation) (/usr/lib/llvm-6.0/bin/clang+0xd973c8)

​12 0x0000557648b60e70 clang::Parser::ParseCompoundStatementBody(bool) (/usr/lib/llvm-6.0/bin/clang+0xd9ae70)

​13 0x0000557648b63559 clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/usr/lib/llvm-6.0/bin/clang+0xd9d559)

​14 0x0000557648ad394b clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) (/usr/lib/llvm-6.0/bin/clang+0xd0d94b)

​15 0x0000557648af4fdf clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::SourceLocation, clang::Parser::ForRangeInit) (/usr/lib/llvm-6.0/bin/clang+0xd2efdf)

​16 0x0000557648acf0d1 clang::Parser::ParseDeclOrFunctionDefInternal(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/usr/lib/llvm-6.0/bin/clang+0xd090d1)

​17 0x0000557648acf729 (/usr/lib/llvm-6.0/bin/clang+0xd09729)

​18 0x0000557648ad59e5 clang::Parser::ParseExternalDeclaration(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec*) (/usr/lib/llvm-6.0/bin/clang+0xd0f9e5)

​19 0x0000557648ad69d9 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&) (/usr/lib/llvm-6.0/bin/clang+0xd109d9)

​20 0x0000557648ad6ca0 clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr&) (/usr/lib/llvm-6.0/bin/clang+0xd10ca0)

​21 0x0000557648acb7fb clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/llvm-6.0/bin/clang+0xd057fb)

​22 0x000055764881ea27 clang::CodeGenAction::ExecuteAction() (/usr/lib/llvm-6.0/bin/clang+0xa58a27)

​23 0x0000557648655b56 clang::FrontendAction::Execute() (/usr/lib/llvm-6.0/bin/clang+0x88fb56)

​24 0x000055764861fbfc clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm-6.0/bin/clang+0x859bfc)

​25 0x0000557648700f13 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-6.0/bin/clang+0x93af13)

​26 0x000055764821ee08 cc1_main(llvm::ArrayRef<char const>, char const, void*) (/usr/lib/llvm-6.0/bin/clang+0x458e08)

​27 0x000055764820d807 main (/usr/lib/llvm-6.0/bin/clang+0x447807)

​28 0x00007fc75d50eb97 __libc_start_main /build/glibc-2ORdQG/glibc-2.27/csu/../csu/libc-start.c:344:0

​29 0x000055764821c8aa _start (/usr/lib/llvm-6.0/bin/clang+0x4568aa)

Stack dump:

  1. Program arguments: /usr/lib/llvm-6.0/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.cc -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/lib/llvm-6.0/lib/clang/6.0.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/backward -internal-isystem /usr/include/clang/6.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-6.0/lib/clang/6.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /data/xxx/docker_share/clang -ferror-limit 19 -fmessage-length 119 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/test-0010a6.o -x c++ test.cc
  2. test.cc:7:1: current parser token '}'
  3. test.cc:1:12: parsing function body 'main'
  4. test.cc:1:12: in compound statement ('{}')
  5. test.cc:2:5: parsing struct/union/class body '' clang: error: unable to execute command: Segmentation fault (core dumped) clang: error: clang frontend command failed due to signal (use -v to see invocation) clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin clang: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script. clang: note: diagnostic msg:

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed source(s) and associated run script(s) are located at: clang: note: diagnostic msg: /tmp/test-ab98fb.cpp clang: note: diagnostic msg: /tmp/test-ab98fb.sh clang: note: diagnostic msg:


$ cat /tmp/test-ab98fb.cpp

1 ""

1 "test.cc"

int main() { union { union { typeof({union {x = 0}}) } } }

$ cat /tmp/test-ab98fb.sh

Crash reproducer for clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)

Driver args: "--driver-mode=g++" "test.cc"

Original command: "/usr/lib/llvm-6.0/bin/clang" "-cc1" "-triple" "x86_64-pc-linux-gnu" "-emit-obj" "-mrelax-all" "-disable-free" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "test.cc" "-mrelocation-model" "static" "-mthread-model" "posix" "-mdisable-fp-elim" "-fmath-errno" "-masm-verbose" "-mconstructor-aliases" "-munwind-tables" "-fuse-init-array" "-target-cpu" "x86-64" "-dwarf-column-info" "-debugger-tuning=gdb" "-resource-dir" "/usr/lib/llvm-6.0/lib/clang/6.0.0" "-internal-isystem" "/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0" "-internal-isystem" "/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0" "-internal-isystem" "/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0" "-internal-isystem" "/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/backward" "-internal-isystem" "/usr/include/clang/6.0.0/include/" "-internal-isystem" "/usr/local/include" "-internal-isystem" "/usr/lib/llvm-6.0/lib/clang/6.0.0/include" "-internal-externc-isystem" "/usr/include/x86_64-linux-gnu" "-internal-externc-isystem" "/include" "-internal-externc-isystem" "/usr/include" "-fdeprecated-macro" "-fdebug-compilation-dir" "/data/xxx/docker_share/clang" "-ferror-limit" "19" "-fmessage-length" "119" "-fobjc-runtime=gcc" "-fcxx-exceptions" "-fexceptions" "-fdiagnostics-show-option" "-fcolor-diagnostics" "-o" "/tmp/test-0010a6.o" "-x" "c++" "test.cc"

"/usr/lib/llvm-6.0/bin/clang" "-cc1" "-triple" "x86_64-pc-linux-gnu" "-emit-obj" "-mrelax-all" "-disable-free" "-disable-llvm-verifier" "-discard-value-names" "-main-file-name" "test.cc" "-mrelocation-model" "static" "-mthread-model" "posix" "-mdisable-fp-elim" "-fmath-errno" "-masm-verbose" "-mconstructor-aliases" "-munwind-tables" "-fuse-init-array" "-target-cpu" "x86-64" "-dwarf-column-info" "-debugger-tuning=gdb" "-fdeprecated-macro" "-ferror-limit" "19" "-fmessage-length" "119" "-fobjc-runtime=gcc" "-fcxx-exceptions" "-fexceptions" "-fdiagnostics-show-option" "-fcolor-diagnostics" "-x" "c++" "test-ab98fb.cpp"

fhahn commented 3 years ago

Crashes on trunk as well: https://godbolt.org/z/cx95K6

  1. Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang++ -g -o ./output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics
  2. :7:1: current parser token '}'
  3. :1:12: parsing function body 'main'
  4. :1:12: in compound statement ('{}')
  5. :2:5: parsing struct/union/class body ''

    ​0 0x000056095414fbec llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x303ebec)

    ​1 0x000056095414d994 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x303c994)

    ​2 0x000056095414dc15 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x303cc15)

    ​3 0x00005609540b45b8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0

    ​4 0x00007f77eb09a3c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x153c0)

    ​5 0x0000560955fbd1da clang::Sema::ActOnTagFinishDefinition(clang::Scope, clang::Decl, clang::SourceRange) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4eac1da)

    ​6 0x0000560955dd7c0e clang::Parser::ParseCXXMemberSpecification(clang::SourceLocation, clang::SourceLocation, clang::Parser::ParsedAttributesWithRange&, unsigned int, clang::Decl*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4cc6c0e)

    ​7 0x0000560955dd9b8b clang::Parser::ParseClassSpecifier(clang::tok::TokenKind, clang::SourceLocation, clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, bool, clang::Parser::DeclSpecContext, clang::Parser::ParsedAttributesWithRange&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4cc8b8b)

    ​8 0x0000560955dbaaee clang::Parser::ParseDeclarationSpecifiers(clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext, clang::Parser::LateParsedAttrList*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4ca9aee)

    ​9 0x0000560955dc050d clang::Parser::ParseSimpleDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::Parser::ParsedAttributesWithRange&, bool, clang::Parser::ForRangeInit, clang::SourceLocation) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4caf50d)

    ​10 0x0000560955dc0882 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::Parser::ParsedAttributesWithRange&, clang::SourceLocation*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4caf882)

    ​11 0x0000560955e3ab59 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation, clang::Parser::ParsedAttributesWithRange&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4d29b59)

    ​12 0x0000560955e3adcd clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4d29dcd)

    ​13 0x0000560955e3b8e1 clang::Parser::ParseCompoundStatementBody(bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4d2a8e1)

    ​14 0x0000560955e3f103 clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4d2e103)

    ​15 0x0000560955d97267 clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4c86267)

    ​16 0x0000560955dbf885 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::SourceLocation, clang::Parser::ForRangeInit) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4cae885)

    ​17 0x0000560955d929c1 clang::Parser::ParseDeclOrFunctionDefInternal(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4c819c1)

    ​18 0x0000560955d930f1 clang::Parser::ParseDeclarationOrFunctionDefinition(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (.part.241) Parser.cpp:0:0

    ​19 0x0000560955d99079 clang::Parser::ParseExternalDeclaration(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4c88079)

    ​20 0x0000560955d9a5f9 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4c895f9)

    ​21 0x0000560955d9abe5 clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4c89be5)

    ​22 0x0000560955d8d823 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4c7c823)

    ​23 0x0000560954fc5932 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3eb4932)

    ​24 0x00005609549c49b1 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x38b39b1)

    ​25 0x000056095495b5f3 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x384a5f3)

    ​26 0x0000560954a8ba13 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x397aa13)

    ​27 0x00005609520f791c cc1_main(llvm::ArrayRef<char const>, char const, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xfe691c)

    ​28 0x00005609520f3a3d ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0

    ​29 0x00005609548120f5 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional >, std::__cxx11::basic_string<char, std::char_traits, std::allocator >, bool) const::'lambda'()>(long) Job.cpp:0:0

    ​30 0x00005609540b4693 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x2fa3693)

    ​31 0x0000560954812a28 clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional >, std::__cxx11::basic_string<char, std::char_traits, std::allocator >, bool) const (.part.165) Job.cpp:0:0

    ​32 0x00005609547ebb29 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x36dab29)

    ​33 0x00005609547ecd4f clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x36dbd4f)

    ​34 0x00005609547fca95 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x36eba95)

    ​35 0x0000560952011dd4 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0xf00dd4)

    ​36 0x00007f77eab4b0b3 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b3)

    ​37 0x00005609520f35ba _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0xfe25ba)

    clang-13: error: clang frontend command failed with exit code 139 (use -v to see invocation) Compiler returned: 139 Compiler Explorer uses cookies and other related techs to serve you

llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-frontend

llvmbot commented 1 year ago

@llvm/issue-subscribers-c-1

AaronBallman commented 1 year ago

The issue reproduces as of Clang 16.0.0: https://gcc.godbolt.org/z/1Mzv9oeE4

Note, it does not reproduce in C, only in C++ mode.

shafik commented 1 year ago

Not exact backtrace but maybe duplicate of: https://github.com/llvm/llvm-project/issues/44285