[PATCH] D46439: Fix incorrect packed aligned structure layout

Momchil Velikov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri May 4 09:45:47 PDT 2018


chill created this revision.
chill added reviewers: rsmith, aaron.ballman.

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.


Repository:
  rC Clang

https://reviews.llvm.org/D46439

Files:
  lib/Sema/SemaDecl.cpp


Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -15573,6 +15573,10 @@
     if (!Completed)
       Record->completeDefinition();
 
+    // Handle attributes before checking for alignas underalignment.
+    if (Attr)
+      ProcessDeclAttributeList(S, Record, Attr);
+
     // We may have deferred checking for a deleted destructor. Check now.
     if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record)) {
       auto *Dtor = CXXRecord->getDestructor();
@@ -15703,9 +15707,6 @@
       CDecl->setIvarRBraceLoc(RBrac);
     }
   }
-
-  if (Attr)
-    ProcessDeclAttributeList(S, Record, Attr);
 }
 
 /// \brief Determine whether the given integral value is representable within


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46439.145213.patch
Type: text/x-patch
Size: 787 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180504/694f7d35/attachment.bin>


More information about the cfe-commits mailing list