Skip to content

clang assertion `NumInitElements <= NumArrayElements' failed in CGExprAgg.cpp with flexible array member in template struct constructor #172974

@AbyssStaror

Description

@AbyssStaror

Link for quick verification: https://godbolt.org/z/zG77Wvv83

template <class T>
struct C {
    C(T t) : b{t} {}
    int a;
    T b[];
};

int main() {
    C<int> c(1);
}

Go back to clang 6.0.0 (assertions) and compiler crashes with ICE when compiling a C++ template struct that contains a flexible array member (FAM) initialized in the constructor. The crash occurs in CGExprAgg.cpp during code generation.

Stack dump

clang++: /root/llvm-project/llvm/tools/clang/lib/CodeGen/CGExprAgg.cpp:568: 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++ -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>:4:5: Generating code for declaration 'C<int>::C'
 #0 0x000000000429ea28 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x429ea28)
 #1 0x000000000429b8d4 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x429b8d4)
 #2 0x000000000429be54 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x429be54)
 #3 0x00000000041e0b98 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #4 0x0000776c69242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #5 0x0000776c692969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #6 0x0000776c69242476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #7 0x0000776c692287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #8 0x0000776c6922871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #9 0x0000776c69239e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#10 0x00000000046240c7 (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x46240c7)
#11 0x000000000462a14a (anonymous namespace)::AggExprEmitter::VisitCXXParenListOrInitListExpr(clang::Expr*, llvm::ArrayRef<clang::Expr*>, clang::FieldDecl*, clang::Expr*) CGExprAgg.cpp:0:0
#12 0x00000000046284bd (anonymous namespace)::AggExprEmitter::Visit(clang::Expr*) CGExprAgg.cpp:0:0
#13 0x0000000004628d2c clang::CodeGen::CodeGenFunction::EmitAggExpr(clang::Expr const*, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4628d2c)
#14 0x0000000004b195e6 clang::CodeGen::CodeGenFunction::EmitInitializerForField(clang::FieldDecl*, clang::CodeGen::LValue, clang::Expr*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4b195e6)
#15 0x0000000004b19c03 EmitMemberInitializer(clang::CodeGen::CodeGenFunction&, clang::CXXRecordDecl const*, clang::CXXCtorInitializer*, clang::CXXConstructorDecl const*, clang::CodeGen::FunctionArgList&) CGClass.cpp:0:0
#16 0x0000000004b251b1 clang::CodeGen::CodeGenFunction::EmitCtorPrologue(clang::CXXConstructorDecl const*, clang::CXXCtorType, clang::CodeGen::FunctionArgList&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4b251b1)
#17 0x0000000004b27452 clang::CodeGen::CodeGenFunction::EmitConstructorBody(clang::CodeGen::FunctionArgList&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4b27452)
#18 0x000000000482814d clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x482814d)
#19 0x0000000004ade58f clang::CodeGen::CodeGenModule::codegenCXXStructor(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ade58f)
#20 0x0000000004909060 (anonymous namespace)::ItaniumCXXABI::emitCXXStructor(clang::GlobalDecl) ItaniumCXXABI.cpp:0:0
#21 0x000000000488dad6 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x488dad6)
#22 0x000000000489adac clang::CodeGen::CodeGenModule::EmitDeferred() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x489adac)
#23 0x000000000489acc8 clang::CodeGen::CodeGenModule::EmitDeferred() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x489acc8)
#24 0x000000000489d453 clang::CodeGen::CodeGenModule::Release() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x489d453)
#25 0x0000000004bf847e (anonymous namespace)::CodeGeneratorImpl::HandleTranslationUnit(clang::ASTContext&) ModuleBuilder.cpp:0:0
#26 0x0000000004bf5e95 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bf5e95)
#27 0x000000000698304c clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x698304c)
#28 0x0000000004bf6678 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bf6678)
#29 0x0000000004eeb2e5 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4eeb2e5)
#30 0x0000000004e6b59e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4e6b59e)
#31 0x0000000004fe538d clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4fe538d)
#32 0x0000000000dd9ee2 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdd9ee2)
#33 0x0000000000dd090a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) driver.cpp:0:0
#34 0x0000000000dd0a8d int llvm::function_ref<int (llvm::SmallVectorImpl<char const*>&)>::callback_fn<clang_main(int, char**, llvm::ToolContext const&)::'lambda'(llvm::SmallVectorImpl<char const*>&)>(long, llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#35 0x0000000004c68359 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
#36 0x00000000041e1034 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x41e1034)
#37 0x0000000004c68986 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
#38 0x0000000004c2a002 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c2a002)
#39 0x0000000004c2afae 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+++0x4c2afae)
#40 0x0000000004c32285 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c32285)
#41 0x0000000000dd62b1 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdd62b1)
#42 0x0000000000c81394 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc81394)
#43 0x0000776c69229d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#44 0x0000776c69229e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#45 0x0000000000dd03a5 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xdd03a5)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions