This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Fix incorrect packed aligned structure layout
ClosedPublic

Authored by chill on May 4 2018, 9:45 AM.

Details

Summary

Executing the following program

#include <cassert>
#include <cstddef>

struct S {
  char x;
  int y;
} __attribute__((packed, aligned(8)));

struct alignas(8) T {
  char x;
  int y;
} __attribute__((packed));

int main() {
  assert(offsetof(S, x) == 0);
  assert(offsetof(S, y) == 1);

  assert(offsetof(T, x) == 0);
  assert(offsetof(T, y) == 1);
}

fails with assertion

a.out: a.cc:19: int main(): Assertion `offsetof(T, y) == 1' failed.

The layout if T is incorrect, because it's computed and effectively cached when checking for alignas under-aligning the structure, however, this happens before __attribute__((packed)) is processed.

This patch moves the processing of attributes before the alignas under-alignment check.

Diff Detail

Repository
rC Clang

Event Timeline

chill created this revision.May 4 2018, 9:45 AM

This looks fine, but needs a test.

lib/Sema/SemaDecl.cpp
15597

More generally: "before checking the layout".

15672

Do we need to do any attribute processing in this Objective-C interface / implementation / category case?

chill updated this revision to Diff 145687.May 8 2018, 7:26 AM
chill marked an inline comment as done.May 8 2018, 7:28 AM

Update: updated comment, added a test.

lib/Sema/SemaDecl.cpp
15672

I think we didn't need it, or else ProcessDeclAttributeList would have been called with a null Record argument.

lebedev.ri retitled this revision from Fix incorrect packed aligned structure layout to [Sema] Fix incorrect packed aligned structure layout.May 15 2018, 3:49 AM
This revision is now accepted and ready to land.May 16 2018, 6:14 AM
chill added a comment.May 21 2018, 7:27 AM

Thanks a lot!

This revision was automatically updated to reflect the committed changes.

This wouldn't be another layout/ABI breakage, would it?

This wouldn't be another layout/ABI breakage, would it?

Yes, albeit for what I'd expect to be an extremely rare case.