Skip to content

Commit 883728b

Browse files
committed
[lldb][DWARFASTParserClang] Prevent unnamed bitfield creation in the presence of overlapping fields (llvm#108343)
This bug surfaced after llvm#105865 (currently reverted, but blocked on this to be relanded). Because Clang doesn't emit `DW_TAG_member`s for unnamed bitfields, LLDB has to make an educated guess about whether they existed in the source. It does so by checking whether there is a gap between where the last field ended and the currently parsed field starts. In the example test case, the empty field `padding` was folded into the storage of `data`. Because the `bit_offset` of `padding` is `0x0` and its `DW_AT_byte_size` is `0x1`, LLDB thinks the field ends at `0x1` (not quite because we first round the size to a word size, but this is an implementation detail), erroneously deducing that there's a gap between `flag` and `padding`. This patch adds the notion of "effective field end", which accounts for fields that share storage. It is set to the end of the storage that the two fields occupy. Then we use this to check for gaps in the unnamed bitfield creation logic. (cherry picked from commit a6a547f)
1 parent d048eab commit 883728b

File tree

3 files changed

+152
-4
lines changed

3 files changed

+152
-4
lines changed

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,14 +3098,21 @@ void DWARFASTParserClang::ParseSingleMember(
30983098
last_field_info = this_field_info;
30993099
last_field_info.SetIsBitfield(true);
31003100
} else {
3101-
last_field_info.bit_offset = field_bit_offset;
3101+
FieldInfo this_field_info{.is_bitfield = false};
3102+
this_field_info.bit_offset = field_bit_offset;
31023103

3104+
// TODO: we shouldn't silently ignore the bit_size if we fail
3105+
// to GetByteSize.
31033106
if (std::optional<uint64_t> clang_type_size =
31043107
llvm::expectedToOptional(member_type->GetByteSize(nullptr))) {
3105-
last_field_info.bit_size = *clang_type_size * character_width;
3108+
this_field_info.bit_size = *clang_type_size * character_width;
31063109
}
31073110

3108-
last_field_info.SetIsBitfield(false);
3111+
if (this_field_info.GetFieldEnd() <= last_field_info.GetEffectiveFieldEnd())
3112+
this_field_info.SetEffectiveFieldEnd(
3113+
last_field_info.GetEffectiveFieldEnd());
3114+
3115+
last_field_info = this_field_info;
31093116
}
31103117

31113118
// Don't turn artificial members such as vtable pointers into real FieldDecls
@@ -3911,7 +3918,7 @@ void DWARFASTParserClang::AddUnnamedBitfieldToRecordTypeIfNeeded(
39113918
const FieldInfo &current_field) {
39123919
// TODO: get this value from target
39133920
const uint64_t word_width = 32;
3914-
uint64_t last_field_end = previous_field.bit_offset + previous_field.bit_size;
3921+
uint64_t last_field_end = previous_field.GetEffectiveFieldEnd();
39153922

39163923
if (!previous_field.IsBitfield()) {
39173924
// The last field was not a bit-field...

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,27 @@ class DWARFASTParserClang : public lldb_private::plugin::dwarf::DWARFASTParser {
270270

271271
private:
272272
struct FieldInfo {
273+
/// Size in bits that this field occupies. Can but
274+
/// need not be the DW_AT_bit_size of the field.
273275
uint64_t bit_size = 0;
276+
277+
/// Offset of this field in bits from the beginning
278+
/// of the containing struct. Can but need not
279+
/// be the DW_AT_data_bit_offset of the field.
274280
uint64_t bit_offset = 0;
281+
282+
/// In case this field is folded into the storage
283+
/// of a previous member's storage (for example
284+
/// with [[no_unique_address]]), the effective field
285+
/// end is the offset in bits from the beginning of
286+
/// the containing struct where the field we were
287+
/// folded into ended.
288+
std::optional<uint64_t> effective_field_end;
289+
290+
/// Set to 'true' if this field is a bit-field.
275291
bool is_bitfield = false;
292+
293+
/// Set to 'true' if this field is DW_AT_artificial.
276294
bool is_artificial = false;
277295

278296
FieldInfo() = default;
@@ -288,6 +306,19 @@ class DWARFASTParserClang : public lldb_private::plugin::dwarf::DWARFASTParser {
288306
// bit offset than any previous bitfield + size.
289307
return (bit_size + bit_offset) <= next_bit_offset;
290308
}
309+
310+
/// Returns the offset in bits of where the storage this field
311+
/// occupies ends.
312+
uint64_t GetFieldEnd() const { return bit_size + bit_offset; }
313+
314+
void SetEffectiveFieldEnd(uint64_t val) { effective_field_end = val; }
315+
316+
/// If this field was folded into storage of a previous field,
317+
/// returns the offset in bits of where that storage ends. Otherwise,
318+
/// returns the regular field end (see \ref GetFieldEnd).
319+
uint64_t GetEffectiveFieldEnd() const {
320+
return effective_field_end.value_or(GetFieldEnd());
321+
}
291322
};
292323

293324
/// Parsed form of all attributes that are relevant for parsing type members.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// RUN: %clang --target=x86_64-apple-macosx -c -gdwarf -o %t %s
2+
// RUN: %lldb %t \
3+
// RUN: -o "target var global" \
4+
// RUN: -o "target var global2" \
5+
// RUN: -o "target var global3" \
6+
// RUN: -o "target var global4" \
7+
// RUN: -o "target var global5" \
8+
// RUN: -o "image dump ast" \
9+
// RUN: -o exit | FileCheck %s
10+
11+
// CHECK: (lldb) image dump ast
12+
// CHECK: CXXRecordDecl {{.*}} struct Foo definition
13+
// CHECK: |-FieldDecl {{.*}} data 'char[5]'
14+
// CHECK-NEXT: |-FieldDecl {{.*}} padding 'Empty'
15+
// CHECK-NEXT: `-FieldDecl {{.*}} flag 'unsigned long'
16+
// CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 1
17+
18+
struct Empty {};
19+
struct Empty2 {};
20+
struct Empty3 {};
21+
22+
struct Foo {
23+
char data[5];
24+
[[no_unique_address]] Empty padding;
25+
unsigned long flag : 1;
26+
};
27+
28+
Foo global;
29+
30+
// CHECK: CXXRecordDecl {{.*}} struct ConsecutiveOverlap definition
31+
// CHECK: |-FieldDecl {{.*}} data 'char[5]'
32+
// CHECK-NEXT: |-FieldDecl {{.*}} p1 'Empty'
33+
// CHECK-NEXT: |-FieldDecl {{.*}} p2 'Empty2'
34+
// CHECK-NEXT: |-FieldDecl {{.*}} p3 'Empty3'
35+
// CHECK-NEXT: `-FieldDecl {{.*}} flag 'unsigned long'
36+
// CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 1
37+
38+
struct ConsecutiveOverlap {
39+
char data[5];
40+
[[no_unique_address]] Empty p1;
41+
[[no_unique_address]] Empty2 p2;
42+
[[no_unique_address]] Empty3 p3;
43+
unsigned long flag : 1;
44+
};
45+
46+
ConsecutiveOverlap global2;
47+
48+
// FIXME: we fail to deduce the unnamed bitfields here.
49+
//
50+
// CHECK: CXXRecordDecl {{.*}} struct MultipleAtOffsetZero definition
51+
// CHECK: |-FieldDecl {{.*}} data 'char[5]'
52+
// CHECK-NEXT: |-FieldDecl {{.*}} p1 'Empty'
53+
// CHECK-NEXT: |-FieldDecl {{.*}} f1 'unsigned long'
54+
// CHECK-NEXT: | `-IntegerLiteral {{.*}} 'int' 1
55+
// CHECK-NEXT: |-FieldDecl {{.*}} p2 'Empty2'
56+
// CHECK-NEXT: `-FieldDecl {{.*}} f2 'unsigned long'
57+
// CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 1
58+
59+
struct MultipleAtOffsetZero {
60+
char data[5];
61+
[[no_unique_address]] Empty p1;
62+
int : 4;
63+
unsigned long f1 : 1;
64+
[[no_unique_address]] Empty2 p2;
65+
int : 4;
66+
unsigned long f2 : 1;
67+
};
68+
69+
MultipleAtOffsetZero global3;
70+
71+
// FIXME: we fail to deduce the unnamed bitfields here.
72+
//
73+
// CHECK: CXXRecordDecl {{.*}} struct MultipleEmpty definition
74+
// CHECK: |-FieldDecl {{.*}} data 'char[5]'
75+
// CHECK-NEXT: |-FieldDecl {{.*}} p1 'Empty'
76+
// CHECK-NEXT: |-FieldDecl {{.*}} f1 'unsigned long'
77+
// CHECK-NEXT: | `-IntegerLiteral {{.*}} 'int' 1
78+
// CHECK-NEXT: |-FieldDecl {{.*}} p2 'Empty'
79+
// CHECK-NEXT: `-FieldDecl {{.*}} f2 'unsigned long'
80+
// CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 1
81+
82+
struct MultipleEmpty {
83+
char data[5];
84+
[[no_unique_address]] Empty p1;
85+
int : 4;
86+
unsigned long f1 : 1;
87+
[[no_unique_address]] Empty p2;
88+
int : 4;
89+
unsigned long f2 : 1;
90+
};
91+
92+
MultipleEmpty global4;
93+
94+
// CHECK: CXXRecordDecl {{.*}} struct FieldBitfieldOverlap definition
95+
// CHECK: |-FieldDecl {{.*}} a 'int'
96+
// CHECK-NEXT: | `-IntegerLiteral {{.*}} 'int' 3
97+
// CHECK-NEXT: |-FieldDecl {{.*}} p1 'Empty'
98+
// CHECK-NEXT: |-FieldDecl {{.*}} b 'int'
99+
// CHECK-NEXT: | `-IntegerLiteral {{.*}} 'int' 6
100+
// CHECK-NEXT: `-FieldDecl {{.*}} c 'int'
101+
// CHECK-NEXT: `-IntegerLiteral {{.*}} 'int' 1
102+
103+
struct FieldBitfieldOverlap {
104+
int a : 3;
105+
[[no_unique_address]] Empty p1;
106+
int b : 6;
107+
int c : 1;
108+
};
109+
110+
FieldBitfieldOverlap global5;

0 commit comments

Comments
 (0)