llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang crash initializing invalid C union with an empty struct

Open justincady opened this issue 1 year ago • 4 comments

Tested with clang 19. Here's the reproducer:

typedef union {
    struct {} first;
    struct { int x; } second;
} foo;

void bar() {
    foo f = {};
}

Compiler Explorer

Note that first is an empty struct, which I understand to be UB in C:

The presence of a struct-declaration-list in a struct-or-union-specifier declares a new type, within a translation unit. The struct-declaration-list is a sequence of declarations for the members of the structure or union. If the struct-declaration-list does not contain any named members, either directly or via an anonymous structure or anonymous union, the behavior is undefined. The type is incomplete until immediately after the } that terminates the list, and complete thereafter.

https://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf

justincady avatar Jan 13 '25 21:01 justincady

Here is the full stacktrace from the crash (see the Compiler Explorer link) for searching purposes:

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-19.1.0/bin/clang -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-13.2.0 -fcolor-diagnostics -fno-crash-diagnostics <source>
1.	<eof> parser at end of file
2.	<source>:6:6: LLVM IR generation of declaration 'bar'
3.	<source>:6:6: Generating code for declaration 'bar'
 #0 0x00000000036fdb08 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x36fdb08)
 #1 0x00000000036fbc74 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x36fbc74)
 #2 0x0000000003647be8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x000074b6bc242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x000000000720b20e clang::Expr::HasSideEffects(clang::ASTContext const&, bool) const (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x720b20e)
 #5 0x0000000003a13878 (anonymous namespace)::ConstStructBuilder::Build(clang::InitListExpr const*, bool) CGExprConstant.cpp:0:0
 #6 0x0000000003a13a9b (anonymous namespace)::ConstStructBuilder::BuildStruct(clang::CodeGen::ConstantEmitter&, clang::InitListExpr const*, clang::QualType) CGExprConstant.cpp:0:0
 #7 0x0000000003a12e26 clang::StmtVisitorBase<llvm::make_const_ptr, (anonymous namespace)::ConstExprEmitter, llvm::Constant*, clang::QualType>::Visit(clang::Stmt const*, clang::QualType) CGExprConstant.cpp:0:0
 #8 0x0000000003a13df3 clang::CodeGen::ConstantEmitter::tryEmitPrivateForVarInit(clang::VarDecl const&) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3a13df3)
 #9 0x0000000003a13f76 clang::CodeGen::ConstantEmitter::tryEmitAbstractForInitializer(clang::VarDecl const&) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3a13f76)
#10 0x0000000003e101cf clang::CodeGen::CodeGenFunction::EmitAutoVarInit(clang::CodeGen::CodeGenFunction::AutoVarEmission const&) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3e101cf)
#11 0x0000000003e1686c clang::CodeGen::CodeGenFunction::EmitVarDecl(clang::VarDecl const&) (.part.0) CGDecl.cpp:0:0
#12 0x0000000003e16938 clang::CodeGen::CodeGenFunction::EmitDecl(clang::Decl const&) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3e16938)
#13 0x0000000003a2d37f clang::CodeGen::CodeGenFunction::EmitDeclStmt(clang::DeclStmt const&) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3a2d37f)
#14 0x0000000003a415aa clang::CodeGen::CodeGenFunction::EmitSimpleStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3a415aa)
#15 0x0000000003a3a472 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3a3a472)
#16 0x0000000003a41059 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3a41059)
#17 0x0000000003aa74aa clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3aa74aa)
#18 0x0000000003ab4478 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3ab4478)
#19 0x0000000003b0d86b clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3b0d86b)
#20 0x0000000003b09595 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3b09595)
#21 0x0000000003b09ef0 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3b09ef0)
#22 0x0000000003b13177 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#23 0x0000000003fd0a1e (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#24 0x0000000003fc2519 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3fc2519)
#25 0x0000000005f18764 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x5f18764)
#26 0x0000000003fce825 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3fce825)
#27 0x0000000004274201 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x4274201)
#28 0x00000000041f699b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x41f699b)
#29 0x000000000435a843 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x435a843)
#30 0x0000000000c874dc cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0xc874dc)
#31 0x0000000000c8146d ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#32 0x0000000004017309 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 0x0000000003648013 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3648013)
#34 0x0000000004017529 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 0x0000000003fdd947 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3fdd947)
#36 0x0000000003fde2fd clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3fde2fd)
#37 0x0000000003fe7d4c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0x3fe7d4c)
#38 0x0000000000c847f1 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-19.1.0/bin/clang+0xc847f1)
#39 0x0000000000b5c2d4 main (/opt/compiler-explorer/clang-19.1.0/bin/clang+0xb5c2d4)
#40 0x000074b6bc229d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#41 0x000074b6bc229e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#42 0x0000000000c80f0e _start (/opt/compiler-explorer/clang-19.1.0/bin/clang+0xc80f0e)
clang: error: clang frontend command failed with exit code 139 (use -v to see invocation)
Compiler returned: 139

justincady avatar Jan 13 '25 21:01 justincady

@llvm/issue-subscribers-clang-codegen

Author: Justin Cady (justincady)

Tested with clang 19. Here's the reproducer:
typedef union {
    struct {} first;
    struct { int x; } second;
} foo;

void bar() {
    foo f = {};
}

Compiler Explorer

Note that first is an empty struct, which I understand to be UB in C:

> The presence of a struct-declaration-list in a struct-or-union-specifier declares a new type, within a translation unit. The struct-declaration-list is a sequence of declarations for the members of the structure or union. If the struct-declaration-list does not contain any named members, either directly or via an anonymous structure or anonymous union, the behavior is undefined. The type is incomplete until immediately after the } that terminates the list, and complete thereafter. > > https://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf

llvmbot avatar Jan 13 '25 22:01 llvmbot

main is OK: https://godbolt.org/z/Mv183o1zz. Since 20 branch will be created soon, it's unlikely that fixes will be backported.

EugeneZelenko avatar Jan 13 '25 22:01 EugeneZelenko

Looks like this was a clang-19 regression that was fixed in clang-20.

shafik avatar Jan 14 '25 04:01 shafik