llvm / llvm-project

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

[CodeGen] Assertion 'Offset >= Size' failed. with `-mms-bitfields` #88208

Open patrick-rivos opened 6 months ago

patrick-rivos commented 6 months ago

Testcase:

#pragma pack(1)
struct {
  char a;
  signed : 1;
  signed : 0;
  unsigned b;
} c;

Backtrace:

clang-19: /root/llvm-project/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp:1008: void {anonymous}::CGRecordLowering::insertPadding(): Assertion `Offset >= Size' 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-20240409/bin/clang-19 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -dumpdir /app/output.s- -disable-free -clear-ast-before-backend -main-file-name example.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -mms-bitfields -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -fdebug-compilation-dir=/app -fcoverage-compilation-dir=/app -resource-dir /opt/compiler-explorer/clang-assertions-trunk-20240409/lib/clang/19 -internal-isystem /opt/compiler-explorer/clang-assertions-trunk-20240409/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/example-a9d207.o -x c <source>
1.  <eof> parser at end of file
 #0 0x0000000003935458 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3935458)
 #1 0x0000000003932bac SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f6dffe42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007f6dffe969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007f6dffe42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007f6dffe287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00007f6dffe2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x00007f6dffe39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x0000000003c4694f (anonymous namespace)::CGRecordLowering::lower(bool) CGRecordLayoutBuilder.cpp:0:0
 #9 0x0000000003c48bb9 clang::CodeGen::CodeGenTypes::ComputeRecordLayout(clang::RecordDecl const*, llvm::StructType*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3c48bb9)
#10 0x0000000003d82a17 clang::CodeGen::CodeGenTypes::ConvertRecordDeclType(clang::RecordDecl const*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3d82a17)
#11 0x0000000003d83d7b clang::CodeGen::CodeGenTypes::getCGRecordLayout(clang::RecordDecl const*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3d83d7b)
#12 0x0000000003d83ff8 clang::CodeGen::CodeGenTypes::isZeroInitializable(clang::QualType) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3d83ff8)
#13 0x0000000003c31fbf clang::CodeGen::CodeGenModule::EmitNullConstant(clang::QualType) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3c31fbf)
#14 0x0000000003d0f58d clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3d0f58d)
#15 0x0000000003d105a5 clang::CodeGen::CodeGenModule::EmitTentativeDefinition(clang::VarDecl const*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3d105a5)
#16 0x000000000635dc2c clang::Sema::ActOnEndOfTranslationUnit() (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x635dc2c)
#17 0x00000000061decc1 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x61decc1)
#18 0x00000000061d182a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x61d182a)
#19 0x00000000041e1498 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x41e1498)
#20 0x000000000445f899 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x445f899)
#21 0x00000000043e913e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x43e913e)
#22 0x000000000454289e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x454289e)
#23 0x0000000000c43a6c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0xc43a6c)
#24 0x0000000000c3ccfa ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#25 0x0000000000c4068e clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0xc4068e)
#26 0x0000000000b23fd4 main (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0xb23fd4)
#27 0x00007f6dffe29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#28 0x00007f6dffe29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#29 0x0000000000c3c7ee _start (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0xc3c7ee)
clang: error: unable to execute command: Aborted (core dumped)
clang: error: clang frontend command failed due to signal (use -v to see invocation)

Godbolt: https://godbolt.org/z/3eqqo8zT7

Found via fuzzer.

llvmbot commented 6 months ago

@llvm/issue-subscribers-clang-codegen

Author: Patrick O'Neill (patrick-rivos)

Testcase: ``` #pragma pack(1) struct { char a; signed : 1; signed : 0; unsigned b; } c; ``` Backtrace: ``` clang-19: /root/llvm-project/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp:1008: void {anonymous}::CGRecordLowering::insertPadding(): Assertion `Offset >= Size' 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-20240409/bin/clang-19 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -dumpdir /app/output.s- -disable-free -clear-ast-before-backend -main-file-name example.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -mms-bitfields -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -fdebug-compilation-dir=/app -fcoverage-compilation-dir=/app -resource-dir /opt/compiler-explorer/clang-assertions-trunk-20240409/lib/clang/19 -internal-isystem /opt/compiler-explorer/clang-assertions-trunk-20240409/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/example-a9d207.o -x c <source> 1. <eof> parser at end of file #0 0x0000000003935458 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3935458) #1 0x0000000003932bac SignalHandler(int) Signals.cpp:0:0 #2 0x00007f6dffe42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520) #3 0x00007f6dffe969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc) #4 0x00007f6dffe42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476) #5 0x00007f6dffe287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3) #6 0x00007f6dffe2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b) #7 0x00007f6dffe39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96) #8 0x0000000003c4694f (anonymous namespace)::CGRecordLowering::lower(bool) CGRecordLayoutBuilder.cpp:0:0 #9 0x0000000003c48bb9 clang::CodeGen::CodeGenTypes::ComputeRecordLayout(clang::RecordDecl const*, llvm::StructType*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3c48bb9) #10 0x0000000003d82a17 clang::CodeGen::CodeGenTypes::ConvertRecordDeclType(clang::RecordDecl const*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3d82a17) #11 0x0000000003d83d7b clang::CodeGen::CodeGenTypes::getCGRecordLayout(clang::RecordDecl const*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3d83d7b) #12 0x0000000003d83ff8 clang::CodeGen::CodeGenTypes::isZeroInitializable(clang::QualType) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3d83ff8) #13 0x0000000003c31fbf clang::CodeGen::CodeGenModule::EmitNullConstant(clang::QualType) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3c31fbf) #14 0x0000000003d0f58d clang::CodeGen::CodeGenModule::EmitGlobalVarDefinition(clang::VarDecl const*, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3d0f58d) #15 0x0000000003d105a5 clang::CodeGen::CodeGenModule::EmitTentativeDefinition(clang::VarDecl const*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x3d105a5) #16 0x000000000635dc2c clang::Sema::ActOnEndOfTranslationUnit() (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x635dc2c) #17 0x00000000061decc1 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x61decc1) #18 0x00000000061d182a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x61d182a) #19 0x00000000041e1498 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x41e1498) #20 0x000000000445f899 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x445f899) #21 0x00000000043e913e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x43e913e) #22 0x000000000454289e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0x454289e) #23 0x0000000000c43a6c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0xc43a6c) #24 0x0000000000c3ccfa ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0 #25 0x0000000000c4068e clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0xc4068e) #26 0x0000000000b23fd4 main (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0xb23fd4) #27 0x00007f6dffe29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90) #28 0x00007f6dffe29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40) #29 0x0000000000c3c7ee _start (/opt/compiler-explorer/clang-assertions-trunk-20240409/bin/clang-19+0xc3c7ee) clang: error: unable to execute command: Aborted (core dumped) clang: error: clang frontend command failed due to signal (use -v to see invocation) ``` Godbolt: https://godbolt.org/z/3eqqo8zT7 Found via fuzzer.