[PATCH] D20561: Warn when taking address of packed member

Roger via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 1 04:15:20 PST 2017


royger added a comment.

In https://reviews.llvm.org/D20561#663006, @kimgr wrote:

> ... and both revisions should be in the 4.0 branch (taken from r291814)
>
> I was looking forward to this warning to help iron out alignment issues at compile-time instead of runtime (our ARM CPUs don't like unaligned access). @royger, can you expand a little on what you mean by
>
> > For once, it doesn't really check whether the access is actually unaligned or no
>
> ? Isn't that exactly what it does? Or do you mean in relation to whether the target allows unaligned access or not?


Take the following example structure:

  typedef union __attribute__((__packed__)) segment_attributes {
      uint16_t bytes;
      struct
      {
          uint16_t type:4;    /* 0;  Bit 40-43 */
          uint16_t s:   1;    /* 4;  Bit 44 */
          uint16_t dpl: 2;    /* 5;  Bit 45-46 */
          uint16_t p:   1;    /* 7;  Bit 47 */
          uint16_t avl: 1;    /* 8;  Bit 52 */
          uint16_t l:   1;    /* 9;  Bit 53 */
          uint16_t db:  1;    /* 10; Bit 54 */
          uint16_t g:   1;    /* 11; Bit 55 */
          uint16_t pad: 4;
      } fields;
  } segment_attributes_t;
  
  
  struct __attribute__((__packed__)) segment_register {
      uint16_t   sel;
      segment_attributes_t attr;
      uint32_t   limit;
      uint64_t   base;
  };

clang is complaining about:

  vmx.c:973:38: error: taking address of packed member 'base' of class or structure 'segment_register' may result in an unaligned pointer value [-Werror,-Waddress-of-packed-member]
          __vmread(GUEST_CS_BASE,     &reg->base);
                                       ^~~~~~~~~

But "base" is indeed aligned (it would be at the same offset without the __packed__ attribute), so I'm not sure why it complains.


Repository:
  rL LLVM

https://reviews.llvm.org/D20561





More information about the cfe-commits mailing list