https://github.com/akash-manna-sky updated https://github.com/llvm/llvm-project/pull/238422
>From d6c32c5f6f807ff95a049bd310394651c7006f07 Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Tue, 3 Nov 2026 21:34:10 +0530 Subject: [PATCH 1/2] Fixes #119207 `CGRecordLowering::setBitFieldInfo` wrote the declared width of a bit-field into the 15-bit `CGBitFieldInfo::Size` and clamped it to the type width afterwards. For a C++ bit-field such as `int bar : 32768;` the width wrapped to zero and hit the `Empty bitfield!` assertion; widths just above that silently became one-bit fields, and the big-endian offset was derived from the wrapped size. Clamp the width in a 64-bit value before it is narrowed, and route both writers through the `CGBitFieldInfo` constructor, which now checks that the offset, size and storage size fit their packed members. Assisted-by: AI --- clang/docs/ReleaseNotes.md | 1 + clang/lib/CodeGen/CGRecordLayout.h | 18 +- clang/lib/CodeGen/CGRecordLayoutBuilder.cpp | 28 ++- .../big-endian-oversized-bitfield.cpp | 19 ++ clang/test/CodeGenCXX/oversized-bitfield.cpp | 165 ++++++++++++++++++ 5 files changed, 211 insertions(+), 20 deletions(-) create mode 100644 clang/test/CodeGenCXX/oversized-bitfield.cpp diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 6d1dbbbb6eb77b..114c9e5e7b311a 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -821,6 +821,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and - Fixed a bug where a bit-field accessed as the result of a statement expression (e.g. `({ s.b; })`) was not subject to integer promotion, unlike an ordinary bit-field access. (#GH221542) +- Fixed a crash when a C++ bit-field is declared 32768 or more bits wide. (#GH119207) - No longer crashing due to follow-on diagnostics when there is an invalid operand in a logical operator involving a vector operand. (#GH227588) - Fixed assertion failures caused by stale linkage information when an extern variable or function declaration is merged with a preceding static declaration. (#GH204759, #GH204754) - Fixed a crash due to typo correction mishandling custom keywords `_virtual_inheritance` and `_multiple_inheritance` in `-fms-compatibility` mode. (#GH228003) diff --git a/clang/lib/CodeGen/CGRecordLayout.h b/clang/lib/CodeGen/CGRecordLayout.h index 44e888c93108fb..dbda44449b0271 100644 --- a/clang/lib/CodeGen/CGRecordLayout.h +++ b/clang/lib/CodeGen/CGRecordLayout.h @@ -14,6 +14,7 @@ #include "clang/Basic/LLVM.h" #include "llvm/ADT/DenseMap.h" #include "llvm/IR/DerivedTypes.h" +#include "llvm/Support/MathExtras.h" namespace llvm { class StructType; @@ -67,7 +68,8 @@ struct CGBitFieldInfo { /// a single "field" within the LLVM struct type. This offset is in bits. unsigned Offset : 16; - /// The total size of the bit-field, in bits. + /// The total size of the bit-field, in bits, clamped to the width of its + /// type (the extra bits of an over-wide C++ bit-field are padding). unsigned Size : 15; /// Whether the bit-field is signed. @@ -97,10 +99,18 @@ struct CGBitFieldInfo { : Offset(), Size(), IsSigned(), StorageSize(), VolatileOffset(), VolatileStorageSize() {} - CGBitFieldInfo(unsigned Offset, unsigned Size, bool IsSigned, - unsigned StorageSize, CharUnits StorageOffset) + CGBitFieldInfo(uint64_t Offset, uint64_t Size, bool IsSigned, + uint64_t StorageSize, CharUnits StorageOffset) : Offset(Offset), Size(Size), IsSigned(IsSigned), - StorageSize(StorageSize), StorageOffset(StorageOffset) {} + StorageSize(StorageSize), StorageOffset(StorageOffset), + VolatileOffset(), VolatileStorageSize() { + assert(llvm::isUInt<16>(Offset) && + "bit-field offset does not fit in CGBitFieldInfo::Offset"); + assert(llvm::isUInt<15>(Size) && + "bit-field size does not fit in CGBitFieldInfo::Size"); + assert(llvm::isUInt<32>(StorageSize) && + "bit-field storage does not fit in CGBitFieldInfo::StorageSize"); + } void print(raw_ostream &OS) const; void dump() const; diff --git a/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp b/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp index bef456d85ba4ad..4d869d775fd313 100644 --- a/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp +++ b/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp @@ -215,29 +215,25 @@ CGRecordLowering::CGRecordLowering(CodeGenTypes &Types, const RecordDecl *D, void CGRecordLowering::setBitFieldInfo( const FieldDecl *FD, CharUnits StartOffset, llvm::Type *StorageType) { - CGBitFieldInfo &Info = BitFields[FD->getCanonicalDecl()]; - Info.IsSigned = FD->getType()->isSignedIntegerOrEnumerationType(); - Info.Offset = (unsigned)(getFieldBitOffset(FD) - Context.toBits(StartOffset)); - Info.Size = FD->getBitWidthValue(); - Info.StorageSize = (unsigned)DataLayout.getTypeAllocSizeInBits(StorageType); - Info.StorageOffset = StartOffset; - if (Info.Size > Info.StorageSize) - Info.Size = Info.StorageSize; - // CGBitFieldInfo::MakeInfo does the same clamp + uint64_t Offset = getFieldBitOffset(FD) - Context.toBits(StartOffset); + uint64_t StorageSize = + DataLayout.getTypeAllocSizeInBits(StorageType).getFixedValue(); + // Clamp the declared width to the storage and type widths before it is + // narrowed into the packed Size member. MakeInfo does the same clamp. llvm::Type *FieldType = Types.ConvertTypeForMem(FD->getType()); - unsigned FieldTypeSize = DataLayout.getTypeAllocSizeInBits(FieldType); - if (Info.Size > FieldTypeSize) - Info.Size = FieldTypeSize; + uint64_t Size = + std::min({uint64_t(FD->getBitWidthValue()), StorageSize, + DataLayout.getTypeAllocSizeInBits(FieldType).getFixedValue()}); // Reverse the bit offsets for big endian machines. Because we represent // a bitfield as a single large integer load, we can imagine the bits // counting from the most-significant-bit instead of the // least-significant-bit. if (DataLayout.isBigEndian()) - Info.Offset = Info.StorageSize - (Info.Offset + Info.Size); + Offset = StorageSize - (Offset + Size); - Info.VolatileStorageSize = 0; - Info.VolatileOffset = 0; - Info.VolatileStorageOffset = CharUnits::Zero(); + BitFields[FD->getCanonicalDecl()] = CGBitFieldInfo( + Offset, Size, FD->getType()->isSignedIntegerOrEnumerationType(), + StorageSize, StartOffset); } void CGRecordLowering::lower(bool NVBaseType) { diff --git a/clang/test/CodeGenCXX/big-endian-oversized-bitfield.cpp b/clang/test/CodeGenCXX/big-endian-oversized-bitfield.cpp index da51f12b553ab9..8d1daaab10f035 100644 --- a/clang/test/CodeGenCXX/big-endian-oversized-bitfield.cpp +++ b/clang/test/CodeGenCXX/big-endian-oversized-bitfield.cpp @@ -81,3 +81,22 @@ bool get(const B *p) { void set(B *p, bool v) { p->value = v; } + +// GH119207: a 2^15-bit width used to wrap to zero. The value bits come first, +// at the most-significant end of the i32768 storage. +struct H { + int value : 32768; +}; + +// LAYOUT: BitFields:[ +// LAYOUT-NEXT: <CGBitFieldInfo Offset:32736 Size:32 IsSigned:1 StorageSize:32768 StorageOffset:0 +// LAYOUT-NEXT: ]> + +// IR-LABEL: define {{.*}} @_Z3getPK1H( +// IR: [[HP:%.*]] = load ptr, ptr %p.addr +// IR: [[HLOAD:%.*]] = load i32768, ptr [[HP]] +// IR-NEXT: [[HSHR:%.*]] = ashr i32768 [[HLOAD]], 32736 +// IR-NEXT: trunc i32768 [[HSHR]] to i32 +int get(const H *p) { + return p->value; +} diff --git a/clang/test/CodeGenCXX/oversized-bitfield.cpp b/clang/test/CodeGenCXX/oversized-bitfield.cpp new file mode 100644 index 00000000000000..0fded23e86f208 --- /dev/null +++ b/clang/test/CodeGenCXX/oversized-bitfield.cpp @@ -0,0 +1,165 @@ +// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -fdump-record-layouts-simple -o %t.ll %s | FileCheck %s --check-prefix=LAYOUT +// RUN: FileCheck %s --check-prefix=IR < %t.ll +// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -debug-info-kind=limited -o - %s | FileCheck %s --check-prefixes=IR,DEBUG + +// An over-wide C++ bit-field only has type-width value bits. Widths of 2^15 +// and 2^16 used to wrap to zero in CGBitFieldInfo::Size and crash (GH119207). + +#pragma clang diagnostic ignored "-Wbitfield-width" + +// IR: @g32769 = {{.*}}global { i8, i8, i8, i8, [4100 x i8] } { i8 120, i8 86, i8 52, i8 18, [4100 x i8] undef } + +enum E { + a = 1, + b = 2, + c = 3 +}; + +struct S { + E e1 : (1 << 16); +}; + +// LAYOUT-LABEL: LLVMType:%struct.S = +// LAYOUT-SAME: type { i65536 } +// LAYOUT: BitFields:[ +// LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:32 IsSigned:0 StorageSize:65536 StorageOffset:0 +// LAYOUT-NEXT: ]> + +// IR-LABEL: define {{.*}} i32 @_Z4loadP1S( +// IR: [[LOAD:%.*]] = load i65536, ptr +// IR-NEXT: [[CLEAR:%.*]] = and i65536 [[LOAD]], 4294967295 +// IR-NEXT: trunc i65536 [[CLEAR]] to i32 +E load(S *s) { + return s->e1; +} + +struct foo +{ + int bar : 32768; +}; + +// LAYOUT-LABEL: LLVMType:%struct.foo = +// LAYOUT-SAME: type { i32768 } +// LAYOUT: BitFields:[ +// LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:32 IsSigned:1 StorageSize:32768 StorageOffset:0 +// LAYOUT-NEXT: ]> + +// IR-LABEL: define {{.*}} i32 @main( +// IR: alloca %struct.foo, align 8 +int main() { + foo x; +} + +struct W32767 { int w32767 : 32767; }; +struct W32768 { int w32768 : 32768; }; +struct W32769 { int w32769 : 32769; }; +struct W65537 { long long w65537 : 65537; }; + +// LAYOUT-LABEL: LLVMType:%struct.W32767 = +// LAYOUT-SAME: type { i32768 } +// LAYOUT: BitFields:[ +// LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:32 IsSigned:1 StorageSize:32768 StorageOffset:0 +// LAYOUT-NEXT: ]> + +// IR-LABEL: define {{.*}} i32 @_Z7ld32767P6W32767( +// IR: [[LOAD:%.*]] = load i32768, ptr +// IR-NEXT: [[SHL:%.*]] = shl i32768 [[LOAD]], 32736 +// IR-NEXT: [[ASHR:%.*]] = ashr i32768 [[SHL]], 32736 +// IR-NEXT: trunc i32768 [[ASHR]] to i32 +int ld32767(W32767 *p) { return p->w32767; } + +// LAYOUT-LABEL: LLVMType:%struct.W32768 = +// LAYOUT-SAME: type { i32768 } +// LAYOUT: BitFields:[ +// LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:32 IsSigned:1 StorageSize:32768 StorageOffset:0 +// LAYOUT-NEXT: ]> + +// IR-LABEL: define {{.*}} i32 @_Z7ld32768P6W32768( +// IR: [[LOAD:%.*]] = load i32768, ptr +// IR-NEXT: [[SHL:%.*]] = shl i32768 [[LOAD]], 32736 +// IR-NEXT: [[ASHR:%.*]] = ashr i32768 [[SHL]], 32736 +// IR-NEXT: trunc i32768 [[ASHR]] to i32 +int ld32768(W32768 *p) { return p->w32768; } + +// LAYOUT-LABEL: LLVMType:%struct.W32769 = +// LAYOUT-SAME: type { [4097 x i8], [7 x i8] } +// LAYOUT: BitFields:[ +// LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:32 IsSigned:1 StorageSize:32776 StorageOffset:0 +// LAYOUT-NEXT: ]> + +// IR-LABEL: define {{.*}} i32 @_Z7ld32769P6W32769( +// IR: [[LOAD:%.*]] = load i32776, ptr +// IR-NEXT: [[SHL:%.*]] = shl i32776 [[LOAD]], 32744 +// IR-NEXT: [[ASHR:%.*]] = ashr i32776 [[SHL]], 32744 +// IR-NEXT: trunc i32776 [[ASHR]] to i32 +int ld32769(W32769 *p) { return p->w32769; } + +// IR-LABEL: define {{.*}} void @_Z7st32769P6W32769i( +// IR: [[EXT:%.*]] = zext i32 {{.*}} to i32776 +// IR-NEXT: [[OLD:%.*]] = load i32776, ptr +// IR-NEXT: [[VAL:%.*]] = and i32776 [[EXT]], 4294967295 +// IR-NEXT: [[KEPT:%.*]] = and i32776 [[OLD]], -4294967296 +// IR-NEXT: [[SET:%.*]] = or i32776 [[KEPT]], [[VAL]] +// IR-NEXT: store i32776 [[SET]], ptr +void st32769(W32769 *p, int v) { p->w32769 = v; } + +W32769 g32769 = {0x12345678}; + +// LAYOUT-LABEL: LLVMType:%struct.W65537 = +// LAYOUT-SAME: type { [8193 x i8], [7 x i8] } +// LAYOUT: BitFields:[ +// LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:64 IsSigned:1 StorageSize:65544 StorageOffset:0 +// LAYOUT-NEXT: ]> + +// IR-LABEL: define {{.*}} i64 @_Z7ld65537P6W65537( +// IR: [[LOAD:%.*]] = load i65544, ptr +// IR-NEXT: [[SHL:%.*]] = shl i65544 [[LOAD]], 65480 +// IR-NEXT: [[ASHR:%.*]] = ashr i65544 [[SHL]], 65480 +// IR-NEXT: trunc i65544 [[ASHR]] to i64 +long long ld65537(W65537 *p) { return p->w65537; } + +union U { + int w : 32768; +}; + +// LAYOUT-LABEL: LLVMType:%union.U = +// LAYOUT-SAME: type { i32, [4092 x i8] } +// LAYOUT: BitFields:[ +// LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:32 IsSigned:1 StorageSize:32 StorageOffset:0 +// LAYOUT-NEXT: ]> + +// IR-LABEL: define {{.*}} i32 @_Z3ldUP1U( +// IR: [[LOAD:%.*]] = load i32, ptr +// IR-NEXT: ret i32 [[LOAD]] +int ldU(U *p) { return p->w; } + +struct Control { + int lo : 7; + unsigned hi : 25; +}; + +// LAYOUT-LABEL: LLVMType:%struct.Control = +// LAYOUT-SAME: type { i32 } +// LAYOUT: BitFields:[ +// LAYOUT-NEXT: <CGBitFieldInfo Offset:0 Size:7 IsSigned:1 StorageSize:32 StorageOffset:0 +// LAYOUT-NEXT: <CGBitFieldInfo Offset:7 Size:25 IsSigned:0 StorageSize:32 StorageOffset:0 +// LAYOUT-NEXT: ]> + +// IR-LABEL: define {{.*}} i32 @_Z5ldCtlP7Control( +// IR: [[LOAD:%.*]] = load i32, ptr +// IR-NEXT: [[SHL:%.*]] = shl i32 [[LOAD]], 25 +// IR-NEXT: [[ASHR:%.*]] = ashr i32 [[SHL]], 25 +// IR-NEXT: ret i32 [[ASHR]] +int ldCtl(Control *p) { return p->lo; } + +// DEBUG-DAG: !DICompositeType(tag: DW_TAG_structure_type, name: "S", {{.*}}size: 65536, +// DEBUG-DAG: !DIDerivedType(tag: DW_TAG_member, name: "e1", {{.*}}baseType: !{{[0-9]+}}, size: 32, flags: DIFlagBitField, extraData: i64 0) +// DEBUG-DAG: !DICompositeType(tag: DW_TAG_structure_type, name: "foo", {{.*}}size: 32768, +// DEBUG-DAG: !DIDerivedType(tag: DW_TAG_member, name: "bar", {{.*}}baseType: !{{[0-9]+}}, size: 32, flags: DIFlagBitField, extraData: i64 0) +// DEBUG-DAG: !DIDerivedType(tag: DW_TAG_member, name: "w32767", {{.*}}size: 32, flags: DIFlagBitField, extraData: i64 0) +// DEBUG-DAG: !DIDerivedType(tag: DW_TAG_member, name: "w32768", {{.*}}size: 32, flags: DIFlagBitField, extraData: i64 0) +// DEBUG-DAG: !DIDerivedType(tag: DW_TAG_member, name: "w32769", {{.*}}size: 32, flags: DIFlagBitField, extraData: i64 0) +// DEBUG-DAG: !DIDerivedType(tag: DW_TAG_member, name: "w65537", {{.*}}size: 64, flags: DIFlagBitField, extraData: i64 0) +// DEBUG-DAG: !DIDerivedType(tag: DW_TAG_member, name: "w", {{.*}}size: 32, flags: DIFlagBitField, extraData: i64 0) +// DEBUG-DAG: !DIDerivedType(tag: DW_TAG_member, name: "lo", {{.*}}size: 7, flags: DIFlagBitField, extraData: i64 0) +// DEBUG-DAG: !DIDerivedType(tag: DW_TAG_member, name: "hi", {{.*}}size: 25, offset: 7, flags: DIFlagBitField, extraData: i64 0) >From a576711f81dd52ca7e49f664436b540f05a31c88 Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Wed, 4 Nov 2026 08:23:59 +0530 Subject: [PATCH 2/2] Address review comments. --- clang/lib/CodeGen/CGRecordLayout.h | 3 +-- clang/lib/CodeGen/CGRecordLayoutBuilder.cpp | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/clang/lib/CodeGen/CGRecordLayout.h b/clang/lib/CodeGen/CGRecordLayout.h index dbda44449b0271..a97c2589139d1c 100644 --- a/clang/lib/CodeGen/CGRecordLayout.h +++ b/clang/lib/CodeGen/CGRecordLayout.h @@ -68,8 +68,7 @@ struct CGBitFieldInfo { /// a single "field" within the LLVM struct type. This offset is in bits. unsigned Offset : 16; - /// The total size of the bit-field, in bits, clamped to the width of its - /// type (the extra bits of an over-wide C++ bit-field are padding). + /// The total size of the bit-field, in bits, not including padding. unsigned Size : 15; /// Whether the bit-field is signed. diff --git a/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp b/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp index 4d869d775fd313..47ef6ea0df50c9 100644 --- a/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp +++ b/clang/lib/CodeGen/CGRecordLayoutBuilder.cpp @@ -218,8 +218,7 @@ void CGRecordLowering::setBitFieldInfo( uint64_t Offset = getFieldBitOffset(FD) - Context.toBits(StartOffset); uint64_t StorageSize = DataLayout.getTypeAllocSizeInBits(StorageType).getFixedValue(); - // Clamp the declared width to the storage and type widths before it is - // narrowed into the packed Size member. MakeInfo does the same clamp. + // CGBitFieldInfo::MakeInfo does the same clamp llvm::Type *FieldType = Types.ConvertTypeForMem(FD->getType()); uint64_t Size = std::min({uint64_t(FD->getBitWidthValue()), StorageSize, _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
