Skip to content

[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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions clang/lib/AST/RecordLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,13 +1296,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()) {
Expand Down Expand Up @@ -2255,9 +2248,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 &&
ExternalFieldOffset < ComputedOffset &&
!(CXX && CXX->isEmpty());

if (InferAlignment && assume_packed) {
Alignment = CharUnits::One();
PreferredAlignment = CharUnits::One();
InferAlignment = false;
Expand Down
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; }
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// XFAIL: *

// RUN: %clang --target=x86_64-apple-macosx -c -gdwarf -o %t %s
// RUN: %lldb %t \
// RUN: -o "expr alignof(OverlappingFields)" \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// XFAIL: *

// RUN: %clang --target=x86_64-apple-macosx -c -gdwarf -o %t %s
// RUN: %lldb %t \
// RUN: -o "expr alignof(OverlappingDerived)" \
Expand Down
Loading