Clang ICE: assertion failure in ItaniumRecordLayoutBuilder when using __attribute__((mode(QI))) on a 32-bit C bit-field
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
### Link for quick verification: https://godbolt.org/z/bMx1MTd6P
```c
struct S {
int f1 : 32 __attribute__ ((mode (QI)));
};
int main() {
struct S d;
}
```
Go back to `clang 2.8.0 (assertions)`.
Clang crashes with an assertion failure when compiling a C source that applies `__attribute__((mode(QI)))` to a 32-bit bit-field while `mode(QI)` is 8-bit machine mode. The compiler incorrectly treats the field as a wide bit-field, triggering a assertion in `ItaniumRecordLayoutBuilder`.
## Stack dump:
```console
clang++: /root/llvm-project/llvm/tools/clang/lib/AST/RecordLayoutBuilder.cpp:1462: void {anonymous}::ItaniumRecordLayoutBuilder::LayoutWideBitField(uint64_t, uint64_t, bool, const clang::FieldDecl*): Assertion `Context.getLangOpts().CPlusPlus && "Can only have wide bit-fields in C++!"' 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 -x c
1. parser at end of file
2. :6:5: LLVM IR generation of declaration 'main'
3. :6:5: Generating code for declaration 'main'
#0 0x00000000042ebc28 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42ebc28)
#1 0x00000000042e9054 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42e9054)
#2 0x000000000422de38 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x00007c20bea42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007c20bea969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
#5 0x00007c20bea42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
#6 0x00007c20bea287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
#7 0x00007c20bea2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
#8 0x00007c20bea39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#9 0x00000000080a8477 (anonymous namespace)::ItaniumRecordLayoutBuilder::LayoutField(clang::FieldDecl const*, bool) RecordLayoutBuilder.cpp:0:0
#10 0x00000000080a8532 (anonymous namespace)::ItaniumRecordLayoutBuilder::LayoutFields(clang::RecordDecl const*) RecordLayoutBuilder.cpp:0:0
#11 0x00000000080a26dd clang::ASTContext::getASTRecordLayout(clang::RecordDecl const*) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x80a26dd)
#12 0x0000000007980227 clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7980227)
#13 0x000000000797d2d7 clang::ASTContext::getTypeInfo(clang::Type const*) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x797d2d7)
#14 0x000000000798214b clang::ASTContext::getPreferredTypeAlign(clang::Type const*) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x798214b)
#15 0x0000000007982dab clang::ASTContext::getDeclAlign(clang::Decl const*, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7982dab)
#16 0x0000000004ba43f9 clang::CodeGen::CodeGenFunction::EmitAutoVarAlloca(clang::VarDecl const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ba43f9)
#17 0x0000000004baadfb clang::CodeGen::CodeGenFunction::EmitVarDecl(clang::VarDecl const&) (.part.0) CGDecl.cpp:0:0
#18 0x0000000004bab4d7 clang::CodeGen::CodeGenFunction::EmitDecl(clang::Decl const&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bab4d7)
#19 0x00000000047e7c2c clang::CodeGen::CodeGenFunction::EmitDeclStmt(clang::DeclStmt const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47e7c2c)
#20 0x00000000047fea9d clang::CodeGen::CodeGenFunction::EmitSimpleStmt(clang::Stmt const*, llvm::ArrayRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47fea9d)
#21 0x00000000047f6001 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47f6001)
#22 0x00000000047fe5fb clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47fe5fb)
#23 0x000000000486663e clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x486663e)
#24 0x0000000004877a94 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4877a94)
#25 0x00000000048e5980 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x48e5980)
#26 0x00000000048e0624 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x48e0624)
#27 0x00000000048e0e03 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x48e0e03)
#28 0x00000000048ec393 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#29 0x0000000004c4fba9 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#30 0x0000000004c406d4 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c406d4)
#31 0x0000000006a06f74 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a06f74)
#32 0x0000000004c4cfb8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c4cfb8)
#33 0x0000000004f438b5 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f438b5)
#34 0x0000000004ec3b4e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ec3b4e)
#35 0x000000000503dc3d clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x503dc3d)
#36 0x0000000000de5a6c cc1_main(llvm::ArrayRef, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xde5a6c)
#37 0x0000000000ddc4ba ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#38 0x0000000000ddc63d int llvm::function_ref&)>::callback_fn&)>(long, llvm::SmallVectorImpl&) driver.cpp:0:0
#39 0x0000000004cbf909 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#40 0x000000000422e2d4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x422e2d4)
#41 0x0000000004cbff1f clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) Job.cpp:0:0
#42 0x0000000004c80a02 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c80a02)
#43 0x0000000004c819ae clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c819ae)
#44 0x0000000004c88e05 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4c88e05)
#45 0x0000000000de1e61 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xde1e61)
#46 0x0000000000c8ceb4 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc8ceb4)
#47 0x00007c20bea29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#48 0x00007c20bea29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#49 0x0000000000ddbf55 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xddbf55)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
```
Contributor guide
Research direction
Start by compiling the C reproducer from the issue with an assertions-enabled Clang build. Read llvm/tools/clang/lib/AST/RecordLayoutBuilder.cpp, especially ItaniumRecordLayoutBuilder::LayoutField and LayoutWideBitField, using the stack trace to follow the layout path. Done means this input no longer triggers the assertion and has regression coverage for the reported case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100