Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang] Assertion `NumInitElements <= NumArrayElements' failed. #113187

Closed
yijan4845 opened this issue Oct 21, 2024 · 1 comment · Fixed by #113336
Closed

[clang] Assertion `NumInitElements <= NumArrayElements' failed. #113187

yijan4845 opened this issue Oct 21, 2024 · 1 comment · Fixed by #113336

Comments

@yijan4845
Copy link

This testcase is generated by a fuzzer.

Compiler Explorer: https://godbolt.org/z/ve9Yox5q5

This invalid code will crash on Clang Assertion Trunk:

union { char a[]; } z = {};
union { char a[]; } z0 = {z.a[0]};
char keep(){
	return z0.a[0];
}

It seems that this goes back to clang-15.

Stack dump:

clang++: /root/llvm-project/clang/lib/CodeGen/CGExprAgg.cpp:512: void {anonymous}::AggExprEmitter::EmitArrayInit(clang::CodeGen::Address, llvm::ArrayType*, clang::QualType, clang::Expr*, llvm::ArrayRef<clang::Expr*>, clang::Expr*): Assertion `NumInitElements <= NumArrayElements' failed.
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 -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics <source>
1.	<eof> parser at end of file
2.	Per-file LLVM IR generation
3.	<source>:2:21: Generating code for declaration 'z0'
 #0 0x0000000003bba5e8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bba5e8)
 #1 0x0000000003bb82ac llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bb82ac)
 #2 0x0000000003b05918 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x000076e422c42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x000076e422c969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x000076e422c42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x000076e422c287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x000076e422c2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x000076e422c39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x00000000043c1c64 (anonymous namespace)::AggExprEmitter::EmitArrayInit(clang::CodeGen::Address, llvm::ArrayType*, clang::QualType, clang::Expr*, llvm::ArrayRef<clang::Expr*>, clang::Expr*) CGExprAgg.cpp:0:0
#10 0x00000000043c750f (anonymous namespace)::AggExprEmitter::VisitCXXParenListOrInitListExpr(clang::Expr*, llvm::ArrayRef<clang::Expr*>, clang::FieldDecl*, clang::Expr*) CGExprAgg.cpp:0:0
#11 0x00000000043c5900 (anonymous namespace)::AggExprEmitter::Visit(clang::Expr*) CGExprAgg.cpp:0:0
#12 0x00000000043c612c clang::CodeGen::CodeGenFunction::EmitAggExpr(clang::Expr const*, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43c612c)
#13 0x00000000043c8678 (anonymous namespace)::AggExprEmitter::VisitCXXParenListOrInitListExpr(clang::Expr*, llvm::ArrayRef<clang::Expr*>, clang::FieldDecl*, clang::Expr*) CGExprAgg.cpp:0:0
#14 0x00000000043c5900 (anonymous namespace)::AggExprEmitter::Visit(clang::Expr*) CGExprAgg.cpp:0:0
#15 0x00000000043c612c clang::CodeGen::CodeGenFunction::EmitAggExpr(clang::Expr const*, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43c612c)
#16 0x000000000436f4d4 clang::CodeGen::CodeGenFunction::EmitCXXGlobalVarDeclInit(clang::VarDecl const&, llvm::GlobalVariable*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x436f4d4)
#17 0x0000000004370050 clang::CodeGen::CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function*, clang::VarDecl const*, llvm::GlobalVariable*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4370050)
#18 0x0000000004370451 clang::CodeGen::CodeGenModule::EmitCXXGlobalVarDeclInitFunc(clang::VarDecl const*, llvm::GlobalVariable*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4370451)
#19 0x0000000003fe03a3 clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3fe03a3)
#20 0x0000000004003129 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4003129)
#21 0x000000000400f0dd clang::CodeGen::CodeGenModule::EmitDeferred() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x400f0dd)
#22 0x00000000040111f3 clang::CodeGen::CodeGenModule::Release() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x40111f3)
#23 0x0000000004531b1e (anonymous namespace)::CodeGeneratorImpl::HandleTranslationUnit(clang::ASTContext&) ModuleBuilder.cpp:0:0
#24 0x000000000452f775 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x452f775)
#25 0x000000000667a08c clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x667a08c)
#26 0x000000000452ff28 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x452ff28)
#27 0x00000000047eb1e9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47eb1e9)
#28 0x0000000004769cde clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4769cde)
#29 0x00000000048d08be clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x48d08be)
#30 0x0000000000cde37f cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcde37f)
#31 0x0000000000cd607a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#32 0x0000000004573c49 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
#33 0x0000000003b05dc4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3b05dc4)
#34 0x000000000457423f 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
#35 0x0000000004539b3d clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4539b3d)
#36 0x000000000453ac2d 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+++0x453ac2d)
#37 0x0000000004542555 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4542555)
#38 0x0000000000cdb23f clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcdb23f)
#39 0x0000000000baa334 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xbaa334)
#40 0x000076e422c29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#41 0x000076e422c29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#42 0x0000000000cd5b2e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcd5b2e)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2024

@llvm/issue-subscribers-clang-codegen

Author: Yihan Yang (yijan4845)

**This testcase is generated by a fuzzer.**

Compiler Explorer: https://godbolt.org/z/ve9Yox5q5

This invalid code will crash on Clang Assertion Trunk:

union { char a[]; } z = {};
union { char a[]; } z0 = {z.a[0]};
char keep(){
	return z0.a[0];
}

It seems that this goes back to clang-15.

Stack dump:

clang++: /root/llvm-project/clang/lib/CodeGen/CGExprAgg.cpp:512: void {anonymous}::AggExprEmitter::EmitArrayInit(clang::CodeGen::Address, llvm::ArrayType*, clang::QualType, clang::Expr*, llvm::ArrayRef&lt;clang::Expr*&gt;, clang::Expr*): Assertion `NumInitElements &lt;= NumArrayElements' failed.
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 -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics &lt;source&gt;
1.	&lt;eof&gt; parser at end of file
2.	Per-file LLVM IR generation
3.	&lt;source&gt;:2:21: Generating code for declaration 'z0'
 #<!-- -->0 0x0000000003bba5e8 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bba5e8)
 #<!-- -->1 0x0000000003bb82ac llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bb82ac)
 #<!-- -->2 0x0000000003b05918 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #<!-- -->3 0x000076e422c42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #<!-- -->4 0x000076e422c969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #<!-- -->5 0x000076e422c42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #<!-- -->6 0x000076e422c287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #<!-- -->7 0x000076e422c2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #<!-- -->8 0x000076e422c39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #<!-- -->9 0x00000000043c1c64 (anonymous namespace)::AggExprEmitter::EmitArrayInit(clang::CodeGen::Address, llvm::ArrayType*, clang::QualType, clang::Expr*, llvm::ArrayRef&lt;clang::Expr*&gt;, clang::Expr*) CGExprAgg.cpp:0:0
#<!-- -->10 0x00000000043c750f (anonymous namespace)::AggExprEmitter::VisitCXXParenListOrInitListExpr(clang::Expr*, llvm::ArrayRef&lt;clang::Expr*&gt;, clang::FieldDecl*, clang::Expr*) CGExprAgg.cpp:0:0
#<!-- -->11 0x00000000043c5900 (anonymous namespace)::AggExprEmitter::Visit(clang::Expr*) CGExprAgg.cpp:0:0
#<!-- -->12 0x00000000043c612c clang::CodeGen::CodeGenFunction::EmitAggExpr(clang::Expr const*, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43c612c)
#<!-- -->13 0x00000000043c8678 (anonymous namespace)::AggExprEmitter::VisitCXXParenListOrInitListExpr(clang::Expr*, llvm::ArrayRef&lt;clang::Expr*&gt;, clang::FieldDecl*, clang::Expr*) CGExprAgg.cpp:0:0
#<!-- -->14 0x00000000043c5900 (anonymous namespace)::AggExprEmitter::Visit(clang::Expr*) CGExprAgg.cpp:0:0
#<!-- -->15 0x00000000043c612c clang::CodeGen::CodeGenFunction::EmitAggExpr(clang::Expr const*, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43c612c)
#<!-- -->16 0x000000000436f4d4 clang::CodeGen::CodeGenFunction::EmitCXXGlobalVarDeclInit(clang::VarDecl const&amp;, llvm::GlobalVariable*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x436f4d4)
#<!-- -->17 0x0000000004370050 clang::CodeGen::CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function*, clang::VarDecl const*, llvm::GlobalVariable*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4370050)
#<!-- -->18 0x0000000004370451 clang::CodeGen::CodeGenModule::EmitCXXGlobalVarDeclInitFunc(clang::VarDecl const*, llvm::GlobalVariable*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4370451)
#<!-- -->19 0x0000000003fe03a3 clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3fe03a3)
#<!-- -->20 0x0000000004003129 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4003129)
#<!-- -->21 0x000000000400f0dd clang::CodeGen::CodeGenModule::EmitDeferred() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x400f0dd)
#<!-- -->22 0x00000000040111f3 clang::CodeGen::CodeGenModule::Release() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x40111f3)
#<!-- -->23 0x0000000004531b1e (anonymous namespace)::CodeGeneratorImpl::HandleTranslationUnit(clang::ASTContext&amp;) ModuleBuilder.cpp:0:0
#<!-- -->24 0x000000000452f775 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x452f775)
#<!-- -->25 0x000000000667a08c clang::ParseAST(clang::Sema&amp;, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x667a08c)
#<!-- -->26 0x000000000452ff28 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x452ff28)
#<!-- -->27 0x00000000047eb1e9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47eb1e9)
#<!-- -->28 0x0000000004769cde clang::CompilerInstance::ExecuteAction(clang::FrontendAction&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4769cde)
#<!-- -->29 0x00000000048d08be clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x48d08be)
#<!-- -->30 0x0000000000cde37f cc1_main(llvm::ArrayRef&lt;char const*&gt;, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcde37f)
#<!-- -->31 0x0000000000cd607a ExecuteCC1Tool(llvm::SmallVectorImpl&lt;char const*&gt;&amp;, llvm::ToolContext const&amp;) driver.cpp:0:0
#<!-- -->32 0x0000000004573c49 void llvm::function_ref&lt;void ()&gt;::callback_fn&lt;clang::driver::CC1Command::Execute(llvm::ArrayRef&lt;std::optional&lt;llvm::StringRef&gt;&gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;&gt;*, bool*) const::'lambda'()&gt;(long) Job.cpp:0:0
#<!-- -->33 0x0000000003b05dc4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref&lt;void ()&gt;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3b05dc4)
#<!-- -->34 0x000000000457423f clang::driver::CC1Command::Execute(llvm::ArrayRef&lt;std::optional&lt;llvm::StringRef&gt;&gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;&gt;*, bool*) const (.part.0) Job.cpp:0:0
#<!-- -->35 0x0000000004539b3d clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&amp;, clang::driver::Command const*&amp;, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4539b3d)
#<!-- -->36 0x000000000453ac2d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&amp;, llvm::SmallVectorImpl&lt;std::pair&lt;int, clang::driver::Command const*&gt;&gt;&amp;, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x453ac2d)
#<!-- -->37 0x0000000004542555 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&amp;, llvm::SmallVectorImpl&lt;std::pair&lt;int, clang::driver::Command const*&gt;&gt;&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4542555)
#<!-- -->38 0x0000000000cdb23f clang_main(int, char**, llvm::ToolContext const&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcdb23f)
#<!-- -->39 0x0000000000baa334 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xbaa334)
#<!-- -->40 0x000076e422c29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#<!-- -->41 0x000076e422c29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#<!-- -->42 0x0000000000cd5b2e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcd5b2e)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134

HerrCai0907 added a commit that referenced this issue Oct 23, 2024
…e with flexible array init (#113336)

Fixes: #113187
Avoid to create init function since clang does not support global
variable with flexible array init.
It will cause assertion failure later.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants