llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[ABI] Type with potentially-overlapping member (`[[no_unique_address]]`) still considered POD for the purpose of layout

Open MitalAshok opened this issue 2 years ago • 2 comments

https://itanium-cxx-abi.github.io/cxx-abi/abi.html#POD

However, a type is not considered to be a POD for the purpose of layout if it is:

  • [...]
  • a POD-struct with one or more potentially-overlapping non-static data members.

Clang does not seem to respect this. This is visible when tail-padding is not reused

https://github.com/llvm/llvm-project/blob/216787cffc4a864e0effb165c1c32b92328a0a06/clang/lib/AST/RecordLayoutBuilder.cpp#L2421

struct X {  // Not POD for the purpose of layout
    [[no_unique_address]] alignas(4) char a;  // potentially-overlapping
};

struct Y : X {
    char b;  // This should be in the tail-padding
};

static_assert(sizeof(Y) == 4);

Clang makes sizeof(Y) == 8, disagreeing with GCC

MitalAshok avatar Apr 28 '24 17:04 MitalAshok

@llvm/issue-subscribers-clang-frontend

Author: Mital Ashok (MitalAshok)

https://itanium-cxx-abi.github.io/cxx-abi/abi.html#POD

> However, a type is not considered to be a POD for the purpose of layout if it is: > - [...] > - a POD-struct with one or more potentially-overlapping non-static data members.

Clang does not seem to respect this. This is visible when tail-padding is not reused

https://github.com/llvm/llvm-project/blob/216787cffc4a864e0effb165c1c32b92328a0a06/clang/lib/AST/RecordLayoutBuilder.cpp#L2421

struct X {  // Not POD for the purpose of layout
    [[no_unique_address]] alignas(4) char a;  // potentially-overlapping
};

struct Y : X {
    char b;  // This should be in the tail-padding
};

static_assert(sizeof(Y) == 4);

Clang makes sizeof(Y) == 8, disagreeing with GCC

llvmbot avatar Apr 28 '24 22:04 llvmbot

Dupe of https://github.com/llvm/llvm-project/issues/50766?

Michael137 avatar Apr 28 '24 22:04 Michael137