-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[clang][RecordLayoutBuilder] Be stricter about inferring packed-ness in ExternalLayouts #97443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
38b7837
68d71ab
2d9dc34
8ac993d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1280,13 +1280,6 @@ ItaniumRecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) { | |
bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset); | ||
(void)Allowed; | ||
assert(Allowed && "Base subobject externally placed at overlapping offset"); | ||
|
||
if (InferAlignment && Offset < getDataSize().alignTo(AlignTo)) { | ||
// The externally-supplied base offset is before the base offset we | ||
// computed. Assume that the structure is packed. | ||
Alignment = CharUnits::One(); | ||
InferAlignment = false; | ||
} | ||
} | ||
|
||
if (!Base->Class->isEmpty()) { | ||
|
@@ -2243,9 +2236,27 @@ ItaniumRecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field, | |
uint64_t ComputedOffset) { | ||
uint64_t ExternalFieldOffset = External.getExternalFieldOffset(Field); | ||
|
||
if (InferAlignment && ExternalFieldOffset < ComputedOffset) { | ||
// The externally-supplied field offset is before the field offset we | ||
// computed. Assume that the structure is packed. | ||
// DWARF doesn't tell us whether a structure was declared as packed. | ||
// So we try to figure out if the supplied Field is at a packed offset | ||
// (i.e., the externally-supplied offset is less than the layout builder | ||
// expected). | ||
// | ||
// There are cases where fields are placed at overlapping offsets (e.g., | ||
// as a result of [[no_unique_address]]). In those cases we don't want | ||
// to incorrectly deduce that they are placed at packed offsets. Hence, | ||
// ignore empty fields (which are the only fields that can overlap). | ||
// | ||
// FIXME: emit enough information in DWARF to get rid of InferAlignment. | ||
// | ||
CXXRecordDecl *CXX = nullptr; | ||
if (auto *RT = dyn_cast<RecordType>(Field->getType())) | ||
CXX = RT->getAsCXXRecordDecl(); | ||
|
||
const bool assume_packed = ExternalFieldOffset > 0 && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't generally use Why the > 0 condition? I'm not sure I understand why we need to update anything here. When we have an external layout, do we not eventually just take the alignment from the external layout anyway? If so, what's the point of the extra bookkeeping? |
||
ExternalFieldOffset < ComputedOffset && | ||
!(CXX && CXX->isEmpty()); | ||
|
||
if (InferAlignment && assume_packed) { | ||
Alignment = CharUnits::One(); | ||
PreferredAlignment = CharUnits::One(); | ||
InferAlignment = false; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// RUN: %clangxx_host -gdwarf -o %t %s | ||
// RUN: %lldb %t \ | ||
// RUN: -o "expr alignof(PackedNUA)" \ | ||
// RUN: -o "expr sizeof(PackedNUA)" \ | ||
// RUN: -o exit | FileCheck %s | ||
|
||
struct Empty {}; | ||
struct __attribute((packed)) PackedNUA { | ||
[[no_unique_address]] Empty a, b, c, d; | ||
char x; | ||
int y; | ||
}; | ||
static_assert(alignof(PackedNUA) == 1); | ||
static_assert(sizeof(PackedNUA) == 5); | ||
|
||
PackedNUA packed; | ||
|
||
// CHECK: (lldb) expr alignof(PackedNUA) | ||
// CHECK-NEXT: ${{.*}} = 1 | ||
// CHECK: (lldb) expr sizeof(PackedNUA) | ||
// CHECK-NEXT: ${{.*}} = 5 | ||
|
||
int main() { PackedNUA d; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is all just
auto CXX = Field->getType()->getAsCXXRecordDecl();